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.getURI().toString(), method.getRequestHeaders()); printHeaders("Response: " + method.getStatusCode(), method.getResponseHeaders()); } }
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; } }
static int putUrlAsString(String content, String url) throws HTTPException { int status = 0; try { try (HTTPMethod m = HTTPFactory.Put(url)) { m.setRequestContent(new StringEntity(content, ContentType.create("application/text", "UTF-8"))); status = m.execute(); } } catch (UnsupportedCharsetException uce) { throw new HTTPException(uce); } return status; }
static public int putUrlAsString(String content, String url) throws HTTPException { int status = 0; try { try (HTTPMethod m = HTTPFactory.Put(url);) { m.setRequestContent(new StringEntity(content, "application/text", "UTF-8")); status = m.execute(); } } catch (UnsupportedEncodingException uee) { throw new HTTPException(uee); } return status; }
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 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 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()); } }
public void run() { try { try (HTTPMethod m = HTTPFactory.Head(session, this.url)) { int status = m.execute(); Assert.assertTrue("Bad return code: " + status, (status == 200 || status == 404)); } } catch (Exception e) { new Failure("Index: " + index, e).printStackTrace(System.err); } } }
@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 @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) { } }
@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(); } } }