/** * Get an HTTP resource with the response as an InputStream, given a URL. * This method will follow redirects if FOLLOW_REDIRECTS is true. Note that * if the HTTP response has no body, the InputStream will be empty. The * success of a request can be checked with getResponseCode(). Usually * you'll want to see a 200. See * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for other codes. * * @param url * A URL that we want to do an HTTP GET upon * @param failIfNotOK * boolean value indicating if an exception should be thrown if we do * NOT receive an HTTP 200 response (OK) * @return HttpInputStream the HTTP response * @throws IOException */ public HttpInputStream get(URL url, boolean failIfNotOK) throws IOException { return get(url, failIfNotOK, FOLLOW_REDIRECTS); }
/** * Get an HTTP resource with the response as an InputStream, given a * resource locator that either begins with 'info:fedora/' , 'http://', or * '/'. Note that if the HTTP response has no body, the InputStream will be * empty. The success of a request can be checked with getResponseCode(). * Usually you'll want to see a 200. See * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for other codes. * * @param locator * A URL, relative Fedora URL, or Fedora URI that we want to do an * HTTP GET upon * @param failIfNotOK * boolean value indicating if an exception should be thrown if we do * NOT receive an HTTP 200 response (OK) * @param followRedirects * boolean value indicating whether HTTP redirects should be handled * in this method, or be passed along so that they can be handled * later. * @return HttpInputStream the HTTP response * @throws IOException */ public HttpInputStream get(String locator, boolean failIfNotOK, boolean followRedirects) throws IOException { // Convert the locator to a proper Fedora URL and the do a get. String url = getLocatorAsURL(locator); return get(new URL(url), failIfNotOK, followRedirects); }
/** * Get tuples from the remote resource index. The map contains * <em>String</em> values for parameters that should be passed to the * service. Two parameters are required: 1) lang 2) query Two parameters to * the risearch service are implied: 1) type = tuples 2) format = sparql See * http * ://www.fedora.info/download/2.0/userdocs/server/webservices/risearch/# * app.tuples */ public TupleIterator getTuples(Map<String, String> params) throws IOException { params.put("type", "tuples"); params.put("format", RDFFormat.SPARQL.getName()); try { String url = getRIQueryURL(params); return TupleIterator.fromStream(get(url, true, true), RDFFormat.SPARQL); } catch (TrippiException e) { throw new IOException("Error getting tuple iterator: " + e.getMessage()); } }
return get(location, true, false); } else { try {
/** * Get an HTTP resource with the response as an InputStream, given a * resource locator that either begins with 'info:fedora/' , 'http://', or * '/'. This method will follow redirects if FOLLOW_REDIRECTS is true. Note * that if the HTTP response has no body, the InputStream will be empty. The * success of a request can be checked with getResponseCode(). Usually * you'll want to see a 200. See * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html for other codes. * * @param locator * A URL, relative Fedora URL, or Fedora URI that we want to do an * HTTP GET upon * @param failIfNotOK * boolean value indicating if an exception should be thrown if we do * NOT receive an HTTP 200 response (OK) * @return HttpInputStream the HTTP response * @throws IOException */ public HttpInputStream get(String locator, boolean failIfNotOK) throws IOException { return get(locator, failIfNotOK, FOLLOW_REDIRECTS); }
public void reloadPolicies() throws IOException { InputStream in = null; try { in = get("/management/control?action=reloadPolicies", true, true); } finally { try { in.close(); } catch (Exception e) { logger.error("Can't close InputStream: " + e.getMessage()); } } }
public Document getXMLQueryResult(FedoraClient client, String location) throws Exception { InputStream is = client.get(getBaseURL() + location, true, true); Document result = XMLUnit.buildControlDocument(new InputSource(is)); is.close(); return result; }
throws IOException { InputStream in = get(locator, failIfNotOK, followRedirects);
/** * 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(); } }
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 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); }
@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); }
@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); }
@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
/** * 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 TripleIterator queryRI(String query) throws Exception { FedoraClient client = getFedoraClient(); InputStream results = client.get(RISEARCH_QUERY + URLEncoder.encode(query, "UTF-8"), true); return new RIOTripleIterator(results, new NTriplesParser(), "info/fedora", exec); }
@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; }