@Test public void testStreamURL() throws Exception { boolean ok = false; String url = "http://www.apache.org/dist/lucene/solr/"; String txt = null; try { URLConnection connection = new URL(url).openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.connect(); txt = IOUtils.toString(connection.getInputStream()); } catch (Exception ex) { // TODO - should it fail/skip? fail("this test only works if you have a network connection."); return; } SolrCore core = h.getCore(); Map<String, String[]> args = new HashMap<String, String[]>(); args.put(CommonParams.STREAM_URL, new String[] {url}); // Make sure it got a single stream in and out ok List<ContentStream> streams = new ArrayList<ContentStream>(); parser.buildRequestFrom(core, new MultiMapSolrParams(args), streams); assertEquals(1, streams.size()); assertEquals(txt, IOUtils.toString(streams.get(0).getStream())); }
@Test public void testStreamBody() throws Exception { String body1 = "AMANAPLANPANAMA"; String body2 = "qwertasdfgzxcvb"; String body3 = "1234567890"; SolrCore core = h.getCore(); Map<String, String[]> args = new HashMap<String, String[]>(); args.put(CommonParams.STREAM_BODY, new String[] {body1}); // Make sure it got a single stream in and out ok List<ContentStream> streams = new ArrayList<ContentStream>(); parser.buildRequestFrom(core, new MultiMapSolrParams(args), streams); assertEquals(1, streams.size()); assertEquals(body1, IOUtils.toString(streams.get(0).getStream())); // Now add three and make sure they come out ok streams = new ArrayList<ContentStream>(); args.put(CommonParams.STREAM_BODY, new String[] {body1, body2, body3}); parser.buildRequestFrom(core, new MultiMapSolrParams(args), streams); assertEquals(3, streams.size()); ArrayList<String> input = new ArrayList<String>(); ArrayList<String> output = new ArrayList<String>(); input.add(body1); input.add(body2); input.add(body3); output.add(IOUtils.toString(streams.get(0).getStream())); output.add(IOUtils.toString(streams.get(1).getStream())); output.add(IOUtils.toString(streams.get(2).getStream())); // sort them so the output is consistent Collections.sort(input); Collections.sort(output); assertEquals(input.toString(), output.toString()); // set the contentType and make sure tat gets set String ctype = "text/xxx"; streams = new ArrayList<ContentStream>(); args.put(CommonParams.STREAM_CONTENTTYPE, new String[] {ctype}); parser.buildRequestFrom(core, new MultiMapSolrParams(args), streams); for (ContentStream s : streams) { assertEquals(ctype, s.getContentType()); } }
/** * For example: * * <p>String json = solr.request( "/select?qt=dismax&wt=json&q=...", null ); String xml = * solr.request( "/update", "<add><doc><field ..." ); */ public String request(String pathAndParams, String body) throws Exception { String path = null; SolrParams params = null; int idx = pathAndParams.indexOf('?'); if (idx > 0) { path = pathAndParams.substring(0, idx); params = SolrRequestParsers.parseQueryString(pathAndParams.substring(idx + 1)); } else { path = pathAndParams; params = new MapSolrParams(new HashMap<String, String>()); } // Extract the handler from the path or params SolrRequestHandler handler = core.getRequestHandler(path); if (handler == null) { if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) { String qt = params.get(CommonParams.QT); handler = core.getRequestHandler(qt); if (handler == null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + qt); } } } if (handler == null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + path); } // Make a stream for the 'body' content List<ContentStream> streams = new ArrayList<ContentStream>(1); if (body != null && body.length() > 0) { streams.add(new ContentStreamBase.StringStream(body)); } SolrQueryRequest req = null; try { req = parser.buildRequestFrom(core, params, streams); SolrQueryResponse rsp = new SolrQueryResponse(); core.execute(handler, req, rsp); if (rsp.getException() != null) { throw rsp.getException(); } // Now write it out QueryResponseWriter responseWriter = core.getQueryResponseWriter(req); StringWriter out = new StringWriter(); responseWriter.write(out, req, rsp); return out.toString(); } finally { if (req != null) { req.close(); } } }