use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrRequestParserTest method testStandardParseParamsAndFillStreamsISO88591.
@Test
public void testStandardParseParamsAndFillStreamsISO88591() throws Exception {
final String getParams = "qt=%FC&dup=foo&ie=iso-8859-1&dup=%FC", postParams = "qt2=%FC&q=hello&d%75p=bar";
final byte[] postBytes = postParams.getBytes(StandardCharsets.US_ASCII);
final String contentType = "application/x-www-form-urlencoded; charset=iso-8859-1";
// Set up the expected behavior
HttpServletRequest request = getMock("/solr/select", contentType, postBytes.length);
when(request.getMethod()).thenReturn("POST");
when(request.getQueryString()).thenReturn(getParams);
when(request.getInputStream()).thenReturn(new ByteServletInputStream(postBytes));
MultipartRequestParser multipart = new MultipartRequestParser(2048);
RawRequestParser raw = new RawRequestParser();
FormDataRequestParser formdata = new FormDataRequestParser(2048);
StandardRequestParser standard = new StandardRequestParser(multipart, raw, formdata);
SolrParams p = standard.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
assertEquals("contentType: " + contentType, "hello", p.get("q"));
assertEquals("contentType: " + contentType, "ü", p.get("qt"));
assertEquals("contentType: " + contentType, "ü", p.get("qt2"));
assertArrayEquals("contentType: " + contentType, new String[] { "foo", "ü", "bar" }, p.getParams("dup"));
verify(request).getInputStream();
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrRequestParserTest method testStreamFile.
@Test
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));
}
}
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrRequestParserTest method testStandardParseParamsAndFillStreams.
@Test
public void testStandardParseParamsAndFillStreams() throws Exception {
final String getParams = "qt=%C3%BC&dup=foo", postParams = "q=hello&d%75p=bar";
final byte[] postBytes = postParams.getBytes(StandardCharsets.US_ASCII);
// Set up the expected behavior
final String[] ct = new String[] { "application/x-www-form-urlencoded", "Application/x-www-form-urlencoded", "application/x-www-form-urlencoded; charset=utf-8", "application/x-www-form-urlencoded;" };
for (String contentType : ct) {
HttpServletRequest request = getMock("/solr/select", contentType, postBytes.length);
when(request.getMethod()).thenReturn("POST");
when(request.getQueryString()).thenReturn(getParams);
when(request.getInputStream()).thenReturn(new ByteServletInputStream(postBytes));
MultipartRequestParser multipart = new MultipartRequestParser(2048);
RawRequestParser raw = new RawRequestParser();
FormDataRequestParser formdata = new FormDataRequestParser(2048);
StandardRequestParser standard = new StandardRequestParser(multipart, raw, formdata);
SolrParams p = standard.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
assertEquals("contentType: " + contentType, "hello", p.get("q"));
assertEquals("contentType: " + contentType, "ü", p.get("qt"));
assertArrayEquals("contentType: " + contentType, new String[] { "foo", "bar" }, p.getParams("dup"));
verify(request).getInputStream();
}
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class BinaryRequestWriter method getContentStreams.
@Override
public Collection<ContentStream> getContentStreams(SolrRequest req) throws IOException {
if (req instanceof UpdateRequest) {
UpdateRequest updateRequest = (UpdateRequest) req;
if (isNull(updateRequest.getDocuments()) && isNull(updateRequest.getDeleteByIdMap()) && isNull(updateRequest.getDeleteQuery()) && (updateRequest.getDocIterator() == null)) {
return null;
}
List<ContentStream> l = new ArrayList<>();
l.add(new LazyContentStream(updateRequest));
return l;
} else {
return super.getContentStreams(req);
}
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class HttpSolrClient method createMethod.
protected HttpRequestBase createMethod(final SolrRequest request, String collection) throws IOException, SolrServerException {
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);
}
String basePath = baseUrl;
if (collection != null)
basePath += "/" + collection;
if (request instanceof V2Request) {
if (System.getProperty("solr.v2RealPath") == null) {
basePath = baseUrl.replace("/solr", "/v2");
} else {
basePath = baseUrl + "/____v2";
}
}
if (SolrRequest.METHOD.GET == request.getMethod()) {
if (streams != null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
}
return new HttpGet(basePath + path + wparams.toQueryString());
}
if (SolrRequest.METHOD.DELETE == request.getMethod()) {
return new HttpDelete(basePath + path + wparams.toQueryString());
}
if (SolrRequest.METHOD.POST == request.getMethod() || SolrRequest.METHOD.PUT == request.getMethod()) {
String url = basePath + path;
boolean hasNullStreamName = false;
if (streams != null) {
for (ContentStream cs : streams) {
if (cs.getName() == null) {
hasNullStreamName = true;
break;
}
}
}
boolean isMultipart = ((this.useMultiPartPost && SolrRequest.METHOD.POST == request.getMethod()) || (streams != null && streams.size() > 1)) && !hasNullStreamName;
LinkedList<NameValuePair> postOrPutParams = new LinkedList<>();
if (streams == null || isMultipart) {
// send server list and request list as query string params
ModifiableSolrParams queryParams = calculateQueryParams(this.queryParams, wparams);
queryParams.add(calculateQueryParams(request.getQueryParams(), wparams));
String fullQueryUrl = url + queryParams.toQueryString();
HttpEntityEnclosingRequestBase postOrPut = SolrRequest.METHOD.POST == request.getMethod() ? new HttpPost(fullQueryUrl) : new HttpPut(fullQueryUrl);
if (!isMultipart) {
postOrPut.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
}
List<FormBodyPart> parts = new LinkedList<>();
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, StandardCharsets.UTF_8)));
} else {
postOrPutParams.add(new BasicNameValuePair(p, v));
}
}
}
}
// TODO: remove deprecated - first simple attempt failed, see {@link MultipartEntityBuilder}
if (isMultipart && streams != null) {
for (ContentStream content : streams) {
String contentType = content.getContentType();
if (contentType == null) {
// default
contentType = BinaryResponseParser.BINARY_CONTENT_TYPE;
}
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);
}
postOrPut.setEntity(entity);
} else {
//not using multipart
postOrPut.setEntity(new UrlEncodedFormEntity(postOrPutParams, StandardCharsets.UTF_8));
}
return postOrPut;
} else // It is has one stream, it is the post body, put the params in the URL
{
String fullQueryUrl = url + wparams.toQueryString();
HttpEntityEnclosingRequestBase postOrPut = SolrRequest.METHOD.POST == request.getMethod() ? new HttpPost(fullQueryUrl) : new HttpPut(fullQueryUrl);
// 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) {
Long size = contentStream[0].getSize();
postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {
@Override
public Header getContentType() {
return new BasicHeader("Content-Type", contentStream[0].getContentType());
}
@Override
public boolean isRepeatable() {
return false;
}
});
} else {
Long size = contentStream[0].getSize();
postOrPut.setEntity(new InputStreamEntity(contentStream[0].getStream(), size == null ? -1 : size) {
@Override
public Header getContentType() {
return new BasicHeader("Content-Type", contentStream[0].getContentType());
}
@Override
public boolean isRepeatable() {
return false;
}
});
}
return postOrPut;
}
}
throw new SolrServerException("Unsupported method: " + request.getMethod());
}
Aggregations