Search in sources :

Example 1 with InstanceSupport

use of org.apache.catalina.util.InstanceSupport in project Payara by payara.

the class ApplicationDispatcher method doInvoke.

/**
 * Ask the resource represented by this RequestDispatcher to process
 * the associated request, and create (or append to) the associated
 * response.
 * <p>
 * <strong>IMPLEMENTATION NOTE</strong>: This implementation assumes
 * that no filters are applied to a forwarded or included resource,
 * because they were already done for the original request.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param crossContext true if the request dispatch is crossing context
 * boundaries, false otherwise
 * @param state the state of this ApplicationDispatcher
 *
 * @throws IOException if an input/output error occurs
 * @throws ServletException if a servlet error occurs
 */
private void doInvoke(ServletRequest request, ServletResponse response, boolean crossContext, State state) throws IOException, ServletException {
    // Checking to see if the context classloader is the current context
    // classloader. If it's not, we're saving it, and setting the context
    // classloader to the Context classloader
    ClassLoader oldCCL = null;
    if (crossContext) {
        oldCCL = Thread.currentThread().getContextClassLoader();
        ClassLoader contextClassLoader = context.getLoader().getClassLoader();
        Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
    HttpServletResponse hresponse = state.hresponse;
    Servlet servlet = null;
    IOException ioException = null;
    ServletException servletException = null;
    RuntimeException runtimeException = null;
    boolean unavailable = false;
    // Check for the servlet being marked unavailable
    if (wrapper.isUnavailable()) {
        String msg = MessageFormat.format(rb.getString(LogFacade.UNAVAILABLE_SERVLET), wrapper.getName());
        log.log(Level.WARNING, msg);
        if (hresponse == null) {
            // NOTE - Not much we can do generically
            ;
        } else {
            long available = wrapper.getAvailable();
            if ((available > 0L) && (available < Long.MAX_VALUE))
                hresponse.setDateHeader("Retry-After", available);
            hresponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, msg);
        }
        unavailable = true;
    }
    // Allocate a servlet instance to process this request
    String allocateServletMsg = MessageFormat.format(rb.getString(LogFacade.ALLOCATE_SERVLET_EXCEPTION), wrapper.getName());
    try {
        if (!unavailable) {
            servlet = wrapper.allocate();
        }
    } catch (ServletException e) {
        log.log(Level.SEVERE, allocateServletMsg, StandardWrapper.getRootCause(e));
        servletException = e;
        servlet = null;
    } catch (Throwable e) {
        log.log(Level.SEVERE, allocateServletMsg, e);
        servletException = new ServletException(allocateServletMsg, e);
        servlet = null;
    }
    // Get the FilterChain Here
    ApplicationFilterFactory factory = ApplicationFilterFactory.getInstance();
    ApplicationFilterChain filterChain = factory.createFilterChain(request, wrapper, servlet);
    InstanceSupport support = ((StandardWrapper) wrapper).getInstanceSupport();
    // Call the service() method for the allocated servlet instance
    String servletServiceExceptionMsg = MessageFormat.format(rb.getString(LogFacade.SERVLET_SERVICE_EXCEPTION), wrapper.getName());
    RequestFacadeHelper reqFacHelper = RequestFacadeHelper.getInstance(request);
    try {
        String jspFile = wrapper.getJspFile();
        if (jspFile != null) {
            request.setAttribute(Globals.JSP_FILE_ATTR, jspFile);
        }
        support.fireInstanceEvent(InstanceEvent.EventType.BEFORE_DISPATCH_EVENT, servlet, request, response);
        // START IASRI 4665318
        if (servlet != null) {
            // START OF S1AS 4703023
            if (reqFacHelper != null) {
                reqFacHelper.incrementDispatchDepth();
                if (reqFacHelper.isMaxDispatchDepthReached()) {
                    String msg = MessageFormat.format(rb.getString(LogFacade.MAX_DISPATCH_DEPTH_REACHED), new Object[] { Integer.valueOf(Request.getMaxDispatchDepth()) });
                    throw new ServletException(msg);
                }
            }
            // START IASRI 4665318
            if (filterChain != null) {
                filterChain.setWrapper((StandardWrapper) wrapper);
                filterChain.doFilter(request, response);
            } else {
                ((StandardWrapper) wrapper).service(request, response, servlet);
            }
        // END IASRI 4665318
        }
        // Servlet Service Method is called by the FilterChain
        support.fireInstanceEvent(AFTER_DISPATCH_EVENT, servlet, request, response);
    } catch (ClientAbortException e) {
        support.fireInstanceEvent(AFTER_DISPATCH_EVENT, servlet, request, response);
        ioException = e;
    } catch (IOException e) {
        support.fireInstanceEvent(AFTER_DISPATCH_EVENT, servlet, request, response);
        log.log(Level.WARNING, servletServiceExceptionMsg, e);
        ioException = e;
    } catch (UnavailableException e) {
        support.fireInstanceEvent(AFTER_DISPATCH_EVENT, servlet, request, response);
        log.log(Level.WARNING, servletServiceExceptionMsg, e);
        servletException = e;
        wrapper.unavailable(e);
    } catch (ServletException e) {
        support.fireInstanceEvent(AFTER_DISPATCH_EVENT, servlet, request, response);
        Throwable rootCause = StandardWrapper.getRootCause(e);
        if (!(rootCause instanceof ClientAbortException)) {
            log.log(Level.WARNING, servletServiceExceptionMsg, rootCause);
        }
        servletException = e;
    } catch (RuntimeException e) {
        support.fireInstanceEvent(AFTER_DISPATCH_EVENT, servlet, request, response);
        log.log(Level.WARNING, servletServiceExceptionMsg, e);
        runtimeException = e;
    // START OF S1AS 4703023
    } finally {
        if (reqFacHelper != null) {
            reqFacHelper.decrementDispatchDepth();
        }
    // END OF S1AS 4703023
    }
    // Release the filter chain (if any) for this request
    try {
        if (filterChain != null)
            filterChain.release();
    } catch (Throwable e) {
        String msg = MessageFormat.format(rb.getString(LogFacade.RELEASE_FILTERS_EXCEPTION_SEVERE), wrapper.getName());
        log.log(Level.SEVERE, msg, e);
    // FIXME Exception handling needs to be simpiler to what is
    // in the StandardWrapperValue
    }
    // Deallocate the allocated servlet instance
    String deallocateServletExceptionMsg = MessageFormat.format(rb.getString(LogFacade.ALLOCATE_SERVLET_EXCEPTION), wrapper.getName());
    try {
        if (servlet != null) {
            wrapper.deallocate(servlet);
        }
    } catch (ServletException e) {
        log.log(Level.SEVERE, deallocateServletExceptionMsg, e);
        servletException = e;
    } catch (Throwable e) {
        log.log(Level.SEVERE, deallocateServletExceptionMsg, e);
        servletException = new ServletException(deallocateServletExceptionMsg, e);
    }
    // Reset the old context class loader
    if (oldCCL != null)
        Thread.currentThread().setContextClassLoader(oldCCL);
    // Rethrow an exception if one was thrown by the invoked servlet
    if (ioException != null)
        throw ioException;
    if (servletException != null)
        throw servletException;
    if (runtimeException != null)
        throw runtimeException;
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ClientAbortException(org.apache.catalina.connector.ClientAbortException) InstanceSupport(org.apache.catalina.util.InstanceSupport)

Example 2 with InstanceSupport

use of org.apache.catalina.util.InstanceSupport in project Payara by payara.

the class ApplicationFilterChain method internalDoFilter.

private void internalDoFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    if (wrapper == null) {
        throw new IllegalStateException("Missing wrapper");
    }
    InstanceSupport support = wrapper.getInstanceSupport();
    // Call the next filter if there is one
    if (pos < n) {
        ApplicationFilterConfig filterConfig = filters[pos++];
        if (!filterConfig.isAsyncSupported()) {
            RequestFacadeHelper reqFacHelper = RequestFacadeHelper.getInstance(request);
            if (reqFacHelper != null) {
                reqFacHelper.disableAsyncSupport();
            }
        }
        Filter filter = null;
        try {
            filter = filterConfig.getFilter();
            support.fireInstanceEvent(BEFORE_FILTER_EVENT, filter, request, response);
            if (SecurityUtil.isPackageProtectionEnabled()) {
                final ServletRequest req = request;
                final ServletResponse res = response;
                Principal principal = ((HttpServletRequest) req).getUserPrincipal();
                Object[] filterType = new Object[3];
                filterType[0] = req;
                filterType[1] = res;
                filterType[2] = this;
                SecurityUtil.doAsPrivilege("doFilter", filter, classType, filterType, principal);
            } else {
                filter.doFilter(request, response, this);
            }
            support.fireInstanceEvent(AFTER_FILTER_EVENT, filter, request, response);
        } catch (IOException e) {
            if (filter != null)
                support.fireInstanceEvent(AFTER_FILTER_EVENT, filter, request, response, e);
            throw e;
        } catch (ServletException e) {
            if (filter != null)
                support.fireInstanceEvent(AFTER_FILTER_EVENT, filter, request, response, e);
            throw e;
        } catch (RuntimeException e) {
            if (filter != null)
                support.fireInstanceEvent(AFTER_FILTER_EVENT, filter, request, response, e);
            throw e;
        } catch (Throwable e) {
            if (filter != null)
                support.fireInstanceEvent(AFTER_FILTER_EVENT, filter, request, response, e);
            throw new ServletException(rb.getString(LogFacade.FILTER_EXECUTION_EXCEPTION), e);
        }
        return;
    }
    // We fell off the end of the chain -- call the servlet instance
    /* IASRI 4665318
        try {
            support.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,
                                      servlet, request, response);
            if ((request instanceof HttpServletRequest) &&
                (response instanceof HttpServletResponse)) {
                    
                // START SJS WS 7.0 6236329
                //if( System.getSecurityManager() != null) {
                if ( SecurityUtil.executeUnderSubjectDoAs() ){
                // END OF SJS WS 7.0 6236329
                    final ServletRequest req = request;
                    final ServletResponse res = response;
                    Principal principal = 
                        ((HttpServletRequest) req).getUserPrincipal();

                    Object[] serviceType = new Object[2];
                    serviceType[0] = req;
                    serviceType[1] = res;
                    
                    SecurityUtil.doAsPrivilege("service",
                                               servlet,
                                               classTypeUsedInService, 
                                               serviceType,
                                               principal);                                                   
                    serviceType = null;
                } else {  
                    servlet.service((HttpServletRequest) request,
                                    (HttpServletResponse) response);
                }
            } else {
                servlet.service(request, response);
            }
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response);
        } catch (IOException e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            throw e;
        } catch (ServletException e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            throw e;
        } catch (RuntimeException e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            throw e;
        } catch (Throwable e) {
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                                      servlet, request, response, e);
            throw new ServletException
              (sm.getString("filterChain.servlet"), e);
              //filterChain.servlet=PWC1244: Servlet execution threw an exception
        }

        */
    // START IASRI 4665318
    wrapper.service(request, response, servlet);
// END IASRI 4665318
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) InstanceSupport(org.apache.catalina.util.InstanceSupport) Principal(java.security.Principal)

