Search in sources :

Example 1 with CometProcessor

use of org.apache.catalina.comet.CometProcessor in project tomcat70 by apache.

the class ApplicationFilterChain method internalDoFilterEvent.

private void internalDoFilterEvent(CometEvent event) throws IOException, ServletException {
    // Call the next filter if there is one
    if (pos < n) {
        ApplicationFilterConfig filterConfig = filters[pos++];
        CometFilter filter = null;
        try {
            filter = (CometFilter) filterConfig.getFilter();
            if (Globals.IS_SECURITY_ENABLED) {
                final CometEvent ev = event;
                Principal principal = ev.getHttpServletRequest().getUserPrincipal();
                Object[] args = new Object[] { ev, this };
                SecurityUtil.doAsPrivilege("doFilterEvent", filter, cometClassType, args, principal);
            } else {
                filter.doFilterEvent(event, this);
            }
        /*support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                        filter, event);*/
        } catch (IOException e) {
            /*
                if (filter != null)
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                            filter, event, e);
                            */
            throw e;
        } catch (ServletException e) {
            /*
                if (filter != null)
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                            filter, event, e);
                            */
            throw e;
        } catch (RuntimeException e) {
            /*
                if (filter != null)
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                            filter, event, e);
                            */
            throw e;
        } catch (Throwable e) {
            e = ExceptionUtils.unwrapInvocationTargetException(e);
            ExceptionUtils.handleThrowable(e);
            /*if (filter != null)
                    support.fireInstanceEvent(InstanceEvent.AFTER_FILTER_EVENT,
                            filter, event, e);*/
            throw new ServletException(sm.getString("filterChain.filter"), e);
        }
        return;
    }
    // We fell off the end of the chain -- call the servlet instance
    try {
        /*
            support.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,
                    servlet, request, response);
                    */
        if (Globals.IS_SECURITY_ENABLED) {
            final CometEvent ev = event;
            Principal principal = ev.getHttpServletRequest().getUserPrincipal();
            Object[] args = new Object[] { ev };
            SecurityUtil.doAsPrivilege("event", servlet, classTypeUsedInEvent, args, principal);
        } else {
            ((CometProcessor) servlet).event(event);
        }
    /*
            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) {
        ExceptionUtils.handleThrowable(e);
        /*
            support.fireInstanceEvent(InstanceEvent.AFTER_SERVICE_EVENT,
                    servlet, request, response, e);
                    */
        throw new ServletException(sm.getString("filterChain.servlet"), e);
    }
}
Also used : ServletException(javax.servlet.ServletException) CometProcessor(org.apache.catalina.comet.CometProcessor) CometFilter(org.apache.catalina.comet.CometFilter) CometEvent(org.apache.catalina.comet.CometEvent) IOException(java.io.IOException) Principal(java.security.Principal)

Example 2 with CometProcessor

use of org.apache.catalina.comet.CometProcessor in project tomcat70 by apache.

the class StandardWrapperValve method invoke.

// --------------------------------------------------------- Public Methods
/**
 * Invoke the servlet we are managing, respecting the rules regarding
 * servlet lifecycle and SingleThreadModel support.
 *
 * @param request Request to be processed
 * @param response Response to be produced
 *
 * @exception IOException if an input/output error occurred
 * @exception ServletException if a servlet error occurred
 */
