solr SimpleIndexClient
来源:互联网 发布:淘宝销量多久清零 编辑:程序博客网 时间:2024/06/14 11:10
package com.paic.wcm.search.client;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import com.paic.wcm.search.client.dto.IndexPageDTO;
import com.paic.wcm.search.utils.DESUtil;
/**
* 简单实现,不依赖任何第三方jar包
*/
public class SimpleIndexClient {
public static final String KEY_PA_AUTH = "pa.auth";
private String urlStr;
public SimpleIndexClient(String urlStr, String password) {
try {
password = DESUtil.ecryptString(DESUtil.DEFAULT_SEED, password);
} catch (Exception e) {
throw new RuntimeException("ecrypt error");
}
this.urlStr = urlStr + (urlStr.endsWith("/") ? "" : "/") + "update?"
+ KEY_PA_AUTH + "=" + password;
}
/**
* 索引单个页面文件
*
* @param page
* @return
* @throws IOException
* @throws IndexException
*/
public boolean indexPage(IndexPageDTO page) throws IOException, IndexException {
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\""
+ getEncoding() + "\"?>");
sb.append("<add>");
sb.append(page.toDocString());
sb.append("<commit/>");
sb.append("</add>");
return postXmlStr(sb.toString());
}
/**
* 批量索引多个页面文件
*
* @param list
* @return
* @throws IOException
* @throws IndexException
*/
public boolean indexPageList(List<IndexPageDTO> list) throws IOException, IndexException {
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\""
+ getEncoding() + "\"?>");
sb.append("<add>");
for (IndexPageDTO page : list) {
sb.append(page.toDocString());
}
sb.append("<commit/>");
sb.append("</add>");
return postXmlStr(sb.toString());
}
/**
* 删除单个文件索引
*
* @param link
* @return
* @throws IOException
* @throws IndexException
*/
public boolean deleteIndexByLink(String link) throws IOException, IndexException {
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\""
+ getEncoding() + "\"?>");
sb.append("<update><delete><id>" + link
+ "</id></delete><commit/></update>");
return postXmlStr(sb.toString());
}
/**
* 批量删除多个文件索引
*
* @param links
* @return
* @throws IOException
* @throws IndexException
*/
public boolean deleteIndexByLinkList(List<String> links) throws IOException, IndexException {
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\""
+ getEncoding() + "\"?>");
sb.append("<update>");
for (String link : links) {
sb.append("<delete><id>" + link + "</id></delete>");
}
sb.append("<commit/></update>");
return postXmlStr(sb.toString());
}
/**
* post xml
*
* @param str
* @throws IOException
* @throws IndexException
*/
private boolean postXmlStr(String str) throws IOException, IndexException {
InputStream data = new ByteArrayInputStream(str.getBytes());
return postData(data, str.getBytes().length, "application/xml");
}
/**
* Reads data from the data stream and posts it to solr, writes to the
* response to output
*
* @throws IOException
* @throws IndexException
*/
public boolean postData(InputStream data, Integer length, String type)
throws IOException, IndexException {
HttpURLConnection urlc = null;
try {
URL url = new URL(urlStr);
urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestMethod("POST");
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-type", type);
if (null != length)
urlc.setFixedLengthStreamingMode(length);
OutputStream out = null;
try {
out = urlc.getOutputStream();
pipe(data, out);
} finally {
try {
if (out != null)
out.close();
} catch (IOException x) { /* NOOP */
}
}
if (HttpURLConnection.HTTP_OK != urlc.getResponseCode())
throw new IndexException("Server returned an error #" +
urlc.getResponseCode() + " " +
urlc.getResponseMessage());
} finally {
if (urlc != null)
urlc.disconnect();
}
return true;
}
/**
* Pipes everything from the source to the dest. If dest is null, then
* everything is read from source and thrown away.
*/
private static void pipe(InputStream source, OutputStream dest)
throws IOException {
byte[] buf = new byte[1024];
int read = 0;
while ((read = source.read(buf)) >= 0) {
if (null != dest)
dest.write(buf, 0, read);
}
if (null != dest)
dest.flush();
}
/**
* 获取编码格式
*
* @return
*/
private String getEncoding() {
String encoding = System.getProperty("file.encoding");
if (null == encoding || "".equals(encoding)) {
return "UTF-8";
}
return encoding;
}
}
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import com.paic.wcm.search.client.dto.IndexPageDTO;
import com.paic.wcm.search.utils.DESUtil;
/**
* 简单实现,不依赖任何第三方jar包
*/
public class SimpleIndexClient {
public static final String KEY_PA_AUTH = "pa.auth";
private String urlStr;
public SimpleIndexClient(String urlStr, String password) {
try {
password = DESUtil.ecryptString(DESUtil.DEFAULT_SEED, password);
} catch (Exception e) {
throw new RuntimeException("ecrypt error");
}
this.urlStr = urlStr + (urlStr.endsWith("/") ? "" : "/") + "update?"
+ KEY_PA_AUTH + "=" + password;
}
/**
* 索引单个页面文件
*
* @param page
* @return
* @throws IOException
* @throws IndexException
*/
public boolean indexPage(IndexPageDTO page) throws IOException, IndexException {
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\""
+ getEncoding() + "\"?>");
sb.append("<add>");
sb.append(page.toDocString());
sb.append("<commit/>");
sb.append("</add>");
return postXmlStr(sb.toString());
}
/**
* 批量索引多个页面文件
*
* @param list
* @return
* @throws IOException
* @throws IndexException
*/
public boolean indexPageList(List<IndexPageDTO> list) throws IOException, IndexException {
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\""
+ getEncoding() + "\"?>");
sb.append("<add>");
for (IndexPageDTO page : list) {
sb.append(page.toDocString());
}
sb.append("<commit/>");
sb.append("</add>");
return postXmlStr(sb.toString());
}
/**
* 删除单个文件索引
*
* @param link
* @return
* @throws IOException
* @throws IndexException
*/
public boolean deleteIndexByLink(String link) throws IOException, IndexException {
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\""
+ getEncoding() + "\"?>");
sb.append("<update><delete><id>" + link
+ "</id></delete><commit/></update>");
return postXmlStr(sb.toString());
}
/**
* 批量删除多个文件索引
*
* @param links
* @return
* @throws IOException
* @throws IndexException
*/
public boolean deleteIndexByLinkList(List<String> links) throws IOException, IndexException {
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\""
+ getEncoding() + "\"?>");
sb.append("<update>");
for (String link : links) {
sb.append("<delete><id>" + link + "</id></delete>");
}
sb.append("<commit/></update>");
return postXmlStr(sb.toString());
}
/**
* post xml
*
* @param str
* @throws IOException
* @throws IndexException
*/
private boolean postXmlStr(String str) throws IOException, IndexException {
InputStream data = new ByteArrayInputStream(str.getBytes());
return postData(data, str.getBytes().length, "application/xml");
}
/**
* Reads data from the data stream and posts it to solr, writes to the
* response to output
*
* @throws IOException
* @throws IndexException
*/
public boolean postData(InputStream data, Integer length, String type)
throws IOException, IndexException {
HttpURLConnection urlc = null;
try {
URL url = new URL(urlStr);
urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestMethod("POST");
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-type", type);
if (null != length)
urlc.setFixedLengthStreamingMode(length);
OutputStream out = null;
try {
out = urlc.getOutputStream();
pipe(data, out);
} finally {
try {
if (out != null)
out.close();
} catch (IOException x) { /* NOOP */
}
}
if (HttpURLConnection.HTTP_OK != urlc.getResponseCode())
throw new IndexException("Server returned an error #" +
urlc.getResponseCode() + " " +
urlc.getResponseMessage());
} finally {
if (urlc != null)
urlc.disconnect();
}
return true;
}
/**
* Pipes everything from the source to the dest. If dest is null, then
* everything is read from source and thrown away.
*/
private static void pipe(InputStream source, OutputStream dest)
throws IOException {
byte[] buf = new byte[1024];
int read = 0;
while ((read = source.read(buf)) >= 0) {
if (null != dest)
dest.write(buf, 0, read);
}
if (null != dest)
dest.flush();
}
/**
* 获取编码格式
*
* @return
*/
private String getEncoding() {
String encoding = System.getProperty("file.encoding");
if (null == encoding || "".equals(encoding)) {
return "UTF-8";
}
return encoding;
}
}
0 0
- solr SimpleIndexClient
- Solr
- solr
- solr
- solr
- solr
- solr
- Solr
- solr
- solr
- solr
- solr
- Solr
- solr
- Solr
- solr
- solr
- Solr
- Linux 6.2 配置NTP服务
- 安卓手机刷机后如何恢复数据
- git 一些操作
- selenium2 python中的悬停操作
- SQLPLUS中的feedback设置
- solr SimpleIndexClient
- 接着2月份写的文章,股票行情App的成果
- 直接插入排序
- 【PHP】掌握下面常用函数,学php不再难!
- android 屏幕旋转
- android调用系统图片浏览器裁切后出现黑边
- 何为P2P技术?发展前景如何?
- [小技巧] 如何在ubuntu 12.04里使用代理时忽略某些hosts
- bug