public void close() { if (method != null) method.close(); }
/** * Close the session. This implies closing * any open methods. */ synchronized public void close() { if(this.closed) return; // multiple calls ok while(methodList.size() > 0) { HTTPMethod m = methodList.get(0); m.close(); // forcibly close; will invoke removemethod(). } closed = true; }
public String getResponseAsString(String charset) { if(closed) throw new IllegalStateException("HTTPMethod: method is closed"); String content = null; if(response != null) try { Charset cset = Charset.forName(charset); content = EntityUtils.toString(response.getEntity(), cset); } catch (Exception e) { throw new IllegalArgumentException(e.getMessage()); } close();//getting the response will disallow later stream return content; }
public String getResponseAsString(String charset) { if(closed) throw new IllegalStateException("HTTPMethod: method is closed"); String content = null; if(this.lastresponse != null) try { Charset cset = Charset.forName(charset); content = EntityUtils.toString(this.lastresponse.getEntity(), cset); } catch (Exception e) { throw new IllegalArgumentException(e.getMessage()); } close();//getting the response will disallow later stream return content; }
/** * Close the session. This implies closing * any open methods. */ synchronized public void close() { if(this.closed) return; // multiple calls ok closed = true; for(HTTPMethod m : this.methods) { m.close(); // forcibly close; will invoke removemethod(). } methods.clear(); }
/** * Closes this input stream and releases any system resources associated * with the stream; closes the method also. * * @exception IOException if an I/O error occurs; but not if close * is called twice. */ @Override public void close() throws IOException { if(closed) return; /* Allow multiple close calls */ closed = true; try { consume(); } finally { super.close(); } if(method != null) method.close(); }
/** * Closes this input stream and releases any system resources associated * with the stream; closes the method also. * * @exception IOException if an I/O error occurs; but not if close * is called twice. */ @Override public void close() throws IOException { if(closed) return; /* Allow multiple close calls */ closed = true; try { consume(); } finally { super.close(); } if(method != null) method.close(); }
static private ServiceType checkIfDap4(String location) throws IOException { HTTPMethod method = null; // Strip off any trailing DAP4 prefix if (location.endsWith(".dap")) location = location.substring(0, location.length() - ".dap".length()); else if (location.endsWith(".dmr")) location = location.substring(0, location.length() - ".dmr".length()); else if (location.endsWith(".dsr")) location = location.substring(0, location.length() - ".dsr".length()); try { method = HTTPFactory.Get(location + ".dmr"); int status = method.execute(); if (status == 200) { Header h = method.getResponseHeader("Content-Type"); if ((h != null) && (h.getValue() != null)) { String v = h.getValue(); if (v.startsWith("application/vnd.opendap.org")) return ServiceType.DAP4; } } if (status == 401) throw new IOException("Unauthorized to open dataset " + location); // not dods return null; } finally { if (method != null) method.close(); } }
private boolean rangeOk(String url) { HTTPMethod method = null; try { method = HTTPFactory.Get(session,url); method.setRequestHeader("Range", "bytes=" + 0 + "-" + 0); doConnect(method); int code = method.getStatusCode(); if (code != 206) throw new IOException("Server does not support Range requests, code= " + code); Header head = method.getResponseHeader("Content-Range"); total_length=Long.parseLong(head.getValue().substring(head.getValue().lastIndexOf("/")+1)); // clear stream method.close(); return true; } catch (IOException e) { return false; } finally { if (method != null) method.close(); } }
if (method != null) method.close();
private boolean rangeOk(String url) { try { try (HTTPMethod method = HTTPFactory.Get(session, url)) { method.setRange(0,0); doConnect(method); int code = method.getStatusCode(); if(code != 206) throw new IOException("Server does not support Range requests, code= " + code); Header head = method.getResponseHeader("Content-Range"); total_length = Long.parseLong(head.getValue().substring(head.getValue().lastIndexOf("/") + 1)); // clear stream method.close(); return true; } } catch (IOException e) { return false; } }
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; } }
if (method != null) method.close();
if (method != null) method.close();
private HTTPMethod getHttpResponse(URI uri) throws IOException { HTTPMethod method = null; try { method = HTTPFactory.Get(uri.toString()); method.getSession().setConnectionTimeout(this.connectionTimeout); method.getSession().setSoTimeout(this.socketTimeout); method.setFollowRedirects(this.followRedirects); method.setRequestHeader("Accept-Encoding", this.contentEncoding); method.execute(); int statusCode = method.getStatusCode(); if (statusCode == 200 || statusCode == 201) { return method; } method.execute(); return method; } catch (Throwable t) { if (method != null) method.close(); throw t; } } }
SESSIONAGENT.equals(agents.get(0).getValue())); System.out.println("*** Pass: set session agent"); method.close();
@Test public void testClosing1() throws HTTPException { try (HTTPSession s = HTTPFactory.newSession(CLOSEFILE)) { for(int i = 0; i < 500; i++) { HTTPMethod m = HTTPFactory.Head(s); int statusCode = m.execute(); Assert.assertTrue("Unexpected return code: " + statusCode, statusCode == 200); m.close(); } } }
@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()); } } } }
Assert.assertTrue("*** Fail: Connection Timeout", n == 37777); System.out.println("*** Pass: SO Timeout"); method.close();
/** * Test that a large number of open/close does not lose connections; * check for null response. * This test uses an implicit HTTPSession. */ @Test public void testClosing2() throws HTTPException { // Set max # of connections HTTPSession.setGlobalMaxConnections(201); for(int i = 0; i < 200; i++) { HTTPMethod m = HTTPFactory.Get(CLOSEFILE); HttpResponse res = null; try { res = m.executeRaw(); } catch (HTTPException e) { if(e.getCause() instanceof ConnectionPoolTimeoutException) { System.err.println("TestMisc: timeout: " + i); } else throw e; } Assert.assertFalse("Null response", res == null); m.close(); } } }