org.apache.solr.common.params.MultiMapSolrParams Java Examples

The following examples show how to use org.apache.solr.common.params.MultiMapSolrParams. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MCRSolrProxyServlet.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
static Map<String, String[]> toMultiMap(ModifiableSolrParams solrQueryParameter) {
    NamedList<Object> namedList = solrQueryParameter.toNamedList();
    //disabled for MCR-953 and https://issues.apache.org/jira/browse/SOLR-7508
    //Map<String, String[]> parameters = ModifiableSolrParams.toMultiMap(namedList);
    HashMap<String, String[]> parameters = new HashMap<>();
    for (int i = 0; i < namedList.size(); i++) {
        String name = namedList.getName(i);
        Object val = namedList.getVal(i);
        if (val instanceof String[]) {
            MultiMapSolrParams.addParam(name, (String[]) val, parameters);
        } else {
            MultiMapSolrParams.addParam(name, val.toString(), parameters);
        }
    }
    //end of fix
    return parameters;
}
 
Example #2
Source File: UniqFieldsUpdateProcessorFactoryTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void addDoc(String doc) throws Exception {
  Map<String, String[]> params = new HashMap<>();
  MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
  params.put(UpdateParams.UPDATE_CHAIN, new String[] { "uniq-fields" });
  SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(),
      (SolrParams) mmparams) {
  };

  UpdateRequestHandler handler = new UpdateRequestHandler();
  handler.init(null);
  ArrayList<ContentStream> streams = new ArrayList<>(2);
  streams.add(new ContentStreamBase.StringStream(doc));
  req.setContentStreams(streams);
  handler.handleRequestBody(req, new SolrQueryResponse());
  req.close();
}
 
Example #3
Source File: SolrRequestParserTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({"try"})
public void testStreamFile() throws Exception
{
  File file = getFile("README");
  
  byte[] bytes = FileUtils.readFileToByteArray(file);

  SolrCore core = h.getCore();
  
  Map<String,String[]> args = new HashMap<>();
  args.put( CommonParams.STREAM_FILE, new String[] { file.getAbsolutePath() } );
  
  // Make sure it got a single stream in and out ok
  List<ContentStream> streams = new ArrayList<>();
  try (SolrQueryRequest req = parser.buildRequestFrom( core, new MultiMapSolrParams( args ), streams )) {
    assertEquals( 1, streams.size() );
    try (InputStream in = streams.get(0).getStream()) {
      assertArrayEquals( bytes, IOUtils.toByteArray( in ) );
    }
  }
}
 
Example #4
Source File: SolrRequestParserTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({"try"})
public void testStreamURL() throws Exception
{
  URL url = getClass().getResource("/README");
  assertNotNull("Missing file 'README' in test-resources root folder.", url);
  
  byte[] bytes = IOUtils.toByteArray(url);

  SolrCore core = h.getCore();
  
  Map<String,String[]> args = new HashMap<>();
  args.put( CommonParams.STREAM_URL, new String[] { url.toExternalForm() } );
  
  // Make sure it got a single stream in and out ok
  List<ContentStream> streams = new ArrayList<>();
  try (SolrQueryRequest req = parser.buildRequestFrom( core, new MultiMapSolrParams( args ), streams )) {
    assertEquals( 1, streams.size() );
    try (InputStream in = streams.get(0).getStream()) {
      assertArrayEquals( bytes, IOUtils.toByteArray( in ) );
    }
  }
}
 
Example #5
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SolrParams parseParamsAndFillStreams(
    final HttpServletRequest req, ArrayList<ContentStream> streams) throws Exception {
  if (!isMultipart(req)) {
    throw new SolrException( ErrorCode.BAD_REQUEST, "Not multipart content! "+req.getContentType() );
  }
  // Magic way to tell Jetty dynamically we want multi-part processing.  "Request" here is a Jetty class
  req.setAttribute(Request.MULTIPART_CONFIG_ELEMENT, multipartConfigElement);

  MultiMapSolrParams params = parseQueryString( req.getQueryString() );

  // IMPORTANT: the Parts will all have the delete() method called by cleanupMultipartFiles()

  for (Part part : req.getParts()) {
    if (part.getSubmittedFileName() == null) { // thus a form field and not file upload
      // If it's a form field, put it in our parameter map
      String partAsString = org.apache.commons.io.IOUtils.toString(new PartContentStream(part).getReader());
      MultiMapSolrParams.addParam(
          part.getName().trim(),
          partAsString, params.getMap() );
    } else { // file upload
      streams.add(new PartContentStream(part));
    }
  }
  return params;
}
 
