Example usage for org.apache.solr.common.params ModifiableSolrParams add

List of usage examples for org.apache.solr.common.params ModifiableSolrParams add

Introduction

In this page you can find the example usage for org.apache.solr.common.params ModifiableSolrParams add.

Prototype

public void add(SolrParams params) 

Source Link

Document

Add all of the params provided in the parameter to this params.

Usage

From source file:com.nkang.kxmoment.util.SolrUtils.HttpSolrServer.java

License:Apache License

public NamedList<Object> request(final SolrRequest request, final ResponseParser processor)
        throws SolrServerException, IOException {
    HttpRequestBase method = null;/* w  w  w  . j a v a 2 s.co m*/
    InputStream is = null;
    SolrParams params = request.getParams();
    Collection<ContentStream> streams = requestWriter.getContentStreams(request);
    String path = requestWriter.getPath(request);
    if (path == null || !path.startsWith("/")) {
        path = DEFAULT_PATH;
    }

    ResponseParser parser = request.getResponseParser();
    if (parser == null) {
        parser = this.parser;
    }

    // The parser 'wt=' and 'version=' params are used instead of the original
    // params
    ModifiableSolrParams wparams = new ModifiableSolrParams(params);
    if (parser != null) {
        wparams.set(CommonParams.WT, parser.getWriterType());
        wparams.set(CommonParams.VERSION, parser.getVersion());
    }
    if (invariantParams != null) {
        wparams.add(invariantParams);
    }

    int tries = maxRetries + 1;
    try {
        while (tries-- > 0) {
            // Note: since we aren't do intermittent time keeping
            // ourselves, the potential non-timeout latency could be as
            // much as tries-times (plus scheduling effects) the given
            // timeAllowed.
            try {
                if (SolrRequest.METHOD.GET == request.getMethod()) {
                    if (streams != null) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
                    }
                    method = new HttpGet(baseUrl + path + ClientUtils.toQueryString(wparams, false));
                } else if (SolrRequest.METHOD.POST == request.getMethod()) {

                    String url = baseUrl + path;
                    boolean hasNullStreamName = false;
                    if (streams != null) {
                        for (ContentStream cs : streams) {
                            if (cs.getName() == null) {
                                hasNullStreamName = true;
                                break;
                            }
                        }
                    }
                    boolean isMultipart = (this.useMultiPartPost || (streams != null && streams.size() > 1))
                            && !hasNullStreamName;

                    // only send this list of params as query string params
                    ModifiableSolrParams queryParams = new ModifiableSolrParams();
                    for (String param : this.queryParams) {
                        String[] value = wparams.getParams(param);
                        if (value != null) {
                            for (String v : value) {
                                queryParams.add(param, v);
                            }
                            wparams.remove(param);
                        }
                    }

                    LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>();
                    if (streams == null || isMultipart) {
                        HttpPost post = new HttpPost(url + ClientUtils.toQueryString(queryParams, false));
                        post.setHeader("Content-Charset", "UTF-8");
                        if (!isMultipart) {
                            post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                        }

                        List<FormBodyPart> parts = new LinkedList<FormBodyPart>();
                        Iterator<String> iter = wparams.getParameterNamesIterator();
                        while (iter.hasNext()) {
                            String p = iter.next();
                            String[] vals = wparams.getParams(p);
                            if (vals != null) {
                                for (String v : vals) {
                                    if (isMultipart) {
                                        parts.add(new FormBodyPart(p,
                                                new StringBody(v, Charset.forName("UTF-8"))));
                                    } else {
                                        postParams.add(new BasicNameValuePair(p, v));
                                    }
                                }
                            }
                        }

                        if (isMultipart && streams != null) {
                            for (ContentStream content : streams) {
                                String contentType = content.getContentType();
                                if (contentType == null) {
                                    contentType = BinaryResponseParser.BINARY_CONTENT_TYPE; // default
                                }
                                String name = content.getName();
                                if (name == null) {
                                    name = "";
                                }
                                parts.add(new FormBodyPart(name, new InputStreamBody(content.getStream(),
                                        contentType, content.getName())));
                            }
                        }

                        if (parts.size() > 0) {
                            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
                            for (FormBodyPart p : parts) {
                                entity.addPart(p);
                            }
                            post.setEntity(entity);
                        } else {
                            //not using multipart
                            post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8"));
                        }

                        method = post;
                    }
                    // It is has one stream, it is the post body, put the params in the URL
                    else {
                        String pstr = ClientUtils.toQueryString(wparams, false);
                        HttpPost post = new HttpPost(url + pstr);

                        // Single stream as body
                        // Using a loop just to get the first one
                        final ContentStream[] contentStream = new ContentStream[1];
                        for (ContentStream content : streams) {
                            contentStream[0] = content;
                            break;
                        }
                        if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
                            post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
                                @Override
                                public Header getContentType() {
                                    return new BasicHeader("Content-Type", contentStream[0].getContentType());
                                }

                                @Override
                                public boolean isRepeatable() {
                                    return false;
                                }

                            });
                        } else {
                            post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
                                @Override
                                public Header getContentType() {
                                    return new BasicHeader("Content-Type", contentStream[0].getContentType());
                                }

                                @Override
                                public boolean isRepeatable() {
                                    return false;
                                }
                            });
                        }
                        method = post;
                    }
                } else {
                    throw new SolrServerException("Unsupported method: " + request.getMethod());
                }
            } catch (NoHttpResponseException r) {
                method = null;
                if (is != null) {
                    is.close();
                }
                // If out of tries then just rethrow (as normal error).
                if (tries < 1) {
                    throw r;
                }
            }
        }
    } catch (IOException ex) {
        throw new SolrServerException("error reading streams", ex);
    }

    // client already has this set, is this needed
    method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
    method.addHeader("User-Agent", AGENT);

    // add jauth information
    //String username = WebServiceLoaderUtils.getWebServiceProperty(Constants.SECURITY_USERNAME).toString();
    //String password = WebServiceLoaderUtils.getWebServiceProperty(Constants.SECURITY_PASSWORD).toString();

    ResourceBundle bundle = ResourceBundle.getBundle("solrconfig");
    String username;
    String password;
    username = bundle.getString("wsUsername.url");
    password = bundle.getString("wsPassword.url");

    method.addHeader("username", username);
    method.addHeader("password", password);

    InputStream respBody = null;
    boolean shouldClose = true;
    boolean success = false;
    try {
        // Execute the method.
        final HttpResponse response = httpClient.execute(method);
        int httpStatus = response.getStatusLine().getStatusCode();

        // Read the contents
        respBody = response.getEntity().getContent();
        Header ctHeader = response.getLastHeader("content-type");
        String contentType;
        if (ctHeader != null) {
            contentType = ctHeader.getValue();
        } else {
            contentType = "";
        }

        // handle some http level checks before trying to parse the response
        switch (httpStatus) {
        case HttpStatus.SC_OK:
        case HttpStatus.SC_BAD_REQUEST:
        case HttpStatus.SC_CONFLICT: // 409
            break;
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
            if (!followRedirects) {
                throw new SolrServerException(
                        "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ").");
            }
            break;
        default:
            if (processor == null) {
                throw new RemoteSolrException(httpStatus,
                        "Server at " + getBaseURL() + " returned non ok status:" + httpStatus + ", message:"
                                + response.getStatusLine().getReasonPhrase(),
                        null);
            }
        }
        if (processor == null) {

            // no processor specified, return raw stream
            NamedList<Object> rsp = new NamedList<Object>();
            rsp.add("stream", respBody);
            // Only case where stream should not be closed
            shouldClose = false;
            success = true;
            return rsp;
        }

        String procCt = processor.getContentType();
        if (procCt != null) {
            String procMimeType = ContentType.parse(procCt).getMimeType().trim().toLowerCase(Locale.ROOT);
            String mimeType = ContentType.parse(contentType).getMimeType().trim().toLowerCase(Locale.ROOT);
            if (!procMimeType.equals(mimeType)) {
                // unexpected mime type
                String msg = "Expected mime type " + procMimeType + " but got " + mimeType + ".";
                Header encodingHeader = response.getEntity().getContentEncoding();
                String encoding;
                if (encodingHeader != null) {
                    encoding = encodingHeader.getValue();
                } else {
                    encoding = "UTF-8"; // try UTF-8
                }
                try {
                    msg = msg + " " + IOUtils.toString(respBody, encoding);
                } catch (IOException e) {
                    throw new RemoteSolrException(httpStatus,
                            "Could not parse response with encoding " + encoding, e);
                }
                RemoteSolrException e = new RemoteSolrException(httpStatus, msg, null);
                throw e;
            }
        }

        //      if(true) {
        //        ByteArrayOutputStream copy = new ByteArrayOutputStream();
        //        IOUtils.copy(respBody, copy);
        //        String val = new String(copy.toByteArray());
        //        System.out.println(">RESPONSE>"+val+"<"+val.length());
        //        respBody = new ByteArrayInputStream(copy.toByteArray());
        //      }

        NamedList<Object> rsp = null;
        String charset = EntityUtils.getContentCharSet(response.getEntity());
        try {
            rsp = processor.processResponse(respBody, charset);
        } catch (Exception e) {
            throw new RemoteSolrException(httpStatus, e.getMessage(), e);
        }
        if (httpStatus != HttpStatus.SC_OK) {
            String reason = null;
            try {
                NamedList<?> err = (NamedList<?>) rsp.get("error");
                if (err != null) {
                    reason = (String) err.get("msg");
                    // TODO? get the trace?
                }
            } catch (Exception ex) {
            }
            if (reason == null) {
                StringBuilder msg = new StringBuilder();
                msg.append(response.getStatusLine().getReasonPhrase());
                msg.append("\n\n");
                msg.append("request: " + method.getURI());
                reason = java.net.URLDecoder.decode(msg.toString(), UTF_8);
            }
            throw new RemoteSolrException(httpStatus, reason, null);
        }
        success = true;
        return rsp;
    } catch (ConnectException e) {
        throw new SolrServerException("Server refused connection at: " + getBaseURL(), e);
    } catch (SocketTimeoutException e) {
        throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(),
                e);
    } catch (IOException e) {
        throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e);
    } finally {
        if (respBody != null && shouldClose) {
            try {
                respBody.close();
            } catch (IOException e) {
                //log.error("", e);
            } finally {
                if (!success) {
                    method.abort();
                }
            }
        }
    }
}

