use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrInfoMBeanHandler method handleRequestBody.
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
NamedList<NamedList<NamedList<Object>>> cats = getMBeanInfo(req);
if (req.getParams().getBool("diff", false)) {
ContentStream body = null;
try {
body = req.getContentStreams().iterator().next();
} catch (Exception ex) {
throw new SolrException(ErrorCode.BAD_REQUEST, "missing content-stream for diff");
}
String content = IOUtils.toString(body.getReader());
NamedList<NamedList<NamedList<Object>>> ref = fromXML(content);
// Normalize the output
SolrQueryResponse wrap = new SolrQueryResponse();
wrap.add("solr-mbeans", cats);
cats = (NamedList<NamedList<NamedList<Object>>>) BinaryResponseWriter.getParsedResponse(req, wrap).get("solr-mbeans");
// Get rid of irrelevant things
ref = normalize(ref);
cats = normalize(cats);
// Only the changes
boolean showAll = req.getParams().getBool("all", false);
rsp.add("solr-mbeans", getDiff(ref, cats, showAll));
} else {
rsp.add("solr-mbeans", cats);
}
// never cache, no matter what init config looks like
rsp.setHttpCaching(false);
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class AutoCommitTest method toContentStreams.
/**
* Take a string and make it an iterable ContentStream
*
* This should be moved to a helper class. (it is useful for the client too!)
*/
public static Collection<ContentStream> toContentStreams(final String str, final String contentType) {
ArrayList<ContentStream> streams = new ArrayList<>();
ContentStreamBase stream = new ContentStreamBase.StringStream(str);
if (contentType != null)
stream.setContentType(contentType);
streams.add(stream);
return streams;
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class BinaryRequestWriter method getContentStream.
@Override
public ContentStream getContentStream(final UpdateRequest request) throws IOException {
final BAOS baos = new BAOS();
new JavaBinUpdateRequestCodec().marshal(request, baos);
return new ContentStream() {
@Override
public String getName() {
return null;
}
@Override
public String getSourceInfo() {
return "javabin";
}
@Override
public String getContentType() {
return "application/javabin";
}
@Override
public // size if we know it, otherwise null
Long getSize() {
return new Long(baos.size());
}
@Override
public InputStream getStream() {
return new ByteArrayInputStream(baos.getbuf(), 0, baos.size());
}
@Override
public Reader getReader() {
throw new RuntimeException("No reader available . this is a binarystream");
}
};
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrRequestParserTest method doAutoDetect.
public void doAutoDetect(String userAgent, String method, final String body, String expectedContentType, String expectedKey, String expectedValue) throws Exception {
String uri = "/solr/select";
String contentType = "application/x-www-form-urlencoded";
// does this mean auto-detect?
int contentLength = -1;
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader("User-Agent")).thenReturn(userAgent);
when(request.getRequestURI()).thenReturn(uri);
when(request.getContentType()).thenReturn(contentType);
when(request.getContentLength()).thenReturn(contentLength);
when(request.getMethod()).thenReturn(method);
// we dont pass a content-length to let the security mechanism limit it:
when(request.getQueryString()).thenReturn("foo=1&bar=2");
when(request.getInputStream()).thenReturn(new ByteServletInputStream(body.getBytes(StandardCharsets.US_ASCII)));
SolrRequestParsers parsers = new SolrRequestParsers(h.getCore().getSolrConfig());
SolrQueryRequest req = parsers.parse(h.getCore(), "/select", request);
int num = 0;
if (expectedContentType != null) {
for (ContentStream cs : req.getContentStreams()) {
num++;
assertTrue(cs.getContentType().startsWith(expectedContentType));
String returnedBody = IOUtils.toString(cs.getReader());
assertEquals(body, returnedBody);
}
assertEquals(1, num);
}
assertEquals("1", req.getParams().get("foo"));
assertEquals("2", req.getParams().get("bar"));
if (expectedKey != null) {
assertEquals(expectedValue, req.getParams().get(expectedKey));
}
req.close();
verify(request).getInputStream();
}
use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.
the class SolrRequestParserTest method testParameterIncompatibilityException2.
@Test
public void testParameterIncompatibilityException2() throws Exception {
HttpServletRequest request = getMock("/solr/select", "application/x-www-form-urlencoded", 100);
when(request.getMethod()).thenReturn("POST");
// we emulate Tomcat that throws IllegalStateException when parameters were parsed before:
when(request.getInputStream()).thenThrow(new IllegalStateException());
FormDataRequestParser formdata = new FormDataRequestParser(2048);
try {
formdata.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
fail("should throw SolrException");
} catch (SolrException solre) {
assertTrue(solre.getMessage().startsWith("Solr requires that request parameters"));
assertEquals(500, solre.code());
}
verify(request).getInputStream();
}
Aggregations