Package com.volantis.shared.net.url.http

Examples of com.volantis.shared.net.url.http.CachedHttpContentStateBuilder


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


        }


        final String mimeType = DISCOVERER.discoverMimeType(sis);

        CachedHttpContentStateBuilder builder =
            new CachedHttpContentStateBuilder();


        final Map responses = new HashMap();
        Date now = new Date();
        // formaters are NOT thread safe so we must create a new one
        DateFormat format = DateFormats.RFC_1123_GMT.create();
        responses.put("date",  format.format(now));

        responses.put("cache-control", "max-age=" + LOCAL_FILE_MAX_AGE);
        responses.put("expires", format.format(
            new Date(now.getTime() + LOCAL_FILE_MAX_AGE * 1000)));

        // create a "fake" accessor
        HttpResponseHeaderAccessor accessor = new HttpResponseHeaderAccessor() {

            // javadoc inherited
            public String getProtocol() {
                return "HTTP";
            }

            // javadoc inherited
            public String getResponseHeaderValue(String s) {
                s = s.toLowerCase(Locale.ENGLISH);
                // get returns null if the entry does not exist. This meets the
                // specification for getResponseHeaderValue
                return (String) responses.get(s);
            }
        };

        builder.setMethodAccessor(accessor);

        Time t = Time.inMilliSeconds(System.currentTimeMillis());
        builder.setRequestTime(t);
        builder.setResponseTime(t);
        final CachedHttpContentState state = builder.build();

        // A mock Representation that adds a "default" timeout to local and
        // non-http image sources.
        return new Representation() {

View Full Code Here

                    }
                }
            }

            // record the request time
            final CachedHttpContentStateBuilder builder =
                new CachedHttpContentStateBuilder();
            builder.setRequestTime(clock.getCurrentTime());
            accessor = executor.execute();
            // record response time
            builder.setResponseTime(clock.getCurrentTime());

            // process the response
            builder.setMethodAccessor(
                new HttpResponseHeaderAccessorWrapper(accessor));
            httpContent = new HTTPResponseAccessorWrapper(accessor);
            final int statusCode = accessor.getStatusCode();
            if (isStatusCodeCachable(statusCode)) {
                state = builder.build();
                if (state != null && state.isCacheable()) {
                    httpContent = new CachedHttpContent(httpContent, state, clock,
                        new CacheableDependency(key, state, clock,
                            cache, this));
                    cacheable = true;
                }
            } else if (statusCode == 304) {
                // 304 Not Modified
                // it is only possible if it is a revalidation cache entry holds
                // the exisiting cache config as an extension object
                state = builder.merge(
                    ((CachedHttpContentState) entry.getExtensionObject()));
                cacheable = state.isCacheable();
                // entry must have a CachedHttpContent
                final CachedHttpContent originalContent =
                    (CachedHttpContent) entry.getValue();
View Full Code Here

                final SystemClock clock) {
            super(manager);
            this.cache = cache;
            this.entry = entry;
            this.clock = clock;
            builder = new CachedHttpContentStateBuilder();
        }
View Full Code Here

TOP

Related Classes of com.volantis.shared.net.url.http.CachedHttpContentStateBuilder

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.