From source file:com.su.search.client.solrj.PaHttpSolrServer.java

License:Apache License

public NamedList<Object> request(final SolrRequest request, final ResponseParser processor)
        throws SolrServerException, IOException {
    HttpRequestBase method = null;/*from w w  w  . j  a  va  2  s . c  o m*/
    InputStream is = null;
    SolrParams params = request.getParams();

    // modified by wangqiang406 2012-07-27
    // ??
    if (null != password) {
        ModifiableSolrParams wparams = new ModifiableSolrParams(params);
        wparams.set(SimpleIndexClient.KEY_PA_AUTH, password);
        params = wparams;
    }

    Collection<ContentStream> streams = requestWriter.getContentStreams(request);
    String path = requestWriter.getPath(request);
    if (path == null || !path.startsWith("/")) {
        path = DEFAULT_PATH;
    }

    ResponseParser parser = request.getResponseParser();
    if (parser == null) {
        parser = this.parser;
    }

    // The parser 'wt=' and 'version=' params are used instead of the original
    // params
    ModifiableSolrParams wparams = new ModifiableSolrParams(params);
    wparams.set(CommonParams.WT, parser.getWriterType());
    wparams.set(CommonParams.VERSION, parser.getVersion());
    if (invariantParams != null) {
        wparams.add(invariantParams);
    }
    params = wparams;

    int tries = maxRetries + 1;
    try {
        while (tries-- > 0) {
            // Note: since we aren't do intermittent time keeping
            // ourselves, the potential non-timeout latency could be as
            // much as tries-times (plus scheduling effects) the given
            // timeAllowed.
            try {
                if (SolrRequest.METHOD.GET == request.getMethod()) {
                    if (streams != null) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
                    }
                    method = new HttpGet(baseUrl + path + ClientUtils.toQueryString(params, false));
                } else if (SolrRequest.METHOD.POST == request.getMethod()) {

                    String url = baseUrl + path;

                    boolean isMultipart = (streams != null && streams.size() > 1);

                    LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>();
                    if (streams == null || isMultipart) {
                        HttpPost post = new HttpPost(url);
                        post.setHeader("Content-Charset", "UTF-8");
                        if (!this.useMultiPartPost && !isMultipart) {
                            post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                        }

                        List<FormBodyPart> parts = new LinkedList<FormBodyPart>();
                        Iterator<String> iter = params.getParameterNamesIterator();
                        while (iter.hasNext()) {
                            String p = iter.next();
                            String[] vals = params.getParams(p);
                            if (vals != null) {
                                for (String v : vals) {
                                    if (this.useMultiPartPost || isMultipart) {
                                        parts.add(new FormBodyPart(p,
                                                new StringBody(v, Charset.forName("UTF-8"))));
                                    } else {
                                        postParams.add(new BasicNameValuePair(p, v));
                                    }
                                }
                            }
                        }

                        if (isMultipart) {
                            for (ContentStream content : streams) {
                                parts.add(new FormBodyPart(content.getName(),
                                        new InputStreamBody(content.getStream(), content.getName())));
                            }
                        }

                        if (parts.size() > 0) {
                            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
                            for (FormBodyPart p : parts) {
                                entity.addPart(p);
                            }
                            post.setEntity(entity);
                        } else {
                            //not using multipart
                            HttpEntity e;
                            post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8"));
                        }

                        method = post;
                    }
                    // It is has one stream, it is the post body, put the params in the URL
                    else {
                        String pstr = ClientUtils.toQueryString(params, false);

                        HttpPost post = new HttpPost(url + pstr);

                        // Single stream as body
                        // Using a loop just to get the first one
                        final ContentStream[] contentStream = new ContentStream[1];
                        for (ContentStream content : streams) {
                            contentStream[0] = content;
                            break;
                        }
                        if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
                            post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
                                @Override
                                public Header getContentType() {
                                    return new BasicHeader("Content-Type", contentStream[0].getContentType());
                                }

                                @Override
                                public boolean isRepeatable() {
                                    return false;
                                }

                            });
                        } else {
                            post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
                                @Override
                                public Header getContentType() {
                                    return new BasicHeader("Content-Type", contentStream[0].getContentType());
                                }

                                @Override
                                public boolean isRepeatable() {
                                    return false;
                                }
                            });
                        }
                        method = post;
                    }
                } else {
                    throw new SolrServerException("Unsupported method: " + request.getMethod());
                }
            } catch (NoHttpResponseException r) {
                method = null;
                if (is != null) {
                    is.close();
                }
                // If out of tries then just rethrow (as normal error).
                if (tries < 1) {
                    throw r;
                }
            }
        }
    } catch (IOException ex) {
        throw new SolrServerException("error reading streams", ex);
    }

    // TODO: move to a interceptor?
    method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
    method.addHeader("User-Agent", AGENT);

    InputStream respBody = null;

    try {
        // Execute the method.
        final HttpResponse response = httpClient.execute(method);
        int httpStatus = response.getStatusLine().getStatusCode();

        // Read the contents
        String charset = EntityUtils.getContentCharSet(response.getEntity());
        respBody = response.getEntity().getContent();

        // handle some http level checks before trying to parse the response
        switch (httpStatus) {
        case HttpStatus.SC_OK:
            break;
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
            if (!followRedirects) {
                throw new SolrServerException(
                        "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ").");
            }
            break;
        case HttpStatus.SC_NOT_FOUND:
            throw new SolrServerException("Server at " + getBaseURL() + " was not found (404).");
        default:
            throw new SolrServerException("Server at " + getBaseURL() + " returned non ok status:" + httpStatus
                    + ", message:" + response.getStatusLine().getReasonPhrase());

        }
        NamedList<Object> rsp = processor.processResponse(respBody, charset);
        if (httpStatus != HttpStatus.SC_OK) {
            String reason = null;
            try {
                NamedList err = (NamedList) rsp.get("error");
                if (err != null) {
                    reason = (String) err.get("msg");
                    // TODO? get the trace?
                }
            } catch (Exception ex) {
            }
            if (reason == null) {
                StringBuilder msg = new StringBuilder();
                msg.append(response.getStatusLine().getReasonPhrase());
                msg.append("\n\n");
                msg.append("request: " + method.getURI());
                reason = java.net.URLDecoder.decode(msg.toString(), UTF_8);
            }
            throw new SolrException(SolrException.ErrorCode.getErrorCode(httpStatus), reason);
        }
        return rsp;
    } catch (ConnectException e) {
        throw new SolrServerException("Server refused connection at: " + getBaseURL(), e);
    } catch (SocketTimeoutException e) {
        throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(),
                e);
    } catch (IOException e) {
        throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e);
    } finally {
        if (respBody != null) {
            try {
                respBody.close();
            } catch (Throwable t) {
            } // ignore
        }
    }
}