Example 3 with InstanceSupport

use of org.apache.catalina.util.InstanceSupport in project Payara by payara.

the class StandardWrapper method service.

// START IASRI 4665318
/**
 * Wrapper for the service method on the actual servlet
 * @param request The request sent
 * @param response
 * @param servlet The servlet to process
 * @throws IOException
 * @throws ServletException
 * @see Servlet#service(ServletRequest, ServletResponse)
 */
void service(ServletRequest request, ServletResponse response, Servlet servlet) throws IOException, ServletException {
    InstanceSupport supp = getInstanceSupport();
    try {
        supp.fireInstanceEvent(BEFORE_SERVICE_EVENT, servlet, request, response);
        if (!isAsyncSupported()) {
            RequestFacadeHelper reqFacHelper = RequestFacadeHelper.getInstance(request);
            if (reqFacHelper != null) {
                reqFacHelper.disableAsyncSupport();
            }
        }
        if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
            if (SecurityUtil.executeUnderSubjectDoAs()) {
                final ServletRequest req = request;
                final ServletResponse res = response;
                Principal principal = ((HttpServletRequest) req).getUserPrincipal();
                Object[] serviceType = new Object[2];
                serviceType[0] = req;
                serviceType[1] = res;
                SecurityUtil.doAsPrivilege("service", servlet, classTypeUsedInService, serviceType, principal);
            } else {
                RequestTraceSpan span = null;
                if (requestTracing.isRequestTracingEnabled()) {
                    if (servlet instanceof ServletContainer) {
                        span = constructWebServiceRequestSpan((HttpServletRequest) request);
                    } else if (servlet instanceof Servlet) {
                        span = constructServletRequestSpan((HttpServletRequest) request, servlet);
                    }
                }
                try {
                    if (isJspServlet) {
                        isInSuppressFFNFThread.set(true);
                    }
                    servlet.service((HttpServletRequest) request, (HttpServletResponse) response);
                } finally {
                    if (requestTracing.isRequestTracingEnabled() && span != null) {
                        requestTracing.traceSpan(span);
                    }
                    isInSuppressFFNFThread.set(false);
                }
            }
        } else {
            servlet.service(request, response);
        }
        supp.fireInstanceEvent(AFTER_SERVICE_EVENT, servlet, request, response);
    } catch (IOException e) {
        // Set response status before firing event, see IT 10022
        if (response instanceof HttpServletResponse) {
            ((HttpServletResponse) response).setStatus(SC_INTERNAL_SERVER_ERROR);
        }
        supp.fireInstanceEvent(AFTER_SERVICE_EVENT, servlet, request, response, e);
        throw e;
    } catch (ServletException e) {
        // Set response status before firing event, see IT 10022
        if (response instanceof HttpServletResponse) {
            ((HttpServletResponse) response).setStatus(SC_INTERNAL_SERVER_ERROR);
        }
        supp.fireInstanceEvent(AFTER_SERVICE_EVENT, servlet, request, response, e);
        throw e;
    } catch (RuntimeException e) {
        // Set response status before firing event, see IT 10022
        if (response instanceof HttpServletResponse) {
            ((HttpServletResponse) response).setStatus(SC_INTERNAL_SERVER_ERROR);
        }
        supp.fireInstanceEvent(AFTER_SERVICE_EVENT, servlet, request, response, e);
        throw e;
    } catch (Error e) {
        // Set response status before firing event, see IT 10022
        if (response instanceof HttpServletResponse) {
            ((HttpServletResponse) response).setStatus(SC_INTERNAL_SERVER_ERROR);
        }
        supp.fireInstanceEvent(AFTER_SERVICE_EVENT, servlet, request, response, e);
        throw e;
    } catch (Throwable e) {
        // Set response status before firing event, see IT 10022
        ((HttpServletResponse) response).setStatus(SC_INTERNAL_SERVER_ERROR);
        supp.fireInstanceEvent(AFTER_SERVICE_EVENT, servlet, request, response, e);
        throw new ServletException(rb.getString(SERVLET_EXECUTION_EXCEPTION), e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) ContainerServlet(org.apache.catalina.ContainerServlet) InstanceSupport(org.apache.catalina.util.InstanceSupport) Principal(java.security.Principal)

Aggregations

IOException (java.io.IOException)3 InstanceSupport (org.apache.catalina.util.InstanceSupport)3 Principal (java.security.Principal)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServlet (javax.servlet.http.HttpServlet)1 ContainerServlet (org.apache.catalina.ContainerServlet)1 ClientAbortException (org.apache.catalina.connector.ClientAbortException)1 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)1