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());
}
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;
}
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;
}
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)));
}
}
}
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));
}
}
}
Aggregations