Package com.volantis.map.retriever

Examples of com.volantis.map.retriever.ResourceRetrieverException


    public Representation execute(URL url, HttpHeaders headers)
        throws ResourceRetrieverException, IOException {
        if(! (url.getProtocol().equalsIgnoreCase("http")
                || url.getProtocol().equalsIgnoreCase("https"))) {

            throw new ResourceRetrieverException(exceptionLocalizer.format(
                    "protocol-not-supported",
                    new String [] {url.getProtocol(), "http"}));
        }

        // create HTTPClient
        HttpClient httpClient = prepareHttpClient();

        // create request method and configure it
        final GetMethod method = new GetMethod(url.toExternalForm());
        method.setFollowRedirects(true);

        // create cache information object
        SystemClock clock = SystemClock.getDefaultInstance();
        CachedHttpContentStateBuilder cacheBuilder = new CachedHttpContentStateBuilder();
        cacheBuilder.setMethodAccessor(
            ResponseHeaderAccessorFactory.getDefaultInstance().
                createHttpClientResponseHeaderAccessor(method));
        cacheBuilder.setRequestTime(clock.getCurrentTime());

        // save request headers to request method
        setRequestHeaders(method, headers);

        DefaultRepresentation responseInfo = null;

        // execute request
        try {
            httpClient.executeMethod(method);
        } catch (IOException e) {
            throw new ResourceRetrieverException(exceptionLocalizer.format(
                    "connection-refused", url.toString()), e);
        }

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Requested resource at: " + url.toString());
        }

        // If the get failed then return immediately.
        if (method.getStatusCode() == 200) {

            // get response caching information
            cacheBuilder.setResponseTime(clock.getCurrentTime());

            // read resource stream
            InputStream stream = method.getResponseBodyAsStream();

            // a custom closer to ensure the method releases its connection
            CloseListener closer = new CloseListener() {
                public void close() {
                    method.releaseConnection();
                }
            };

            SeekableInputStream seekableStream =
                new DefaultSeekableInputStream(closer, stream, true);

            if(stream != null) {

                // the mimeDiscoverer should not effect the Seekable stream
                // and should restore its position but we mark it just in case.
                String mimeType = null;
                try {
                    seekableStream.mark();
                    mimeType = mimeDiscoverer.discoverMimeType(seekableStream);
                } finally {
                    seekableStream.reset();
                }
                responseInfo = new DefaultRepresentation(
                    method,
                    mimeType,
                    cacheBuilder.build(),
                    seekableStream);
            }
        } else {
            LOGGER.error("request-failed",
                new String[]{ url.toString(),
                    String.valueOf(method.getStatusCode()) });
            throw new ResourceRetrieverException(
                "error-response-code", method.getStatusCode());
        }
        return responseInfo;

        // Note that we DO NOT close the connection at this point. that is
View Full Code Here


       
        resourceRetrieverMock.fuzzy
            .execute(
                    mockFactory.expectsEqual(url),
                    mockFactory.expectsAny())
            .fails(new ResourceRetrieverException("error"));
       
        // ==================================================================
        // Do the test.
        // ==================================================================
        try {
View Full Code Here

TOP

Related Classes of com.volantis.map.retriever.ResourceRetrieverException

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.