Repository: cxf Updated Branches: refs/heads/master 84c4048a9 -> 1e927cdfa
CXF-5923: Create sample to demonstrate usage of search extension with Lucene and Tika. Integrated browser demo into the server package. Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1e927cdf Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1e927cdf Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1e927cdf Branch: refs/heads/master Commit: 1e927cdfaafb2efc8d29184cb5b7b05ce3c3ad7f Parents: 84c4048 Author: reta <[email protected]> Authored: Sun Sep 21 10:42:30 2014 -0400 Committer: reta <[email protected]> Committed: Sun Sep 21 10:42:30 2014 -0400 ---------------------------------------------------------------------- .../release/samples/jax_rs/search/README.txt | 17 ++--- .../main/release/samples/jax_rs/search/pom.xml | 4 ++ .../java/demo/jaxrs/search/client/Client.java | 2 +- .../java/demo/jaxrs/search/server/Catalog.java | 4 ++ .../java/demo/jaxrs/search/server/Server.java | 65 +++++++++++++------- .../src/main/resources/browser/index.html | 6 +- 6 files changed, 64 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/1e927cdf/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 11c7aab..4d3b830 100644 --- a/distribution/src/main/release/samples/jax_rs/search/README.txt +++ b/distribution/src/main/release/samples/jax_rs/search/README.txt @@ -4,9 +4,10 @@ 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 RESTful catalog service is provided on URL http://localhost:9000/jaxrs/catalog +A web browser demo is available at: http://localhost:9000/browser -A HTTP GET request to URL http://localhost:9000/catalog +A HTTP GET request to URL http://localhost:9000/jaxrs/catalog returns all the documents currently stored and managed (in JSON format): [ @@ -14,29 +15,29 @@ 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 +A HTTP POST request to URL http://localhost:9000/jaxrs/catalog uploads document and stores it. -A HTTP GET request to URL http://localhost:9000/catalog/search?$filter=<query> +A HTTP GET request to URL http://localhost:9000/jaxrs/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" + "url":"http://localhost:9000/jaxrs/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" + "url":"http://localhost:9000/jaxrs/catalog/jsr339-jaxrs-2.0-final-spec.pdf" } ] -A HTTP GET request to URL http://localhost:9000/catalog/<document> returns the +A HTTP GET request to URL http://localhost:9000/jaxrs/catalog/<document> returns the original document content (in binary form). -A HTTP DELETE request to URL http://localhost:9000/catalog removes all documents +A HTTP DELETE request to URL http://localhost:9000/jaxrs/catalog removes all documents from the catalog. Building and running the demo using Maven http://git-wip-us.apache.org/repos/asf/cxf/blob/1e927cdf/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 index 05c1ef1..b3231d7 100644 --- a/distribution/src/main/release/samples/jax_rs/search/pom.xml +++ b/distribution/src/main/release/samples/jax_rs/search/pom.xml @@ -153,5 +153,9 @@ <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-webapp</artifactId> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/cxf/blob/1e927cdf/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 e277132..fb689a6 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 @@ -37,7 +37,7 @@ public final class Client { } public static void main(String args[]) throws Exception { - final String url = "http://localhost:9000/catalog"; + final String url = "http://localhost:9000/jaxrs/catalog"; final HttpClient httpClient = new HttpClient(); uploadToCatalog(url, httpClient, "jsr339-jaxrs-2.0-final-spec.pdf"); http://git-wip-us.apache.org/repos/asf/cxf/blob/1e927cdf/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 0732f8c..8707ed8 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 @@ -94,6 +94,10 @@ public class Catalog { private final ExecutorService executor = Executors.newFixedThreadPool( Runtime.getRuntime().availableProcessors()); + public Catalog() throws IOException { + this(new Storage()); + } + public Catalog(final Storage storage) throws IOException { this.storage = storage; this.visitor = createVisitor(); http://git-wip-us.apache.org/repos/asf/cxf/blob/1e927cdf/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 14bb74d..2cd62b8 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 @@ -19,38 +19,59 @@ 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.commons.lang.StringUtils; 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; +import org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet; import org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter; +import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; 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)); - properties.put(SearchUtils.DATE_FORMAT_PROPERTY, "yyyy/MM/dd"); + org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(9000); + + // Register and map the dispatcher servlet + final ServletHolder servletHolder = new ServletHolder(new CXFNonSpringJaxrsServlet()); + final ServletContextHandler context = new ServletContextHandler(); + context.setContextPath("/"); + context.addServlet(servletHolder, "/jaxrs/*"); + + servletHolder.setInitParameter("jaxrs.serviceClasses", Catalog.class.getName()); + servletHolder.setInitParameter("jaxrs.properties", StringUtils.join( + new String[] { + "search.query.parameter.name=$filter", + SearchUtils.DATE_FORMAT_PROPERTY + "=yyyy/MM/dd" + }, " ") + ); + servletHolder.setInitParameter("jaxrs.providers", StringUtils.join( + new String[] { + MultipartProvider.class.getName(), + SearchContextProvider.class.getName(), + JsrJsonpProvider.class.getName(), + CrossOriginResourceSharingFilter.class.getName() + }, ",") + ); + + // Configuring all static web resource + final ServletHolder staticHolder = new ServletHolder(new DefaultServlet()); + final ServletContextHandler htmls = new ServletContextHandler(); + htmls.setContextPath("/browser"); + htmls.addServlet(staticHolder, "/*"); + htmls.setResourceBase(getClass().getResource("/browser").toURI().toString()); - final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); - sf.setProperties(properties); - sf.setResourceClasses(Catalog.class); - sf.setResourceProvider(Catalog.class, new SingletonResourceProvider(new Catalog(storage))); - sf.setAddress("http://localhost:9000/"); - sf.setProvider(new MultipartProvider()); - sf.setProvider(new SearchContextProvider()); - sf.setProvider(new JsrJsonpProvider()); - sf.setProvider(new CrossOriginResourceSharingFilter()); - sf.create(); + final HandlerList handlers = new HandlerList(); + handlers.addHandler(htmls); + handlers.addHandler(context); + + server.setHandler(handlers); + server.start(); + server.join(); } public static void main(String args[]) throws Exception { http://git-wip-us.apache.org/repos/asf/cxf/blob/1e927cdf/distribution/src/main/release/samples/jax_rs/search/src/main/resources/browser/index.html ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/resources/browser/index.html b/distribution/src/main/release/samples/jax_rs/search/src/main/resources/browser/index.html index c61f55d..b7520ee 100644 --- a/distribution/src/main/release/samples/jax_rs/search/src/main/resources/browser/index.html +++ b/distribution/src/main/release/samples/jax_rs/search/src/main/resources/browser/index.html @@ -18,14 +18,14 @@ <div class="alert alert-danger hide" role="alert" id="error-message"></div> </div> - <form id="form" class="form-upload" role="form" enctype="multipart/form-data" method="post" action="http://localhost:9000/catalog"> + <form id="form" class="form-upload" role="form" enctype="multipart/form-data" method="post" action="http://localhost:9000/jaxrs/catalog"> <h2 class="form-upload-heading">Please select a PDF file</h2> <div class="form-group"> <input id="file" name="file" type="file" multiple="true" class="file"></input> </div> </form> - <form id="form-search" role="search" method="get" action="http://localhost:9000/catalog/search"> + <form id="form-search" role="search" method="get" action="http://localhost:9000/jaxrs/catalog/search"> <h2 class="form-upload-heading">Query the Catalog</h2> <div class="input-group add-on"> <input id="search-query" type="text" class="form-control" placeholder="Search"> @@ -107,7 +107,7 @@ <tr>\ <td>" + value.source + "</td>\ <td>" + value.score + "</td>\ - <td><a href='" + value.url + "'>Open</a></td>\ + <td><a href='" + value.url + "'>Download</a></td>\ </tr>"; });
