You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2014/08/07 00:28:23 UTC
[3/3] git commit: CXF-5923: Create sample to demonstrate usage of
search extension with Lucene and Tika
CXF-5923: Create sample to demonstrate usage of search extension with Lucene and Tika
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/f18acbcd
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/f18acbcd
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/f18acbcd
Branch: refs/heads/master
Commit: f18acbcdfc48aca354610614c0ba3a46dcb8332a
Parents: c044f72
Author: reta <dr...@gmail.com>
Authored: Wed Aug 6 18:27:48 2014 -0400
Committer: reta <dr...@gmail.com>
Committed: Wed Aug 6 18:27:48 2014 -0400
----------------------------------------------------------------------
.../release/samples/jax_rs/search/README.txt | 35 +
.../main/release/samples/jax_rs/search/pom.xml | 151 +
.../java/demo/jaxrs/search/client/Client.java | 89 +
.../java/demo/jaxrs/search/server/Catalog.java | 194 +
.../java/demo/jaxrs/search/server/Server.java | 60 +
.../resources/JavaWebSocketAPI_1.0_Final.pdf | Bin 0 -> 214897 bytes
.../resources/jsr339-jaxrs-2.0-final-spec.pdf | 12183 +++++++++++++++++
distribution/src/main/release/samples/pom.xml | 1 +
8 files changed, 12713 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cxf/blob/f18acbcd/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
new file mode 100644
index 0000000..322becc
--- /dev/null
+++ b/distribution/src/main/release/samples/jax_rs/search/README.txt
@@ -0,0 +1,35 @@
+JAX-RS Search Demo
+=================
+
+The demo shows a basic usage of search extension with REST based Web Services using
+JAX-RS 2.0 (JSR-339). The REST server provides the following services:
+
+A RESTful catalog service is provided on URL http://localhost:9000/catalog.
+
+A HTTP GET request to URL http://localhost:9000/catalog
+returns all the documents currently stored and managed (in JSON format):
+
+[
+ "jsr339-jaxrs-2.0-final-spec.pdf",
+ "JavaWebSocketAPI_1.0_Final.pdf"
+]
+
+
+Building and running the demo using Maven
+---------------------------------------
+
+From the base directory of this sample (i.e., where this README file is
+located), the Maven pom.xml file can be used to build and run the demo.
+
+
+Using either UNIX or Windows:
+
+ mvn install
+ mvn -Pserver (from one command line window)
+ mvn -Pclient (from a second command line window)
+
+
+To remove the target dir, run mvn clean".
+
+
+
http://git-wip-us.apache.org/repos/asf/cxf/blob/f18acbcd/distribution/src/main/release/samples/jax_rs/search/pom.xml
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/pom.xml b/distribution/src/main/release/samples/jax_rs/search/pom.xml
new file mode 100644
index 0000000..4f9f9c9
--- /dev/null
+++ b/distribution/src/main/release/samples/jax_rs/search/pom.xml
@@ -0,0 +1,151 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>jax_rs_search</artifactId>
+ <name>JAX-RS Basic Demo</name>
+ <description>JAX-RS Basic Demo</description>
+ <parent>
+ <groupId>org.apache.cxf.samples</groupId>
+ <artifactId>cxf-samples</artifactId>
+ <version>3.1.0-SNAPSHOT</version>
+ <relativePath>../..</relativePath>
+ </parent>
+ <properties>
+ <cxf.version>${project.version}</cxf.version>
+ <httpclient.version>3.1</httpclient.version>
+ </properties>
+ <profiles>
+ <profile>
+ <id>server</id>
+ <build>
+ <defaultGoal>test</defaultGoal>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>test</phase>
+ <goals>
+ <goal>java</goal>
+ </goals>
+ <configuration>
+ <mainClass>demo.jaxrs.search.server.Server</mainClass>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ <profile>
+ <id>client</id>
+ <build>
+ <defaultGoal>test</defaultGoal>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>test</phase>
+ <goals>
+ <goal>java</goal>
+ </goals>
+ <configuration>
+ <mainClass>demo.jaxrs.search.client.Client</mainClass>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-rt-transports-http</artifactId>
+ <version>3.1.0-SNAPSHOT</version>
+ </dependency>
+ <!-- This dependency is needed if you're using the Jetty container -->
+ <dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-rt-transports-http-jetty</artifactId>
+ <version>3.1.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-rt-frontend-jaxrs</artifactId>
+ <version>3.1.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-rt-rs-extension-search</artifactId>
+ <version>3.1.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-rt-rs-extension-providers</artifactId>
+ <version>3.1.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.lucene</groupId>
+ <artifactId>lucene-core</artifactId>
+ <version>4.0.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.lucene</groupId>
+ <artifactId>lucene-analyzers-common</artifactId>
+ <version>4.0.0</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.json</groupId>
+ <artifactId>javax.json-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.glassfish</groupId>
+ <artifactId>javax.json</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tika</groupId>
+ <artifactId>tika-parsers</artifactId>
+ <exclusions>
+ <exclusion>
+ <groupId>org.apache.poi</groupId>
+ <artifactId>poi-ooxml</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-httpclient</groupId>
+ <artifactId>commons-httpclient</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>javax.ws.rs</groupId>
+ <artifactId>javax.ws.rs-api</artifactId>
+ </dependency>
+ </dependencies>
+</project>
http://git-wip-us.apache.org/repos/asf/cxf/blob/f18acbcd/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
new file mode 100644
index 0000000..d1fad9c
--- /dev/null
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
@@ -0,0 +1,89 @@
+/**
+ * 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.client;
+
+import java.io.IOException;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
+import org.apache.commons.httpclient.methods.multipart.FilePart;
+import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
+import org.apache.commons.httpclient.methods.multipart.Part;
+import org.apache.cxf.helpers.IOUtils;
+
+public final class Client {
+ private Client() {
+ }
+
+ public static void main(String args[]) throws Exception {
+ final String url = "http://localhost:9000/catalog";
+ final HttpClient httpClient = new HttpClient();
+
+ uploadToCatalog(url, httpClient, "jsr339-jaxrs-2.0-final-spec.pdf");
+ uploadToCatalog(url, httpClient, "JavaWebSocketAPI_1.0_Final.pdf");
+ list(url, httpClient);
+ }
+
+ private static void list(final String url, final HttpClient httpClient)
+ throws IOException, HttpException {
+
+ System.out.println("Sent HTTP GET request to query all books in catalog");
+
+ final GetMethod get = new GetMethod(url);
+ try {
+ int status = httpClient.executeMethod(get);
+ if (status == 200) {
+ System.out.println(get.getResponseBodyAsString());
+ }
+ } finally {
+ get.releaseConnection();
+ }
+ }
+
+ private static void uploadToCatalog(final String url, final HttpClient httpClient,
+ final String filename) throws IOException, HttpException {
+
+ System.out.println("Sent HTTP POST request to upload the file into catalog: " + filename);
+
+ final PostMethod post = new PostMethod(url);
+ final Part[] parts = {
+ new FilePart(filename,
+ new ByteArrayPartSource(filename,
+ IOUtils.readBytesFromStream(Client.class.getResourceAsStream("/" + filename))
+ )
+ )
+ };
+
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
+
+ try {
+ int status = httpClient.executeMethod(post);
+ if (status == 201) {
+ System.out.println(post.getResponseHeader("Location"));
+ }
+ } finally {
+ post.releaseConnection();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cxf/blob/f18acbcd/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
new file mode 100644
index 0000000..eb9241a
--- /dev/null
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
@@ -0,0 +1,194 @@
+/**
+ * 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.BufferedInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.activation.DataHandler;
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+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.UriInfo;
+
+import org.apache.cxf.jaxrs.ext.multipart.Attachment;
+import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
+import org.apache.cxf.jaxrs.ext.search.SearchBean;
+import org.apache.cxf.jaxrs.ext.search.SearchContext;
+import org.apache.cxf.jaxrs.ext.search.lucene.LuceneQueryVisitor;
+import org.apache.cxf.jaxrs.ext.search.tika.LuceneDocumentMetadata;
+import org.apache.cxf.jaxrs.ext.search.tika.TikaLuceneContentExtractor;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.DocumentStoredFieldVisitor;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.util.Version;
+import org.apache.tika.parser.pdf.PDFParser;
+
+@Path("/catalog")
+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 LuceneQueryVisitor<SearchBean> visitor = createVisitor();
+
+ public Catalog() throws IOException {
+ initIndex();
+ }
+
+ @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);
+
+ try {
+ writer.addDocument(document);
+ writer.commit();
+ } finally {
+ writer.close();
+ }
+ }
+ } finally {
+ if (in != null) {
+ in.close();
+ }
+ }
+
+ return Response.created(uri.getRequestUriBuilder().path(source).build()).build();
+ }
+ }
+
+ return Response.status(Status.BAD_REQUEST).build();
+ }
+
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public JsonArray getBooks() throws IOException {
+ final IndexReader reader = DirectoryReader.open(directory);
+ final IndexSearcher searcher = new IndexSearcher(reader);
+ final JsonArrayBuilder builder = Json.createArrayBuilder();
+
+ try {
+ final Query query = new MatchAllDocsQuery();
+
+ for (final ScoreDoc scoreDoc: searcher.search(query, 1000).scoreDocs) {
+ final DocumentStoredFieldVisitor fieldVisitor =
+ new DocumentStoredFieldVisitor("source");
+
+ reader.document(scoreDoc.doc, fieldVisitor);
+ builder.add(fieldVisitor.getDocument().getField("source").stringValue());
+ }
+
+ return builder.build();
+ } finally {
+ reader.close();
+ }
+ }
+
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("/search")
+ public Collection<ScoreDoc> findBook(@Context SearchContext searchContext) throws IOException {
+ final IndexReader reader = DirectoryReader.open(directory);
+ final IndexSearcher searcher = new IndexSearcher(reader);
+
+ try {
+ visitor.visit(searchContext.getCondition(SearchBean.class));
+ return Arrays.asList(searcher.search(visitor.getQuery(), null, 1000).scoreDocs);
+ } finally {
+ reader.close();
+ }
+ }
+
+ @DELETE
+ public Response delete() throws IOException {
+ final IndexWriter writer = new IndexWriter(directory, config);
+
+ try {
+ writer.deleteAll();
+ writer.commit();
+ } finally {
+ writer.close();
+ }
+
+ return Response.ok().build();
+ }
+
+ private void initIndex() throws IOException {
+ final IndexWriter writer = new IndexWriter(directory, config);
+
+ try {
+ writer.commit();
+ } finally {
+ writer.close();
+ }
+ }
+
+ private static LuceneQueryVisitor< SearchBean > createVisitor() {
+ final Map< String, Class< ? > > fieldTypes = new HashMap< String, Class< ? > >();
+ fieldTypes.put("modified", Date.class);
+
+ LuceneQueryVisitor<SearchBean> visitor = new LuceneQueryVisitor<SearchBean>("ct", "contents");
+ visitor.setPrimitiveFieldTypeMap(fieldTypes);
+ return visitor;
+ }
+}
+
+
http://git-wip-us.apache.org/repos/asf/cxf/blob/f18acbcd/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
new file mode 100644
index 0000000..aeacb54
--- /dev/null
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
@@ -0,0 +1,60 @@
+/**
+ * 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.util.HashMap;
+import java.util.Map;
+
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.ext.search.SearchBean;
+import org.apache.cxf.jaxrs.ext.search.SearchContextProvider;
+import org.apache.cxf.jaxrs.ext.search.SearchUtils;
+import org.apache.cxf.jaxrs.ext.search.fiql.FiqlParser;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.cxf.jaxrs.provider.MultipartProvider;
+import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider;
+
+public class Server {
+
+ protected Server() throws Exception {
+ 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));
+ properties.put(SearchUtils.DATE_FORMAT_PROPERTY, "yyyy/MM/dd");
+
+ final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+ sf.setResourceClasses(Catalog.class);
+ sf.setResourceProvider(Catalog.class, new SingletonResourceProvider(new Catalog()));
+ sf.setAddress("http://localhost:9000/");
+ sf.setProvider(new MultipartProvider());
+ sf.setProvider(new SearchContextProvider());
+ sf.setProvider(new JsrJsonpProvider());
+ sf.create();
+ }
+
+ public static void main(String args[]) throws Exception {
+ new Server();
+ System.out.println("Server ready...");
+
+ Thread.sleep(5 * 6000 * 1000);
+ System.out.println("Server exiting");
+ System.exit(0);
+ }
+}
http://git-wip-us.apache.org/repos/asf/cxf/blob/f18acbcd/distribution/src/main/release/samples/jax_rs/search/src/main/resources/JavaWebSocketAPI_1.0_Final.pdf
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/resources/JavaWebSocketAPI_1.0_Final.pdf b/distribution/src/main/release/samples/jax_rs/search/src/main/resources/JavaWebSocketAPI_1.0_Final.pdf
new file mode 100644
index 0000000..6b99899
Binary files /dev/null and b/distribution/src/main/release/samples/jax_rs/search/src/main/resources/JavaWebSocketAPI_1.0_Final.pdf differ