public static InputStream sendQuery(HTTPSession session, String remoteURI, String query) throws IOException { long start = System.currentTimeMillis(); StringBuilder sbuff = new StringBuilder(remoteURI); sbuff.append("?"); sbuff.append(query); if (showRequest) System.out.printf(" CdmRemote sendQuery= %s", sbuff); HTTPMethod method = HTTPFactory.Get(session, sbuff.toString()); try { int statusCode = method.execute(); if (statusCode == 404) { throw new FileNotFoundException(method.getPath() + " " + method.getStatusLine()); } else if (statusCode >= 400) { throw new IOException(method.getPath() + " " + method.getStatusLine()); } InputStream stream = method.getResponseBodyAsStream(); if (showRequest) System.out.printf(" took %d msecs %n", System.currentTimeMillis() - start); // Leave the stream open. We must also leave the HTTPMethod open because the two are linked: // calling close() on one object causes the other object to be closed as well. return stream; } catch (IOException e) { // Close the HTTPMethod if there was an exception; otherwise leave it open. method.close(); throw e; } }
throw new IOException(method.getPath() + " " + method.getStatusLine()); stream = method.getResponseBodyAsStream(); if (showRequest) System.out.printf(" took %d msecs %n", System.currentTimeMillis() - start); return stream;
throw new IOException(getErrorMessage(method)); InputStream is = method.getResponseBodyAsStream(); size += IO.copyB(is, fos, IO.default_socket_buffersize); InputStream is = method.getResponseBodyAsStream(); size += IO.copyB(is, fos, IO.default_socket_buffersize);
throw new IOException(method.getURL() + " " + method.getStatusLine()); InputStream is = method.getResponseBodyAsStream(); size += IO.copyB(is, fos, IO.default_socket_buffersize); InputStream is = method.getResponseBodyAsStream(); size += IO.copyB(is, fos, IO.default_socket_buffersize);
@Test public void testGetStream() throws Exception { String url = baseurl + "/" + testcase; String baseline = getThreddsroot() + relativebaseline + "/" + testcase; System.out.println("*** Testing: HTTPMethod"); System.out.println("*** URL: " + url); System.out.println("*** Testing: HTTPMethod.getResponseBodyAsStream"); try (HTTPMethod method = HTTPFactory.Get(url)) { method.execute(); InputStream stream = method.getResponseBodyAsStream(); // Read the whole thing byte[] buffer = new byte[EXPECTED]; int count = stream.read(buffer); stream.close(); /* should close the method also */ Assert.assertTrue("TestHTTPMethod: stream close did not close method",method.isClosed()); } }
@Test public void testStream1() throws URISyntaxException, IOException { String catalogName = TestOnLocalServer.withHttpPath("catalog.xml"); URI catalogURI = new URI(catalogName); try (HTTPSession client = HTTPFactory.newSession(catalogName)) { HTTPMethod m = HTTPFactory.Get(client,catalogName); int statusCode = m.execute(); logger.debug("status = {}", statusCode); InputStream stream = m.getResponseBodyAsStream(); CatalogBuilder builder = new CatalogBuilder(); Catalog cat = builder.buildFromStream(stream, catalogURI); CatalogXmlWriter writer = new CatalogXmlWriter(); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { writer.writeXML(cat, baos, false); logger.debug(baos.toString()); } } }
assert (status == 200) : path + " response status= " + status; try (InputStream is = method.getResponseBodyAsStream()) { System.out.printf("response= '%s'%n", IO.readContents(is));
@Test public void testGetStreamPartial() throws Exception { String url = baseurl + "/" + testcase; String baseline = getThreddsroot() + relativebaseline + "/" + testcase; System.out.println("*** Testing: HTTPMethod"); System.out.println("*** URL: " + url); System.out.println("*** Testing: HTTPMethod.getResponseBodyAsStream partial read"); try (HTTPMethod method = HTTPFactory.Get(url)) { method.execute(); InputStream stream = method.getResponseBodyAsStream(); byte[] buffer = new byte[EXPECTED]; int count = stream.read(buffer, 0, 10); // partial read Assert.assertTrue("TestHTTPMethod: partial stream read closed ,ethod",!method.isClosed()); method.close(); Assert.assertTrue("TestHTTPMetthod: method.close() did not close",method.isClosed()); try { count = stream.read(buffer); } catch (Throwable t) { Assert.assertTrue("TestHTTPMethod: stream read after method.close()",method.isClosed()); } } } }
HTTPMethodStream stream = (HTTPMethodStream) method.getResponseBodyAsStream(); Assert.assertTrue(method.hasStreamOpen()); stream.close(); Assert.assertTrue(methodcount == 1); status = method.execute(); stream = (HTTPMethodStream) method.getResponseBodyAsStream(); method.close(); Assert.assertTrue(stream.isClosed()); Assert.assertTrue(methodcount == 1); status = method.execute(); stream = (HTTPMethodStream) method.getResponseBodyAsStream(); session.close(); Assert.assertTrue(stream.isClosed()); stream = (HTTPMethodStream) method.getResponseBodyAsStream(); readbinaryfile(stream); System.err.println("Stream not closed.");