static public HTTPMethod Get(HTTPSession session) throws HTTPException { return Get(session, null); }
static public HTTPMethod Get(String legalurl) throws HTTPException { return Get(null, legalurl); }
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; } }
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; } }
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; } }
@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()); } }
@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 testBackslashEscaped() throws Exception { String url = TestOnLocalServer.withHttpPath(urlPath + "/hdf5/grid_1_3d_xyz_aug.h5?req=data&var=HDFEOS_INFORMATION/StructMetadata\\.0"); try (HTTPMethod m = HTTPFactory.Get(url)) { int statusCode = m.execute(); System.out.printf("status = %d%n", statusCode); } }
@Test public void testMissingCollection() throws IOException { String catalogPath = TestOnLocalServer.withHttpPath("catalog/Grib/Nonexist/catalog.xml"); try (HTTPMethod method = HTTPFactory.Get(null, catalogPath)) { int statusCode = method.execute(); assert statusCode == 404; // not 500 } }
@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 testEmptyCollection() throws IOException { String catalogPath = TestOnLocalServer.withHttpPath("catalog/Grib/Emptiness/catalog.xml"); try (HTTPMethod method = HTTPFactory.Get(null, catalogPath)) { int statusCode = method.execute(); assert statusCode == 404; // not 500 } }
@Test public void testFailBadUser() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { session.setCredentials(new UsernamePasswordCredentials("baadss", "changeme")); HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertTrue(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test public void testSuccess() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { session.setCredentials(new UsernamePasswordCredentials("tds", "secret666")); HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertEquals(200, statusCode); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test public void testFailNoAuth() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertTrue(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test public void testFailNoAuth() { String endpoint = TestOnLocalServer.withHttpPath("/dodsC/testRestrictedDataset/testData2.nc.dds"); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertTrue("Expected HttpStatus.SC_UNAUTHORIZED|HttpStatus.SC_FORBIDDEN", statusCode== HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } } }
@Test public void testFailBadPassword() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { session.setCredentials(new UsernamePasswordCredentials("tiggeUser", "changeme")); HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); //if (statusCode != HttpStatus.SC_UNAUTHORIZED && statusCode != HttpStatus.SC_FORBIDDEN) // assert false; Assert.assertTrue(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test @Ignore("Temporary to test Caron's case specifically") public void testCaron() { try { try (HTTPMethod m = HTTPFactory.Get(CARON)) { int code = m.execute(); Assert.assertTrue("Unexpected return code: " + code, code == 200); } } catch (Exception use) { use.printStackTrace(); Assert.assertTrue("URISyntaxException", false); } }
@Test public void testIllegalRequest() throws IOException, InvalidRangeException { String url = TestOnLocalServer.withHttpPath("/dodsC/scanLocal/testWrite.nc.dds?"); String esc = url + URLEncoder.encode("<bad>\nworse", CDM.UTF8); try (HTTPSession session = HTTPFactory.newSession(esc)) { HTTPMethod method = HTTPFactory.Get(session); method.execute(); Assert.assertEquals(400, method.getStatusCode()); } } }
@Test public void testByteRange() { String file = "http://" + TestDir.remoteTestServer + "/thredds/fileServer/scanLocal/testData.nc"; try { try (HTTPMethod m = HTTPFactory.Get(file)) { m.setRange(0, 9); int statusCode = m.execute(); System.err.printf("status = %d%n", statusCode); Assert.assertTrue("Unexpected return code: " + statusCode, statusCode == 206); byte[] result = m.getResponseAsBytes(); Assert.assertTrue("Wrong size result", result.length == 10); } } catch (Exception e) { } }