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; } }
String url = remoteURI + "?req=header"; method = HTTPFactory.Get(httpClient, url); method.setFollowRedirects(true); if (showRequest) System.out.printf("CdmRemote request %s %n", url); int statusCode = method.execute(); throw new FileNotFoundException(method.getURL() + " " + method.getStatusLine()); throw new IOException(method.getURL() + " " + method.getStatusLine()); InputStream is = method.getResponseAsStream(); NcStreamReader reader = new NcStreamReader(); reader.readStream(is, this); if (method != null) method.close();
int status = method.execute(); if (status == 200) { Header h = method.getResponseHeader("Content-Description"); if ((h != null) && (h.getValue() != null)) { String v = h.getValue(); return ServiceType.OPENDAP; else throw new IOException("OPeNDAP Server Error= " + method.getResponseAsString()); if (method != null) method.close();
private static String getErrorMessage(HTTPMethod method) { String path = method.getURI().toString(); String status = method.getStatusLine(); String content = method.getResponseAsString(); return (content == null) ? path+" "+status : path+" "+status +"\n "+content; }
private void doConnect(HTTPMethod method) throws IOException { // Execute the method. int statusCode = method.execute(); if (statusCode == 404) throw new FileNotFoundException(url + " " + method.getStatusLine()); if (statusCode >= 300) throw new IOException(url + " " + method.getStatusLine()); if (debugDetails) { // request headers dont seem to be available until after execute() printHeaders("Request: " + method.getName() + " " + method.getPath(), method.getRequestHeaders()); printHeaders("Response: " + method.getStatusCode(), method.getResponseHeaders()); } }
static String getUrlAsString(String url) throws HTTPException { try ( HTTPMethod m = HTTPFactory.Get(url);) { int status = m.execute(); String content = null; if(status == 200) { content = m.getResponseAsString(); } return content; } }
method = HTTPFactory.Get(httpClient, url); if (showRequest) System.out.printf("CdmRemote request %s %n", url); int statusCode = method.execute(); throw new FileNotFoundException(method.getURL() + " " + method.getStatusLine()); throw new IOException(method.getURL() + " " + method.getStatusLine()); InputStream is = method.getResponseBodyAsStream(); size += IO.copyB(is, fos, IO.default_socket_buffersize); if (method != null) method.close(); int statusCode = method.execute(); throw new FileNotFoundException(method.getPath() + " " + method.getStatusLine()); throw new IOException(method.getPath() + " " + method.getStatusLine()); Header h = method.getResponseHeader("Content-Length"); if (h != null) { String s = h.getValue(); InputStream is = method.getResponseBodyAsStream(); size += IO.copyB(is, fos, IO.default_socket_buffersize); if (method != null) method.close();
try { method = HTTPFactory.Get(httpClient, f.toString()); int statusCode = method.execute(); throw new FileNotFoundException(method.getPath() + " " + method.getStatusLine()); throw new IOException(method.getPath() + " " + method.getStatusLine()); Header h = method.getResponseHeader("Content-Length"); if (h != null) { String s = h.getValue(); InputStream is = method.getResponseAsStream(); NcStreamReader reader = new NcStreamReader(); NcStreamReader.DataResult result = reader.readData(is, this); 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; } } }
try { method = HTTPFactory.Get(session); method.setFollowRedirects(true); method.setRequestHeader("Range", "bytes=" + pos + "-" + end); doConnect(method); int code = method.getStatusCode(); if (code != 206) throw new IOException("Server does not support Range requests, code= " + code); String s = method.getResponseHeader("Content-Length").getValue(); if (s == null) throw new IOException("Server does not send Content-Length header"); readLen = Math.min(len, readLen); InputStream is = method.getResponseAsStream(); readLen = copy(is, buff, offset, readLen); return readLen; if (method != null) method.close();
Assert.assertFalse(method.isClosed()); int methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 1); status = method.execute(); HTTPMethodStream stream = (HTTPMethodStream) method.getResponseBodyAsStream(); Assert.assertTrue(method.hasStreamOpen()); stream.close(); Assert.assertTrue(method.isClosed()); Assert.assertFalse(method.hasStreamOpen()); methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 0); methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 1); status = method.execute(); stream = (HTTPMethodStream) method.getResponseBodyAsStream(); method.close(); Assert.assertTrue(stream.isClosed()); Assert.assertFalse(method.hasStreamOpen()); Assert.assertTrue(method.isClosed()); methodcount = session.getMethodcount(); Assert.assertTrue(methodcount == 1); status = method.execute(); stream = (HTTPMethodStream) method.getResponseBodyAsStream(); session.close(); Assert.assertTrue(stream.isClosed()); Assert.assertTrue(method.isClosed());
public static void saveContentToFile(String endpoint, int expectCode, ContentType expectContentType, File saveTo) { logger.debug("req = '{}'", endpoint); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); if (statusCode != 200) { logger.warn("statusCode = {} '{}'", statusCode, method.getResponseAsString()); Assert.assertEquals(expectCode, statusCode); return; } Assert.assertEquals(expectCode, statusCode); if (expectContentType != null) { Header header = method.getResponseHeader(ContentType.HEADER); Assert.assertEquals(expectContentType.getContentHeader(), header.getValue()); } InputStream content = method.getResponseAsStream(); // closing method may close stream ?? IO.appendToFile(content, saveTo.getAbsolutePath()); } catch (Exception e) { logger.error("Problem with HTTP request", e); assert false; } } }
method.setCompression("deflate,gzip"); method.setUseSessions(true); int statusCode; for(; ; ) { statusCode = method.execute(); if(statusCode != HttpStatus.SC_SERVICE_UNAVAILABLE) break; throw new DAP2Exception(DAP2Exception.NO_SUCH_FILE, method.getStatusText() + ": " + urlString); throw new InvalidCredentialsException(method.getStatusText()); throw new DAP2Exception("Method failed:" + method.getStatusText() + " on URL= " + urlString); is = method.getResponseAsStream(); Header header = method.getResponseHeader("Content-Description"); if(header != null && (header.getValue().equals("dods-error") || header.getValue().equals("dods_error"))) { Header h = method.getResponseHeader("content-encoding"); String encoding = (h == null) ? null : h.getValue();
static private boolean checkIfRemoteNcml(String location) throws IOException { if (decodePathExtension(location)==ServiceType.NCML) { // just because location ends with ncml does not mean it's ncml // if the ncml file is being served up via http by a remote server, // we should be able to read the first bit of it and see if it even // looks like an ncml file. try (HTTPMethod method = HTTPFactory.Get(location)) { method.setRange(0, NUM_BYTES_TO_DETERMINE_NCML); method.setRequestHeader("accept-encoding", "identity"); int statusCode = method.execute(); if (statusCode >= 300) { if (statusCode == 401) { throw new IOException("Unauthorized to open dataset " + location); } else if (statusCode == 406) { String msg = location + " - this server does not support returning content without any encoding."; msg = msg + " Please download the file locally. Return status=" + statusCode; throw new IOException(msg); } else { throw new IOException(location + " is not a valid URL, return status=" + statusCode); } } return checkIfNcml(method.getResponseAsString()); } } return false; }
@Test public void testRestriction() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try { try (HTTPMethod method = HTTPFactory.Get(endpoint)) { int statusCode = method.execute(); if (statusCode != HttpStatus.SC_UNAUTHORIZED && statusCode != HttpStatus.SC_FORBIDDEN) { logger.error(String.format("statuscode=%d expected HttpStatus.SC_UNAUTHORIZED or HttpStatus.SC_FORBIDDEN", statusCode)); assert false; } } } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
int statusCode = method.execute(); Header h = method.getResponseHeader("Content-Length"); if (h != null) { String s = h.getValue(); InputStream is = method.getResponseAsStream(); if (!NcStream.readAndTest(is, NcStream.MAGIC_DATACOV)) throw new IOException("Data transfer corrupted");
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(); } }
int statusCode = method.execute(); throw new IOException(getErrorMessage(method)); InputStream is = method.getResponseAsStream();
int statusCode = method.execute(); logger.warn("statusCode = {} '{}'", statusCode, method.getResponseAsString()); return null; Header header = method.getResponseHeader(ContentType.HEADER); Assert.assertEquals(expectContentType.getContentHeader().toLowerCase(), header.getValue().toLowerCase()); return method.getResponseAsBytes();
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(); } }