From source file:org.alfresco.solr.AlfrescoSolrUtils.java

License:Open Source License

/**
 * Creates a core using the specified template
 * @param coreContainer/* w ww. ja v  a  2  s  .c  o m*/
 * @param coreAdminHandler
 * @param coreName
 * @param templateName
 * @param shards
 * @param nodes
 * @param extraParams Any number of additional parameters in name value pairs.
 * @return
 * @throws InterruptedException
 */
public static SolrCore createCoreUsingTemplate(CoreContainer coreContainer,
        AlfrescoCoreAdminHandler coreAdminHandler, String coreName, String templateName, int shards, int nodes,
        String... extraParams) throws InterruptedException {
    SolrCore testingCore = null;
    ModifiableSolrParams coreParams = params(CoreAdminParams.ACTION, "newcore", "storeRef",
            "workspace://SpacesStore", "coreName", coreName, "numShards", String.valueOf(shards),
            "nodeInstance", String.valueOf(nodes), "template", templateName);
    coreParams.add(params(extraParams));
    SolrQueryRequest request = new LocalSolrQueryRequest(null, coreParams);
    SolrQueryResponse response = new SolrQueryResponse();
    coreAdminHandler.handleCustomAction(request, response);
    TimeUnit.SECONDS.sleep(1);
    if (shards > 1) {
        NamedList vals = response.getValues();
        List<String> coreNames = vals.getAll("core");
        assertEquals(shards, coreNames.size());
        testingCore = getCore(coreContainer, coreNames.get(0));
    } else {
        assertEquals(coreName, response.getValues().get("core"));
        //Get a reference to the new core
        testingCore = getCore(coreContainer, coreName);
    }

    TimeUnit.SECONDS.sleep(4); //Wait a little for background threads to catchup
    assertNotNull(testingCore);
    return testingCore;
}

