cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject git commit: CXF-5923: Create sample to demonstrate usage of search extension with Lucene and Tika. Added async POST support.
Date Tue, 12 Aug 2014 13:06:42 GMT
Repository: cxf
Updated Branches:
  refs/heads/master ff742436a -> 1674e7fb3


CXF-5923: Create sample to demonstrate usage of search extension with Lucene and Tika. Added
async POST support.


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1674e7fb
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1674e7fb
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1674e7fb

Branch: refs/heads/master
Commit: 1674e7fb3353fc9e5d2b81deb2d02617cc09a373
Parents: ff74243
Author: reta <drreta@gmail.com>
Authored: Tue Aug 12 09:06:06 2014 -0400
Committer: reta <drreta@gmail.com>
Committed: Tue Aug 12 09:06:06 2014 -0400

----------------------------------------------------------------------
 .../java/demo/jaxrs/search/client/Client.java   |  1 +
 .../java/demo/jaxrs/search/server/Catalog.java  | 74 ++++++++++++--------
 2 files changed, 45 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/1674e7fb/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
index 211ee18..bd62180 100644
--- a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
@@ -45,6 +45,7 @@ public final class Client {
         list(url, httpClient);        
         
         search(url, httpClient, "ct==java");        
+        search(url, httpClient, "ct==websockets");
     }
 
     private static void list(final String url, final HttpClient httpClient) 

http://git-wip-us.apache.org/repos/asf/cxf/blob/1674e7fb/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
index 42cb4d5..bd7ecdd 100644
--- a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
@@ -25,6 +25,9 @@ import java.io.IOException;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 import javax.activation.DataHandler;
 import javax.json.Json;
@@ -36,6 +39,8 @@ import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.container.AsyncResponse;
+import javax.ws.rs.container.Suspended;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -72,7 +77,8 @@ public class Catalog {
     private final TikaLuceneContentExtractor extractor = new TikaLuceneContentExtractor(new
PDFParser());    
     private final Directory directory = new RAMDirectory();
     private final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
-    private final IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
   
+    private final IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
+    private final ExecutorService executor = Executors.newFixedThreadPool( Runtime.getRuntime().availableProcessors()
);
     
     public Catalog() throws IOException {
         initIndex();
@@ -80,40 +86,48 @@ public class Catalog {
     
     @POST
     @Consumes("multipart/form-data")
-    public Response addBook(@Context final UriInfo uri, final MultipartBody body) throws
Exception {
-        for (final Attachment attachment: body.getAllAttachments()) {
-            final DataHandler handler =  attachment.getDataHandler();
-            
-            if (handler != null) {
-                final String source = handler.getName();                
-                final LuceneDocumentMetadata metadata = new LuceneDocumentMetadata()
-                    .withSource(source)
-                    .withField("modified", Date.class);
-                
-                final BufferedInputStream in = new BufferedInputStream(handler.getInputStream());
-                try {
-                    final Document document = extractor.extract(in, metadata);
-                    if (document != null) {                    
-                        final IndexWriter writer = new IndexWriter(directory, config);
+    public void addBook(@Suspended final AsyncResponse response, @Context final UriInfo uri,

+            final MultipartBody body)  {
+        
+        executor.submit(new Callable< Void >() {
+            public Void call() throws Exception {
+                for (final Attachment attachment: body.getAllAttachments()) {
+                    final DataHandler handler =  attachment.getDataHandler();
+                    
+                    if (handler != null) {
+                        final String source = handler.getName();                
+                        final LuceneDocumentMetadata metadata = new LuceneDocumentMetadata()
+                            .withSource(source)
+                            .withField("modified", Date.class);
                         
+                        final BufferedInputStream in = new BufferedInputStream(handler.getInputStream());
                         try {
-                            writer.addDocument(document);
-                            writer.commit();
+                            final Document document = extractor.extract(in, metadata);
+                            if (document != null) {                    
+                                final IndexWriter writer = new IndexWriter(directory, config);
+                                
+                                try {
+                                    writer.addDocument(document);
+                                    writer.commit();
+                                } finally {
+                                    writer.close();
+                                }
+                            }
                         } finally {
-                            writer.close();
+                            if (in != null) { 
+                                in.close(); 
+                            }
                         }
-                    }
-                } finally {
-                    if (in != null) { 
-                        in.close(); 
-                    }
-                }
+                        
+                        response.resume( Response.created(uri.getRequestUriBuilder().path(source).build()).build()
);
+                        return null;
+                    }                       
+                }              
                 
-                return Response.created(uri.getRequestUriBuilder().path(source).build()).build();
-            }                       
-        }              
-        
-        return Response.status(Status.BAD_REQUEST).build();
+                response.resume( Response.status(Status.BAD_REQUEST).build() );   
+                return null;
+            }
+        });
     }
     
     @GET


Mime
View raw message