incubator-connectors-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1154989 - /incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/
Date Mon, 08 Aug 2011 15:27:45 GMT
Author: kwright
Date: Mon Aug  8 15:27:44 2011
New Revision: 1154989

URL: http://svn.apache.org/viewvc?rev=1154989&view=rev
Log:
Applied second patch

Modified:
    incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnection.java
    incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java
    incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerDelete.java
    incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java
    incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerParam.java

Modified: incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnection.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnection.java?rev=1154989&r1=1154988&r2=1154989&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnection.java
(original)
+++ incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnection.java
Mon Aug  8 15:27:44 2011
@@ -1,14 +1,111 @@
 package org.apache.manifoldcf.agents.output.opensearchserver;
 
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.manifoldcf.agents.output.opensearchserver.OpenSearchServerParam.ParameterEnum;
+import org.apache.manifoldcf.core.interfaces.ManifoldCFException;
+
 public class OpenSearchServerConnection {
 
+	private String serverLocation;
+
+	private String indexName;
+
+	private String userName;
+
+	private String apiKey;
+
+	private String resultCode;
+
+	private String resultDescription;
+
+	private String callUrlSnippet;
+
+	protected OpenSearchServerConnection(OpenSearchServerParam params) {
+		resultCode = null;
+		resultDescription = null;
+		callUrlSnippet = null;
+		serverLocation = params.get(ParameterEnum.SERVERLOCATION);
+		indexName = params.get(ParameterEnum.INDEXNAME);
+		userName = params.get(ParameterEnum.USERNAME);
+		apiKey = params.get(ParameterEnum.APIKEY);
+	}
+
+	protected final String urlEncode(String t) throws ManifoldCFException {
+		try {
+			return URLEncoder.encode(t, "UTF-8");
+		} catch (UnsupportedEncodingException e) {
+			throw new ManifoldCFException(e);
+		}
+	}
+
+	protected StringBuffer getApiUrl(String command) throws ManifoldCFException {
+		StringBuffer url = new StringBuffer(serverLocation);
+		if (!serverLocation.endsWith("/"))
+			url.append('/');
+		url.append(command);
+		url.append("?use=");
+		url.append(urlEncode(indexName));
+		callUrlSnippet = url.toString();
+		if (userName != null && apiKey != null && userName.length() > 0
+				&& apiKey.length() > 0) {
+			url.append("&login=");
+			url.append(urlEncode(userName));
+			url.append("&key=");
+			url.append(apiKey);
+		}
+		return url;
+	}
+
+	protected void call(HttpMethod method) throws ManifoldCFException {
+		HttpClient hc = new HttpClient();
+		try {
+			hc.executeMethod(method);
+		} catch (HttpException e) {
+			throw new ManifoldCFException(e);
+		} catch (IOException e) {
+			throw new ManifoldCFException(e);
+		} finally {
+			if (method != null) {
+				setResultCode(method.getStatusCode());
+				setResultDescription(method.getStatusText());
+				method.releaseConnection();
+			}
+		}
+	}
+
+	private void setResultDescription(String desc) {
+		resultDescription = desc;
+	}
+
 	public String getResultDescription() {
-		// TODO Auto-generated method stub
-		return null;
+		return resultDescription;
+	}
+
+	private void setResultCode(int code) {
+		switch (code) {
+		case 0:
+			resultCode = null;
+			break;
+		case 200:
+			resultCode = "OK";
+			break;
+		default:
+			resultCode = "ERR (" + code + ")";
+			break;
+		}
 	}
 
 	public String getResultCode() {
-		// TODO Auto-generated method stub
-		return null;
+		return resultCode;
+	}
+
+	public String getCallUrlSnippet() {
+		return callUrlSnippet;
 	}
 }

Modified: incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java?rev=1154989&r1=1154988&r2=1154989&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java
(original)
+++ incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java
Mon Aug  8 15:27:44 2011
@@ -6,14 +6,15 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 import java.util.List;
-import java.util.Set;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.manifoldcf.agents.interfaces.IOutputAddActivity;
+import org.apache.manifoldcf.agents.interfaces.IOutputNotifyActivity;
 import org.apache.manifoldcf.agents.interfaces.IOutputRemoveActivity;
 import org.apache.manifoldcf.agents.interfaces.RepositoryDocument;
 import org.apache.manifoldcf.agents.interfaces.ServiceInterruption;
 import org.apache.manifoldcf.agents.output.BaseOutputConnector;
+import org.apache.manifoldcf.agents.output.opensearchserver.OpenSearchServerAction.CommandEnum;
 import org.apache.manifoldcf.core.interfaces.ConfigParams;
 import org.apache.manifoldcf.core.interfaces.IHTTPOutput;
 import org.apache.manifoldcf.core.interfaces.IPostParameters;
