Search in sources :

Example 21 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class DocumentAnalysisRequestHandlerTest method testCharsetInDocument.

// This test should also test charset detection in UpdateRequestHandler,
// but the DocumentAnalysisRequestHandler is simplier to use/check.
@Test
public void testCharsetInDocument() throws Exception {
    final byte[] xmlBytes = ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n" + "<docs>\r\n" + " <doc>\r\n" + "  <field name=\"id\">Müller</field>\r\n" + " </doc>" + "</docs>").getBytes(StandardCharsets.ISO_8859_1);
    // we declare a content stream without charset:
    final ContentStream cs = new ByteStream(xmlBytes, "application/xml");
    ModifiableSolrParams params = new ModifiableSolrParams();
    SolrQueryRequest req = new SolrQueryRequestBase(h.getCore(), params) {

        @Override
        public Iterable<ContentStream> getContentStreams() {
            return Collections.singleton(cs);
        }
    };
    DocumentAnalysisRequest request = handler.resolveAnalysisRequest(req);
    assertNotNull(request);
    final List<SolrInputDocument> documents = request.getDocuments();
    assertNotNull(documents);
    assertEquals(1, documents.size());
    SolrInputDocument doc = documents.get(0);
    assertEquals("Müller", doc.getField("id").getValue());
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) DocumentAnalysisRequest(org.apache.solr.client.solrj.request.DocumentAnalysisRequest) Test(org.junit.Test)

Example 22 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class ClientUtils method toContentStreams.

/**
   * Take a string and make it an iterable ContentStream
   */
public static Collection<ContentStream> toContentStreams(final String str, final String contentType) {
    if (str == null)
        return null;
    ArrayList<ContentStream> streams = new ArrayList<>(1);
    ContentStreamBase ccc = new ContentStreamBase.StringStream(str);
    ccc.setContentType(contentType);
    streams.add(ccc);
    return streams;
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) ArrayList(java.util.ArrayList) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 23 with ContentStream

use of org.apache.solr.common.util.ContentStream in project streamline by hortonworks.

the class StreamlineSolrJsonMapper method createSolrRequest.

private SolrRequest<UpdateResponse> createSolrRequest(String json) {
    final ContentStreamUpdateRequest request = new ContentStreamUpdateRequest(jsonUpdateUrl);
    final ContentStream cs = new ContentStreamBase.StringStream(json, CONTENT_TYPE);
    request.addContentStream(cs);
    LOG.debug("Request generated with JSON: {}", json);
    return request;
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest)

Example 24 with ContentStream

use of org.apache.solr.common.util.ContentStream in project SearchServices by Alfresco.

the class AlfrescoSearchHandler method readJsonIntoContent.

private void readJsonIntoContent(SolrQueryRequest req) throws IOException {
    SolrParams solrParams = req.getParams();
    String jsonFacet = solrParams.get("json.facet");
    if (jsonFacet != null) {
        Object o = ObjectBuilder.fromJSON(jsonFacet);
        Map<String, Object> json = new HashMap();
        json.put("facet", o);
        req.setJSON(json);
    }
    Iterable<ContentStream> streams = req.getContentStreams();
    JSONObject json = (JSONObject) req.getContext().get(AbstractQParser.ALFRESCO_JSON);
    if (json == null) {
        if (streams != null) {
            try {
                Reader reader = null;
                for (ContentStream stream : streams) {
                    reader = new BufferedReader(new InputStreamReader(stream.getStream(), "UTF-8"));
                }
                // SimpleJSON ContentHandler
                if (reader != null) {
                    json = new JSONObject(new JSONTokener(reader));
                    req.getContext().put(AbstractQParser.ALFRESCO_JSON, json);
                }
            } catch (JSONException e) {
            // This is expected when there is no json element to the
            // request
            } catch (IOException e) {
                throw new AlfrescoRuntimeException("IO Error parsing query parameters", e);
            }
        } else if (req.getParams().get(AbstractQParser.ALFRESCO_JSON) != null) {
            // json is in the params.
            req.getContext().put(AbstractQParser.ALFRESCO_JSON, new JSONObject(req.getParams().get(AbstractQParser.ALFRESCO_JSON)));
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ExitableDirectoryReader(org.apache.lucene.index.ExitableDirectoryReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONTokener(org.json.JSONTokener) ContentStream(org.apache.solr.common.util.ContentStream) JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) JSONObject(org.json.JSONObject)

Example 25 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class SolrRequestParserTest method testStreamURL.

@Test
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));
        }
    }
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) HashMap(java.util.HashMap) SolrCore(org.apache.solr.core.SolrCore) BufferedInputStream(java.io.BufferedInputStream) ServletInputStream(javax.servlet.ServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) URL(java.net.URL) 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