Example #6
Source File: LocalSolrQueryRequest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected static SolrParams makeParams(String query, String qtype, int start, int limit,
                                       @SuppressWarnings({"rawtypes"})Map args) {
  Map<String,String[]> map = new HashMap<>();
  for (@SuppressWarnings({"rawtypes"})Iterator iter = args.entrySet().iterator(); iter.hasNext();) {
    @SuppressWarnings({"rawtypes"})
    Map.Entry e = (Map.Entry)iter.next();
    String k = e.getKey().toString();
    Object v = e.getValue();
    if (v instanceof String[]) map.put(k,(String[])v);
    else map.put(k,new String[]{v.toString()});
  }
  if (query!=null) map.put(CommonParams.Q, new String[]{query});
  if (qtype!=null) map.put(CommonParams.QT, new String[]{qtype});
  map.put(CommonParams.START, new String[]{Integer.toString(start)});
  map.put(CommonParams.ROWS, new String[]{Integer.toString(limit)});
  return new MultiMapSolrParams(map);
}
 
Example #7
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static void addDoc(String doc, String updateRequestProcessorChain) throws Exception {
  Map<String, String[]> params = new HashMap<>();
  MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
  params.put(UpdateParams.UPDATE_CHAIN, new String[]{updateRequestProcessorChain});
  SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(),
      (SolrParams) mmparams) {
  };

  UpdateRequestHandler handler = new UpdateRequestHandler();
  handler.init(null);
  ArrayList<ContentStream> streams = new ArrayList<>(2);
  streams.add(new ContentStreamBase.StringStream(doc));
  req.setContentStreams(streams);
  handler.handleRequestBody(req, new SolrQueryResponse());
  req.close();
}
 
Example #8
Source File: NamedList.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Create SolrParams from NamedList.  Values must be {@code String[]} or {@code List}
 * (with toString()-appropriate entries), or otherwise have a toString()-appropriate value.
 * Nulls are retained as such in arrays/lists but otherwise will NPE.
 */
public SolrParams toSolrParams() {
  HashMap<String,String[]> map = new HashMap<>();
  for (int i=0; i<this.size(); i++) {
    String name = this.getName(i);
    Object val = this.getVal(i);
    if (val instanceof String[]) {
      MultiMapSolrParams.addParam(name, (String[]) val, map);
    } else if (val instanceof List) {
      List l = (List) val;
      String[] s = new String[l.size()];
      for (int j = 0; j < l.size(); j++) {
        s[j] = l.get(j) == null ? null : l.get(j).toString();
      }
      MultiMapSolrParams.addParam(name, s, map);
    } else {
      //TODO: we NPE if val is null; yet we support val members above. A bug?
      MultiMapSolrParams.addParam(name, val.toString(), map);
    }
  }
  // always use MultiMap for easier processing further down the chain
  return new MultiMapSolrParams(map);
}
 
Example #9
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void decodeBuffer(final LinkedList<Object> input, final Map<String,String[]> map, CharsetDecoder charsetDecoder) {
  for (final Iterator<Object> it = input.iterator(); it.hasNext(); ) {
    final byte[] keyBytes = (byte[]) it.next();
    it.remove();
    final Long keyPos = (Long) it.next();
    it.remove();
    final byte[] valueBytes = (byte[]) it.next();
    it.remove();
    final Long valuePos = (Long) it.next();
    it.remove();
    MultiMapSolrParams.addParam(decodeChars(keyBytes, keyPos.longValue(), charsetDecoder).trim(),
        decodeChars(valueBytes, valuePos.longValue(), charsetDecoder), map);
  }
}
 