@@ -23,11 +24,18 @@ import org.apache.manifoldcf.core.interf
 public class OpenSearchServerConnector extends BaseOutputConnector {
 
 	private final static String OPENSEARCHSERVER_INDEXATION_ACTIVITY = "Indexation";
-	private final static String OPENSEARCHSERVER_DELETION_ACTIVTY = "Deletion";
+	private final static String OPENSEARCHSERVER_DELETION_ACTIVITY = "Deletion";
+	private final static String OPENSEARCHSERVER_OPTIMIZE_ACTIVITY = "Optimize";
 
 	private final static String[] OPENSEARCHSERVER_ACTIVITIES = {
 			OPENSEARCHSERVER_INDEXATION_ACTIVITY,
-			OPENSEARCHSERVER_DELETION_ACTIVTY };
+			OPENSEARCHSERVER_DELETION_ACTIVITY,
+			OPENSEARCHSERVER_OPTIMIZE_ACTIVITY };
+
+	private final static String OPENSEARCHSERVER_TAB_PARAMETER = "Parameters";
+
+	// private final static String OPENSEARCHSERVER_TAB_FIELDMAPPING =
+	// "Field mapping";
 
 	@Override
 	public String[] getActivitiesList() {
@@ -43,14 +51,14 @@ public class OpenSearchServerConnector e
 	 * @throws ManifoldCFException
 	 */
 	private void outputResource(String resName, IHTTPOutput out,
-			Set<OpenSearchServerParam> params) throws ManifoldCFException {
+			OpenSearchServerParam params) throws ManifoldCFException {
 		InputStream is = getClass().getResourceAsStream(resName);
 		BufferedReader br = null;
 		try {
 			br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
 			String line;
 			while ((line = br.readLine()) != null)
-				out.println(OpenSearchServerParam.replace(line, params));
+				out.println(params.replace(line));
 		} catch (UnsupportedEncodingException e) {
 			throw new ManifoldCFException(e);
 		} catch (IOException e) {
@@ -69,7 +77,7 @@ public class OpenSearchServerConnector e
 			throws ManifoldCFException, IOException {
 		super.outputConfigurationHeader(threadContext, out, parameters,
 				tabsArray);
-		tabsArray.add("Parameters");
+		tabsArray.add(OPENSEARCHSERVER_TAB_PARAMETER);
 	}
 
 	@Override
@@ -77,21 +85,51 @@ public class OpenSearchServerConnector e
 			IHTTPOutput out, ConfigParams parameters, String tabName)
 			throws ManifoldCFException, IOException {
 		super.outputConfigurationBody(threadContext, out, parameters, tabName);
-		Set<OpenSearchServerParam> params = OpenSearchServerParam
-				.getParameters(OpenSearchServerParam.CONFIGURATIONLIST,
-						parameters);
-		if ("Parameters".equals(tabName))
-			outputResource("configuration.html", out, params);
+		if (OPENSEARCHSERVER_TAB_PARAMETER.equals(tabName)) {
+			outputResource("configuration.html", out, getParameters(parameters));
+		}
+	}
+
+	// @Override
+	// public void outputSpecificationHeader(IHTTPOutput out,
+	// OutputSpecification os, List<String> tabsArray)
+	// throws ManifoldCFException, IOException {
+	// super.outputSpecificationHeader(out, os, tabsArray);
+	// tabsArray.add(OPENSEARCHSERVER_TAB_FIELDMAPPING);
+	// }
+	//
+	// @Override
+	// public void outputSpecificationBody(IHTTPOutput out,
+	// OutputSpecification os, String tabName) throws ManifoldCFException,
+	// IOException {
+	// super.outputSpecificationBody(out, os, tabName);
+	// if (OPENSEARCHSERVER_TAB_FIELDMAPPING.equals(tabName)) {
+	// Set<OpenSearchServerParam> params = OpenSearchServerParam
+	// .getParameters(OpenSearchServerParam.SPECIFICATIONLIST, os);
+	// outputResource("fieldmapping.html", out, params);
+	// }
+	// }
+
+	/**
+	 * Build a Set of OpenSearchServerParam. If configParams is null,
+	 * getConfiguration() is used.
+	 * 
+	 * @param configParams
+	 * @return
+	 */
+	final private OpenSearchServerParam getParameters(ConfigParams configParams) {
+		if (configParams == null)
+			configParams = getConfiguration();
+		OpenSearchServerParam parameters = new OpenSearchServerParam(
+				OpenSearchServerParam.CONFIGURATIONLIST, configParams);
+		return parameters;
 	}
 
 	@Override
 	public void viewConfiguration(IThreadContext threadContext,
 			IHTTPOutput out, ConfigParams parameters)
 			throws ManifoldCFException, IOException {
-		Set<OpenSearchServerParam> params = OpenSearchServerParam
-				.getParameters(OpenSearchServerParam.CONFIGURATIONLIST,
-						parameters);
-		outputResource("view.html", out, params);
+		outputResource("view.html", out, getParameters(parameters));
 	}
 
 	@Override
@@ -113,7 +151,7 @@ public class OpenSearchServerConnector e
 		try {
 			long startTime = System.currentTimeMillis();
 			OpenSearchServerIndex oi = new OpenSearchServerIndex(documentURI,
-					inputStream);
+					inputStream, getParameters(null));
 			activities.recordActivity(startTime,
 					OPENSEARCHSERVER_INDEXATION_ACTIVITY,
 					document.getBinaryLength(), documentURI,
@@ -129,11 +167,24 @@ public class OpenSearchServerConnector e
 			IOutputRemoveActivity activities) throws ManifoldCFException,
 			ServiceInterruption {
 		long startTime = System.currentTimeMillis();
-		OpenSearchServerDelete od = new OpenSearchServerDelete(documentURI);
-		activities.recordActivity(startTime, OPENSEARCHSERVER_DELETION_ACTIVTY,
-				null, documentURI, od.getResultCode(),
-				od.getResultDescription());
-		System.out.flush();
+		OpenSearchServerDelete od = new OpenSearchServerDelete(documentURI,
+				getParameters(null));
+		activities.recordActivity(startTime,
+				OPENSEARCHSERVER_DELETION_ACTIVITY, null, documentURI,
+				od.getResultCode(), od.getResultDescription());
+	}
+
+	@Override
+	public void noteJobComplete(IOutputNotifyActivity activities)
+			throws ManifoldCFException, ServiceInterruption {
+		long startTime = System.currentTimeMillis();
+		OpenSearchServerAction oo = new OpenSearchServerAction(
+				CommandEnum.optimize, getParameters(null));
+		activities.recordActivity(startTime,
+				OPENSEARCHSERVER_OPTIMIZE_ACTIVITY, null,
+				oo.getCallUrlSnippet(), oo.getResultCode(),
+				oo.getResultDescription());
+
 	}
 
 }

Modified: incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerDelete.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerDelete.java?rev=1154989&r1=1154988&r2=1154989&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerDelete.java
(original)
+++ incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerDelete.java
Mon Aug  8 15:27:44 2011
@@ -1,9 +1,17 @@
 package org.apache.manifoldcf.agents.output.opensearchserver;
 
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.manifoldcf.core.interfaces.ManifoldCFException;
+
 public class OpenSearchServerDelete extends OpenSearchServerConnection {
 
-	public OpenSearchServerDelete(String documentURI) {
-		// TODO Auto-generated constructor stub
+	public OpenSearchServerDelete(String documentURI,
+			OpenSearchServerParam params) throws ManifoldCFException {
+		super(params);
+		StringBuffer url = getApiUrl("delete");
+		url.append("&uniq=");
+		url.append(urlEncode(documentURI));
+		GetMethod method = new GetMethod(url.toString());
+		call(method);
 	}
-
 }

Modified: incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java?rev=1154989&r1=1154988&r2=1154989&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java
(original)
+++ incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java
Mon Aug  8 15:27:44 2011
@@ -1,10 +1,63 @@
 package org.apache.manifoldcf.agents.output.opensearchserver;
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
+
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.RequestEntity;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.manifoldcf.core.interfaces.ManifoldCFException;
 
 public class OpenSearchServerIndex extends OpenSearchServerConnection {
 
-	public OpenSearchServerIndex(String documentURI, InputStream inputStream) {
+	public OpenSearchServerIndex(String documentURI, InputStream inputStream,
+			OpenSearchServerParam params) throws ManifoldCFException {
+		super(params);
+		try {
+			StringBuffer url = getApiUrl("update");
+			PutMethod put = new PutMethod(url.toString());
+			RequestEntity entity = new StringRequestEntity(getXML(documentURI,
+					inputStream), "text/xml", "UTF-8");
+			put.setRequestEntity(entity);
+			call(put);
+		} catch (UnsupportedEncodingException e) {
+			throw new ManifoldCFException(e);
+		} catch (XMLStreamException e) {
+			throw new ManifoldCFException(e);
+		} catch (IOException e) {
+			throw new ManifoldCFException(e);
+		}
 	}
 
+	private String getXML(String documentURI, InputStream inputStream)
+			throws XMLStreamException, IOException {
+		String fileName = FilenameUtils.getName(documentURI);
+		XMLOutputFactory factory = XMLOutputFactory.newInstance();
+		StringWriter sw = new StringWriter();
+		XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
+		writer.writeStartDocument("UTF-8", "1.0");
+		writer.writeStartElement("index");
+		writer.writeStartElement("document");
+		writer.writeStartElement("field");
+		writer.writeAttribute("name", "uri");
+		writer.writeStartElement("value");
+		writer.writeCData(documentURI);
+		writer.writeEndElement(); // ends value
+		writer.writeEndElement(); // ends field
+		writer.writeStartElement("binary");
+		writer.writeAttribute("fileName", fileName);
+		writer.writeCharacters(new String(Base64.encodeBase64(
+				IOUtils.toByteArray(inputStream), true)));
+		writer.writeEndDocument();
+		return sw.toString();
+	}
 }

Modified: incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerParam.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerParam.java?rev=1154989&r1=1154988&r2=1154989&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerParam.java
(original)
+++ incubator/lcf/branches/CONNECTORS-224/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerParam.java
Mon Aug  8 15:27:44 2011
@@ -1,24 +1,27 @@
 package org.apache.manifoldcf.agents.output.opensearchserver;
 
-import java.util.HashSet;
-import java.util.Set;
+import java.util.HashMap;
+import java.util.Map;
 
+import org.apache.manifoldcf.agents.output.opensearchserver.OpenSearchServerParam.ParameterEnum;
 import org.apache.manifoldcf.core.interfaces.ConfigParams;
 import org.apache.manifoldcf.core.interfaces.IPostParameters;
 
-public class OpenSearchServerParam {
+public class OpenSearchServerParam extends HashMap<ParameterEnum, String> {
 
 	/**
 	 * Parameters constants
 	 */
-	private enum ParameterEnum {
+	public enum ParameterEnum {
 		SERVERLOCATION("http://localhost:8080/"),
 
 		INDEXNAME("index"),
 
 		USERNAME(""),
 
-		APIKEY("");
+		APIKEY(""),
+
+		FIELDLIST("");
 
 		final protected String defaultValue;
 
@@ -27,14 +30,6 @@ public class OpenSearchServerParam {
 		}
 	}
 
-	final public ParameterEnum key;
-	final public String value;
-
-	protected OpenSearchServerParam(ParameterEnum key, String value) {
-		this.key = key;
-		this.value = value;
-	}
-
 	/**
 	 * Parameters used for the configuration
 	 */
@@ -42,36 +37,37 @@ public class OpenSearchServerParam {
 			ParameterEnum.SERVERLOCATION, ParameterEnum.INDEXNAME,
 			ParameterEnum.USERNAME, ParameterEnum.APIKEY };
 
+	final public static ParameterEnum[] SPECIFICATIONLIST = { ParameterEnum.FIELDLIST };
+
+	private static final long serialVersionUID = -1593234685772720029L;
+
 	/**
 	 * Build a set of OpenSearchServerParameters by reading ConfigParams. If the
 	 * value returned by ConfigParams.getParameter is null, the default value is
 	 * set.
 	 * 
+	 * @param paramList
 	 * @param params
-	 * @return
 	 */
-	final public static Set<OpenSearchServerParam> getParameters(
-			ParameterEnum[] paramList, ConfigParams params) {
-		Set<OpenSearchServerParam> ossParams = new HashSet<OpenSearchServerParam>();
+	public OpenSearchServerParam(ParameterEnum[] paramList, ConfigParams params) {
 		for (ParameterEnum param : paramList) {
 			String value = params.getParameter(param.name());
 			if (value == null)
 				value = param.defaultValue;
-			ossParams.add(new OpenSearchServerParam(param, value));
+			put(param, value);
 		}
-		return ossParams;
 	}
 
 	/**
 	 * Replace the variables ${PARAMNAME} with the value contained in the set.
 	 * 
 	 * @param text
-	 * @param params
+	 * @return
 	 */
-	final public static String replace(String text,
-			Set<OpenSearchServerParam> params) {
-		for (OpenSearchServerParam param : params)
-			text = text.replace("${" + param.key.name() + "}", param.value);
+	final public String replace(String text) {
+		for (Map.Entry<ParameterEnum, String> entry : this.entrySet())
+			text = text.replace("${" + entry.getKey().name() + "}",
+					entry.getValue());
 		return text;
 	}
 
@@ -83,4 +79,5 @@ public class OpenSearchServerParam {
 				parameters.setParameter(param.name(), p);
 		}
 	}
+
 }



Mime
View raw message