Search in sources :

Example 11 with ContentStream

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);
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) NamedList(org.apache.solr.common.util.NamedList) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException)

Example 12 with ContentStream

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;
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) ArrayList(java.util.ArrayList) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 13 with ContentStream

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");
        }
    };
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) JavaBinUpdateRequestCodec(org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec)

Example 14 with ContentStream

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) ContentStream(org.apache.solr.common.util.ContentStream)

Example 15 with ContentStream

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FormDataRequestParser(org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser) ContentStream(org.apache.solr.common.util.ContentStream) SolrException(org.apache.solr.common.SolrException) Test(org.junit.Test)

Aggregations

ContentStream (org.apache.solr.common.util.ContentStream)45 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)15 SolrException (org.apache.solr.common.SolrException)13 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)12 SolrParams (org.apache.solr.common.params.SolrParams)11 MultiMapSolrParams (org.apache.solr.common.params.MultiMapSolrParams)10 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)10 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)8 HashMap (java.util.HashMap)7 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)7 SolrCore (org.apache.solr.core.SolrCore)7 SolrQueryRequestBase (org.apache.solr.request.SolrQueryRequestBase)7 IOException (java.io.IOException)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 SolrInputDocument (org.apache.solr.common.SolrInputDocument)6 NamedList (org.apache.solr.common.util.NamedList)6 Reader (java.io.Reader)5 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)5 FormDataRequestParser (org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser)5