Package org.mortbay.jetty

Examples of org.mortbay.jetty.Request


    {
        if (!isStarted())
            return;

        // Get the base requests
        final Request base_request=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest();
        final String old_servlet_name=base_request.getServletName();
        final String old_servlet_path=base_request.getServletPath();
        final String old_path_info=base_request.getPathInfo();
        final Map old_role_map=base_request.getRoleMap();
        Object request_listeners=null;
        ServletRequestEvent request_event=null;
       
        try
        {
            ServletHolder servlet_holder=null;
            FilterChain chain=null;
           
            // find the servlet
            if (target.startsWith("/"))
            {
                // Look for the servlet by path
                PathMap.Entry entry=getHolderEntry(target);
                if (entry!=null)
                {
                    servlet_holder=(ServletHolder)entry.getValue();
                    base_request.setServletName(servlet_holder.getName());
                    base_request.setRoleMap(servlet_holder.getRoleMap());
                    if(Log.isDebugEnabled())Log.debug("servlet="+servlet_holder);
                   
                    String servlet_path_spec=(String)entry.getKey();
                    String servlet_path=entry.getMapped()!=null?entry.getMapped():PathMap.pathMatch(servlet_path_spec,target);
                    String path_info=PathMap.pathInfo(servlet_path_spec,target);
                   
                    if (type==INCLUDE)
                    {
                        base_request.setAttribute(Dispatcher.__INCLUDE_SERVLET_PATH,servlet_path);
                        base_request.setAttribute(Dispatcher.__INCLUDE_PATH_INFO, path_info);
                    }
                    else
                    {
                        base_request.setServletPath(servlet_path);
                        base_request.setPathInfo(path_info);
                    }
                   
                    if (servlet_holder!=null && _filterMappings!=null && _filterMappings.length>0)
                        chain=getFilterChain(type, target, servlet_holder);
                }     
            }
            else
            {
                // look for a servlet by name!
                servlet_holder=(ServletHolder)_servletNameMap.get(target);
                if (servlet_holder!=null && _filterMappings!=null && _filterMappings.length>0)
                {
                    base_request.setServletName(servlet_holder.getName());
                    chain=getFilterChain(type, null,servlet_holder);
                }
            }

            if (Log.isDebugEnabled())
            {
                Log.debug("chain="+chain);
                Log.debug("servlet holder="+servlet_holder);
            }

            // Handle context listeners
            request_listeners = base_request.takeRequestListeners();
            if (request_listeners!=null)
            {
                request_event = new ServletRequestEvent(getServletContext(),request);
                final int s=LazyList.size(request_listeners);
                for(int i=0;i<s;i++)
                {
                    final ServletRequestListener listener = (ServletRequestListener)LazyList.get(request_listeners,i);
                    listener.requestInitialized(request_event);
                }
            }
           
            // Do the filter/handling thang
            if (servlet_holder!=null)
            {
                base_request.setHandled(true);
                if (chain!=null)
                    chain.doFilter(request, response);
                else
                    servlet_holder.handle(request,response);
            }
            else
                notFound(request, response);
        }
        catch(RetryRequest e)
        {
            base_request.setHandled(false);
            throw e;
        }
        catch(EofException e)
        {
            throw e;
        }
        catch(RuntimeIOException e)
        {
            throw e;
        }
        catch(Exception e)
        {
            if (type!=REQUEST)
            {
                if (e instanceof IOException)
                    throw (IOException)e;
                if (e instanceof RuntimeException)
                    throw (RuntimeException)e;
                if (e instanceof ServletException)
                    throw (ServletException)e;
            }
           
           
            // unwrap cause
            Throwable th=e;
            if (th instanceof UnavailableException)
            {
                Log.debug(th);
            }
            else if (th instanceof ServletException)
            {
                Log.debug(th);
                Throwable cause=((ServletException)th).getRootCause();
                if (cause!=th && cause!=null)
                    th=cause;
            }
           
            // hnndle or log exception
            if (th instanceof RetryRequest)
            {
                base_request.setHandled(false);
                throw (RetryRequest)th; 
            }
            else if (th instanceof HttpException)
                throw (HttpException)th;
            else if (th instanceof RuntimeIOException)
                throw (RuntimeIOException)th;
            else if (th instanceof EofException)
                throw (EofException)th;
            else if (Log.isDebugEnabled())
            {
                Log.warn(request.getRequestURI(), th);
                Log.debug(request.toString());
            }
            else if (th instanceof IOException || th instanceof UnavailableException)
            {
                Log.warn(request.getRequestURI()+": "+th);
            }
            else
            {
                Log.warn(request.getRequestURI(),th);
            }
           
            // TODO httpResponse.getHttpConnection().forceClose();
            if (!response.isCommitted())
            {
                request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION_TYPE,th.getClass());
                request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION,th);
                if (th instanceof UnavailableException)
                {
                    UnavailableException ue = (UnavailableException)th;
                    if (ue.isPermanent())
                        response.sendError(HttpServletResponse.SC_NOT_FOUND,th.getMessage());
                    else
                        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,th.getMessage());
                }
                else
                    response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,th.getMessage());
            }
            else
                if(Log.isDebugEnabled())Log.debug("Response already committed for handling "+th);
        }
        catch(Error e)
        {  
            if (type!=REQUEST)
                throw e;
            Log.warn("Error for "+request.getRequestURI(),e);
            if(Log.isDebugEnabled())Log.debug(request.toString());
           
            // TODO httpResponse.getHttpConnection().forceClose();
            if (!response.isCommitted())
            {
                request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION_TYPE,e.getClass());
                request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION,e);
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());
            }
            else
                if(Log.isDebugEnabled())Log.debug("Response already committed for handling ",e);
        }
        finally
        {
            if (request_listeners!=null)
            {
                for(int i=LazyList.size(request_listeners);i-->0;)
                {
                    final ServletRequestListener listener = (ServletRequestListener)LazyList.get(request_listeners,i);
                    listener.requestDestroyed(request_event);
                }
            }
           
            base_request.setServletName(old_servlet_name);
            base_request.setRoleMap(old_role_map);
            if (type!=INCLUDE)
            {
                base_request.setServletPath(old_servlet_path);
                base_request.setPathInfo(old_path_info);
            }
        }
        return;
    }
