/** * Automatically close on garbage collection. */ @Override public void finalize() { try { close(); } catch (Exception e) { } }
/** * Automatically close on garbage collection. */ @Override public void finalize() { try { close(); } catch (Exception e) { } }
/** * 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(); } }
@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); }
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); }
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 testGetChainedDissemination() throws Exception { // test chained dissemination using local services // The object contains an E datastream that is a dissemination of the local SAXON service. // This datastream is input to another dissemination that uses the local FOP service. HttpInputStream his = client.get("/get/demo:26/demo:19/getPDF", false); String contentType = his.getContentType(); his.close(); assertEquals("application/pdf", contentType); }
/** * 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) { } } }
@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); }
public static Set<String> getDemoObjects(FedoraClient client) throws Exception { HttpInputStream queryResult; queryResult = client.get(getBaseURL() + "/search?query=pid~demo:*" + "&maxResults=1000&pid=true&xml=true", true, true); SearchResultParser parser = new SearchResultParser(queryResult); Set<String> result = parser.getPIDs(); queryResult.close(); return result; }
@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; }
&& count <= wconfig.getMaxRedirects()) { if (in.getResponseHeader(HttpHeaders.LOCATION) == null) { in.close(); throw new IOException("Redirect HTTP response provided no location header."); in.close(); setHeaders(request, wconfig.getUserAgent(), ifNoneMatch, ifModifiedSince, range); in = new HttpInputStream(client, request); in.close(); throw new IOException("Too many redirects"); } else if (status != 200) { in.close(); throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() } finally { try { in.close(); } catch (Exception e) { logger.error("Can't close InputStream: "
&& count <= wconfig.getMaxRedirects()) { if (in.getResponseHeader(HttpHeaders.LOCATION) == null) { in.close(); throw new IOException("Redirect HTTP response provided no location header."); in.close(); setHeaders(request, wconfig.getUserAgent(), ifNoneMatch, ifModifiedSince, range); in = new HttpInputStream(client, request); in.close(); throw new IOException("Too many redirects"); } else if (status != 200) { in.close(); throw new IOException("Request failed [" + in.getStatusCode() + " " + in.getStatusText() } finally { try { in.close(); } catch (Exception e) { logger.error("Can't close InputStream: "
response.close(); Header[] respHeaders = response.getResponseHeaders(); Property[] properties = new Property[respHeaders.length]; 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());
response.close(); Header[] respHeaders = response.getResponseHeaders(); Property[] properties = new Property[respHeaders.length]; 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());