From source file:org.alfresco.solr.CoresCreateUpdateDistributedTest.java

License:Open Source License

public static void createSimpleCore(AlfrescoCoreAdminHandler coreAdminHandler, String coreName, String storeRef,
        String templateName, String... extraParams) throws InterruptedException {

    ModifiableSolrParams coreParams = params(CoreAdminParams.ACTION, "NEWDEFAULTINDEX", "storeRef", storeRef,
            "coreName", coreName, "template", templateName);
    coreParams.add(params(extraParams));
    SolrQueryRequest request = new LocalSolrQueryRequest(null, coreParams);
    SolrQueryResponse response = new SolrQueryResponse();
    coreAdminHandler.handleCustomAction(request, response);
    TimeUnit.SECONDS.sleep(2);//from w w  w .  jav  a  2  s  .c  o  m
}

From source file:org.alfresco.solr.CoresCreateUpdateDistributedTest.java

License:Open Source License

public static void updateCore(AlfrescoCoreAdminHandler coreAdminHandler, String coreName, String... extraParams)
        throws InterruptedException {

    ModifiableSolrParams coreParams = params(CoreAdminParams.ACTION, "UPDATECORE", "coreName", coreName);
    coreParams.add(params(extraParams));
    SolrQueryRequest request = new LocalSolrQueryRequest(null, coreParams);
    SolrQueryResponse response = new SolrQueryResponse();
    coreAdminHandler.handleCustomAction(request, response);
    TimeUnit.SECONDS.sleep(2);/*ww  w .ja v a 2s.co m*/
}

From source file:org.alfresco.solr.CoresCreateUpdateDistributedTest.java

License:Open Source License

public static void updateShared(AlfrescoCoreAdminHandler coreAdminHandler, String... extraParams)
        throws InterruptedException {

    ModifiableSolrParams coreParams = params(CoreAdminParams.ACTION, "UPDATESHARED");
    coreParams.add(params(extraParams));
    SolrQueryRequest request = new LocalSolrQueryRequest(null, coreParams);
    SolrQueryResponse response = new SolrQueryResponse();
    coreAdminHandler.handleCustomAction(request, response);
    TimeUnit.SECONDS.sleep(2);// ww  w .  ja  v  a2s.c  om
}

From source file:org.alfresco.solr.query.Solr4QueryParser.java

License:Open Source License

/**
 * @param field//  ww w.  j  a  v  a 2s.  com
 * @param queryText
 * @param analysisMode
 * @param luceneFunction
 * @return the query
 * @throws ParseException
 * @throws IOException
 */