View Full Code Here


        }

        public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
            throws IOException, ServletException
        {
            Request base_request =
                request instanceof Request ? (Request) request : HttpConnection.getCurrentConnection().getRequest();

            if ( base_request.isHandled() || !"PUT".equals( base_request.getMethod() ) )
            {
                return;
            }

            base_request.setHandled( true );

            File file = new File( resourceBase, URLDecoder.decode( request.getPathInfo() ) );
            file.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream( file );
            ServletInputStream in = request.getInputStream();
View Full Code Here

            endpointInfo.setAddress(addr);
        }
    }
  
    protected void doService(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Request baseRequest = (req instanceof Request)
            ? (Request)req : HttpConnection.getCurrentConnection().getRequest();
           
        if (getServer().isSetRedirectURL()) {
            resp.sendRedirect(getServer().getRedirectURL());
            resp.flushBuffer();
            baseRequest.setHandled(true);
            return;
        }
        QueryHandlerRegistry queryHandlerRegistry = bus.getExtension(QueryHandlerRegistry.class);
       
        if (null != req.getQueryString() && queryHandlerRegistry != null) {       
            String requestURL = req.getRequestURL() + "?" + req.getQueryString();
            String pathInfo = req.getPathInfo();                    
            for (QueryHandler qh : queryHandlerRegistry.getHandlers()) {
                if (qh.isRecognizedQuery(requestURL, pathInfo, endpointInfo)) {
                    //replace the endpointInfo address with request url only for get wsdl          
                    updateEndpointAddress(req.getRequestURL().toString());  
                    resp.setContentType(qh.getResponseContentType(requestURL, pathInfo));
                    qh.writeResponse(requestURL, pathInfo, endpointInfo, resp.getOutputStream());
                    resp.getOutputStream().flush();                    
                    baseRequest.setHandled(true);
                    return;
                }
            }
        }
View Full Code Here

        serviceRequest(req, resp);
    }

    protected void serviceRequest(final HttpServletRequest req, final HttpServletResponse resp)
        throws IOException {
        Request baseRequest = (req instanceof Request)
            ? (Request)req : HttpConnection.getCurrentConnection().getRequest();
        try {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Service http request on thread: " + Thread.currentThread());
            }

            MessageImpl inMessage = new MessageImpl();
            inMessage.setContent(InputStream.class, req.getInputStream());
            inMessage.put(HTTP_REQUEST, req);
            inMessage.put(HTTP_RESPONSE, resp);
            inMessage.put(Message.HTTP_REQUEST_METHOD, req.getMethod());
            inMessage.put(Message.PATH_INFO, req.getContextPath() + req.getPathInfo());
           
            inMessage.put(Message.QUERY_STRING, req.getQueryString());
            inMessage.put(Message.CONTENT_TYPE, req.getContentType());
            if (!StringUtils.isEmpty(endpointInfo.getAddress())) {
                inMessage.put(Message.BASE_PATH, new URL(endpointInfo.getAddress()).getPath());
            }
            inMessage.put(Message.FIXED_PARAMETER_ORDER, isFixedParameterOrder());
            inMessage.put(Message.ASYNC_POST_RESPONSE_DISPATCH, Boolean.TRUE);
            inMessage.put(SecurityContext.class, new SecurityContext() {
                public Principal getUserPrincipal() {
                    return req.getUserPrincipal();
                }
                public boolean isUserInRole(String role) {
                    return req.isUserInRole(role);
                }
            });
           
            setHeaders(inMessage);
            inMessage.setDestination(this);
           
            SSLUtils.propogateSecureSession(req, inMessage);

            ExchangeImpl exchange = new ExchangeImpl();
            exchange.setInMessage(inMessage);
            exchange.setSession(new HTTPSession(req));
           
            incomingObserver.onMessage(inMessage);

            resp.flushBuffer();
            baseRequest.setHandled(true);
        } finally {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Finished servicing http request on thread: " + Thread.currentThread());
            }
        }
