private static void checkGetCode(FedoraClient client, String requestPath, String errorMessage, int expectedCode) throws Exception { HttpInputStream in = client.get(requestPath, false); try { int gotCode = in.getStatusCode(); assertEquals(errorMessage + " (" + requestPath + ")", expectedCode, gotCode); if (expectedCode != 200) { String expectedString = "Fedora: " + expectedCode + " "; BufferedReader reader = new BufferedReader(new InputStreamReader(in)); boolean foundExpectedString = false; String line = reader.readLine(); while (line != null) { if (line.indexOf(expectedString) != -1) { foundExpectedString = true; } line = reader.readLine(); } assertTrue("HTTP status code was correct (" + expectedCode + "), but body did not contain " + "the string \"" + expectedString + "\"", foundExpectedString); } } finally { in.close(); } }
private static void checkGetCode(FedoraClient client, String requestPath, String errorMessage, int expectedCode) throws Exception { HttpInputStream in = client.get(requestPath, false); try { int gotCode = in.getStatusCode(); assertEquals(errorMessage + " (" + requestPath + ")", expectedCode, gotCode); if (expectedCode != 200) { String expectedString = "Fedora: " + expectedCode + " "; BufferedReader reader = new BufferedReader(new InputStreamReader(in)); boolean foundExpectedString = false; String line = reader.readLine(); while (line != null) { if (line.indexOf(expectedString) != -1) { foundExpectedString = true; } line = reader.readLine(); } assertTrue("HTTP status code was correct (" + expectedCode + "), but body did not contain " + "the string \"" + expectedString + "\"", foundExpectedString); } } finally { in.close(); } }
@Test public void testAccessParmResolver() throws Exception { String location = "/getAccessParmResolver?PID=fedora-system:ContentModel-3.0&sDefPID=fedora-system:3&methodName=viewObjectProfile"; HttpInputStream result = s_client.get(getBaseURL() + location, false, false); assertEquals(302, result.getStatusCode()); String expected = getBaseURL() + "/get/fedora-system:ContentModel-3.0/fedora-system:3/viewObjectProfile/"; assertEquals(expected, result.getResponseHeader(HttpHeaders.LOCATION).getValue()); } @Test
/** * Performs the given HTTP request, logging a warning if we don't get a 200 * OK response. */ private void sendRESTMessage(String url) { HttpInputStream response = null; try { logger.debug("Getting " + url); response = _webClient.get(url, false, _gSearchCredentials); int code = response.getStatusCode(); if (code != 200) { logger.warn("Error sending update to GSearch service (url=" + url + "). HTTP response code was " + code + ". " + "Body of response from GSearch follows:\n" + getString(response)); } } catch (Exception e) { logger.warn("Error sending update to GSearch service via URL: " + url, e); } finally { if (response != null) { try { response.close(); } catch (Exception e) { logger.warn("Error closing GSearch response", e); } } } }
/** * Performs the given HTTP request, logging a warning if we don't get a 200 * OK response. */ private void sendRESTMessage(String url) { HttpInputStream response = null; try { logger.debug("Getting " + url); response = _webClient.get(url, false, _gSearchCredentials); int code = response.getStatusCode(); if (code != 200) { logger.warn("Error sending update to GSearch service (url=" + url + "). HTTP response code was " + code + ". " + "Body of response from GSearch follows:\n" + getString(response)); } } catch (Exception e) { logger.warn("Error sending update to GSearch service via URL: " + url, e); } finally { if (response != null) { try { response.close(); } catch (Exception e) { logger.warn("Error closing GSearch response", e); } } } }
/** * Ping the given endpoint to see if an HTTP 302 status code is returned. If * so, return the location given in the HTTP response header. If not, return * null. */ private URL getRedirectURL(String location) throws IOException { HttpInputStream in = get(location, false, false); try { if (in.getStatusCode() == 302) { Header h = in.getResponseHeader("location"); if (h != null) { return new URL(h.getValue()); } } return null; } finally { try { in.close(); } catch (Exception e) { } } }
private void mockResponseFor(String httpMethod, int status) { when(mockParams.getUrl()).thenReturn(TEST_URL); when(mockParams.getProtocol()).thenReturn(HTTP); when(mockContext.getEnvironmentValue(Constants.HTTP_REQUEST.METHOD.attributeId)).thenReturn(httpMethod); String cLen = Long.toString(System.currentTimeMillis()); when(mockResponse.getResponseHeaderValue(HttpHeaders.CONTENT_LENGTH, "-1")).thenReturn(cLen); when(mockResponse.getResponseHeaders()).thenReturn(new Header[0]); when(mockResponse.getStatusCode()).thenReturn(status); when(mockTranslator.makeAbsoluteURLs(TEST_URL)).thenReturn(TEST_URL); when(mockParams.getTranslator()).thenReturn(mockTranslator); mockStatic(ServerUtility.class); when(ServerUtility.isURLFedoraServer(TEST_URL)).thenReturn(false); }
HttpGet getMethod = new HttpGet(urlString); HttpInputStream in = new HttpInputStream(client, getMethod); int status = in.getStatusCode(); logger.debug("GET {} : {}", urlString, status); if (failIfNotOK) { try { throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() + "] : " + urlString); } finally {
int status = in.getStatusCode(); if (failIfNotOK) { if (status != HttpStatus.SC_OK && setHeaders(request, wconfig.getUserAgent(), ifNoneMatch, ifModifiedSince, range); in = new HttpInputStream(client, request); status = in.getStatusCode(); count++; in.close(); throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() + "]"); try { throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() + "]"); } finally {
int status = in.getStatusCode(); if (failIfNotOK) { if (status != HttpStatus.SC_OK && setHeaders(request, wconfig.getUserAgent(), ifNoneMatch, ifModifiedSince, range); in = new HttpInputStream(client, request); status = in.getStatusCode(); count++; in.close(); throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() + "]"); try { throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() + "]"); } finally {
@Test public void testGetDatastreamDissemination() throws Exception { Document result; // test for type X datastream result = getXMLQueryResult(s_client, "/get/demo:5/DC"); assertXpathExists("/oai_dc:dc", result); assertXpathEvaluatesTo("demo:5", "/oai_dc:dc/dc:identifier/text()", result); // test for type E datastream HttpInputStream in = s_client.get("/get/demo:SmileyBeerGlass/MEDIUM_SIZE", true); String actualType = in.getContentType(); long actualLength = in.getContentLength(); in.close(); assertEquals("image/jpeg", actualType); assertTrue(actualLength > 0); // test for type R datastream in = s_client.get("/get/demo:31/DS3", false, false); int actualCode = in.getStatusCode(); in.close(); assertEquals(302, actualCode); // test for type M datastream in = s_client.get("/get/demo:5/THUMBRES_IMG", true); actualType = in.getContentType(); in.close(); assertEquals("image/jpeg", actualType); }
mimeType = DEFAULT_MIMETYPE; if (response.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) { response.close(); Header[] respHeaders = response.getResponseHeaders(); } else if (response.getStatusCode() == HttpStatus.SC_PARTIAL_CONTENT) { MIMETypedStream pc_response = new MIMETypedStream(mimeType, response, headerArray, length); pc_response.setStatusCode(HttpStatus.SC_PARTIAL_CONTENT); return pc_response; } else if (response.getStatusCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) { response.close(); throw new RangeNotSatisfiableException("External URL datastream request returned 416 Range Not Satisfiable");
mimeType = DEFAULT_MIMETYPE; if (response.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) { response.close(); Header[] respHeaders = response.getResponseHeaders(); } else if (response.getStatusCode() == HttpStatus.SC_PARTIAL_CONTENT) { MIMETypedStream pc_response = new MIMETypedStream(mimeType, response, headerArray, length); pc_response.setStatusCode(HttpStatus.SC_PARTIAL_CONTENT); return pc_response; } else if (response.getStatusCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) { response.close(); throw new RangeNotSatisfiableException("External URL datastream request returned 416 Range Not Satisfiable");