@SuppressWarnings("deprecation")
public Query getFieldQuery(String field, String queryText, AnalysisMode analysisMode,
        LuceneFunction luceneFunction) throws ParseException {
    try {
        if (field.equals(FIELD_PATH)) {
            return createPathQuery(queryText, false);
        } else if (field.equals(FIELD_PATHWITHREPEATS)) {
            return createPathQuery(queryText, true);
        } else if (field.equals(FIELD_TEXT)) {
            return createTextQuery(queryText, analysisMode, luceneFunction);
        } else if (field.equals(FIELD_ID)) {
            return createIdQuery(queryText);
        } else if (field.equals(FIELD_SOLR4_ID)) {
            return createSolr4IdQuery(queryText);
        } else if (field.equals(FIELD_DBID)) {
            return createDbidQuery(queryText);
        } else if (field.equals(FIELD_ACLID)) {
            return createAclIdQuery(queryText);
        } else if (field.equals(FIELD_OWNER)) {
            return createOwnerQuery(queryText);
        } else if (field.equals(FIELD_OWNERSET)) {
            return createOwnerSetQuery(queryText);
        } else if (field.equals(FIELD_READER)) {
            return createReaderQuery(queryText);
        } else if (field.equals(FIELD_READERSET)) {
            return createReaderSetQuery(queryText);
        } else if (field.equals(FIELD_AUTHORITY)) {
            /*
            * ACL DOCUMENTATION STARTS HERE
            * This creates the query that applies the ACL filter
            */
            return createAuthorityQuery(queryText);
        } else if (field.equals(FIELD_AUTHORITYSET)) {
            /*
            * ACL DOCUMENTATION STARTS HERE
            * This creates the query that applies the ACL filter
            */
            return createAuthoritySetQuery(queryText);
        } else if (field.equals(FIELD_DENIED)) {
            return createDeniedQuery(queryText);
        } else if (field.equals(FIELD_DENYSET)) {
            return createDenySetQuery(queryText);
        } else if (field.equals(FIELD_ISROOT)) {
            return createIsRootQuery(queryText);
        } else if (field.equals(FIELD_ISCONTAINER)) {
            return createIsContainerQuery(queryText);
        } else if (field.equals(FIELD_ISNODE)) {
            return createIsNodeQuery(queryText);
        } else if (field.equals(FIELD_TX)) {
            return createTransactionQuery(queryText);
        } else if (field.equals(FIELD_INTXID)) {
            return createInTxIdQuery(queryText);
        } else if (field.equals(FIELD_INACLTXID)) {
            return createInAclTxIdQuery(queryText);
        } else if (field.equals(FIELD_PARENT)) {
            return createParentQuery(queryText);
        } else if (field.equals(FIELD_PRIMARYPARENT)) {
            return createPrimaryParentQuery(queryText);
        } else if (field.equals(FIELD_QNAME)) {
            return createQNameQuery(queryText);
        } else if (field.equals(FIELD_PRIMARYASSOCQNAME)) {
            return createPrimaryAssocQNameQuery(queryText);
        } else if (field.equals(FIELD_PRIMARYASSOCTYPEQNAME)) {
            return createPrimaryAssocTypeQNameQuery(queryText);
        } else if (field.equals(FIELD_ASSOCTYPEQNAME)) {
            return createAssocTypeQNameQuery(queryText);
        } else if (field.equals(FIELD_CLASS)) {
            ClassDefinition target = QueryParserUtils.matchClassDefinition(searchParameters.getNamespace(),
                    namespacePrefixResolver, dictionaryService, queryText);
            if (target == null) {
                return new TermQuery(new Term(FIELD_TYPE, "_unknown_"));
            }
            return getFieldQuery(target.isAspect() ? FIELD_ASPECT : FIELD_TYPE, queryText, analysisMode,
                    luceneFunction);
        } else if (field.equals(FIELD_TYPE)) {
            return createTypeQuery(queryText, false);
        } else if (field.equals(FIELD_EXACTTYPE)) {
            return createTypeQuery(queryText, true);
        } else if (field.equals(FIELD_ASPECT)) {
            return createAspectQuery(queryText, false);
        } else if (field.equals(FIELD_EXACTASPECT)) {
            return createAspectQuery(queryText, true);
        } else if (isPropertyField(field)) {
            Query query = attributeQueryBuilder(field, queryText, new FieldQuery(), analysisMode,
                    luceneFunction);
            return query;
        } else if (field.equals(FIELD_ALL)) {
            return createAllQuery(queryText, analysisMode, luceneFunction);
        } else if (field.equals(FIELD_ISUNSET)) {
            return createIsUnsetQuery(queryText, analysisMode, luceneFunction);
        } else if (field.equals(FIELD_ISNULL)) {
            return createIsNullQuery(queryText, analysisMode, luceneFunction);
        } else if (field.equals(FIELD_ISNOTNULL)) {
            return createIsNotNull(queryText, analysisMode, luceneFunction);
        } else if (field.equals(FIELD_EXISTS)) {
            return createExistsQuery(queryText, analysisMode, luceneFunction);
        } else if (QueryParserUtils.matchDataTypeDefinition(searchParameters.getNamespace(),
                namespacePrefixResolver, dictionaryService, field) != null) {
            return createDataTypeDefinitionQuery(field, queryText, analysisMode, luceneFunction);
        } else if (field.equals(FIELD_FTSSTATUS)) {
            return createTermQuery(field, queryText);
        } else if (field.equals(FIELD_TXID)) {
            return createTxIdQuery(queryText);
        } else if (field.equals(FIELD_ACLTXID)) {
            return createAclTxIdQuery(queryText);
        } else if (field.equals(FIELD_TXCOMMITTIME)) {
            return createTxCommitTimeQuery(queryText);
        } else if (field.equals(FIELD_ACLTXCOMMITTIME)) {
            return createAclTxCommitTimeQuery(queryText);
        } else if (field.equals(FIELD_TAG)) {
            return createTagQuery(queryText);
        } else if (field.equals(FIELD_SITE)) {
            return createSiteQuery(queryText);
        } else if (field.equals(FIELD_PNAME)) {
            return createPNameQuery(queryText);
        } else if (field.equals(FIELD_NPATH)) {
            return createNPathQuery(queryText);
        } else if (field.equals(FIELD_TENANT)) {
            return createTenantQuery(queryText);
        } else if (field.equals(FIELD_ANCESTOR)) {
            return createAncestorQuery(queryText);
        } else if (field.equals(FIELD_FINGERPRINT)) {
            String[] parts = queryText.split("_");
            Collection values = null;
            String nodeId = parts[0];

            JSONObject json = (JSONObject) request.getContext().get(AbstractQParser.ALFRESCO_JSON);
            String fingerPrint = null;
            if (json != null) {
                //Was the fingerprint passed in
                String fingerPrintKey = "fingerprint." + nodeId;
                if (json.has(fingerPrintKey)) {
                    fingerPrint = (String) json.get("fingerprint." + nodeId);
                    if (fingerPrint != null) {
                        List l = new ArrayList();
                        String[] hashes = fingerPrint.split(" ");
                        for (String hash : hashes) {
                            l.add(hash);
                        }
                        values = l;
                    }
                }
            } else {
                json = new JSONObject();
            }

            //Is the fingerprint in the local SolrContentStore
            if (values == null) {
                long dbid = fetchDBID(nodeId);
                if (dbid == -1 && isNumber(nodeId)) {
                    dbid = Long.parseLong(nodeId);
                }

                if (dbid > -1) {
                    SolrInputDocument solrDoc = solrContentStore.retrieveDocFromSolrContentStore(
                            AlfrescoSolrDataModel.getTenantId(TenantService.DEFAULT_DOMAIN), dbid);
                    if (solrDoc != null) {
                        SolrInputField mh = solrDoc.getField("MINHASH");
                        if (mh != null) {
                            values = mh.getValues();
                        }
                    }
                }
            }

            String shards = this.solrParams.get("shards");
            if (values == null && shards != null) {
                //we are in distributed mode
                //Fetch the fingerPrint from the shards.
                //The UUID and DBID will both work for method call.
                values = fetchFingerPrint(shards, nodeId);
            }

            //If we're in distributed mode then add the fingerprint to the json
            if (values != null && shards != null && fingerPrint == null) {
                ModifiableSolrParams newParams = new ModifiableSolrParams();
                newParams.add(solrParams);
                solrParams = newParams;
                json.put("fingerprint." + nodeId, join(values, " "));
                String jsonString = json.toString();
                newParams.add(AbstractQParser.ALFRESCO_JSON, jsonString);
                request.getContext().put(AbstractQParser.ALFRESCO_JSON, json);
                request.setParams(newParams);
            }

            return createFingerPrintQuery(field, queryText, values, analysisMode, luceneFunction);

        } else {
            return getFieldQueryImpl(field, queryText, analysisMode, luceneFunction);
        }

    } catch (SAXPathException e) {
        throw new ParseException("Failed to parse XPath...\n" + e.getMessage());
    } catch (IOException e) {
        throw new ParseException("IO: " + e.getMessage());
    }

}

