`
Clayz
  • 浏览: 293964 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

Solr中SimplePostTool的修改

阅读更多

将源代码修改为传递字符串,去掉files以及stdin模式。

package com;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

/**
 * @author Clay Zhong - Email: zjclay@gmail.com
 * @date Sep 15, 2008
 */
public class SimplePostTool {
	public static final String DEFAULT_POST_URL = "http://localhost:8080/solr/update";
	public static final String POST_ENCODING = "UTF-8";
	private static final String SOLR_OK_RESPONSE_EXCERPT = "<int name=\"status\">0</int>";
	private static final boolean DEFAULT_COMMIT = true;
	protected URL solrUrl;

	public SimplePostTool(URL solrUrl) {
		this.solrUrl = solrUrl;
	}

	private class PostException extends RuntimeException {
		PostException(String reason, Throwable cause) {
			super(reason + " (POST URL=" + solrUrl + ")", cause);
		}
	}

	public static void main(String[] args) {
		StringBuffer request = new StringBuffer("<add><doc>");
		request.append("<field name=\"id\">F8V7067-APL-KIT</field>");
		request.append("<field name=\"name\">Belkin Mobile Power Cord for iPod w/ Dock</field>");
		request.append("<field name=\"manu\">Belkin</field>");
		request.append("<field name=\"cat\">electronics</field>");
		request.append("<field name=\"cat\">connector</field>");
		request.append("<field name=\"features\">car power adapter, white</field>");
		request.append("<field name=\"weight\">4</field>");
		request.append("<field name=\"price\">19.95</field>");
		request.append("<field name=\"popularity\">1</field>");
		request.append("<field name=\"inStock\">false</field>");
		request.append("</doc></add>");

		URL url = null;
		try {
			url = new URL(DEFAULT_POST_URL);
		}
		catch (MalformedURLException e) {
			fatal("System Property 'url' is not a valid URL: " + url);
		}

		final SimplePostTool simplePostTool = new SimplePostTool(url);

		try {
			if (request.length() > 0) {
				info("Posting args to " + url);
				final StringWriter writer = new StringWriter();
				simplePostTool.postData(new StringReader(request.toString()), writer);
				warnIfNotExpectedResponse(writer.toString(), SOLR_OK_RESPONSE_EXCERPT);
			}

			if (DEFAULT_COMMIT) {
				info("Commiting Solr index changes..");
				final StringWriter writer = new StringWriter();
				simplePostTool.commit(writer);
				warnIfNotExpectedResponse(writer.toString(), SOLR_OK_RESPONSE_EXCERPT);
			}
		}
		catch (IOException ioe) {
			fatal("Unexpected IOException " + ioe);
		}
	}

	/**
	 * Does a simple commit operation
	 */
	public void commit(Writer output) throws IOException {
		postData(new StringReader("<commit/>"), output);
	}

	/**
	 * Reads data from the data reader and posts it to solr, writes to the response to output
	 */
	public void postData(Reader data, Writer output) {
		HttpURLConnection urlc = null;

		try {
			urlc = (HttpURLConnection) solrUrl.openConnection();
			try {
				urlc.setRequestMethod("POST");
			}
			catch (ProtocolException e) {
				throw new PostException("HttpURLConnection doesn't support POST?", e);
			}

			urlc.setDoOutput(true);
			urlc.setDoInput(true);
			urlc.setUseCaches(false);
			urlc.setAllowUserInteraction(false);
			urlc.setRequestProperty("Content-type", "text/xml; charset=" + POST_ENCODING);

			OutputStream out = urlc.getOutputStream();

			try {
				Writer writer = new OutputStreamWriter(out, POST_ENCODING);
				pipe(data, writer);
				writer.close();
			}
			catch (IOException e) {
				throw new PostException("IOException while posting data", e);
			}
			finally {
				if (out != null) out.close();
			}

			InputStream in = urlc.getInputStream();
			try {
				Reader reader = new InputStreamReader(in);
				pipe(reader, output);
				reader.close();
			}
			catch (IOException e) {
				throw new PostException("IOException while reading response", e);
			}
			finally {
				if (in != null) in.close();
			}
		}
		catch (IOException e) {
			fatal("Connection error (is Solr running at " + solrUrl + " ?): " + e);
		}
		finally {
			if (urlc != null) urlc.disconnect();
		}
	}

	/**
	 * Pipes everything from the reader to the writer via a buffer
	 */
	private static void pipe(Reader reader, Writer writer) throws IOException {
		char[] buf = new char[1024];
		int read = 0;
		while ((read = reader.read(buf)) >= 0) {
			writer.write(buf, 0, read);
		}

		writer.flush();
	}

	/**
	 * Check what Solr replied to a POST, and complain if it's not what we expected. TODO: parse the
	 * response and check it XMLwise, here we just check it as an unparsed String
	 */
	static void warnIfNotExpectedResponse(String actual, String expected) {
		if (actual.indexOf(expected) < 0) {
			warn("Unexpected response from Solr: '" + actual + "' does not contain '" + expected
					+ "'");
		}
	}

	static void warn(String msg) {
		System.err.println("SimplePostTool WARNING: " + msg);
	}

	static void info(String msg) {
		System.out.println("SimplePostTool: " + msg);
	}

	static void fatal(String msg) {
		System.err.println("SimplePostTool FATAL: " + msg);
		System.exit(1);
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics