Author: kwright
Date: Mon Aug 11 00:50:31 2014
New Revision: 1617187

URL: http://svn.apache.org/r1617187
Log:
Fix for CONNECTORS-1000.

Modified:
    manifoldcf/trunk/CHANGES.txt
    
manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java
    
manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java

Modified: manifoldcf/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/manifoldcf/trunk/CHANGES.txt?rev=1617187&r1=1617186&r2=1617187&view=diff
==============================================================================
--- manifoldcf/trunk/CHANGES.txt (original)
+++ manifoldcf/trunk/CHANGES.txt Mon Aug 11 00:50:31 2014
@@ -3,6 +3,10 @@ $Id$
 
 ======================= 1.7-dev =====================
 
+CONNECTORS-1000: Add support for auth to open search server
+connector.
+(Emmanuel Keller)
+
 CONNECTORS-1004: CMIS connector was not using latest version
 of document, which matters for repositories like Alfresco.
 (Prasad Perera, Karl Wright)

Modified: 
manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java
URL: 
http://svn.apache.org/viewvc/manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java?rev=1617187&r1=1617186&r2=1617187&view=diff
==============================================================================
--- 
manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java
 (original)
+++ 
manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerConnector.java
 Mon Aug 11 00:50:31 2014
@@ -17,51 +17,44 @@
 
 package org.apache.manifoldcf.agents.output.opensearchserver;
 
-
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.TreeMap;
-import java.util.Locale;
-import java.util.HashMap;
 
-import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.commons.io.FilenameUtils;
 import org.apache.http.client.HttpClient;
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
-import org.apache.http.impl.client.HttpClientBuilder;
-import org.apache.http.protocol.HttpRequestExecutor;
-import org.apache.http.impl.client.HttpClients;
 import org.apache.http.client.config.RequestConfig;
-import org.apache.http.client.CredentialsProvider;
-import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.apache.http.config.SocketConfig;
-import org.apache.http.client.HttpRequestRetryHandler;
-import org.apache.http.protocol.HttpContext;
-import org.apache.commons.io.FilenameUtils;
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.protocol.HttpRequestExecutor;
 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.agents.output.opensearchserver.OpenSearchServerConnection.Result;
-import org.apache.manifoldcf.core.interfaces.Specification;
 import org.apache.manifoldcf.core.interfaces.ConfigParams;
 import org.apache.manifoldcf.core.interfaces.ConfigurationNode;
 import org.apache.manifoldcf.core.interfaces.IHTTPOutput;
 import org.apache.manifoldcf.core.interfaces.IPostParameters;
 import org.apache.manifoldcf.core.interfaces.IThreadContext;
 import org.apache.manifoldcf.core.interfaces.ManifoldCFException;
+import org.apache.manifoldcf.core.interfaces.Specification;
 import org.apache.manifoldcf.core.interfaces.SpecificationNode;
 import org.apache.manifoldcf.core.interfaces.VersionContext;
-import org.apache.manifoldcf.core.system.Logging;
 import org.json.JSONException;
 import org.json.JSONObject;
 
-public class OpenSearchServerConnector extends BaseOutputConnector {
+public class OpenSearchServerConnector extends BaseOutputConnector
+{
 
   private final static String OPENSEARCHSERVER_INDEXATION_ACTIVITY = 
"Indexation";
   private final static String OPENSEARCHSERVER_DELETION_ACTIVITY = "Deletion";
@@ -75,22 +68,22 @@ public class OpenSearchServerConnector e
 
   private final static String OPENSEARCHSERVER_TAB_MESSAGE = 
"OpenSearchServerConnector.OpenSearchServer";
   private final static String PARAMETERS_TAB_MESSAGE = 
"OpenSearchServerConnector.Parameters";
-      
+
   // Velocity templates
   // These are not broken down by tabs because the design of this connector 
makes it difficult to do it that way.
 
   /** Forward to the HTML template to edit the configuration parameters */
   private static final String EDIT_CONFIG_FORWARD = "editConfiguration.html";
-  
+
   /** Forward to the HTML template to view the configuration parameters */
   private static final String VIEW_CONFIG_FORWARD = "viewConfiguration.html";
- 
+
   /** Forward to the javascript to check the configuration parameters */
   private static final String EDIT_CONFIG_HEADER_FORWARD = 
"editConfiguration.js";
 
   /** Forward to the template to view the specification parameters for the job 
*/
   private static final String VIEW_SPEC_FORWARD = "viewSpecification.html";
-  
+
   /** Forward to the template to edit the configuration parameters for the job 
*/
   private static final String EDIT_SPEC_FORWARD = "editSpecification.html";
 
@@ -109,7 +102,8 @@ public class OpenSearchServerConnector e
   private String specsCacheOutpuDescription;
   private OpenSearchServerSpecs specsCache;
 
-  public OpenSearchServerConnector() {
+  public OpenSearchServerConnector()
+  {
     specsCacheOutpuDescription = null;
     specsCache = null;
   }
@@ -119,9 +113,9 @@ public class OpenSearchServerConnector e
   {
     super.connect(configParams);
   }
-  
+
   protected HttpClient getSession()
-    throws ManifoldCFException
+      throws ManifoldCFException
   {
     if (client == null)
     {
@@ -130,26 +124,24 @@ public class OpenSearchServerConnector e
       final int executorTimeout = 300000;
       final int socketTimeout = 60000;
       final int connectionTimeout = 60000;
- 
+
       RequestConfig.Builder requestBuilder = RequestConfig.custom()
-           .setCircularRedirectsAllowed(true)
-           .setSocketTimeout(socketTimeout)
-           .setStaleConnectionCheckEnabled(true)
-           .setExpectContinueEnabled(true)
-           .setConnectTimeout(connectionTimeout)
-           .setConnectionRequestTimeout(socketTimeout);
+          .setCircularRedirectsAllowed(true).setSocketTimeout(socketTimeout)
+          .setStaleConnectionCheckEnabled(true).setExpectContinueEnabled(true)
+          .setConnectTimeout(connectionTimeout)
+          .setConnectionRequestTimeout(socketTimeout);
 
-      HttpClientBuilder clientBuilder = HttpClients.custom()
+      HttpClientBuilder clientBuilder = HttpClients
+          .custom()
           .setConnectionManager(connectionManager)
-           .setMaxConnTotal(1)
-           .disableAutomaticRetries()
-           .setDefaultRequestConfig(requestBuilder.build())
-           .setRequestExecutor(new HttpRequestExecutor(executorTimeout))
-            .setDefaultSocketConfig(SocketConfig.custom()
-               .setTcpNoDelay(true)
-               .setSoTimeout(socketTimeout)
-               .build());
-          
+          .setMaxConnTotal(1)
+          .disableAutomaticRetries()
+          .setDefaultRequestConfig(requestBuilder.build())
+          .setRequestExecutor(new HttpRequestExecutor(executorTimeout))
+          .setDefaultSocketConfig(
+              SocketConfig.custom().setTcpNoDelay(true)
+                  .setSoTimeout(socketTimeout).build());
+
       client = clientBuilder.build();
 
     }
@@ -167,19 +159,18 @@ public class OpenSearchServerConnector e
     client = null;
     expirationTime = -1L;
   }
-  
+
   @Override
   public void disconnect()
-    throws ManifoldCFException
+      throws ManifoldCFException
   {
     super.disconnect();
     closeSession();
   }
-  
-  
+
   @Override
   public void poll()
-    throws ManifoldCFException
+      throws ManifoldCFException
   {
     super.poll();
     if (connectionManager != null)
@@ -191,10 +182,12 @@ public class OpenSearchServerConnector e
     }
   }
 
-  /** This method is called to assess whether to count this connector instance 
should
-  * actually be counted as being connected.
-  *@return true if the connector instance is actually connected.
-  */
+  /**
+   * This method is called to assess whether to count this connector instance 
should
+   * actually be counted as being connected.
+   *
+   * @return true if the connector instance is actually connected.
+   */
   @Override
   public boolean isConnected()
   {
@@ -202,7 +195,8 @@ public class OpenSearchServerConnector e
   }
 
   @Override
-  public String[] getActivitiesList() {
+  public String[] getActivitiesList()
+  {
     return OPENSEARCHSERVER_ACTIVITIES;
   }
 
@@ -215,135 +209,173 @@ public class OpenSearchServerConnector e
    * @throws ManifoldCFException
    */
   private static void outputResource(String resName, IHTTPOutput out,
-    Locale locale, OpenSearchServerParam params, String tabName,
-    Integer sequenceNumber, Integer actualSequenceNumber) throws 
ManifoldCFException {
-    Map<String,String> paramMap = null;
-    if (params != null) {
+      Locale locale, OpenSearchServerParam params, String tabName,
+      Integer sequenceNumber, Integer actualSequenceNumber)
+      throws ManifoldCFException
+  {
+    Map<String, String> paramMap = null;
+    if (params != null)
+    {
       paramMap = params.buildMap();
-      if (tabName != null) {
+      if (tabName != null)
+      {
         paramMap.put("TabName", tabName);
       }
       if (actualSequenceNumber != null)
-        paramMap.put("SelectedNum",actualSequenceNumber.toString());
+        paramMap.put("SelectedNum", actualSequenceNumber.toString());
     }
     else
     {
-      paramMap = new HashMap<String,String>();
+      paramMap = new HashMap<String, String>();
     }
     if (sequenceNumber != null)
-      paramMap.put("SeqNum",sequenceNumber.toString());
+      paramMap.put("SeqNum", sequenceNumber.toString());
 
-    Messages.outputResourceWithVelocity(out,locale,resName,paramMap,false);
+    Messages.outputResourceWithVelocity(out, locale, resName, paramMap, false);
   }
 
   @Override
   public void outputConfigurationHeader(IThreadContext threadContext,
-      IHTTPOutput out, Locale locale, ConfigParams parameters, List<String> 
tabsArray)
-      throws ManifoldCFException, IOException {
-    super.outputConfigurationHeader(threadContext, out, locale, parameters, 
tabsArray);
-    tabsArray.add(Messages.getString(locale,PARAMETERS_TAB_MESSAGE));
-    outputResource(EDIT_CONFIG_HEADER_FORWARD, out, locale, null, null, null, 
null);
+      IHTTPOutput out, Locale locale, ConfigParams parameters,
+      List<String> tabsArray)
+      throws ManifoldCFException, IOException
+  {
+    super.outputConfigurationHeader(threadContext, out, locale, parameters,
+        tabsArray);
+    tabsArray.add(Messages.getString(locale, PARAMETERS_TAB_MESSAGE));
+    outputResource(EDIT_CONFIG_HEADER_FORWARD, out, locale, null, null, null,
+        null);
   }
 
   @Override
   public void outputConfigurationBody(IThreadContext threadContext,
       IHTTPOutput out, Locale locale, ConfigParams parameters, String tabName)
-      throws ManifoldCFException, IOException {
-    super.outputConfigurationBody(threadContext, out, locale, parameters, 
tabName);
+      throws ManifoldCFException, IOException
+  {
+    super.outputConfigurationBody(threadContext, out, locale, parameters,
+        tabName);
     OpenSearchServerConfig config = this.getConfigParameters(parameters);
-    outputResource(EDIT_CONFIG_FORWARD, out, locale, config, tabName, null, 
null);
+    outputResource(EDIT_CONFIG_FORWARD, out, locale, config, tabName, null,
+        null);
   }
 
-  /** Obtain the name of the form check javascript method to call.
-  *@param connectionSequenceNumber is the unique number of this connection 
within the job.
-  *@return the name of the form check javascript method.
+  /**
+  * Obtain the name of the form check javascript method to call.
+  *
+  * @param connectionSequenceNumber is the unique number of this connection 
within the job.
+  * @return the name of the form check javascript method.
   */
   @Override
   public String getFormCheckJavascriptMethodName(int connectionSequenceNumber)
   {
-    return "s"+connectionSequenceNumber+"_checkSpecification";
+    return "s" + connectionSequenceNumber + "_checkSpecification";
   }
 
-  /** Obtain the name of the form presave check javascript method to call.
-  *@param connectionSequenceNumber is the unique number of this connection 
within the job.
-  *@return the name of the form presave check javascript method.
+  /**
+  * Obtain the name of the form presave check javascript method to call.
+  *
+  * @param connectionSequenceNumber is the unique number of this connection 
within the job.
+  * @return the name of the form presave check javascript method.
   */
   @Override
-  public String getFormPresaveCheckJavascriptMethodName(int 
connectionSequenceNumber)
+  public String getFormPresaveCheckJavascriptMethodName(
+      int connectionSequenceNumber)
   {
-    return "s"+connectionSequenceNumber+"_checkSpecificationForSave";
+    return "s" + connectionSequenceNumber + "_checkSpecificationForSave";
   }
 
-  /** Output the specification header section.
-  * This method is called in the head section of a job page which has selected 
a pipeline connection of the current type.  Its purpose is to add the required 
tabs
-  * to the list, and to output any javascript methods that might be needed by 
the job editing HTML.
-  *@param out is the output to which any HTML should be sent.
-  *@param locale is the preferred local of the output.
-  *@param os is the current pipeline specification for this connection.
-  *@param connectionSequenceNumber is the unique number of this connection 
within the job.
-  *@param tabsArray is an array of tab names.  Add to this array any tab names 
that are specific to the connector.
+  /**
+  * Output the specification header section. This method is called in the head
+  * section of a job page which has selected a pipeline connection of the
+  * current type. Its purpose is to add the required tabs to the list, and to
+  * output any javascript methods that might be needed by the job editing HTML.
+  *
+  * @param out is the output to which any HTML should be sent.
+  * @param locale is the preferred local of the output.
+  * @param os is the current pipeline specification for this connection.
+  * @param connectionSequenceNumber is the unique number of this connection 
within the job.
+  * @param tabsArray is an array of tab names. Add to this array any tab names 
that are specific to the connector.
   */
   @Override
-  public void outputSpecificationHeader(IHTTPOutput out, Locale locale, 
Specification os,
-    int connectionSequenceNumber, List<String> tabsArray)
-    throws ManifoldCFException, IOException {
-    super.outputSpecificationHeader(out, locale, os, connectionSequenceNumber, 
tabsArray);
-    tabsArray.add(Messages.getString(locale,OPENSEARCHSERVER_TAB_MESSAGE));
-    outputResource(EDIT_SPEC_HEADER_FORWARD, out, locale, null, null, new 
Integer(connectionSequenceNumber), null);
+  public void outputSpecificationHeader(IHTTPOutput out, Locale locale,
+      Specification os, int connectionSequenceNumber, List<String> tabsArray)
+      throws ManifoldCFException, IOException
+  {
+    super.outputSpecificationHeader(out, locale, os, connectionSequenceNumber,
+        tabsArray);
+    tabsArray.add(Messages.getString(locale, OPENSEARCHSERVER_TAB_MESSAGE));
+    outputResource(EDIT_SPEC_HEADER_FORWARD, out, locale, null, null,
+        new Integer(connectionSequenceNumber), null);
   }
 
-  final private SpecificationNode getSpecNode(Specification os) {
+  final private SpecificationNode getSpecNode(Specification os)
+  {
     int l = os.getChildCount();
-    for (int i = 0; i < l; i++) {
+    for (int i = 0; i < l; i++)
+    {
       SpecificationNode node = os.getChild(i);
       if (OpenSearchServerSpecs.OPENSEARCHSERVER_SPECS_NODE.equals(node
-          .getType())) {
+          .getType()))
+      {
         return node;
       }
     }
     return null;
   }
 
-  /** Output the specification body section.
-  * This method is called in the body section of a job page which has selected 
a pipeline connection of the current type.  Its purpose is to present the 
required form elements for editing.
-  * The coder can presume that the HTML that is output from this configuration 
will be within appropriate <html>, <body>, and <form> tags.  The name of the
-  * form is "editjob".
-  *@param out is the output to which any HTML should be sent.
-  *@param locale is the preferred local of the output.
-  *@param os is the current pipeline specification for this job.
-  *@param connectionSequenceNumber is the unique number of this connection 
within the job.
-  *@param actualSequenceNumber is the connection within the job that has 
currently been selected.
-  *@param tabName is the current tab name.
+  /**
+  * Output the specification body section. This method is called in the body
+  * section of a job page which has selected a pipeline connection of the
+  * current type. Its purpose is to present the required form elements for
+  * editing. The coder can presume that the HTML that is output from this
+  * configuration will be within appropriate <html>, <body>, and <form> tags.
+  * The name of the form is "editjob".
+  *
+  * @param out is the output to which any HTML should be sent.
+  * @param locale is the preferred local of the output.
+  * @param os is the current pipeline specification for this job.
+  * @param connectionSequenceNumber is the unique number of this connection 
within the job.
+  * @param actualSequenceNumber is the connection within the job that has 
currently been selected.
+  * @param tabName is the current tab name.
   */
   @Override
-  public void outputSpecificationBody(IHTTPOutput out, Locale locale, 
Specification os,
-    int connectionSequenceNumber, int actualSequenceNumber, String tabName)
-    throws ManifoldCFException, IOException {
+  public void outputSpecificationBody(IHTTPOutput out, Locale locale,
+      Specification os, int connectionSequenceNumber, int actualSequenceNumber,
+      String tabName)
+      throws ManifoldCFException, IOException
+  {
     OpenSearchServerSpecs specs = getSpecParameters(os);
-    outputResource(EDIT_SPEC_FORWARD, out, locale, specs, tabName, new 
Integer(connectionSequenceNumber), new Integer(actualSequenceNumber));
+    outputResource(EDIT_SPEC_FORWARD, out, locale, specs, tabName, new Integer(
+        connectionSequenceNumber), new Integer(actualSequenceNumber));
   }
 
-  /** Process a specification post.
-  * This method is called at the start of job's edit or view page, whenever 
there is a possibility that form data for a connection has been
-  * posted.  Its purpose is to gather form information and modify the 
transformation specification accordingly.
-  * The name of the posted form is "editjob".
-  *@param variableContext contains the post data, including binary file-upload 
information.
-  *@param locale is the preferred local of the output.
-  *@param os is the current pipeline specification for this job.
-  *@param connectionSequenceNumber is the unique number of this connection 
within the job.
-  *@return null if all is well, or a string error message if there is an error 
that should prevent saving of the job (and cause a redirection to an error 
page).
-  */
+  /**
+   * Process a specification post. This method is called at the start of job's
+   * edit or view page, whenever there is a possibility that form data for a
+   * connection has been posted. Its purpose is to gather form information and
+   * modify the transformation specification accordingly. The name of the 
posted
+   * form is "editjob".
+   *
+   * @param variableContext contains the post data, including binary 
file-upload information.
+   * @param locale is the preferred local of the output.
+   * @param os is the current pipeline specification for this job.
+   * @param connectionSequenceNumber is the unique number of this connection 
within the job.
+   * @return null if all is well, or a string error message if there is an 
error that should prevent saving of the job (and cause a redirection to an 
error page).
+   */
   @Override
-  public String processSpecificationPost(IPostParameters variableContext, 
Locale locale, Specification os,
-    int connectionSequenceNumber)
-    throws ManifoldCFException {
+  public String processSpecificationPost(IPostParameters variableContext,
+      Locale locale, Specification os, int connectionSequenceNumber)
+      throws ManifoldCFException
+  {
     ConfigurationNode specNode = getSpecNode(os);
     boolean bAdd = (specNode == null);
-    if (bAdd) {
+    if (bAdd)
+    {
       specNode = new SpecificationNode(
           OpenSearchServerSpecs.OPENSEARCHSERVER_SPECS_NODE);
     }
-    OpenSearchServerSpecs.contextToSpecNode(variableContext, specNode, 
connectionSequenceNumber);
+    OpenSearchServerSpecs.contextToSpecNode(variableContext, specNode,
+        connectionSequenceNumber);
     if (bAdd)
       os.addChild(os.getChildCount(), specNode);
     return null;
@@ -356,21 +388,26 @@ public class OpenSearchServerConnector e
    * @param configParams
    */
   final private OpenSearchServerConfig getConfigParameters(
-      ConfigParams configParams) {
+      ConfigParams configParams)
+  {
     if (configParams == null)
       configParams = getConfiguration();
     return new OpenSearchServerConfig(configParams);
   }
 
   final private OpenSearchServerSpecs getSpecParameters(Specification os)
-      throws ManifoldCFException {
+      throws ManifoldCFException
+  {
     return new OpenSearchServerSpecs(getSpecNode(os));
   }
 
   final private OpenSearchServerSpecs getSpecsCache(String outputDescription)
-      throws ManifoldCFException {
-    try {
-      synchronized (this) {
+      throws ManifoldCFException
+  {
+    try
+    {
+      synchronized (this)
+      {
         if (!outputDescription.equals(specsCacheOutpuDescription))
           specsCache = null;
         if (specsCache == null)
@@ -378,21 +415,25 @@ public class OpenSearchServerConnector e
               outputDescription));
         return specsCache;
       }
-    } catch (JSONException e) {
+    }
+    catch (JSONException e)
+    {
       throw new ManifoldCFException(e);
     }
   }
 
   @Override
   public VersionContext getPipelineDescription(Specification os)
-      throws ManifoldCFException {
+      throws ManifoldCFException
+  {
     OpenSearchServerSpecs specs = new OpenSearchServerSpecs(getSpecNode(os));
-    return new VersionContext(specs.toJson().toString(),params,os);
+    return new VersionContext(specs.toJson().toString(), params, os);
   }
 
   @Override
   public boolean checkLengthIndexable(String outputDescription, long length)
-      throws ManifoldCFException, ServiceInterruption {
+      throws ManifoldCFException, ServiceInterruption
+  {
     OpenSearchServerSpecs specs = getSpecsCache(outputDescription);
     long maxFileSize = specs.getMaxFileSize();
     if (length > maxFileSize)
@@ -402,80 +443,104 @@ public class OpenSearchServerConnector e
 
   @Override
   public boolean checkDocumentIndexable(String outputDescription, File 
localFile)
-      throws ManifoldCFException, ServiceInterruption {
+      throws ManifoldCFException, ServiceInterruption
+  {
     return true;
   }
 
   @Override
   public boolean checkMimeTypeIndexable(String outputDescription,
-      String mimeType) throws ManifoldCFException, ServiceInterruption {
+      String mimeType)
+      throws ManifoldCFException, ServiceInterruption
+  {
     OpenSearchServerSpecs specs = getSpecsCache(outputDescription);
     return specs.checkMimeType(mimeType);
   }
 
-  /** Pre-determine whether a document's URL is indexable by this connector.  
This method is used by participating repository connectors
-  * to help filter out documents that are not worth indexing.
-  *@param outputDescription is the document's output version.
-  *@param url is the URL of the document.
-  *@return true if the file is indexable.
-  */
+  /**
+   * Pre-determine whether a document's URL is indexable by this connector. 
This
+   * method is used by participating repository connectors to help filter out
+   * documents that are not worth indexing.
+   *
+   * @param outputDescription
+   *          is the document's output version.
+   * @param url
+   *          is the URL of the document.
+   * @return true if the file is indexable.
+   */
   @Override
   public boolean checkURLIndexable(String outputDescription, String url)
-    throws ManifoldCFException, ServiceInterruption {
+      throws ManifoldCFException, ServiceInterruption
+  {
     OpenSearchServerSpecs specs = getSpecsCache(outputDescription);
     return specs.checkExtension(FilenameUtils.getExtension(url));
   }
-    
+
   @Override
   public void viewConfiguration(IThreadContext threadContext, IHTTPOutput out,
-      Locale locale, ConfigParams parameters) throws ManifoldCFException, 
IOException {
-    outputResource(VIEW_CONFIG_FORWARD, out, locale, 
getConfigParameters(parameters), null, null, null);
+      Locale locale, ConfigParams parameters)
+      throws ManifoldCFException, IOException
+  {
+    outputResource(VIEW_CONFIG_FORWARD, out, locale,
+        getConfigParameters(parameters), null, null, null);
   }
 
-  /** View specification.
-  * This method is called in the body section of a job's view page.  Its 
purpose is to present the pipeline specification information to the user.
-  * The coder can presume that the HTML that is output from this configuration 
will be within appropriate <html> and <body> tags.
-  *@param out is the output to which any HTML should be sent.
-  *@param locale is the preferred local of the output.
-  *@param connectionSequenceNumber is the unique number of this connection 
within the job.
-  *@param os is the current pipeline specification for this job.
-  */
+  /**
+   * View specification. This method is called in the body section of a job's
+   * view page. Its purpose is to present the pipeline specification 
information
+   * to the user. The coder can presume that the HTML that is output from this
+   * configuration will be within appropriate <html> and <body> tags.
+   *
+   * @param out is the output to which any HTML should be sent.
+   * @param locale is the preferred local of the output.
+   * @param connectionSequenceNumber is the unique number of this connection 
within the job.
+   * @param os is the current pipeline specification for this job.
+   */
   @Override
-  public void viewSpecification(IHTTPOutput out, Locale locale, Specification 
os,
-    int connectionSequenceNumber)
-    throws ManifoldCFException, IOException {
-    outputResource(VIEW_SPEC_FORWARD, out, locale, getSpecParameters(os), 
null, new Integer(connectionSequenceNumber), null);
+  public void viewSpecification(IHTTPOutput out, Locale locale,
+      Specification os, int connectionSequenceNumber)
+      throws ManifoldCFException, IOException
+  {
+    outputResource(VIEW_SPEC_FORWARD, out, locale, getSpecParameters(os), null,
+        new Integer(connectionSequenceNumber), null);
   }
 
   @Override
   public String processConfigurationPost(IThreadContext threadContext,
       IPostParameters variableContext, ConfigParams parameters)
-      throws ManifoldCFException {
+      throws ManifoldCFException
+  {
     OpenSearchServerConfig.contextToConfig(variableContext, parameters);
     return null;
   }
 
   private static Map<String, Integer> ossInstances = null;
 
-  private synchronized final Integer addInstance(OpenSearchServerConfig 
config) {
+  private synchronized final Integer addInstance(OpenSearchServerConfig config)
+  {
     if (ossInstances == null)
       ossInstances = new TreeMap<String, Integer>();
-    synchronized (ossInstances) {
+    synchronized (ossInstances)
+    {
       String uii = config.getUniqueIndexIdentifier();
       Integer count = ossInstances.get(uii);
-      if (count == null) {
+      if (count == null)
+      {
         count = new Integer(1);
         ossInstances.put(uii, count);
-      } else
+      }
+      else
         count++;
       return count;
     }
   }
 
-  private synchronized final void removeInstance(OpenSearchServerConfig 
config) {
+  private synchronized final void removeInstance(OpenSearchServerConfig config)
+  {
     if (ossInstances == null)
       return;
-    synchronized (ossInstances) {
+    synchronized (ossInstances)
+    {
       String uii = config.getUniqueIndexIdentifier();
       Integer count = ossInstances.get(uii);
       if (count == null)
@@ -488,25 +553,28 @@ public class OpenSearchServerConnector e
   @Override
   public int addOrReplaceDocument(String documentURI, String outputDescription,
       RepositoryDocument document, String authorityNameString,
-      IOutputAddActivity activities) throws ManifoldCFException,
-      ServiceInterruption {
+      IOutputAddActivity activities)
+      throws ManifoldCFException, ServiceInterruption
+  {
     HttpClient client = getSession();
     OpenSearchServerConfig config = getConfigParameters(null);
+
     Integer count = addInstance(config);
-    synchronized (count) {
-      try {
+    synchronized (count)
+    {
+      try
+      {
         long startTime = System.currentTimeMillis();
-        OpenSearchServerIndex oi = new OpenSearchServerIndex(
-            client,
-            documentURI,
-            config,
-            document);
+        OpenSearchServerIndex oi = new OpenSearchServerIndex(client,
+            documentURI, config, document, authorityNameString, activities);
         activities.recordActivity(startTime,
             OPENSEARCHSERVER_INDEXATION_ACTIVITY, document.getBinaryLength(),
             documentURI, oi.getResult().name(), oi.getResultDescription());
         if (oi.getResult() != Result.OK)
           return DOCUMENTSTATUS_REJECTED;
-      } finally {
+      }
+      finally
+      {
         removeInstance(config);
       }
       return DOCUMENTSTATUS_ACCEPTED;
@@ -515,41 +583,43 @@ public class OpenSearchServerConnector e
 
   @Override
   public void removeDocument(String documentURI, String outputDescription,
-      IOutputRemoveActivity activities) throws ManifoldCFException,
-      ServiceInterruption {
+      IOutputRemoveActivity activities)
+      throws ManifoldCFException, ServiceInterruption
+  {
     HttpClient client = getSession();
     long startTime = System.currentTimeMillis();
-    OpenSearchServerDelete od = new OpenSearchServerDelete(
-        client,
-        documentURI,
+    OpenSearchServerDelete od = new OpenSearchServerDelete(client, documentURI,
         getConfigParameters(null));
     activities.recordActivity(startTime, OPENSEARCHSERVER_DELETION_ACTIVITY,
         null, documentURI, od.getResult().name(), od.getResultDescription());
   }
 
   @Override
-  public String check() throws ManifoldCFException {
+  public String check()
+      throws ManifoldCFException
+  {
     HttpClient client = getSession();
-    OpenSearchServerSchema oss = new OpenSearchServerSchema(
-        client,
+    OpenSearchServerSchema oss = new OpenSearchServerSchema(client,
         getConfigParameters(null));
     return oss.getResult().name() + " " + oss.getResultDescription();
   }
 
   @Override
   public void noteJobComplete(IOutputNotifyActivity activities)
-      throws ManifoldCFException, ServiceInterruption {
+      throws ManifoldCFException, ServiceInterruption
+  {
     HttpClient client = getSession();
     long startTime = System.currentTimeMillis();
     OpenSearchServerConfig config = getConfigParameters(null);
     String schedulerJob = config.getSchedulerJob();
-    if (schedulerJob != null && schedulerJob.trim().length() > 0) {
-      OpenSearchServerScheduler oo = new OpenSearchServerScheduler(
-        client, getConfigParameters(null), schedulerJob.trim());
+    if (schedulerJob != null && schedulerJob.trim().length() > 0)
+    {
+      OpenSearchServerScheduler oo = new OpenSearchServerScheduler(client,
+          getConfigParameters(null), schedulerJob.trim());
       activities.recordActivity(startTime, OPENSEARCHSERVER_SCHEDULER_ACTIVITY,
-        null, oo.getCallUrlSnippet(), oo.getResult().name(),
-        oo.getResultDescription());
-    } 
+          null, oo.getCallUrlSnippet(), oo.getResult().name(),
+          oo.getResultDescription());
+    }
   }
 
 }

Modified: 
manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java
URL: 
http://svn.apache.org/viewvc/manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java?rev=1617187&r1=1617186&r2=1617187&view=diff
==============================================================================
--- 
manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java
 (original)
+++ 
manifoldcf/trunk/connectors/opensearchserver/connector/src/main/java/org/apache/manifoldcf/agents/output/opensearchserver/OpenSearchServerIndex.java
 Mon Aug 11 00:50:31 2014
@@ -21,77 +21,157 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
-import java.io.Reader;
 import java.util.ArrayList;
-import java.util.Date;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpPut;
-import org.apache.http.HttpEntity;
 import org.apache.http.message.BasicHeader;
-import org.apache.http.Header;
 import org.apache.http.util.EntityUtils;
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.manifoldcf.agents.interfaces.IOutputAddActivity;
 import org.apache.manifoldcf.agents.interfaces.RepositoryDocument;
 import org.apache.manifoldcf.core.common.Base64;
 import org.apache.manifoldcf.core.interfaces.ManifoldCFException;
 
-public class OpenSearchServerIndex extends OpenSearchServerConnection {
+public class OpenSearchServerIndex extends OpenSearchServerConnection
+{
 
-  private static class IndexRequestEntity implements HttpEntity {
+  private static class Acls
+  {
+    private String[] acls = null;
+    private String[] denyAcls = null;
+    private String[] shareAcls = null;
+    private String[] shareDenyAcls = null;
+
+    private void setDocument(String[] acls, String[] denyAcls)
+    {
+      this.acls = acls;
+      this.denyAcls = denyAcls;
+    }
 
-    private final String documentURI;
+    private void setShare(String[] acls, String[] denyAcls)
+    {
+      this.shareAcls = acls;
+      this.shareDenyAcls = denyAcls;
+    }
+  }
 
-    private final RepositoryDocument document;
+  private static class IndexRequestEntity implements HttpEntity
+  {
 
+    private final String documentURI;
+    private final RepositoryDocument document;
     private final String fileName;
+    private final Acls acls;
 
-    public IndexRequestEntity(String documentURI, RepositoryDocument document) 
{
+    public IndexRequestEntity(String documentURI, RepositoryDocument document,
+        Acls acls)
+    {
       this.documentURI = documentURI;
       this.document = document;
       this.fileName = FilenameUtils.getName(documentURI);
+      this.acls = acls;
     }
 
     @Override
-    public boolean isChunked() {
+    public boolean isChunked()
+    {
       return false;
     }
 
     @Override
     @Deprecated
-    public void consumeContent() throws IOException {
+    public void consumeContent()
+        throws IOException
+    {
       EntityUtils.consume(this);
     }
 
     @Override
-    public boolean isRepeatable() {
+    public boolean isRepeatable()
+    {
       return false;
     }
 
     @Override
-    public boolean isStreaming() {
+    public boolean isStreaming()
+    {
       return true;
     }
 
     @Override
-    public InputStream getContent() throws IOException, IllegalStateException {
+    public InputStream getContent()
+        throws IOException, IllegalStateException
+    {
       return null;
     }
 
+    private static final void writeFieldCdata(String fieldName,
+        Collection<String> values, PrintWriter pw)
+    {
+      if (CollectionUtils.isEmpty(values))
+        return;
+      pw.print("<field name=\"");
+      pw.print(StringEscapeUtils.escapeXml(fieldName));
+      pw.print("\">");
+      for (String value : values)
+      {
+        pw.print("<value><![CDATA[");
+        pw.print(value);
+        pw.print("]]></value>");
+      }
+      pw.println("</field>");
+    }
+
+    private static final void writeField(String fieldName, String value,
+        PrintWriter pw)
+    {
+      if (StringUtils.isEmpty(value))
+        return;
+      pw.print("<field name=\"");
+      pw.print(fieldName);
+      pw.print("\"><value>");
+      pw.print(value);
+      pw.println("</value></field>");
+    }
+
+    private static final void writeFieldValues(String fieldName,
+        String[] values, PrintWriter pw)
+    {
+      if (values == null || values.length == 0)
+        return;
+      pw.print("<field name=\"");
+      pw.print(fieldName);
+      pw.println("\">");
+      for (String value : values)
+      {
+        pw.print("<value>");
+        pw.print(value);
+        pw.println("</value>");
+      }
+      pw.println("</field>");
+    }
+
     @Override
     public void writeTo(OutputStream out)
-      throws IOException {
+        throws IOException
+    {
       PrintWriter pw = new PrintWriter(out);
       try
       {
         pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
         pw.println("<index>);");
         pw.print("<document>");
-        List<String> values = new ArrayList<>(1);
+        List<String> values = new ArrayList<String>(1);
         Iterator<String> iter = document.getFields();
         if (iter != null)
         {
@@ -99,33 +179,19 @@ public class OpenSearchServerIndex exten
           {
             String fieldName = iter.next();
             if ("uri".equals(fieldName))
-               continue;
+              continue;
             Object[] fieldValues = document.getField(fieldName);
             if (fieldValues != null && fieldValues.length > 0)
             {
-               values.clear();
-                for (Object fieldValue : fieldValues)
-                  if (fieldValue != null)
-                    values.add(fieldValue.toString());
-                if (!values.isEmpty())
-                {
-                  pw.print("<field name=\"");
-                  pw.print(StringEscapeUtils.escapeXml(fieldName));
-                  pw.print("\">");
-                  for (String value : values)
-                  {
-                   pw.print("<value><![CDATA[");
-                   pw.print(value);
-                    pw.print("]]></value>");
-                    pw.println("</field>");
-                  }
-               }
-             }
-           }
-        }
-        pw.print("<field name=\"uri\"><value>");
-        pw.print(documentURI);         
-        pw.println("</value></field>");
+              values.clear();
+              for (Object fieldValue : fieldValues)
+                if (fieldValue != null)
+                  values.add(fieldValue.toString());
+              writeFieldCdata(fieldName, values, pw);
+            }
+          }
+        }
+        writeField("uri", documentURI, pw);
         if (document.getBinaryLength() > 0)
         {
           Base64 base64 = new Base64();
@@ -135,6 +201,13 @@ public class OpenSearchServerIndex exten
           base64.encodeStream(document.getBinaryStream(), pw);
           pw.println("</binary>");
         }
+        if (acls != null)
+        {
+          writeFieldValues("userAllow", acls.acls, pw);
+          writeFieldValues("userDeny", acls.denyAcls, pw);
+          writeFieldValues("groupAllow", acls.shareAcls, pw);
+          writeFieldValues("groupDeny", acls.shareDenyAcls, pw);
+        }
         pw.println("</document>");
         pw.println("</index>");
       }
@@ -149,30 +222,95 @@ public class OpenSearchServerIndex exten
     }
 
     @Override
-    public long getContentLength() {
+    public long getContentLength()
+    {
       // Unknown (chunked) length
       return -1L;
     }
 
     @Override
-    public Header getContentType() {
+    public Header getContentType()
+    {
       return new BasicHeader("Content-type", "text/xml; charset=utf-8");
     }
 
     @Override
-    public Header getContentEncoding() {
+    public Header getContentEncoding()
+    {
       return null;
     }
+  }
 
-       }
+  /**
+   * Convert an unqualified ACL to qualified form.
+   * 
+   * @param acl
+   *          is the initial, unqualified ACL.
+   * @param authorityNameString
+   *          is the name of the governing authority for this document's acls,
+   *          or null if none.
+   * @param activities
+   *          is the activities object, so we can report what's happening.
+   * @return the modified ACL.
+   */
+  protected static String[] convertACL(String[] acl,
+      String authorityNameString, IOutputAddActivity activities)
+      throws ManifoldCFException
+  {
+    if (acl != null)
+    {
+      String[] rval = new String[acl.length];
+      int i = 0;
+      while (i < rval.length)
+      {
+        rval[i] = activities.qualifyAccessToken(authorityNameString, acl[i]);
+        i++;
+      }
+      return rval;
+    }
+    return new String[0];
+  }
 
   public OpenSearchServerIndex(HttpClient client, String documentURI,
-    OpenSearchServerConfig config, RepositoryDocument document)
-    throws ManifoldCFException {
+      OpenSearchServerConfig config, RepositoryDocument document,
+      String authorityNameString, IOutputAddActivity activities)
+      throws ManifoldCFException
+  {
     super(client, config);
+
+    Acls acls = new Acls();
+    Iterator<String> a = document.securityTypesIterator();
+    if (a != null)
+    {
+      while (a.hasNext())
+      {
+        String securityType = a.next();
+        String[] convertedAcls = convertACL(
+            document.getSecurityACL(securityType), authorityNameString,
+            activities);
+        String[] convertedDenyAcls = convertACL(
+            document.getSecurityDenyACL(securityType), authorityNameString,
+            activities);
+        if (securityType.equals(RepositoryDocument.SECURITY_TYPE_DOCUMENT))
+        {
+          acls.setDocument(convertedAcls, convertedDenyAcls);
+        }
+        else if (securityType.equals(RepositoryDocument.SECURITY_TYPE_SHARE))
+        {
+          acls.setShare(convertedAcls, convertedDenyAcls);
+        }
+        else
+        {
+          // Don't know how to deal with it
+          setResult(Result.ERROR, "Unhandled security type: " + securityType);
+          return;
+        }
+      }
+    }
+
     StringBuffer url = getApiUrl("update");
     HttpPut put = new HttpPut(url.toString());
-    put.setEntity(new IndexRequestEntity(documentURI, document));
+    put.setEntity(new IndexRequestEntity(documentURI, document, acls));
     call(put);
     if ("OK".equals(checkXPath(xPathStatus)))
       return;


Reply via email to