From source file:org.apache.manifoldcf.agents.output.solr.ModifiedHttpSolrServer.java

License:Apache License

@Override
public NamedList<Object> request(final SolrRequest request, final ResponseParser processor)
        throws SolrServerException, IOException {
    HttpRequestBase method = null;//w w w .ja v  a  2s  .  com
    InputStream is = null;
    SolrParams params = request.getParams();
    Collection<ContentStream> streams = requestWriter.getContentStreams(request);
    String path = requestWriter.getPath(request);
    if (path == null || !path.startsWith("/")) {
        path = DEFAULT_PATH;
    }

    ResponseParser parser = request.getResponseParser();
    if (parser == null) {
        parser = this.parser;
    }

    // The parser 'wt=' and 'version=' params are used instead of the original
    // params
    ModifiableSolrParams wparams = new ModifiableSolrParams(params);
    if (parser != null) {
        wparams.set(CommonParams.WT, parser.getWriterType());
        wparams.set(CommonParams.VERSION, parser.getVersion());
    }
    if (invariantParams != null) {
        wparams.add(invariantParams);
    }
    params = wparams;

    int tries = maxRetries + 1;
    try {
        while (tries-- > 0) {
            // Note: since we aren't do intermittent time keeping
            // ourselves, the potential non-timeout latency could be as
            // much as tries-times (plus scheduling effects) the given
            // timeAllowed.
            try {
                if (SolrRequest.METHOD.GET == request.getMethod()) {
                    if (streams != null) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
                    }
                    method = new HttpGet(baseUrl + path + toQueryString(params, false));
                } else if (SolrRequest.METHOD.POST == request.getMethod()) {

                    String url = baseUrl + path;
                    boolean hasNullStreamName = false;
                    if (streams != null) {
                        for (ContentStream cs : streams) {
                            if (cs.getName() == null) {
                                hasNullStreamName = true;
                                break;
                            }
                        }
                    }
                    boolean isMultipart = (this.useMultiPartPost || (streams != null && streams.size() > 1))
                            && !hasNullStreamName;

                    LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>();
                    if (streams == null || isMultipart) {
                        HttpPost post = new HttpPost(url);
                        post.setHeader("Content-Charset", "UTF-8");
                        if (!isMultipart) {
                            post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                        }

                        List<FormBodyPart> parts = new LinkedList<FormBodyPart>();
                        Iterator<String> iter = params.getParameterNamesIterator();
                        while (iter.hasNext()) {
                            String p = iter.next();
                            String[] vals = params.getParams(p);
                            if (vals != null) {
                                for (String v : vals) {
                                    if (isMultipart) {
                                        parts.add(
                                                new FormBodyPart(p, new StringBody(v, StandardCharsets.UTF_8)));
                                    } else {
                                        postParams.add(new BasicNameValuePair(p, v));
                                    }
                                }
                            }
                        }

                        if (isMultipart && streams != null) {
                            for (ContentStream content : streams) {
                                String contentType = content.getContentType();
                                if (contentType == null) {
                                    contentType = "application/octet-stream"; // default
                                }
                                String contentName = content.getName();
                                parts.add(new FormBodyPart(contentName, new InputStreamBody(content.getStream(),
                                        contentType, content.getName())));
                            }
                        }

                        if (parts.size() > 0) {
                            ModifiedMultipartEntity entity = new ModifiedMultipartEntity(
                                    HttpMultipartMode.STRICT, null, StandardCharsets.UTF_8);
                            for (FormBodyPart p : parts) {
                                entity.addPart(p);
                            }
                            post.setEntity(entity);
                        } else {
                            //not using multipart
                            post.setEntity(new UrlEncodedFormEntity(postParams, StandardCharsets.UTF_8));
                        }

                        method = post;
                    }
                    // It is has one stream, it is the post body, put the params in the URL
                    else {
                        String pstr = toQueryString(params, false);
                        HttpPost post = new HttpPost(url + pstr);

                        // Single stream as body
                        // Using a loop just to get the first one
                        final ContentStream[] contentStream = new ContentStream[1];
                        for (ContentStream content : streams) {
                            contentStream[0] = content;
                            break;
                        }
                        if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
                            post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
                                @Override
                                public Header getContentType() {
                                    return new BasicHeader("Content-Type", contentStream[0].getContentType());
                                }

                                @Override
                                public boolean isRepeatable() {
                                    return false;
                                }

                            });
                        } else {
                            post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) {
                                @Override
                                public Header getContentType() {
                                    return new BasicHeader("Content-Type", contentStream[0].getContentType());
                                }

                                @Override
                                public boolean isRepeatable() {
                                    return false;
                                }
                            });
                        }
                        method = post;
                    }
                } else {
                    throw new SolrServerException("Unsupported method: " + request.getMethod());
                }
            } catch (NoHttpResponseException r) {
                method = null;
                if (is != null) {
                    is.close();
                }
                // If out of tries then just rethrow (as normal error).
                if (tries < 1) {
                    throw r;
                }
            }
        }
    } catch (IOException ex) {
        throw new SolrServerException("error reading streams", ex);
    }

    // XXX client already has this set, is this needed?
    //method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,
    //    followRedirects);
    method.addHeader("User-Agent", AGENT);

    InputStream respBody = null;
    boolean shouldClose = true;

    try {
        // Execute the method.
        final HttpResponse response = httpClient.execute(method);
        int httpStatus = response.getStatusLine().getStatusCode();

        // Read the contents
        respBody = response.getEntity().getContent();

        // handle some http level checks before trying to parse the response
        switch (httpStatus) {
        case HttpStatus.SC_OK:
        case HttpStatus.SC_BAD_REQUEST:
        case HttpStatus.SC_CONFLICT: // 409
            break;
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
            if (!followRedirects) {
                throw new SolrServerException(
                        "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ").");
            }
            break;
        default:
            throw new SolrException(SolrException.ErrorCode.getErrorCode(httpStatus),
                    "Server at " + getBaseURL() + " returned non ok status:" + httpStatus + ", message:"
                            + response.getStatusLine().getReasonPhrase());

        }
        if (processor == null) {
            // no processor specified, return raw stream
            NamedList<Object> rsp = new NamedList<Object>();
            rsp.add("stream", respBody);
            // Only case where stream should not be closed
            shouldClose = false;
            return rsp;
        }
        Charset charsetObject = ContentType.getOrDefault(response.getEntity()).getCharset();
        String charset;
        if (charsetObject != null)
            charset = charsetObject.name();
        else
            charset = "utf-8";
        NamedList<Object> rsp = processor.processResponse(respBody, charset);
        if (httpStatus != HttpStatus.SC_OK) {
            String reason = null;
            try {
                NamedList err = (NamedList) rsp.get("error");
                if (err != null) {
                    reason = (String) err.get("msg");
                    // TODO? get the trace?
                }
            } catch (Exception ex) {
            }
            if (reason == null) {
                StringBuilder msg = new StringBuilder();
                msg.append(response.getStatusLine().getReasonPhrase());
                msg.append("\n\n");
                msg.append("request: " + method.getURI());
                reason = URLDecoder.decode(msg.toString());
            }
            throw new SolrException(SolrException.ErrorCode.getErrorCode(httpStatus), reason);
        }
        return rsp;
    } catch (ConnectException e) {
        throw new SolrServerException("Server refused connection at: " + getBaseURL(), e);
    } catch (SocketTimeoutException e) {
        throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(),
                e);
    } catch (IOException e) {
        throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e);
    } finally {
        if (respBody != null && shouldClose) {
            try {
                respBody.close();
            } catch (Throwable t) {
            } // ignore
        }
    }
}

