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 document download.
Date Sat, 16 Aug 2014 20:19:51 GMT
Repository: cxf
Updated Branches:
  refs/heads/master d54a21628 -> 66cc4a4e3


CXF-5923: Create sample to demonstrate usage of search extension with Lucene and Tika. Added
document download.


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

Branch: refs/heads/master
Commit: 66cc4a4e3c0e92d9e6931a5d416f11c2f07f268c
Parents: d54a216
Author: reta <drreta@gmail.com>
Authored: Sat Aug 16 16:19:14 2014 -0400
Committer: reta <drreta@gmail.com>
Committed: Sat Aug 16 16:19:14 2014 -0400

----------------------------------------------------------------------
 .../release/samples/jax_rs/search/README.txt    | 24 +++++++++++
 .../java/demo/jaxrs/search/client/Client.java   | 19 +++++++++
 .../java/demo/jaxrs/search/server/Catalog.java  | 42 +++++++++++++++++++-
 .../java/demo/jaxrs/search/server/Storage.java  | 20 ++++++++++
 .../search/tika/TikaLuceneContentExtractor.java |  4 +-
 5 files changed, 105 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/66cc4a4e/distribution/src/main/release/samples/jax_rs/search/README.txt
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/README.txt b/distribution/src/main/release/samples/jax_rs/search/README.txt
index 322becc..11c7aab 100644
--- a/distribution/src/main/release/samples/jax_rs/search/README.txt
+++ b/distribution/src/main/release/samples/jax_rs/search/README.txt
@@ -14,6 +14,30 @@ returns all the documents currently stored and managed (in JSON format):
     "JavaWebSocketAPI_1.0_Final.pdf"
 ]
 
+A HTTP POST request to URL http://localhost:9000/catalog
+uploads document and stores it.
+
+A HTTP GET request to URL http://localhost:9000/catalog/search?$filter=<query>
+searches the relevant documents which match the query and returns them (in JSON format):
+
+[
+    {
+        "source":"JavaWebSocketAPI_1.0_Final.pdf",
+        "score":0.07321092486381531,
+        "url":"http://localhost:9000/catalog/JavaWebSocketAPI_1.0_Final.pdf"
+    },
+    {
+        "source":"jsr339-jaxrs-2.0-final-spec.pdf",
+        "score":0.03448590263724327,
+        "url":"http://localhost:9000/catalog/jsr339-jaxrs-2.0-final-spec.pdf"
+    }
+]
+
+A HTTP GET request to URL http://localhost:9000/catalog/<document> returns the
+original document content (in  binary form).
+
+A HTTP DELETE request to URL http://localhost:9000/catalog removes all documents
+from the catalog.
 
 Building and running the demo using Maven
 ---------------------------------------

http://git-wip-us.apache.org/repos/asf/cxf/blob/66cc4a4e/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 671755b..def695c 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
@@ -23,6 +23,7 @@ import java.io.IOException;
 
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.DeleteMethod;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
@@ -46,6 +47,8 @@ public final class Client {
         
         search(url, httpClient, "ct==java");        
         search(url, httpClient, "ct==websockets");
+        
+        delete(url, httpClient);
     }
 
     private static void list(final String url, final HttpClient httpClient) 
@@ -111,5 +114,21 @@ public final class Client {
             post.releaseConnection();
         }
     }