Example #10
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public SolrParams parseParamsAndFillStreams(HttpServletRequest req, ArrayList<ContentStream> streams, InputStream in) throws Exception {
  final Map<String,String[]> map = new HashMap<>();

  // also add possible URL parameters and include into the map (parsed using UTF-8):
  final String qs = req.getQueryString();
  if (qs != null) {
    parseQueryString(qs, map);
  }

  // may be -1, so we check again later. But if it's already greater we can stop processing!
  final long totalLength = req.getContentLength();
  final long maxLength = ((long) uploadLimitKB) * 1024L;
  if (totalLength > maxLength) {
    throw new SolrException(ErrorCode.BAD_REQUEST, "application/x-www-form-urlencoded content length (" +
        totalLength + " bytes) exceeds upload limit of " + uploadLimitKB + " KB");
  }

  // get query String from request body, using the charset given in content-type:
  final String cs = ContentStreamBase.getCharsetFromContentType(req.getContentType());
  final Charset charset = (cs == null) ? StandardCharsets.UTF_8 : Charset.forName(cs);

  try {
    // Protect container owned streams from being closed by us, see SOLR-8933
    in = FastInputStream.wrap( in == null ? new CloseShieldInputStream(req.getInputStream()) : in );

    final long bytesRead = parseFormDataContent(in, maxLength, charset, map, false);
    if (bytesRead == 0L && totalLength > 0L) {
      throw getParameterIncompatibilityException();
    }
  } catch (IOException ioe) {
    throw new SolrException(ErrorCode.BAD_REQUEST, ioe);
  } catch (IllegalStateException ise) {
    throw (SolrException) getParameterIncompatibilityException().initCause(ise);
  } finally {
    IOUtils.closeWhileHandlingException(in);
  }

  return new MultiMapSolrParams(map);
}
 
Example #11
Source File: TestCollectionAPIs.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Pair<SolrQueryRequest, SolrQueryResponse> makeCall(final ApiBag apiBag, String path,
                                                                 final SolrRequest.METHOD method,
                                                                 final String payload, final CoreContainer cc) throws Exception {
  SolrParams queryParams = new MultiMapSolrParams(Collections.emptyMap());
  if (path.indexOf('?') > 0) {
    String queryStr = path.substring(path.indexOf('?') + 1);
    path = path.substring(0, path.indexOf('?'));
    queryParams = SolrRequestParsers.parseQueryString(queryStr);
  }
  final HashMap<String, String> parts = new HashMap<>();
  Api api = apiBag.lookup(path, method.toString(), parts);
  if (api == null) throw new RuntimeException("No handler at path :" + path);
  SolrQueryResponse rsp = new SolrQueryResponse();
  LocalSolrQueryRequest req = new LocalSolrQueryRequest(null, queryParams) {
    @Override
    public List<CommandOperation> getCommands(boolean validateInput) {
      if (payload == null) return Collections.emptyList();
      return ApiBag.getCommandOperations(new ContentStreamBase.StringStream(payload), api.getCommandSchema(), true);
    }

    @Override
    public Map<String, String> getPathTemplateValues() {
      return parts;
    }

    @Override
    public String getHttpMethod() {
      return method.toString();
    }
  };
  try {
    api.call(req, rsp);
  } catch (ApiBag.ExceptionWithErrObject e) {
    throw new RuntimeException(e.getMessage() + Utils.toJSONString(e.getErrs()), e);

  }
  return new Pair<>(req, rsp);
}
 
Example #12
Source File: OntologyUpdateProcessorFactoryTest.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
static void addDoc(String doc, String chain) throws Exception {
	Map<String, String[]> params = new HashMap<>();
	MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
	params.put(UpdateParams.UPDATE_CHAIN, new String[] { chain });
	SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(), mmparams) {
	};

	UpdateRequestHandler handler = new UpdateRequestHandler();
	handler.init(null);
	ArrayList<ContentStream> streams = new ArrayList<>(2);
	streams.add(new ContentStreamBase.StringStream(doc));
	req.setContentStreams(streams);
	handler.handleRequestBody(req, new SolrQueryResponse());
	req.close();
}
 
