Package dk.brics.jwig.server

Examples of dk.brics.jwig.server.ThreadContext


    @URLPattern("**")
    @Priority(CACHE_PRIORITY)
    public Object cache() {
        try {
            ThreadContext.getDependencyMap().beginTransaction(true);
            ThreadContext c = ThreadContext.get();
            HttpServletRequest request = c.getServletRequest();
            HttpServletResponse response = c.getServletResponse();
            Cache cache = ThreadContext.getCache();
            String cacheURL = c.getRequestURL();
            Response cachedResponse = cache.get(cacheURL);
            if (cachedResponse == null) {
                String augmentationString = "<|>"
                        + WebApp.get().getWebSite()
                                .getCacheAugmentationString();
                cacheURL = c.getRequestURL() + augmentationString;
                cachedResponse = cache.get(cacheURL);
            }
            if (cachedResponse != null) {
                String cacheControl = request.getHeader("cache-control");
                long if_modified_since = -1;
                try {
                    if_modified_since = request
                            .getDateHeader("If-Modified-Since");
                } catch (IllegalArgumentException e) {
                    log.info("Client sent invalid If-Modified-Since");
                }
                try {
                    String if_none_match = request.getHeader("If-None-Match");
                    long lastModified = (cachedResponse.getLastModified() / 1000) * 1000;
                    long ifModifiedSince = (if_modified_since / 1000) * 1000;
                    if (if_none_match == null && if_modified_since == -1
                            && cacheControl != null
                            && cacheControl.equals("max-age=0")) {
                        log.info("Client refreshed the page. Removing the old one from the cache");
                        cache.remove(cacheURL);

                    } else {
                        if ((if_none_match != null && if_none_match.equals("\""
                                + cachedResponse.getETag() + "\""))
                                || (if_none_match == null
                                        && if_modified_since != -1 && lastModified <= ifModifiedSince)) {
                            response.setStatus(304);
                            response.flushBuffer();
                            log.info("Client cache valid");
                            return null;
                        }
                        log.info("Response in server cache");
                        ThreadContext.get().setResponse(cachedResponse);
                        return cachedResponse.getObject();
                    }
                } catch (IOException e) {
                    log.info("IOException in serverCache", e);
                    if (response.isCommitted()) {
                        return null;
                    }
                }
            }
            Object result = next();
            if (result instanceof XML || result instanceof String) {
                Response currentResponse = ThreadContext.get().getResponse();
                currentResponse.setResult(result);
                // This may also remove pages from the cache
                boolean shouldCache = ThreadContext.getDependencyMap().mergeTransaction();
                if (shouldCache && currentResponse.getStatus() == HttpServletResponse.SC_OK) {
                    String url = c.getRequestURL();
                    url = getCacheAugmentedString(url);
                    Response newResponseForCache = new Response(currentResponse);
                    newResponseForCache.setResult(currentResponse.getResult());
                    ThreadContext.getCache().put(url, newResponseForCache);
                }
View Full Code Here


     * Invoked automatically from generated XML pages for executing a handler.
     */
    @Priority(HANDLERS)
    @URLPattern("handlers/*")
    public Object handlers() throws IOException {
        ThreadContext c = ThreadContext.get();
        String referer = getRequestReferer(c.getServletRequest());
        Response referer_response = findOrRegenerateRefererResponse(referer);
        String requestURL = c.getRequestURL();
        if (requestURL.endsWith("-validate")) {
            requestURL = requestURL.substring(0, requestURL.length() - "-validate".length());
            AbstractHandler handler = referer_response.getHandler(requestURL);
            if (handler instanceof SubmitHandler) {
                SubmitHandler submitHandler = (SubmitHandler) handler;
                return submitHandler.validate();
            } else {
                return null;
            }
        }
        AbstractHandler handler = referer_response.getHandler(requestURL);

        if (handler == null) {
            throw new BadRequestException("Handler not found: " + c.getRequestURL());
        } else {
            //Transport all sessions from referer. This is useful because people may use
            //sessions declared in the outer method of an anonymous handler. This pattern
            //exists for example in the GuessingGame example
            for (Session s : referer_response.getSessions()) {
View Full Code Here

            return o;
        }
    }

    private Response findOrRegenerateRefererResponse(String referer) throws IOException {
        ThreadContext c = ThreadContext.get();
        HttpServletRequest request = c.getServletRequest();
        HttpServletResponse response = c.getServletResponse();
        Response referer_response = getReferer(referer);
        //If the referer response does not exist, then we try to reconstruct it
        if (referer_response == null || referer_response.getHandler(c.getRequestURL()) == null) {
            log.info("Handler could not be found in the cache. Referer is: " + referer);
            HttpURLConnection con = (HttpURLConnection) new URL(referer)
                    .openConnection();
            final String authorization = "Authorization";
            String auth = request.getHeader(authorization);
View Full Code Here

    /**
     * Invokes the next web method in the chain.
     */
    public static Object next() {
        ThreadContext.getDispatcher().invokeNextWebMethod();
        ThreadContext threadContext = ThreadContext.get();
        RuntimeException throwable = threadContext.getThrowable();
        if (throwable != null) {
            threadContext.setThrowable(null);
            throw throwable;
        }
        return threadContext.getCurrentResult();
    }
View Full Code Here

    /**
     * Returns the web app parameter of the given name.
     */
    @SuppressWarnings("unchecked")
    public <E> E getWebAppParam(String name, Class<E> type) {
        ThreadContext threadContext = ThreadContext.get();
        String objects = threadContext.getWebAppParams().get(name);
        return (E) threadContext.getRequestManager().deserializeArgument(
                new Object[] { objects }, type, name, false, false, null);
    }
View Full Code Here

TOP

Related Classes of dk.brics.jwig.server.ThreadContext

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.