+    
+    private static void delete(final String url, final HttpClient httpClient) 
+        throws IOException, HttpException {
+                
+        System.out.println("Sent HTTP DELETE request to remove all books from catalog");
+        
+        final DeleteMethod delete = new DeleteMethod(url);            
+        try {
+            int status = httpClient.executeMethod(delete);
+            if (status == 200) {   
+                System.out.println(delete.getResponseBodyAsString());
+            }
+        } finally {
+            delete.releaseConnection();
+        }
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/66cc4a4e/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 0102d08..b2bb376 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
@@ -22,7 +22,10 @@ package demo.jaxrs.search.server;
 
 import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
@@ -36,15 +39,19 @@ import javax.json.JsonArrayBuilder;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
+import javax.ws.rs.NotFoundException;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
 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;
 import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.core.StreamingOutput;
 import javax.ws.rs.core.UriInfo;
 
 import org.apache.cxf.helpers.IOUtils;
@@ -163,7 +170,9 @@ public class Catalog {
     @GET
     @Produces(MediaType.APPLICATION_JSON)
     @Path("/search")
-    public JsonArray findBook(@Context SearchContext searchContext) throws IOException {
+    public JsonArray findBook(@Context SearchContext searchContext, 
+            @Context final UriInfo uri) throws IOException {
+        
         final IndexReader reader = DirectoryReader.open(directory);
         final IndexSearcher searcher = new IndexSearcher(reader);
         final JsonArrayBuilder builder = Json.createArrayBuilder();
@@ -175,11 +184,16 @@ public class Catalog {
             final TopDocs topDocs = searcher.search(visitor.getQuery(), 1000);
             for (final ScoreDoc scoreDoc: topDocs.scoreDocs) {
                 final Document document = reader.document(scoreDoc.doc);
+                final String source = document.getField(LuceneDocumentMetadata.SOURCE_FIELD).stringValue();
                 
                 builder.add(
                     Json.createObjectBuilder()
-                        .add("source", document.getField(LuceneDocumentMetadata.SOURCE_FIELD).stringValue())
+                        .add("source", source)
                         .add("score", scoreDoc.score)
+                        .add("url", uri.getBaseUriBuilder()
+                                .path(Catalog.class)
+                                .path(source)
+                                .build().toString())
                 );
             }
             
@@ -189,11 +203,35 @@ public class Catalog {
         }
     }
     
+    @GET
+    @Path("/{source}")
+    @Produces(MediaType.APPLICATION_OCTET_STREAM)
+    public StreamingOutput getBook(@PathParam("source") final String source) throws IOException
{            
+        return new StreamingOutput() {            
+            @Override
+            public void write(final OutputStream out) throws IOException, WebApplicationException
{
+                InputStream in = null;
+                
+                try {
+                    in = storage.getDocument(source);
+                    out.write(IOUtils.readBytesFromStream(in));
+                } catch (final FileNotFoundException ex) {
+                    throw new NotFoundException("Document does not exist: " + source);
+                } finally {
+                    if (in != null) { 
+                        try { in.close(); } catch (IOException ex) { /* do nothing */ }
+                    }    
+                }                
+            }
+        };
+    }
+    
     @DELETE
     public Response delete() throws IOException {
         final IndexWriter writer = new IndexWriter(directory, config);
         
         try {
+            storage.deleteAll();
             writer.deleteAll();
             writer.commit();
         } finally {

http://git-wip-us.apache.org/repos/asf/cxf/blob/66cc4a4e/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
index e15c64f..c35a416 100644
--- 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
@@ -21,6 +21,8 @@ package demo.jaxrs.search.server;
 import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -73,4 +75,22 @@ public class Storage {
             }
         }
     }
+    
+    public InputStream getDocument(final String name) throws IOException {
+        final File f = new File(folder, name);
+        
+        if (!f.exists() || !f.isFile()) {
+            throw new FileNotFoundException("Unable to access FS file:" + f.getAbsolutePath());
+        }
+        
+        return new FileInputStream(f);
+    }
+    
+    public void deleteAll() throws IOException {
+        for (final File f: folder.listFiles()) {
+            if (!f.delete()) {
+                throw new IOException("Unable to delete FS file:" + f.getAbsolutePath());
+            }
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/66cc4a4e/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
index 1c25203..0e526de 100644
--- a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
+++ b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/tika/TikaLuceneContentExtractor.java
@@ -24,7 +24,7 @@ import java.util.List;
 
 import javax.ws.rs.ext.ParamConverterProvider;
 
-import org.apache.commons.lang.StringUtils;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.jaxrs.ext.search.tika.TikaContentExtractor.TikaContent;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.DoubleField;
@@ -192,7 +192,7 @@ public class TikaLuceneContentExtractor {
             }
         }
         
-        if (!StringUtils.isBlank(documentMetadata.getSource())) {
+        if (!StringUtils.isEmpty(documentMetadata.getSource())) {
             document.add(new StringField(documentMetadata.getSourceFieldName(), 
                 documentMetadata.getSource(), Store.YES));
         }


Mime
View raw message