From source file:org.dice.solrenhancements.spellchecker.DiceSpellCheckComponent.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    if (!params.getBool(COMPONENT_NAME, false) || spellCheckers.isEmpty()) {
        return;/*from  ww  w.  j a v a2 s.com*/
    }
    boolean shardRequest = "true".equals(params.get(ShardParams.IS_SHARD));
    String q = params.get(SPELLCHECK_Q);
    SolrSpellChecker spellChecker = getSpellChecker(params);
    Collection<Token> tokens = null;

    if (q == null) {
        // enforce useage of the spellcheck.q parameter - i.e. a query we can tokenize with a regular tokenizer and not
        // a solr query for the spell checking. Useage of the SolrQueryConverter is buggy and breaks frequently
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "The spellcheck.q parameter is required.");
    } else {
        //we have a spell check param, tokenize it with the query analyzer applicable for this spellchecker
        tokens = getTokens(q, spellChecker.getQueryAnalyzer());
    }
    if (tokens != null && tokens.isEmpty() == false) {
        if (spellChecker != null) {
            int count = params.getInt(SPELLCHECK_COUNT, 1);
            boolean onlyMorePopular = params.getBool(SPELLCHECK_ONLY_MORE_POPULAR, DEFAULT_ONLY_MORE_POPULAR);
            boolean extendedResults = params.getBool(SPELLCHECK_EXTENDED_RESULTS, false);
            boolean collate = params.getBool(SPELLCHECK_COLLATE, false);
            float accuracy = params.getFloat(SPELLCHECK_ACCURACY, Float.MIN_VALUE);
            Integer alternativeTermCount = params.getInt(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT);
            Integer maxResultsForSuggest = params.getInt(SpellingParams.SPELLCHECK_MAX_RESULTS_FOR_SUGGEST);
            ModifiableSolrParams customParams = new ModifiableSolrParams();
            for (String checkerName : getDictionaryNames(params)) {
                customParams.add(getCustomParams(checkerName, params));
            }

            Integer hitsInteger = (Integer) rb.rsp.getToLog().get("hits");
            long hits = 0;
            if (hitsInteger == null) {
                hits = rb.getNumberDocumentsFound();
            } else {
                hits = hitsInteger.longValue();
            }
            SpellingResult spellingResult = null;
            if (maxResultsForSuggest == null || hits <= maxResultsForSuggest) {
                SuggestMode suggestMode = SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX;
                if (onlyMorePopular) {
                    suggestMode = SuggestMode.SUGGEST_MORE_POPULAR;
                } else if (alternativeTermCount != null) {
                    suggestMode = SuggestMode.SUGGEST_ALWAYS;
                }

                IndexReader reader = rb.req.getSearcher().getIndexReader();
                SpellingOptions options = new SpellingOptions(tokens, reader, count, alternativeTermCount,
                        suggestMode, extendedResults, accuracy, customParams);
                spellingResult = spellChecker.getSuggestions(options);
            } else {
                spellingResult = new SpellingResult();
            }
            boolean isCorrectlySpelled = hits > (maxResultsForSuggest == null ? 0 : maxResultsForSuggest);
            NamedList suggestions = toNamedList(shardRequest, spellingResult, q, extendedResults, collate,
                    isCorrectlySpelled);
            if (collate) {
                ModifiableSolrParams modParams = new ModifiableSolrParams(params);
                // SH: having both spellcheck.q and q set screws up collations for some queries, such as "java develope"
                modParams.remove(CommonParams.Q);

                //SH: Note that the collator runs a query against the DF specified field. Ideally it should
                //run the query against the spellchecker field but that's inaccessible here
                addCollationsToResponse(modParams, spellingResult, rb, q, suggestions,
                        spellChecker.isSuggestionsMayOverlap());
            }
            NamedList response = new SimpleOrderedMap();
            response.add("suggestions", suggestions);
            rb.rsp.add("spellcheck", response);

        } else {
            throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Specified dictionaries do not exist: "
                    + getDictionaryNameAsSingleString(getDictionaryNames(params)));
        }
    }
}

