/** * 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) { } } }
@Override public HttpInputStream call() throws Exception { HttpInputStream in = m_client.get(m_uri, true); lastType = in.getContentType(); lastLength = in.getContentLength(); if (lastLength == -1) { lastLength = 0; while(in.read() != -1) lastLength++; } in.close(); return in; }
@Test public void testGetDisseminationUserInput() throws Exception { HttpInputStream his = s_client.get("/get/demo:29/demo:27/convertImage?convertTo=gif", true); String actual = his.getContentType(); his.close(); assertEquals("image/gif", actual); }
HttpInputStream in = new HttpInputStream(client, getMethod); int status = in.getStatusCode(); logger.debug("GET {} : {}", urlString, status); if (failIfNotOK) { Header hLoc = in.getResponseHeader(HttpHeaders.LOCATION); if (hLoc != null) { String location = hLoc.getValue(); in.close(); return get(location, true, false); } else { try { throw new IOException("Request failed [" + status + " " + in.getStatusText() + "]"); } finally { try { in.close(); } catch (Exception e) { logger.error("Can't close InputStream: " try { throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() + "] : " + urlString); } finally { try { in.close(); } catch (Exception e) {
response.getResponseHeaderValue(HttpHeaders.CONTENT_TYPE, knownMimeType); long length = Long.parseLong(response.getResponseHeaderValue(HttpHeaders.CONTENT_LENGTH,"-1")); Property[] headerArray = toPropertyArray(response.getResponseHeaders()); if (mimeType == null || mimeType.isEmpty()) { mimeType = DEFAULT_MIMETYPE; if (response.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) { response.close(); Header[] respHeaders = response.getResponseHeaders(); Property[] properties = new Property[respHeaders.length]; for (int i = 0; i < respHeaders.length; i++){ } 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"); } else { if (headOnly) { try { response.close(); } catch (IOException ioe) { logger.warn("problem closing HEAD response: {}", ioe.getMessage());
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 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); }
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); }
@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
/** * Return the current date as reported by the Fedora server. * * @throws IOException * if the HTTP Date header is not provided by the server for any * reason, or it is in the wrong format. */ public Date getServerDate() throws IOException { HttpInputStream in = get("/describe", false, false); String dateString = null; try { Header header = in.getResponseHeader("Date"); if (header == null) { throw new IOException("Date was not supplied in HTTP response " + "header for " + m_baseURL + "describe"); } dateString = header.getValue(); // This is the date format recommended by RFC2616 SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); format.setTimeZone(TimeZone.getTimeZone("UTC")); return format.parse(dateString); } catch (ParseException e) { throw new IOException("Unparsable date (" + dateString + ") in HTTP response header for " + m_baseURL + "describe"); } finally { in.close(); } }
/** * Automatically close on garbage collection. */ @Override public void finalize() { try { close(); } catch (Exception e) { } }
/** * Get CONTENT-TYPE */ public String getContentType() { return getResponseHeader("Content-Type").getValue(); }
HttpInputStream in = new HttpInputStream(client, request); int status = in.getStatusCode(); if (failIfNotOK) { if (status != HttpStatus.SC_OK && while (300 <= status && status <= 399 && count <= wconfig.getMaxRedirects()) { if (in.getResponseHeader(HttpHeaders.LOCATION) == null) { in.close(); throw new IOException("Redirect HTTP response provided no location header."); url = in.getResponseHeader(HttpHeaders.LOCATION).getValue(); in.close(); setHeaders(request, wconfig.getUserAgent(), ifNoneMatch, ifModifiedSince, range); in = new HttpInputStream(client, request); status = in.getStatusCode(); count++; in.close(); throw new IOException("Too many redirects"); } else if (status != 200) { in.close(); throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() + "]"); try { throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText()
response.getResponseHeaderValue(HttpHeaders.CONTENT_TYPE, knownMimeType); long length = Long.parseLong(response.getResponseHeaderValue(HttpHeaders.CONTENT_LENGTH,"-1")); Property[] headerArray = toPropertyArray(response.getResponseHeaders()); if (mimeType == null || mimeType.isEmpty()) { mimeType = DEFAULT_MIMETYPE; if (response.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) { response.close(); Header[] respHeaders = response.getResponseHeaders(); Property[] properties = new Property[respHeaders.length]; for (int i = 0; i < respHeaders.length; i++){ } 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"); } else { if (headOnly) { try { response.close(); } catch (IOException ioe) { logger.warn("problem closing HEAD response: {}", ioe.getMessage());
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 testGetDisseminationDefault() throws Exception { HttpInputStream his = s_client.get("/get/demo:5/fedora-system:3/viewDublinCore", true); String actual = his.getContentType(); his.close(); assertEquals("text/html", actual); }
/** * Automatically close on garbage collection. */ @Override public void finalize() { try { close(); } catch (Exception e) { } }
/** * Get CONTENT-TYPE */ public String getContentType() { return getResponseHeader("Content-Type").getValue(); }
HttpInputStream in = new HttpInputStream(client, request); int status = in.getStatusCode(); if (failIfNotOK) { if (status != HttpStatus.SC_OK && while (300 <= status && status <= 399 && count <= wconfig.getMaxRedirects()) { if (in.getResponseHeader(HttpHeaders.LOCATION) == null) { in.close(); throw new IOException("Redirect HTTP response provided no location header."); url = in.getResponseHeader(HttpHeaders.LOCATION).getValue(); in.close(); setHeaders(request, wconfig.getUserAgent(), ifNoneMatch, ifModifiedSince, range); in = new HttpInputStream(client, request); status = in.getStatusCode(); count++; in.close(); throw new IOException("Too many redirects"); } else if (status != 200) { in.close(); throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() + "]"); try { throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText()
/** * 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); } } } }