Example #13
Source File: PutSolrRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void index(boolean isSolrCloud, String collection, Long commitWithin, String contentStreamPath, MultiMapSolrParams requestParams, List<SolrInputDocument> inputDocumentList)
        throws IOException, SolrServerException,SolrException {
    UpdateRequest request = new UpdateRequest(contentStreamPath);
    request.setParams(new ModifiableSolrParams());

    // add the extra params, don't use 'set' in case of repeating params
    Iterator<String> paramNames = requestParams.getParameterNamesIterator();
    while (paramNames.hasNext()) {
        String paramName = paramNames.next();
        for (String paramValue : requestParams.getParams(paramName)) {
            request.getParams().add(paramName, paramValue);
        }
    }

    // specify the collection for SolrCloud
    if (isSolrCloud) {
        request.setParam(COLLECTION_PARAM_NAME, collection);
    }

    if (commitWithin != null && commitWithin > 0) {
        request.setParam(COMMIT_WITHIN_PARAM_NAME, commitWithin.toString());
    }

    // if a username and password were provided then pass them for basic auth
    if (isBasicAuthEnabled()) {
        request.setBasicAuthCredentials(getUsername(), getPassword());
    }
    request.add(inputDocumentList);
    UpdateResponse response = request.process(getSolrClient());
    getLogger().debug("Got {} response from Solr", new Object[]{response.getStatus()});
    inputDocumentList.clear();
}
 
Example #14
Source File: LocalSolrQueryRequest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public LocalSolrQueryRequest(SolrCore core, Map<String,String[]> args) {
  super(core, new MultiMapSolrParams(args));
}
 
Example #15
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Given a url-encoded query string (UTF-8), map it into solr params
 */
public static MultiMapSolrParams parseQueryString(String queryString) {
  Map<String,String[]> map = new HashMap<>();
  parseQueryString(queryString, map);
  return new MultiMapSolrParams(map);
}
 
Example #16
Source File: RestTestBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Insures that the given param is included in the query with the given value.
 *
 * <ol>
 *   <li>If the param is already included with the given value, the request is returned unchanged.</li>
 *   <li>If the param is not already included, it is added with the given value.</li>
 *   <li>If the param is already included, but with a different value, the value is replaced with the given value.</li>
 *   <li>If the param is already included multiple times, they are replaced with a single param with given value.</li>
 * </ol>
 *
 * The passed-in valueToSet should NOT be URL encoded, as it will be URL encoded by this method.
 *
 * @param query The query portion of a request URL, e.g. "wt=xml&indent=off&fl=id,_version_"
 * @param paramToSet The parameter name to insure the presence of in the returned request 
 * @param valueToSet The parameter value to insure in the returned request
 * @return The query with the given param set to the given value 
 */