View Full Code Here

        //TODO
        //do we need to check that this request should be handled by this handler?
        if (! target.startsWith(contextPath)) {
            return;
        }
        Request jettyRequest = (Request) req;
        Response jettyResponse = (Response) res;
        res.setContentType("text/xml");
        RequestAdapter request = new RequestAdapter(jettyRequest);
        ResponseAdapter response = new ResponseAdapter(jettyResponse);

        request.setAttribute(WebServiceContainer.SERVLET_REQUEST, req);
        request.setAttribute(WebServiceContainer.SERVLET_RESPONSE, res);
        // TODO: add support for context
        request.setAttribute(WebServiceContainer.SERVLET_CONTEXT, null);

        if (req.getParameter("wsdl") != null) {
            try {
                webServiceContainer.getWsdl(request, response);
                jettyRequest.setHandled(true);
            } catch (IOException e) {
                throw e;
            } catch (Exception e) {
                throw (HttpException) new HttpException(500, "Could not fetch wsdl!").initCause(e);
            }
        } else {
            if (isConfidentialTransportGuarantee) {
                if (!req.isSecure()) {
                    throw new HttpException(403, null);
                }
            } else if (isIntegralTransportGuarantee) {
                if (!jettyRequest.getConnection().isIntegral(jettyRequest)) {
                    throw new HttpException(403, null);
                }
            }
            PolicyContext.setHandlerData((realm == null) ? null : req);
            Thread currentThread = Thread.currentThread();
            ClassLoader oldClassLoader = currentThread.getContextClassLoader();
            currentThread.setContextClassLoader(classLoader);
            //hard to imagine this could be anything but null, but....
//            Subject oldSubject = ContextManager.getCurrentCaller();
            try {
                if (authenticator != null) {
                    String pathInContext = org.mortbay.util.URIUtil.canonicalPath(req.getContextPath());
                    if (authenticator.authenticate(realm, pathInContext, jettyRequest, jettyResponse) == null) {
                        throw new HttpException(403, null);
                    }
                } else {
                    //EJB will figure out correct defaultSubject shortly
                    //TODO consider replacing the GenericEJBContainer.DefaultSubjectInterceptor with this line
                    //setting the defaultSubject.
                    ContextManager.popCallers(null);
                }
                try {
                    webServiceContainer.invoke(request, response);
                    jettyRequest.setHandled(true);
                } catch (IOException e) {
                    throw e;
                } catch (Exception e) {
                    throw (HttpException) new HttpException(500, "Could not process message!").initCause(e);
                }
View Full Code Here

                    throws IOException, ServletException {
                boolean auth = request.getHeader("Authorization") != null;
                String resLoc = request.getPathInfo();
                boolean noRealm = "/norealm".equals(resLoc);

                Request base_request = (request instanceof Request) ? (Request) request
                        : HttpConnection.getCurrentConnection().getRequest();
                base_request.setHandled(true);
                response.setContentType("text/html");
                response.addDateHeader("Date", System.currentTimeMillis());
                if (noRealm) {
                    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                    response.getWriter().print("<h1>No WWW-Authenticate</h1>");
View Full Code Here

    }
   
    public void reset()
    {
        _connection = new HttpConnection(_connector,_endpoint,_server);
        _request = new Request(_connection);
        _response = new Response(_connection);
       
        _request.setRequestURI("/test/");
    }
View Full Code Here

    private void assertMatch(boolean flag, String[] matchCase) throws IOException
    {
        _rule.setRegex(matchCase[0]);
        final String uri=matchCase[1];
        String result = _rule.matchAndApply(uri,
        new Request()
        {
            public String getRequestURI()
            {
                return uri;
            }
View Full Code Here

    @Override
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
    {
        setRequestedId(request, dispatch);

        Request currentRequest = (request instanceof Request) ? (Request)request: HttpConnection.getCurrentConnection().getRequest();

        SessionManager requestSessionManager = currentRequest.getSessionManager();
        HttpSession requestSession = currentRequest.getSession(false);

        TerracottaSessionManager sessionManager = (TerracottaSessionManager)getSessionManager();
        Log.debug("SessionManager = {}", sessionManager);

        // Is it a cross context dispatch or a direct hit to this context ?
        if (sessionManager != requestSessionManager)
        {
            // Setup the request for this context
            currentRequest.setSessionManager(sessionManager);
            currentRequest.setSession(null);
        }

        // Tell the session manager that the request is entering
        if (sessionManager != null) sessionManager.enter(currentRequest);
        try
        {
            HttpSession currentSession = null;
            if (sessionManager != null)
            {
                currentSession = currentRequest.getSession(false);
                if (currentSession != null)
                {
                    if (currentSession != requestSession)
                    {
                        // Access the session only if we did not already
                        Cookie cookie = sessionManager.access(currentSession, request.isSecure());
                        if (cookie != null)
                        {
                            // Handle changed session id or cookie max-age refresh
                            response.addCookie(cookie);
                        }
                    }
                    else
                    {
                        // Handle resume of the request
                        currentSession = currentRequest.recoverNewSession(sessionManager);
                        if (currentSession != null) currentRequest.setSession(currentSession);
                    }
                }
            }
            Log.debug("Session = {}", currentSession);

            getHandler().handle(target, request, response, dispatch);
        }
        catch (RetryRequest x)
        {
            // User may have invalidated the session, must get it again
            HttpSession currentSession = currentRequest.getSession(false);
            if (currentSession != null && currentSession.isNew())
                currentRequest.saveNewSession(sessionManager, currentSession);

            throw x;
        }
        finally
        {
            if (sessionManager != null)
            {
                // User may have invalidated the session, must get it again
                HttpSession currentSession = currentRequest.getSession(false);
                if (currentSession != null) sessionManager.complete(currentSession);

                sessionManager.exit(currentRequest);
            }

            // Restore cross context dispatch
            if (sessionManager != requestSessionManager)
            {
                currentRequest.setSessionManager(requestSessionManager);
                currentRequest.setSession(requestSession);
            }
        }
    }
View Full Code Here

    private void assertMatch(boolean flag, String[] matchCase) throws IOException
    {
        _rule.setPattern(matchCase[0]);
        final String uri=matchCase[1];
        String result = _rule.matchAndApply(uri,
        new Request()
        {
            {
                setRequestURI(uri);
            }
        }, null
View Full Code Here

TOP

Related Classes of org.mortbay.jetty.Request

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.