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 simple file storage (to keep original content).
Date Thu, 14 Aug 2014 00:58:45 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 77d7a4628 -> 2afb9d3ca


CXF-5923: Create sample to demonstrate usage of search extension with Lucene and Tika. Added
simple file storage (to keep original content).


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

Branch: refs/heads/master
Commit: 2afb9d3cacb299bea854d1ff4824e4c981a41d6a
Parents: 77d7a46
Author: reta <drreta@gmail.com>
Authored: Wed Aug 13 20:58:26 2014 -0400
Committer: reta <drreta@gmail.com>
Committed: Wed Aug 13 20:58:26 2014 -0400

----------------------------------------------------------------------
 .../java/demo/jaxrs/search/server/Catalog.java  | 81 ++++++++++++--------
 .../java/demo/jaxrs/search/server/Server.java   |  3 +-
 .../java/demo/jaxrs/search/server/Storage.java  | 76 ++++++++++++++++++
 3 files changed, 129 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/2afb9d3c/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 4ffccdf..3c3d75f 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
@@ -21,11 +21,11 @@ package demo.jaxrs.search.server;
 
 
 import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
 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;
 
@@ -47,6 +47,7 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriInfo;
 
+import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
 import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
 import org.apache.cxf.jaxrs.ext.search.SearchBean;
@@ -78,10 +79,12 @@ public class Catalog {
     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 ExecutorService executor = 
-            Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
+    private final Storage storage; 
+    private final ExecutorService executor = Executors.newFixedThreadPool(
+        Runtime.getRuntime().availableProcessors());
     
-    public Catalog() throws IOException {
+    public Catalog(final Storage storage) throws IOException {
+        this.storage = storage;
         initIndex();
     }
     
@@ -90,45 +93,38 @@ public class Catalog {
     public void addBook(@Suspended final AsyncResponse response, @Context final UriInfo uri,

             final MultipartBody body)  {
         
-        executor.submit(new Callable< Void >() {
-            public Void call() throws Exception {
+        executor.submit(new Runnable() {
+            public void run() {
                 for (final Attachment attachment: body.getAllAttachments()) {
                     final DataHandler handler =  attachment.getDataHandler();
                     
                     if (handler != null) {
-                        final String source = handler.getName();                
+                        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);
-                                
-                                try {
-                                    writer.addDocument(document);
-                                    writer.commit();
-                                } finally {
-                                    writer.close();
-                                }
-                            }
-                        } finally {
-                            if (in != null) { 
-                                in.close(); 
-                            }
-                        }
+                            final byte[] content = IOUtils.readBytesFromStream(handler.getInputStream());
+                            storeAndIndex(metadata, content);
+                        } catch (final IOException ex) {
+                            response.resume(Response.serverError().build());  
+                        } 
                         
-                        response.resume(Response.created(uri.getRequestUriBuilder()
+                        if (response.isSuspended()) {
+                            response.resume(Response.created(uri.getRequestUriBuilder()
                                 .path(source).build()).build());
-                        return null;
+                        }
                     }                       
                 }              
                 
-                response.resume(Response.status(Status.BAD_REQUEST).build());   
-                return null;
+                if (response.isSuspended()) {
+                    response.resume(Response.status(Status.BAD_REQUEST).build());
+                }
             }
+
+            
         });
     }
     
@@ -144,8 +140,7 @@ public class Catalog {
             
             for (final ScoreDoc scoreDoc: searcher.search(query, 1000).scoreDocs) {
                 final DocumentStoredFieldVisitor visitor = 
-                    new DocumentStoredFieldVisitor("source");
-                
+                    new DocumentStoredFieldVisitor("source");                
                 
                 reader.document(scoreDoc.doc, visitor);
                 builder.add(visitor.getDocument().getField("source").stringValue());
@@ -218,6 +213,32 @@ public class Catalog {
         visitor.setPrimitiveFieldTypeMap(fieldTypes);
         return visitor;
     }
+    
+    private void storeAndIndex(final LuceneDocumentMetadata metadata, final byte[] content)
+        throws IOException {
+        
+        BufferedInputStream in = null;        
+        try {
+            in = new BufferedInputStream(new ByteArrayInputStream(content));
+            
+            final Document document = extractor.extract(in, metadata);
+            if (document != null) {                    
+                final IndexWriter writer = new IndexWriter(directory, config);
+                
+                try {                                              
+                    storage.addDocument(metadata.getSource(), content);
+                    writer.addDocument(document);
+                    writer.commit();
+                } finally {
+                    writer.close();
+                }
+            }
+        } finally {
+            if (in != null) { 
+                try { in.close(); } catch (IOException ex) { /* do nothing */ }
+            }
+        }
+    }
 }
 
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/2afb9d3c/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
index 1b25496..9b885c2 100644
--- a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
@@ -34,6 +34,7 @@ import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider;
 public class Server {
 
     protected Server() throws Exception {
+        final Storage storage = new Storage();
         final Map< String, Object > properties = new HashMap< String, Object >();
       
         properties.put("search.query.parameter.name", "$filter");
         properties.put("search.parser", new FiqlParser< SearchBean >(SearchBean.class));
@@ -42,7 +43,7 @@ public class Server {
         final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
         sf.setProperties(properties);
         sf.setResourceClasses(Catalog.class);
-        sf.setResourceProvider(Catalog.class, new SingletonResourceProvider(new Catalog()));
+        sf.setResourceProvider(Catalog.class, new SingletonResourceProvider(new Catalog(storage)));
         sf.setAddress("http://localhost:9000/");
         sf.setProvider(new MultipartProvider());
         sf.setProvider(new SearchContextProvider());

http://git-wip-us.apache.org/repos/asf/cxf/blob/2afb9d3c/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Storage.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Storage.java
b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Storage.java
new file mode 100644
index 0000000..e15c64f
--- /dev/null
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Storage.java
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package demo.jaxrs.search.server;
+
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.cxf.helpers.IOUtils;
+
+public class Storage {
+    private final File folder = new File("files");
+    
+    public Storage() throws IOException {
+        if (!folder.exists() && !folder.mkdirs()) {
+            throw new IOException("Unable to initialize FS storage:" + folder.getAbsolutePath());
+        }
+        
+        if (!folder.isDirectory() || !folder.canWrite() || !folder.canWrite()) {
+            throw new IOException("Unable to access FS storage:" + folder.getAbsolutePath());
+        }        
+    }
+    
+    public void addDocument(final String name, final byte[] content) throws IOException {
+        InputStream in = null;
+        
+        try {
+            in = new ByteArrayInputStream(content);
+            addDocument(name, in);
+        } finally {
+            if (in != null) {
+                try { in.close(); } catch (IOException ex) { /* do nothing */ }
+            }
+        }
+    }
+    
+    public void addDocument(final String name, final InputStream in) throws IOException {

+        final File f = new File(folder, name);
+        
+        if (f.exists() && !f.delete()) {
+            throw new IOException("Unable to delete FS file:" + f.getAbsolutePath());
+        } 
+        
+        OutputStream out = null;
+        try {            
+            out = new BufferedOutputStream(new FileOutputStream(f));
+            out.write(IOUtils.readBytesFromStream(in));
+            
+            f.deleteOnExit();
+        } finally {
+            if (out != null) {
+                try { out.close(); } catch (IOException ex) { /* do nothing */ }
+            }
+        }
+    }
+}


Mime
View raw message