summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorreger <reger18@arcor.de>2016-03-14 02:22:06 +0100
committerreger <reger18@arcor.de>2016-03-14 02:22:06 +0100
commit6783ef5540d630fc8c7543531c6ca32f1e47db79 (patch)
treea67f38e5c575733c6ff2f5dd0de091cc564e5f75 /examples
parent6a8f3f3ebaf3fae8e082fc3ee6188ae1917fa937 (diff)
move example code SearchClient out of yacycore package
to example directory
Diffstat (limited to 'examples')
-rw-r--r--examples/SimpleSearchClient/pom.xml20
-rw-r--r--examples/SimpleSearchClient/src/YaCySearchClient.java152
2 files changed, 172 insertions, 0 deletions
diff --git a/examples/SimpleSearchClient/pom.xml b/examples/SimpleSearchClient/pom.xml
new file mode 100644
index 000000000..87b8c08b3
--- /dev/null
+++ b/examples/SimpleSearchClient/pom.xml
@@ -0,0 +1,20 @@
+<?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>net.yacy</groupId>
+ <artifactId>SimpleSearchClient</artifactId>
+ <version>1.0</version>
+ <packaging>jar</packaging>
+ <description>Simple search client example to query YaCy and receive search results as RSS feed</description>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <maven.compiler.source>1.7</maven.compiler.source>
+ <maven.compiler.target>1.7</maven.compiler.target>
+ </properties>
+
+ <build>
+ <sourceDirectory>src</sourceDirectory>
+ </build>
+
+</project> \ No newline at end of file
diff --git a/examples/SimpleSearchClient/src/YaCySearchClient.java b/examples/SimpleSearchClient/src/YaCySearchClient.java
new file mode 100644
index 000000000..0c2a6f535
--- /dev/null
+++ b/examples/SimpleSearchClient/src/YaCySearchClient.java
@@ -0,0 +1,152 @@
+/**
+ * YaCySearchClient
+ * an interface for Adaptive Replacement Caches
+ * Copyright 2010 by Michael Peter Christen, mc@yacy.net, Frankfurt a. M., Germany
+ * First released 20.09.2010 at http://yacy.net
+ *
+ * $LastChangedDate$
+ * $LastChangedRevision$
+ * $LastChangedBy$
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program in the file lgpl21.txt
+ * If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.yacy.simplesearchclient;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.regex.Pattern;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+/**
+ * most simple rss reader application for YaCy search result retrieval
+ * this is an example application that you can use to integrate YaCy search results in your own java applications
+ */
+public class YaCySearchClient {
+
+ /*
+ * YaCy Search Results are produced in Opensearch format which is basically RSS.
+ * The YaCy Search Result API Client is therefore implemented as a simple RSS reader.
+ */
+ private final String host, query;
+ private final int port;
+ private int offset;
+ private final static Pattern SPACE = Pattern.compile(" ");
+
+ public YaCySearchClient(final String host, final int port, final String query) {
+ this.host = host;
+ this.port = port;
+ this.offset = -10;
+ this.query = query;
+ }
+
+ public SearchResult next() throws IOException {
+ this.offset += 10; // you may call this again and get the next results
+ return new SearchResult();
+ }
+
+ public class SearchResult extends ArrayList<RSSEntry> {
+ private static final long serialVersionUID = 1337L;
+ public SearchResult() throws IOException {
+ URL url;
+ Document doc;
+ String u = new StringBuilder(120).append("http://")
+ .append(YaCySearchClient.this.host)
+ .append(":")
+ .append(YaCySearchClient.this.port)
+ .append("/yacysearch.rss?verify=false&startRecord=")
+ .append(YaCySearchClient.this.offset)
+ .append("&maximumRecords=10&resource=local&query=")
+ .append(SPACE.matcher(YaCySearchClient.this.query).replaceAll("+")).toString();
+ try { url = new URL(u); } catch (final MalformedURLException e) { throw new IOException (e); }
+ try { doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream()); }
+ catch (final ParserConfigurationException e) { throw new IOException (e); }
+ catch (final SAXException e) { throw new IOException (e); }
+ final NodeList nodes = doc.getElementsByTagName("item");
+ for (int i = 0; i < nodes.getLength(); i++)
+ this.add(new RSSEntry((Element) nodes.item(i)));
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder(this.size() * 80 + 1);
+ for (RSSEntry entry: this) sb.append(entry.toString());
+ return sb.toString();
+ }
+ }
+
+ public static class RSSEntry {
+ String title, link, snippet;
+ public RSSEntry(Element element) {
+ this.title = val(element, "title", "");
+ this.link = val(element, "link", "");
+ this.snippet = val(element, "description", "");
+ }
+ private static String val(Element parent, String label, String dflt) {
+ Element e = (Element) parent.getElementsByTagName(label).item(0);
+ Node child = e.getFirstChild();
+ return (child instanceof CharacterData) ?
+ ((CharacterData) child).getData() : dflt;
+ }
+
+ @Override
+ public String toString() {
+ return new StringBuilder(80).append("Title : ")
+ .append(this.title)
+ .append("\nLink : ")
+ .append(this.link)
+ .append("\nDescription: ")
+ .append(this.snippet)
+ .append("\n").toString();
+ }
+ }
+
+ /**
+ * Call the main method with one argument, the query string
+ * search results are then simply printed out.
+ * Multiple search requests can be submitted by adding more call arguments.
+ * Use this method as stub for an integration in your own programs
+ */
+ public static void main(String[] args) {
+ if (args.length < 1) {
+ System.out.println("No query string as argument found.");
+ System.out.println("Call the main method with one argument, the query string,");
+ System.out.println("while YaCy is running on localhost.");
+ } else {
+ for (String query : args) {
+ try {
+ long t = System.currentTimeMillis();
+ YaCySearchClient search = new YaCySearchClient("localhost", 8090, query);
+ System.out.println("Search result for '" + query + "':");
+ System.out.print(search.next().toString()); // get 10 results; you may repeat this for next 10
+ System.out.println("Search Time: " + (System.currentTimeMillis() - t) + " milliseconds\n");
+ } catch (final IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+}