From source file:org.vootoo.client.netty.NettySolrClient.java

License:Apache License

protected ResponseParser createRequest(SolrRequest request, ProtobufRequestSetter saveRequestSetter)
        throws SolrServerException, IOException {

    SolrParams params = request.getParams();
    Collection<ContentStream> streams = requestWriter.getContentStreams(request);//throw IOException
    String path = requestWriter.getPath(request);
    if (path == null || !path.startsWith("/")) {
        path = DEFAULT_PATH;//from w  w  w  .j  a v  a  2 s .  c  o m
    }

    ResponseParser parser = request.getResponseParser();
    if (parser == null) {
        parser = this.parser;
    }

    // The parser 'wt=' and 'version=' params are used instead of the original
    // params
    ModifiableSolrParams wparams = new ModifiableSolrParams(params);
    if (parser != null) {
        wparams.set(CommonParams.WT, parser.getWriterType());
        wparams.set(CommonParams.VERSION, parser.getVersion());
    }
    if (invariantParams != null) {
        wparams.add(invariantParams);
    }

    Integer timeout = wparams.getInt(CommonParams.TIME_ALLOWED);
    if (timeout == null) {
        //mandatory use TIME_ALLOWED
        timeout = defaultTimeout;
        wparams.set(CommonParams.TIME_ALLOWED, timeout);
    }

    saveRequestSetter.setTimeout(timeout);
    saveRequestSetter.setPath(path);
    saveRequestSetter.setSolrParams(wparams);

    if (request.getMethod() != null) {
        saveRequestSetter.setMethod(request.getMethod());
    }

    if (SolrRequest.METHOD.GET == request.getMethod()) {
        if (streams != null) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
        }
        return parser;
    }

    if (SolrRequest.METHOD.POST == request.getMethod() || SolrRequest.METHOD.PUT == request.getMethod()) {
        if (streams != null) {
            saveRequestSetter.setContentStreams(streams);
        }

        return parser;
    }

    throw new SolrServerException("Unsupported method: " + request.getMethod());
}