@Override
public final void invoke(Request request, Response response) throws IOException, ServletException {
    // Initialize local variables we may need
    boolean unavailable = false;
    Throwable throwable = null;
    // This should be a Request attribute...
    long t1 = System.currentTimeMillis();
    requestCount.incrementAndGet();
    StandardWrapper wrapper = (StandardWrapper) getContainer();
    Servlet servlet = null;
    Context context = (Context) wrapper.getParent();
    // Check for the application being marked unavailable
    if (!context.getState().isAvailable()) {
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, sm.getString("standardContext.isUnavailable"));
        unavailable = true;
    }
    // Check for the servlet being marked unavailable
    if (!unavailable && wrapper.isUnavailable()) {
        container.getLogger().info(sm.getString("standardWrapper.isUnavailable", wrapper.getName()));
        long available = wrapper.getAvailable();
        if ((available > 0L) && (available < Long.MAX_VALUE)) {
            response.setDateHeader("Retry-After", available);
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, sm.getString("standardWrapper.isUnavailable", wrapper.getName()));
        } else if (available == Long.MAX_VALUE) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, sm.getString("standardWrapper.notFound", wrapper.getName()));
        }
        unavailable = true;
    }
    // Allocate a servlet instance to process this request
    try {
        if (!unavailable) {
            servlet = wrapper.allocate();
        }
    } catch (UnavailableException e) {
        container.getLogger().error(sm.getString("standardWrapper.allocateException", wrapper.getName()), e);
        long available = wrapper.getAvailable();
        if ((available > 0L) && (available < Long.MAX_VALUE)) {
            response.setDateHeader("Retry-After", available);
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, sm.getString("standardWrapper.isUnavailable", wrapper.getName()));
        } else if (available == Long.MAX_VALUE) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, sm.getString("standardWrapper.notFound", wrapper.getName()));
        }
    } catch (ServletException e) {
        container.getLogger().error(sm.getString("standardWrapper.allocateException", wrapper.getName()), StandardWrapper.getRootCause(e));
        throwable = e;
        exception(request, response, e);
    } catch (Throwable e) {
        ExceptionUtils.handleThrowable(e);
        container.getLogger().error(sm.getString("standardWrapper.allocateException", wrapper.getName()), e);
        throwable = e;
        exception(request, response, e);
        servlet = null;
    }
    // Identify if the request is Comet related now that the servlet has been allocated
    boolean comet = false;
    if (servlet instanceof CometProcessor && Boolean.TRUE.equals(request.getAttribute(Globals.COMET_SUPPORTED_ATTR))) {
        comet = true;
        request.setComet(true);
    }
    MessageBytes requestPathMB = request.getRequestPathMB();
    DispatcherType dispatcherType = DispatcherType.REQUEST;
    if (request.getDispatcherType() == DispatcherType.ASYNC)
        dispatcherType = DispatcherType.ASYNC;
    request.setAttribute(Globals.DISPATCHER_TYPE_ATTR, dispatcherType);
    request.setAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR, requestPathMB);
    // Create the filter chain for this request
    ApplicationFilterFactory factory = ApplicationFilterFactory.getInstance();
    ApplicationFilterChain filterChain = factory.createFilterChain(request, wrapper, servlet);
    // Reset comet flag value after creating the filter chain
    request.setComet(false);
    // NOTE: This also calls the servlet's service() method
    try {
        if ((servlet != null) && (filterChain != null)) {
            // Swallow output if needed
            if (context.getSwallowOutput()) {
                try {
                    SystemLogHandler.startCapture();
                    if (request.isAsyncDispatching()) {
                        request.getAsyncContextInternal().doInternalDispatch();
                    } else if (comet) {
                        filterChain.doFilterEvent(request.getEvent());
                        request.setComet(true);
                    } else {
                        filterChain.doFilter(request.getRequest(), response.getResponse());
                    }
                } finally {
                    String log = SystemLogHandler.stopCapture();
                    if (log != null && log.length() > 0) {
                        context.getLogger().info(log);
                    }
                }
            } else {
                if (request.isAsyncDispatching()) {
                    request.getAsyncContextInternal().doInternalDispatch();
                } else if (comet) {
                    request.setComet(true);
                    filterChain.doFilterEvent(request.getEvent());
                } else {
                    filterChain.doFilter(request.getRequest(), response.getResponse());
                }
            }
        }
    } catch (ClientAbortException e) {
        throwable = e;
        exception(request, response, e);
    } catch (IOException e) {
        container.getLogger().error(sm.getString("standardWrapper.serviceException", wrapper.getName(), context.getName()), e);
        throwable = e;
        exception(request, response, e);
    } catch (UnavailableException e) {
        container.getLogger().error(sm.getString("standardWrapper.serviceException", wrapper.getName(), context.getName()), e);
        // throwable = e;
        // exception(request, response, e);
        wrapper.unavailable(e);
        long available = wrapper.getAvailable();
        if ((available > 0L) && (available < Long.MAX_VALUE)) {
            response.setDateHeader("Retry-After", available);
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, sm.getString("standardWrapper.isUnavailable", wrapper.getName()));
        } else if (available == Long.MAX_VALUE) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, sm.getString("standardWrapper.notFound", wrapper.getName()));
        }
    // Do not save exception in 'throwable', because we
    // do not want to do exception(request, response, e) processing
    } catch (ServletException e) {
        Throwable rootCause = StandardWrapper.getRootCause(e);
        if (!(rootCause instanceof ClientAbortException)) {
            container.getLogger().error(sm.getString("standardWrapper.serviceExceptionRoot", wrapper.getName(), context.getName(), e.getMessage()), rootCause);
        }
        throwable = e;
        exception(request, response, e);
    } catch (Throwable e) {
        ExceptionUtils.handleThrowable(e);
        container.getLogger().error(sm.getString("standardWrapper.serviceException", wrapper.getName(), context.getName()), e);
        throwable = e;
        exception(request, response, e);
    }
    // Release the filter chain (if any) for this request
    if (filterChain != null) {
        if (request.isComet()) {
            // If this is a Comet request, then the same chain will be used for the
            // processing of all subsequent events.
            filterChain.reuse();
        } else {
            filterChain.release();
        }
    }
    // Deallocate the allocated servlet instance
    try {
        if (servlet != null) {
            wrapper.deallocate(servlet);
        }
    } catch (Throwable e) {
        ExceptionUtils.handleThrowable(e);
        container.getLogger().error(sm.getString("standardWrapper.deallocateException", wrapper.getName()), e);
        if (throwable == null) {
            throwable = e;
            exception(request, response, e);
        }
    }
    // unload it and release this instance
    try {
        if ((servlet != null) && (wrapper.getAvailable() == Long.MAX_VALUE)) {
            wrapper.unload();
        }
    } catch (Throwable e) {
        ExceptionUtils.handleThrowable(e);
        container.getLogger().error(sm.getString("standardWrapper.unloadException", wrapper.getName()), e);
        if (throwable == null) {
            throwable = e;
            exception(request, response, e);
        }
    }
    long t2 = System.currentTimeMillis();
    long time = t2 - t1;
    processingTime += time;
    if (time > maxTime)
        maxTime = time;
    if (time < minTime)
        minTime = time;
}
Also used : Context(org.apache.catalina.Context) UnavailableException(javax.servlet.UnavailableException) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) CometProcessor(org.apache.catalina.comet.CometProcessor) Servlet(javax.servlet.Servlet) ClientAbortException(org.apache.catalina.connector.ClientAbortException) DispatcherType(javax.servlet.DispatcherType)

