You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by th...@apache.org on 2008/03/14 10:59:47 UTC
svn commit: r637032 - in /labs/droids/trunk/src/core/java/org/apache:
droids/handle/Solr.java droids/helper/Loggable.java droids/helper/StAX.java
http/PostFile.java
Author: thorsten
Date: Fri Mar 14 02:59:44 2008
New Revision: 637032
URL: http://svn.apache.org/viewvc?rev=637032&view=rev
Log:
Adding solr handler example with corresponding helper classes
Added:
labs/droids/trunk/src/core/java/org/apache/droids/helper/Loggable.java (with props)
labs/droids/trunk/src/core/java/org/apache/droids/helper/StAX.java (with props)
labs/droids/trunk/src/core/java/org/apache/http/PostFile.java (with props)
Modified:
labs/droids/trunk/src/core/java/org/apache/droids/handle/Solr.java
Modified: labs/droids/trunk/src/core/java/org/apache/droids/handle/Solr.java
URL: http://svn.apache.org/viewvc/labs/droids/trunk/src/core/java/org/apache/droids/handle/Solr.java?rev=637032&r1=637031&r2=637032&view=diff
==============================================================================
--- labs/droids/trunk/src/core/java/org/apache/droids/handle/Solr.java (original)
+++ labs/droids/trunk/src/core/java/org/apache/droids/handle/Solr.java Fri Mar 14 02:59:44 2008
@@ -1,17 +1,89 @@
package org.apache.droids.handle;
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.URL;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
import org.apache.droids.api.Handler;
import org.apache.droids.api.Parse;
+import org.apache.droids.helper.StAX;
+import org.apache.http.PostFile;
+
+public class Solr implements Handler {
+
+ private String updateUrl;
-public class Solr implements Handler{
+ private XMLStreamWriter writer;
public void handle(InputStream openStream, URL url, Parse parse)
throws Exception {
-
-
+ ByteArrayOutputStream out = createUpdateDocument(url, parse);
+ BufferedInputStream stream = new BufferedInputStream(new ByteArrayInputStream(out.toByteArray()));
+ PostFile post = new PostFile("updateUrl",stream);
+ }
+
+ public ByteArrayOutputStream createUpdateDocument(URL url, Parse parse)
+ throws XMLStreamException {
+ StAX stax = new StAX();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ writer = stax.getStreamWriter(new DataOutputStream(out));
+ writer.writeStartDocument("UTF-8", "1.0");
+ writer.writeStartElement("add");
+ writer.writeStartElement("doc");
+ writer.writeStartElement("field");
+ writer.writeAttribute("name", "id");
+ writer.writeCharacters(url.getPath());
+ writer.writeEndElement();
+ writer.writeStartElement("field");
+ writer.writeAttribute("name", "content");
+ writer.writeCharacters(parse.getText());
+ writer.writeEndElement();
+ writer.writeEndElement();
+ writer.writeEndElement();
+ writer.writeEndDocument();
+ writer.flush();
+ writer.close();
+ return out;
+ }
+
+ public ByteArrayOutputStream createCommitDocument() throws XMLStreamException {
+ StAX stax = new StAX();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ writer = stax.getStreamWriter(new DataOutputStream(out));
+ writer.writeStartDocument("UTF-8", "1.0");
+ writer.writeStartElement("commit");
+ writer.writeEndElement();
+ writer.writeEndDocument();
+ writer.flush();
+ writer.close();
+ return out;
}
+ public ByteArrayOutputStream createOptimizeDocument()
+ throws XMLStreamException {
+ StAX stax = new StAX();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ writer = stax.getStreamWriter(new DataOutputStream(out));
+ writer.writeStartDocument("UTF-8", "1.0");
+ writer.writeStartElement("optimize");
+ writer.writeEndElement();
+ writer.writeEndDocument();
+ writer.flush();
+ writer.close();
+ return out;
+ }
+
+ public String getUpdateUrl() {
+ return updateUrl;
+ }
+
+ public void setUpdateUrl(String updateUrl) {
+ this.updateUrl = updateUrl;
+ }
}
Added: labs/droids/trunk/src/core/java/org/apache/droids/helper/Loggable.java
URL: http://svn.apache.org/viewvc/labs/droids/trunk/src/core/java/org/apache/droids/helper/Loggable.java?rev=637032&view=auto
==============================================================================
--- labs/droids/trunk/src/core/java/org/apache/droids/helper/Loggable.java (added)
+++ labs/droids/trunk/src/core/java/org/apache/droids/helper/Loggable.java Fri Mar 14 02:59:44 2008
@@ -0,0 +1,21 @@
+package org.apache.droids.helper;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * Simple wrapper class to easier debug/log.
+ *
+ * @author thorsten
+ *
+ */
+public class Loggable {
+
+ protected final Log log = LogFactory.getLog(this.getClass().getCanonicalName());
+
+ public Loggable() {
+ super();
+ }
+
+}
\ No newline at end of file
Propchange: labs/droids/trunk/src/core/java/org/apache/droids/helper/Loggable.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/trunk/src/core/java/org/apache/droids/helper/StAX.java
URL: http://svn.apache.org/viewvc/labs/droids/trunk/src/core/java/org/apache/droids/helper/StAX.java?rev=637032&view=auto
==============================================================================
--- labs/droids/trunk/src/core/java/org/apache/droids/helper/StAX.java (added)
+++ labs/droids/trunk/src/core/java/org/apache/droids/helper/StAX.java Fri Mar 14 02:59:44 2008
@@ -0,0 +1,48 @@
+package org.apache.droids.helper;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.xml.stream.XMLEventFactory;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+public class StAX extends Loggable {
+ private XMLOutputFactory outputFactory;
+
+ private XMLEventFactory eventFactory;
+
+ private XMLInputFactory inputFactory;
+ /**
+ * Easy helper to get StAX based parser and writer.
+ */
+ public StAX(){
+ inputFactory = XMLInputFactory.newInstance();
+ outputFactory=XMLOutputFactory.newInstance();
+ eventFactory = XMLEventFactory.newInstance();
+ }
+ public XMLEventWriter getWriter(OutputStream stream) throws XMLStreamException{
+ XMLEventWriter writer = outputFactory.createXMLEventWriter(stream);
+ return writer;
+ }
+ public XMLStreamWriter getStreamWriter(OutputStream stream) throws XMLStreamException{
+ XMLStreamWriter writer = outputFactory.createXMLStreamWriter(stream);
+ return writer;
+ }
+ public XMLEventReader getEventParser(InputStream in) throws XMLStreamException{
+ XMLEventReader parser = inputFactory.createXMLEventReader(in);
+ return parser;
+ }
+ public XMLStreamReader getParser(InputStream in) throws XMLStreamException{
+ XMLStreamReader parser = inputFactory.createXMLStreamReader(in);
+ return parser;
+ }
+ public XMLEventFactory getEventFactory(){
+ return eventFactory;
+ }
+}
Propchange: labs/droids/trunk/src/core/java/org/apache/droids/helper/StAX.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: labs/droids/trunk/src/core/java/org/apache/http/PostFile.java
URL: http://svn.apache.org/viewvc/labs/droids/trunk/src/core/java/org/apache/http/PostFile.java?rev=637032&view=auto
==============================================================================
--- labs/droids/trunk/src/core/java/org/apache/http/PostFile.java (added)
+++ labs/droids/trunk/src/core/java/org/apache/http/PostFile.java Fri Mar 14 02:59:44 2008
@@ -0,0 +1,114 @@
+/*
+ * 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 org.apache.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+
+public class PostFile {
+ /**
+ * The AGENT name
+ */
+ public final String AGENT = "droid[" + this.getClass().getCanonicalName() + "]";
+
+ private String destinationUrl, srcUrl;
+
+ private HttpClient client;
+
+ private HttpPost filePost;
+
+ private HttpResponse response;
+
+ /**
+ * @param destinationUrl -
+ * the url of the server listener (e.g. servlet)
+ * @param srcUrl -
+ * the src url of the file to post
+ * @throws MalformedURLException
+ * @throws IOException
+ * @throws InterruptedException
+ * @throws HttpException
+ * @throws URISyntaxException
+ */
+ public PostFile(String destinationUrl, InputStream srcStream)
+ throws MalformedURLException, IOException, HttpException, InterruptedException, URISyntaxException {
+ this.destinationUrl = destinationUrl;
+ client = new DefaultHttpClient();
+ filePost = prepareFilePost(destinationUrl, srcStream);
+ response = client.execute(filePost);
+ }
+
+ public void post(String destinationUrl, String srcUrl)
+ throws MalformedURLException, IOException, HttpException, InterruptedException, URISyntaxException {
+ this.destinationUrl = destinationUrl;
+ this.srcUrl = srcUrl;
+ client = new DefaultHttpClient();
+ filePost = prepareFilePost(destinationUrl, srcUrl);
+ response = client.execute(filePost);
+ }
+
+ public int statusCode() {
+ return response.getStatusLine().getStatusCode();
+ }
+
+ public InputStream getResponseBodyAsStream() throws MalformedURLException,
+ IOException {
+ return response.getEntity().getContent();
+ }
+
+ private HttpPost prepareFilePost(String solrBase, String srcUrl)
+ throws IOException, MalformedURLException, URISyntaxException {
+ HttpPost filePost = new HttpPost(solrBase);
+ filePost.addHeader("Content-type", "text/xml; charset=utf-8");
+ filePost.addHeader("User-Agent", AGENT);
+ InputStreamEntity reqEntity = new InputStreamEntity(
+ new URL(srcUrl).openStream(), -1);
+ filePost.setEntity(reqEntity);
+ return filePost;
+ }
+
+ private HttpPost prepareFilePost(String solrBase, InputStream src)
+ throws IOException, MalformedURLException, URISyntaxException {
+ HttpPost filePost = null;
+ filePost = new HttpPost(solrBase);
+ filePost.addHeader("Content-type", "text/xml; charset=utf-8");
+ filePost.addHeader("User-Agent", AGENT);
+
+ InputStreamEntity reqEntity = new InputStreamEntity(
+ src, -1);
+ filePost.setEntity(reqEntity);
+ return filePost;
+ }
+
+ public String getsolrBase() {
+ return destinationUrl;
+ }
+
+ public String getSrc() {
+ return srcUrl;
+ }
+
+}
Propchange: labs/droids/trunk/src/core/java/org/apache/http/PostFile.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org