Search in sources :

Example 1 with ThreadContext

use of org.apache.wicket.ThreadContext in project wicket by apache.

the class JavaSerializer method deserialize.

@Override
public Object deserialize(final byte[] data) {
    ThreadContext old = ThreadContext.get(false);
    final ByteArrayInputStream in = new ByteArrayInputStream(data);
    ObjectInputStream ois = null;
    try {
        Application oldApplication = ThreadContext.getApplication();
        try {
            ois = newObjectInputStream(in);
            String applicationName = (String) ois.readObject();
            if (applicationName != null) {
                Application app = Application.get(applicationName);
                if (app != null) {
                    ThreadContext.setApplication(app);
                }
            }
            return ois.readObject();
        } finally {
            try {
                ThreadContext.setApplication(oldApplication);
                IOUtils.close(ois);
            } finally {
                in.close();
            }
        }
    } catch (ClassNotFoundException | IOException cnfx) {
        throw new RuntimeException("Could not deserialize object from byte[]", cnfx);
    } finally {
        ThreadContext.restore(old);
    }
}
Also used : WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ByteArrayInputStream(java.io.ByteArrayInputStream) ThreadContext(org.apache.wicket.ThreadContext) IOException(java.io.IOException) Application(org.apache.wicket.Application) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with ThreadContext

use of org.apache.wicket.ThreadContext in project wicket by apache.

the class WicketFilter method processRequest.

/**
 * This is Wicket's main method to execute a request
 *
 * @param request
 * @param response
 * @param chain
 * @return false, if the request could not be processed
 * @throws IOException
 * @throws ServletException
 */
boolean processRequest(ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    final ThreadContext previousThreadContext = ThreadContext.detach();
    // Assume we are able to handle the request
    boolean res = true;
    final ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
    final ClassLoader newClassLoader = getClassLoader();
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    HttpServletResponse httpServletResponse = (HttpServletResponse) response;
    try {
        if (previousClassLoader != newClassLoader) {
            Thread.currentThread().setContextClassLoader(newClassLoader);
        }
        // Make sure getFilterPath() gets called before checkIfRedirectRequired()
        String filterPath = getFilterPath(httpServletRequest);
        if (filterPath == null) {
            throw new IllegalStateException("filter path was not configured");
        }
        if (shouldIgnorePath(httpServletRequest)) {
            log.debug("Ignoring request {}", httpServletRequest.getRequestURL());
            if (chain != null) {
                // invoke next filter from within Wicket context
                chain.doFilter(request, response);
            }
            return false;
        }
        if ("OPTIONS".equalsIgnoreCase(httpServletRequest.getMethod())) {
            // handle the OPTIONS request outside of normal request processing.
            // wicket pages normally only support GET and POST methods, but resources and
            // special pages acting like REST clients can also support other methods, so
            // we include them all.
            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
            httpServletResponse.setHeader("Allow", "GET,POST,OPTIONS,PUT,HEAD,PATCH,DELETE,TRACE");
            httpServletResponse.setHeader("Content-Length", "0");
            return true;
        }
        String redirectURL = checkIfRedirectRequired(httpServletRequest);
        if (redirectURL == null) {
            // No redirect; process the request
            ThreadContext.setApplication(application);
            WebRequest webRequest = application.createWebRequest(httpServletRequest, filterPath);
            WebResponse webResponse = application.createWebResponse(webRequest, httpServletResponse);
            RequestCycle requestCycle = application.createRequestCycle(webRequest, webResponse);
            res = processRequestCycle(requestCycle, webResponse, httpServletRequest, httpServletResponse, chain);
        } else {
            if (Strings.isEmpty(httpServletRequest.getQueryString()) == false) {
                redirectURL += "?" + httpServletRequest.getQueryString();
            }
            try {
                // send redirect - this will discard POST parameters if the request is POST
                // - still better than getting an error because of lacking trailing slash
                httpServletResponse.sendRedirect(httpServletResponse.encodeRedirectURL(redirectURL));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    } finally {
        ThreadContext.restore(previousThreadContext);
        if (newClassLoader != previousClassLoader) {
            Thread.currentThread().setContextClassLoader(previousClassLoader);
        }
        if (response.isCommitted() && httpServletRequest.isAsyncStarted() == false) {
            response.flushBuffer();
        }
    }
    return res;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebResponse(org.apache.wicket.request.http.WebResponse) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) WebRequest(org.apache.wicket.request.http.WebRequest) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ThreadContext(org.apache.wicket.ThreadContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 ThreadContext (org.apache.wicket.ThreadContext)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Application (org.apache.wicket.Application)1 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)1 WebRequest (org.apache.wicket.request.http.WebRequest)1 WebResponse (org.apache.wicket.request.http.WebResponse)1