Example 3 with CometProcessor

use of org.apache.catalina.comet.CometProcessor in project tomcat70 by apache.

the class SecurityUtil method doAsPrivilege.

/**
 * Perform work as a particular </code>Subject</code>. Here the work
 * will be granted to a <code>null</code> subject.
 *
 * @param methodName the method to apply the security restriction
 * @param targetObject the <code>Servlet</code> on which the method will
 * be called.
 * @param targetParameterTypes <code>Class</code> array used to instantiate a
 * <code>Method</code> object.
 * @param targetArguments <code>Object</code> array contains the
 * runtime parameters instance.
 * @param principal the <code>Principal</code> to which the security
 * privilege apply..
 */
public static void doAsPrivilege(final String methodName, final Servlet targetObject, final Class<?>[] targetParameterTypes, final Object[] targetArguments, Principal principal) throws java.lang.Exception {
    // CometProcessor instances must not be cached as Servlet or
    // NoSuchMethodException will be thrown.
    Class<? extends Servlet> targetType = targetObject instanceof CometProcessor ? CometProcessor.class : Servlet.class;
    Method method = null;
    Method[] methodsCache = classCache.get(Servlet.class);
    if (methodsCache == null) {
        method = createMethodAndCacheIt(methodsCache, targetType, methodName, targetParameterTypes);
    } else {
        method = findMethod(methodsCache, methodName);
        if (method == null) {
            method = createMethodAndCacheIt(methodsCache, targetType, methodName, targetParameterTypes);
        }
    }
    execute(method, targetObject, targetArguments, principal);
}
Also used : CometProcessor(org.apache.catalina.comet.CometProcessor) Method(java.lang.reflect.Method)

Example 4 with CometProcessor

use of org.apache.catalina.comet.CometProcessor in project tomcat70 by apache.

the class CometConnectionManagerValve method sessionDestroyed.

@Override
public void sessionDestroyed(HttpSessionEvent se) {
    // Close all Comet connections associated with this session
    Request[] reqs = (Request[]) se.getSession().getAttribute(cometRequestsAttribute);
    if (reqs != null) {
        for (int i = 0; i < reqs.length; i++) {
            Request req = reqs[i];
            try {
                CometEventImpl event = req.getEvent();
                event.setEventType(CometEvent.EventType.END);
                event.setEventSubType(CometEvent.EventSubType.SESSION_END);
                ((CometProcessor) req.getWrapper().getServlet()).event(event);
                event.close();
            } catch (Exception e) {
                req.getWrapper().getParent().getLogger().warn(sm.getString("cometConnectionManagerValve.listenerEvent"), e);
            }
        }
    }
}
Also used : CometProcessor(org.apache.catalina.comet.CometProcessor) CometEventImpl(org.apache.catalina.connector.CometEventImpl) Request(org.apache.catalina.connector.Request) LifecycleException(org.apache.catalina.LifecycleException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

CometProcessor (org.apache.catalina.comet.CometProcessor)4 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 Method (java.lang.reflect.Method)1 Principal (java.security.Principal)1 DispatcherType (javax.servlet.DispatcherType)1 Servlet (javax.servlet.Servlet)1 UnavailableException (javax.servlet.UnavailableException)1 Context (org.apache.catalina.Context)1 LifecycleException (org.apache.catalina.LifecycleException)1 CometEvent (org.apache.catalina.comet.CometEvent)1 CometFilter (org.apache.catalina.comet.CometFilter)1 ClientAbortException (org.apache.catalina.connector.ClientAbortException)1 CometEventImpl (org.apache.catalina.connector.CometEventImpl)1 Request (org.apache.catalina.connector.Request)1 MessageBytes (org.apache.tomcat.util.buf.MessageBytes)1