Package org.fcrepo.common.http

Examples of org.fcrepo.common.http.HttpInputStream


        return result;
    }

    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;
    }
View Full Code Here


    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();
        }
    }
View Full Code Here

    /**
     * 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);
                }
            }
        }
View Full Code Here

    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();
        }
    }
View Full Code Here

     */
    private MIMETypedStream get(String url, String user, String pass, String knownMimeType)
            throws GeneralException {
        logger.debug("DefaultExternalContentManager.get(" + url + ")");
        try {
            HttpInputStream response = m_http.get(url, true, user, pass);
            String mimeType =
                    response.getResponseHeaderValue("Content-Type",
                                                    knownMimeType);
            long length = Long.parseLong(response.getResponseHeaderValue("Content-Length","-1"));
            Property[] headerArray =
                    toPropertyArray(response.getResponseHeaders());
            if (mimeType == null || mimeType.equals("")) {
                mimeType = DEFAULT_MIMETYPE;
            }
            return new MIMETypedStream(mimeType, response, headerArray, length);
        } catch (Exception e) {
View Full Code Here

        String urlString = url.toString();
        // presuming that not following redirects means no
        // preemptive authN
        HttpClient client = getHttpClient(followRedirects, followRedirects);
        HttpGet getMethod = new HttpGet(urlString);
        HttpInputStream in = new HttpInputStream(client, getMethod);
        int status = in.getStatusCode();
        logger.debug("GET {} : {}", urlString, status);
        if (failIfNotOK) {
            if (status != 200) {
                if (followRedirects && 300 <= status && status <= 399) {
                    // Handle the redirect here !
                    logger.debug(
                            "FedoraClient is handling redirect for HTTP STATUS={}",
                            status);
                   
                    Header hLoc = in.getResponseHeader(HttpHeaders.LOCATION);
                    if (hLoc != null) {
                        String location = hLoc.getValue();
                        logger.debug("FedoraClient is trying redirect location: {}",
                                location);
                        // Try the redirect location, but don't try to handle another level of redirection.
                        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: "
                                        + e.getMessage());
                            }
                        }
                    }
                } else {
                    try {
                        throw new IOException("Request failed ["
                                + in.getStatusCode() + " " + in.getStatusText()
                                + "] : " + urlString);
                    } finally {
                        try {
                            in.close();
                        } catch (Exception e) {
                            logger.error("Can't close InputStream: "
                                    + e.getMessage());
                        }
                    }
View Full Code Here

     * @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();
        }
    }
View Full Code Here

     * 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) {
            }
        }
    }
View Full Code Here

        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);
    }
View Full Code Here

        assertEquals("image/jpeg", actualType);
    }

    @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);
    }
View Full Code Here

TOP

Related Classes of org.fcrepo.common.http.HttpInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.