public String getResponseAsString() { return getResponseAsString("UTF-8"); }
public String getResponseAsString() { return getResponseAsString("UTF-8"); }
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 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 static String getErrorMessage(HTTPMethod method) { String path = method.getURI().toString(); String status = method.getStatusLine(); String content = method.getResponseAsString(); return (content == null) ? status + " " + path : status + " " + path +"\n "+content; }
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; } }
return ServiceType.OPENDAP; else throw new IOException("OPeNDAP Server Error= " + method.getResponseAsString());
return ServiceType.OPENDAP; else throw new IOException("OPeNDAP Server Error= " + method.getResponseAsString());
static public String getUrlAsString(String url) throws HTTPException { try ( HTTPSession session = HTTPFactory.newSession(url); HTTPMethod m = HTTPFactory.Get(session);) { int status = m.execute(); String content = null; if(status == 200) { content = m.getResponseAsString(); } return content; } }
public static FeatureType isCdmrfEndpoint(String endpoint) throws IOException { HTTPSession httpClient = HTTPFactory.newSession(endpoint); String url = endpoint + "?req=featureType"; // get the header try (HTTPMethod method = HTTPFactory.Get(httpClient, url)) { method.setFollowRedirects(true); int statusCode = method.execute(); if (statusCode != 200) return null; String content = method.getResponseAsString(); return FeatureType.getType(content); } catch (Throwable t) { t.printStackTrace(); return null; } }
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 testUTF8Stream() throws Exception { pass = true; String catalogName = "http://" + TestDir.remoteTestServer + "/thredds/catalog.xml"; try (HTTPMethod m = HTTPFactory.Get(catalogName)) { int statusCode = m.execute(); System.err.printf("status = %d%n", statusCode); try { String content = m.getResponseAsString("ASCII"); System.err.printf("cat = %s%n", content); } catch (Throwable t) { t.printStackTrace(); assert false; } } }
@Test public void testString() throws URISyntaxException, IOException { String catalogName = "http://localhost:8081/thredds/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); String catAsString = m.getResponseAsString(CDM.UTF8); logger.debug("cat = {}", catAsString); CatalogBuilder builder = new CatalogBuilder(); Catalog cat = builder.buildFromString(catAsString, catalogURI); CatalogXmlWriter writer = new CatalogXmlWriter(); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { writer.writeXML(cat, baos, false); logger.debug(baos.toString()); } } } }
@Test public void writeGridAsPointNetcdf() throws JDOMException, IOException { String endpoint = TestOnLocalServer.withHttpPath(ds+"?var="+varName+query+"&accept=netcdf"); File tempFile = tempFolder.newFile(); logger.debug(" write {} to {}", endpoint, tempFile.getAbsolutePath()); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); if (statusCode != 200) { logger.debug("statusCode = {} '{}'", statusCode, method.getResponseAsString()); return; } IO.appendToFile(method.getResponseAsStream(), tempFile.getAbsolutePath()); } logger.debug(" file length {} bytes exists={} {}", tempFile.length(), tempFile.exists(), tempFile.getAbsolutePath()); // Open the result file as Station feature dataset Formatter errlog = new Formatter(); try (FeatureDataset fd = FeatureDatasetFactoryManager.open(FeatureType.STATION, tempFile.getAbsolutePath(), null, errlog)) { assertNotNull(errlog.toString(), fd); VariableSimpleIF v = fd.getDataVariable(varName); assertNotNull(varName, v); } }
logger.warn("statusCode = {} '{}'", statusCode, method.getResponseAsString()); return null;
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; } } }
break; default: sresult = m.getResponseAsString(); Assert.fail(String.format("httpcode=%d%n%s%n", code,sresult)); break;
try (HTTPMethod m = HTTPFactory.Get(url)) { int code = callserver(m); s = m.getResponseAsString(); Assert.assertTrue(String.format("httpcode=%d msg=%s", code, s), code == 200);
Assert.assertTrue(methodcount == 1); status = method.execute(); String body = method.getResponseAsString();// will close stream try { stream = (HTTPMethodStream) method.getResponseBodyAsStream();