diff options
author | Kristian Aune <kraune@yahoo-inc.com> | 2017-01-13 08:48:02 +0100 |
---|---|---|
committer | Kristian Aune <kraune@yahoo-inc.com> | 2017-01-13 08:48:02 +0100 |
commit | 8ec23b82b9ce70ce60c8fac059ce671f73a2f413 (patch) | |
tree | 92f925727e08be8967b20da213c26216eeda0b5c /sample-apps | |
parent | d89de4d0f4ef794a36c5e0a0c0b1824f94f4ca5f (diff) |
Add basic-search-java
- needs to be separate from basic-search (removed java code there)
- does not work yet, testing fails - I am not sure whether to rewrite of fix yet
- add now so I can fix documentation at least - fix code later
Diffstat (limited to 'sample-apps')
13 files changed, 254 insertions, 180 deletions
diff --git a/sample-apps/basic-search-java/pom.xml b/sample-apps/basic-search-java/pom.xml new file mode 100644 index 00000000000..ed6f729fb68 --- /dev/null +++ b/sample-apps/basic-search-java/pom.xml @@ -0,0 +1,83 @@ +<?xml version="1.0"?> +<!-- Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> +<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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.yahoo.example</groupId> + <artifactId>basic-search-java</artifactId> + <version>1.0.1</version> + <packaging>container-plugin</packaging> <!-- Use Vespa packaging --> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <test.hide>true</test.hide> + <vespa_version>6-SNAPSHOT</vespa_version> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>application</artifactId> <!-- Is this needed? --> + <version>${vespa_version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>container</artifactId> <!-- not container-dev --> + <version>${vespa_version}</version> + <scope>provided</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <optimize>true</optimize> + <showDeprecation>true</showDeprecation> + <showWarnings>true</showWarnings> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.13</version> + <configuration> + <systemPropertyVariables> + <isMavenSurefirePlugin>true</isMavenSurefirePlugin> + </systemPropertyVariables> + <redirectTestOutputToFile>${test.hide}</redirectTestOutputToFile> + </configuration> + </plugin> + <plugin> + <groupId>com.yahoo.vespa</groupId> + <artifactId>vespa-application-maven-plugin</artifactId> <!-- Zip the application package --> + <version>${vespa_version}</version> + <executions> + <execution> + <goals> + <goal>packageApplication</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>com.yahoo.vespa</groupId> + <artifactId>bundle-plugin</artifactId> + <extensions>true</extensions> + </plugin> + </plugins> + </build> +</project> diff --git a/sample-apps/basic-search-java/src/main/application/hosts.xml b/sample-apps/basic-search-java/src/main/application/hosts.xml new file mode 100644 index 00000000000..3ab86a21aef --- /dev/null +++ b/sample-apps/basic-search-java/src/main/application/hosts.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="utf-8" ?> +<!-- Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> +<hosts> + <host name="localhost"> + <alias>node1</alias> + </host> +</hosts> diff --git a/sample-apps/basic-search-java/src/main/application/services.xml b/sample-apps/basic-search-java/src/main/application/services.xml new file mode 100644 index 00000000000..11d74a8133f --- /dev/null +++ b/sample-apps/basic-search-java/src/main/application/services.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8" ?> +<!-- Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> +<services version="1.0"> + <admin version="2.0"> + <adminserver hostalias="node1"/> + </admin> + <jdisc version="1.0"> + <processing> + <chain id="default"> + <processor id="${package}.ExampleProcessor" bundle="${artifactId}"> + <config name="example.example-processor"> + <message>Hello, services!</message> + </config> + </processor> + </chain> + </processing> + <nodes> + <node hostalias="node1"/> + </nodes> + </jdisc> +</services> diff --git a/sample-apps/basic-search-java/src/main/java/ExampleProcessor.java b/sample-apps/basic-search-java/src/main/java/ExampleProcessor.java new file mode 100644 index 00000000000..1c29c479190 --- /dev/null +++ b/sample-apps/basic-search-java/src/main/java/ExampleProcessor.java @@ -0,0 +1,35 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +//package com.yahoo.example; + +import com.google.inject.Inject; +import com.yahoo.example.ExampleProcessorConfig; +import com.yahoo.processing.Processor; +import com.yahoo.processing.Request; +import com.yahoo.processing.Response; +import com.yahoo.processing.execution.Execution; + +public class ExampleProcessor extends Processor { + + private final String message; + + @Inject + public ExampleProcessor(ExampleProcessorConfig config) { + this.message = config.message(); + } + + @SuppressWarnings("unchecked") + @Override + public Response process(Request request, Execution execution) { + // process the request + request.properties().set("foo", "bar"); + + // pass it down the chain to get a response + Response response = execution.process(request); + + // process the response + response.data().add(new StringData(request, message)); + + // return the response up the chain + return response; + } +} diff --git a/sample-apps/basic-search-java/src/main/java/StringData.java b/sample-apps/basic-search-java/src/main/java/StringData.java new file mode 100644 index 00000000000..37519c82f44 --- /dev/null +++ b/sample-apps/basic-search-java/src/main/java/StringData.java @@ -0,0 +1,20 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +//package ${package}; + +import com.yahoo.processing.Request; +import com.yahoo.processing.response.AbstractData; + +public class StringData extends AbstractData { + + private final String string; + + public StringData(Request request, String string) { + super(request); + this.string = string; + } + + @Override + public String toString() { + return string; + } +}
\ No newline at end of file diff --git a/sample-apps/basic-search-java/src/main/resources/configdefinitions/example-processor.def b/sample-apps/basic-search-java/src/main/resources/configdefinitions/example-processor.def new file mode 100644 index 00000000000..1028451c312 --- /dev/null +++ b/sample-apps/basic-search-java/src/main/resources/configdefinitions/example-processor.def @@ -0,0 +1,3 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +namespace=example +message string default="" diff --git a/sample-apps/basic-search-java/src/test/java/ApplicationMain.java b/sample-apps/basic-search-java/src/test/java/ApplicationMain.java new file mode 100644 index 00000000000..620e12f4277 --- /dev/null +++ b/sample-apps/basic-search-java/src/test/java/ApplicationMain.java @@ -0,0 +1,27 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +//package ${package}; + +import com.yahoo.application.Networking; +import org.junit.Test; + +import java.nio.file.FileSystems; + +import static org.junit.Assume.assumeTrue; + +public class ApplicationMain { + + @Test + public void runFromMaven() throws Exception { + assumeTrue(Boolean.valueOf(System.getProperty("isMavenSurefirePlugin"))); + main(null); + } + + public static void main(String[] args) throws Exception { + try (com.yahoo.application.Application app = com.yahoo.application.Application.fromApplicationPackage( + FileSystems.getDefault().getPath("src/main/application"), + Networking.enable)) { + app.getClass(); // throws NullPointerException + Thread.sleep(Long.MAX_VALUE); + } + } +}
\ No newline at end of file diff --git a/sample-apps/basic-search-java/src/test/java/ApplicationTest.java b/sample-apps/basic-search-java/src/test/java/ApplicationTest.java new file mode 100644 index 00000000000..5cf5b62c261 --- /dev/null +++ b/sample-apps/basic-search-java/src/test/java/ApplicationTest.java @@ -0,0 +1,29 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +//package ${package}; + +import com.yahoo.application.Application; +import com.yahoo.application.Networking; +import com.yahoo.application.container.Processing; +import com.yahoo.component.ComponentSpecification; +import com.yahoo.processing.Request; +import com.yahoo.processing.Response; +import org.junit.Test; + +import java.nio.file.FileSystems; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; + +public class ApplicationTest { + + @Test + public void requireThatResultContainsHelloWorld() throws Exception { + try (Application app = Application.fromApplicationPackage( + FileSystems.getDefault().getPath("src/main/application"), + Networking.disable)) { + Processing processing = app.getJDisc("jdisc").processing(); + Response response = processing.process(ComponentSpecification.fromString("default"), new Request()); + assertThat(response.data().get(0).toString(), containsString("Hello, services!")); + } + } +} diff --git a/sample-apps/basic-search-java/src/test/java/ExampleProcessorTest.java b/sample-apps/basic-search-java/src/test/java/ExampleProcessorTest.java new file mode 100644 index 00000000000..447ae9ff6d2 --- /dev/null +++ b/sample-apps/basic-search-java/src/test/java/ExampleProcessorTest.java @@ -0,0 +1,29 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +//package ${package}; + +import com.yahoo.component.chain.Chain; +import com.yahoo.example.ExampleProcessorConfig; +import com.yahoo.processing.Processor; +import com.yahoo.processing.Request; +import com.yahoo.processing.Response; +import com.yahoo.processing.execution.Execution; +import org.junit.Test; + +import static org.junit.Assert.assertThat; +import static org.junit.matchers.JUnitMatchers.containsString; + +public class ExampleProcessorTest { + + @Test + public void requireThatResultContainsHelloWorld() { + ExampleProcessorConfig.Builder config = new ExampleProcessorConfig.Builder().message("Hello, processor!"); + Processor processor = new ExampleProcessor(new ExampleProcessorConfig(config)); + + Response response = newExecution(processor).process(new Request()); + assertThat(response.data().get(0).toString(), containsString("Hello, processor!")); + } + + private static Execution newExecution(Processor... processors) { + return Execution.createRoot(new Chain<>(processors), 0, Execution.Environment.createEmpty()); + } +} diff --git a/sample-apps/basic-search/pom.xml b/sample-apps/basic-search/pom.xml deleted file mode 100644 index 5801d201595..00000000000 --- a/sample-apps/basic-search/pom.xml +++ /dev/null @@ -1,41 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<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/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <groupId>com.yahoo.example</groupId> - <artifactId>basic-application</artifactId> - <packaging>container-plugin</packaging> <!-- Use Vespa packaging --> - <version>1.0.1</version> - - <build> - <plugins> - <plugin> <!-- Build the bundles --> - <groupId>com.yahoo.vespa</groupId> - <artifactId>bundle-plugin</artifactId> - <version>6-SNAPSHOT</version> - <extensions>true</extensions> - </plugin> - <plugin> <!-- Zip the application package --> - <groupId>com.yahoo.vespa</groupId> - <artifactId>vespa-application-maven-plugin</artifactId> - <version>6-SNAPSHOT</version> - <executions> - <execution> - <goals> - <goal>packageApplication</goal> - </goals> - </execution> - </executions> - </plugin> - </plugins> - </build> - - <dependencies> - <dependency> <!-- Vespa dependencies --> - <groupId>com.yahoo.vespa</groupId> - <artifactId>container</artifactId> - <version>6-SNAPSHOT</version> - <scope>provided</scope> - </dependency> - </dependencies> - -</project> diff --git a/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleDocumentProcessor.java b/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleDocumentProcessor.java deleted file mode 100644 index cbcb5ae406c..00000000000 --- a/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleDocumentProcessor.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.yahoo.example; - -import com.yahoo.docproc.DocumentProcessor; -import com.yahoo.docproc.Processing; -import com.yahoo.document.Document; -import com.yahoo.document.DocumentOperation; -import com.yahoo.document.DocumentPut; -import com.yahoo.document.DocumentRemove; -import com.yahoo.document.DocumentUpdate; - -import java.net.*; -import java.io.*; - -import com.yahoo.document.datatypes.StringFieldValue; -import org.json.*; - -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * A document processor - * - * @author Joe Developer - */ -public class ExampleDocumentProcessor extends DocumentProcessor { - - private static final String artistField = "artist"; - private static final String artistIdField = "artistId"; - private static final String spotifyUrl = "https://api.spotify.com/v1/search?type=artist&limit=1&q="; - private static final Logger log = Logger.getLogger(ExampleDocumentProcessor.class.getName()); - - public Progress process(Processing processing) { - for (DocumentOperation op : processing.getDocumentOperations()) { - if (op instanceof DocumentPut) { - Document document = ((DocumentPut) op).getDocument(); - addArtistId(document); - } - else if (op instanceof DocumentUpdate) { - DocumentUpdate update = (DocumentUpdate) op; - // Updates to existing documents can be modified here - } - else if (op instanceof DocumentRemove) { - DocumentRemove remove = (DocumentRemove) op; - // Document removes can be modified here - } - } - return Progress.DONE; - } - - /** Queries the Spotify API, parses JSON and sets Artist ID */ - public void addArtistId(Document document) { - StringFieldValue artistString = (StringFieldValue) document.getFieldValue(artistField); - - HttpURLConnection connection = null; - try { - connection = getConnection(spotifyUrl + java.net.URLEncoder.encode(artistString.getString(), "UTF-8")); - String artistId = parseSpotifyResponse(new InputStreamReader(connection.getInputStream(), "UTF-8")); - if (artistId == null) return; - - document.setFieldValue(artistIdField, new StringFieldValue(artistId)); - } - catch (Exception e) { - log.log(Level.WARNING, "Could not find artist id for '" + document.getId(), e); - } - finally { - if (connection != null) - connection.disconnect(); - } - } - - /** Returns an artist id from spotify or null if not found */ - private String parseSpotifyResponse(InputStreamReader streamReader) throws IOException, JSONException { - // Read JSON data from API - BufferedReader reader = new BufferedReader(streamReader); - StringBuilder builder = new StringBuilder(); - for (String line; (line = reader.readLine()) != null; ) { - builder.append(line).append("\n"); - } - - // Parse the JSON to find the first artist item returned or null if not found - JSONObject json = new JSONObject(builder.toString()); - JSONObject artists = json.getJSONObject("artists"); - JSONArray items = artists.getJSONArray("items"); - JSONObject artist = items.getJSONObject(0); - return artist.getString("id"); - } - - /** Establishes an HTTP Connection */ - private HttpURLConnection getConnection(String urlString) throws IOException { - HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection(); - connection.setRequestProperty("User-Agent", "Vespa Tutorial DocProc"); - connection.setReadTimeout(10000); - connection.setConnectTimeout(5000); - connection.connect(); - return connection; - } - -} - diff --git a/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleSearcher.java b/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleSearcher.java deleted file mode 100644 index 76b07a46b93..00000000000 --- a/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleSearcher.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.yahoo.example; - -import com.yahoo.search.Query; -import com.yahoo.search.Result; -import com.yahoo.search.Searcher; -import com.yahoo.search.result.Hit; -import com.yahoo.search.searchchain.Execution; - -/** - * A searcher adding a new hit. - * - * @author Joe Developer - */ -public class ExampleSearcher extends Searcher { - - public static final String hitId = "ExampleHit"; - private final String message; - - public ExampleSearcher(MessageConfig config) { - message = config.message(); - } - - public Result search(Query query, Execution execution) { - // Pass on to the next searcher to execute the query - Result result = execution.search(query); - - // Add an extra hit to the result - Hit hit = new Hit(hitId); - hit.setField("message", message); - result.hits().add(hit); - - // Return it to the upstream searcher, or to rendering - return result; - } - -} diff --git a/sample-apps/basic-search/src/main/resources/configdefinitions/message.def b/sample-apps/basic-search/src/main/resources/configdefinitions/message.def deleted file mode 100644 index 95039ec77c7..00000000000 --- a/sample-apps/basic-search/src/main/resources/configdefinitions/message.def +++ /dev/null @@ -1,4 +0,0 @@ -namespace=example - -# Explanation of the 'message' config variable -message string default="Hello, World!"
\ No newline at end of file |