private static String setParam(String query, String paramToSet, String valueToSet) {
  if (null == valueToSet) {
    valueToSet = "";
  }
  try {
    StringBuilder builder = new StringBuilder();
    if (null == query || query.trim().isEmpty()) {
      // empty query -> return "paramToSet=valueToSet"
      builder.append(paramToSet);
      builder.append('=');
      StrUtils.partialURLEncodeVal(builder, valueToSet);
      return builder.toString();
    }
    MultiMapSolrParams requestParams = SolrRequestParsers.parseQueryString(query);
    String[] values = requestParams.getParams(paramToSet);
    if (null == values) {
      // paramToSet isn't present in the request -> append "&paramToSet=valueToSet"
      builder.append(query);
      builder.append('&');
      builder.append(paramToSet);
      builder.append('=');
      StrUtils.partialURLEncodeVal(builder, valueToSet);
      return builder.toString();
    }
    if (1 == values.length && valueToSet.equals(values[0])) {
      // paramToSet=valueToSet is already in the query - just return the query as-is.
      return query;
    }
    // More than one value for paramToSet on the request, or paramToSet's value is not valueToSet
    // -> rebuild the query
    boolean isFirst = true;
    for (Map.Entry<String,String[]> entry : requestParams.getMap().entrySet()) {
      String key = entry.getKey();
      String[] valarr = entry.getValue();

      if ( ! key.equals(paramToSet)) {
        for (String val : valarr) {
          builder.append(isFirst ? "" : '&');
          isFirst = false;
          builder.append(key);
          builder.append('=');
          StrUtils.partialURLEncodeVal(builder, null == val ? "" : val);
        }
      }
    }
    builder.append(isFirst ? "" : '&');
    builder.append(paramToSet);
    builder.append('=');
    StrUtils.partialURLEncodeVal(builder, valueToSet);
    return builder.toString();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #17
Source File: SolrRequestParsers.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Returns the parameter map if a different content type was auto-detected */
private static SolrParams autodetect(HttpServletRequest req, ArrayList<ContentStream> streams, FastInputStream in) throws IOException {
  String detectedContentType = null;
  boolean shouldClose = true;

  try {
    in.peek();  // should cause some bytes to be read
    byte[] arr = in.getBuffer();
    int pos = in.getPositionInBuffer();
    int end = in.getEndInBuffer();

    for (int i = pos; i < end - 1; i++) {  // we do "end-1" because we check "arr[i+1]" sometimes in the loop body
      int ch = arr[i];
      boolean isWhitespace = ((WS_MASK >> ch) & 0x01) != 0 && (ch <= ' ' || ch == 0xa0);
      if (!isWhitespace) {
        // first non-whitespace chars
        if (ch == '#'                         // single line comment
            || (ch == '/' && (arr[i + 1] == '/' || arr[i + 1] == '*'))  // single line or multi-line comment
            || (ch == '{' || ch == '[')       // start of JSON object
            )
        {
          detectedContentType = "application/json";
        }
        if (ch == '<') {
          detectedContentType = "text/xml";
        }
        break;
      }
    }

    if (detectedContentType == null) {
      shouldClose = false;
      return null;
    }

    Long size = null;
    String v = req.getHeader("Content-Length");
    if (v != null) {
      size = Long.valueOf(v);
    }
    streams.add(new InputStreamContentStream(in, detectedContentType, size));


    final Map<String, String[]> map = new HashMap<>();
    // also add possible URL parameters and include into the map (parsed using UTF-8):
    final String qs = req.getQueryString();
    if (qs != null) {
      parseQueryString(qs, map);
    }

    return new MultiMapSolrParams(map);

  } catch (IOException ioe) {
    throw new SolrException(ErrorCode.BAD_REQUEST, ioe);
  } catch (IllegalStateException ise) {
    throw (SolrException) FormDataRequestParser.getParameterIncompatibilityException().initCause(ise);
  } finally {
    if (shouldClose) {
      IOUtils.closeWhileHandlingException(in);
    }
  }
}
 
Example #18
Source File: SolrRequestParserTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testStreamBody() throws Exception
{
  String body1 = "AMANAPLANPANAMA";
  String body2 = "qwertasdfgzxcvb";
  String body3 = "1234567890";
  
  SolrCore core = h.getCore();
  
  Map<String,String[]> args = new HashMap<>();
  args.put( CommonParams.STREAM_BODY, new String[] {body1} );
  
  // Make sure it got a single stream in and out ok
  List<ContentStream> streams = new ArrayList<>();
  SolrQueryRequest req = parser.buildRequestFrom( core, new MultiMapSolrParams( args ), streams );
  assertEquals( 1, streams.size() );
  assertEquals( body1, IOUtils.toString( streams.get(0).getReader() ) );
  req.close();

  // Now add three and make sure they come out ok
  streams = new ArrayList<>();
  args.put( CommonParams.STREAM_BODY, new String[] {body1,body2,body3} );
  req = parser.buildRequestFrom( core, new MultiMapSolrParams( args ), streams );
  assertEquals( 3, streams.size() );
  ArrayList<String> input  = new ArrayList<>();
  ArrayList<String> output = new ArrayList<>();
  input.add( body1 );
  input.add( body2 );
  input.add( body3 );
  output.add( IOUtils.toString( streams.get(0).getReader() ) );
  output.add( IOUtils.toString( streams.get(1).getReader() ) );
  output.add( IOUtils.toString( streams.get(2).getReader() ) );
  // sort them so the output is consistent
  Collections.sort( input );
  Collections.sort( output );
  assertEquals( input.toString(), output.toString() );
  req.close();

  // set the contentType and make sure tat gets set
  String ctype = "text/xxx";
  streams = new ArrayList<>();
  args.put( CommonParams.STREAM_CONTENTTYPE, new String[] {ctype} );
  req = parser.buildRequestFrom( core, new MultiMapSolrParams( args ), streams );
  for( ContentStream s : streams ) {
    assertEquals( ctype, s.getContentType() );
  }
  req.close();
}
 
Example #19
Source File: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
public SolrServletRequest(SolrCore core, HttpServletRequest req)
{
    super(core, new MultiMapSolrParams(Collections.emptyMap()));
}