Search in sources :

Example 1 with HttpSession

use of javax.servlet.http.HttpSession in project camel by apache.

the class EchoServiceSessionImpl method echo.

public String echo(String text) {
    // Find the HttpSession
    MessageContext mc = context.getMessageContext();
    HttpSession session = ((javax.servlet.http.HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST)).getSession();
    if (session == null) {
        throw new WebServiceException("No HTTP Session found");
    }
    if (session.getAttribute("foo") == null) {
        session.setAttribute("foo", "bar");
        return "New " + text;
    }
    return "Old " + text;
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) HttpSession(javax.servlet.http.HttpSession) MessageContext(javax.xml.ws.handler.MessageContext)

Example 2 with HttpSession

use of javax.servlet.http.HttpSession in project camel by apache.

the class AhcProducerSessionTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to("ahc:" + getTestServerEndpointSessionUrl()).to("ahc:" + getTestServerEndpointSessionUrl()).to("mock:result");
            from("direct:instance").to("ahc:" + getTestServerEndpointSessionUrl() + "?cookieHandler=#instanceCookieHandler").to("ahc:" + getTestServerEndpointSessionUrl() + "?cookieHandler=#instanceCookieHandler").to("mock:result");
            from("direct:exchange").to("ahc:" + getTestServerEndpointSessionUrl() + "?cookieHandler=#exchangeCookieHandler").to("ahc:" + getTestServerEndpointSessionUrl() + "?cookieHandler=#exchangeCookieHandler").to("mock:result");
            from(getTestServerEndpointSessionUri()).process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    HttpMessage message = exchange.getIn(HttpMessage.class);
                    HttpSession session = message.getRequest().getSession();
                    String body = message.getBody(String.class);
                    if ("bar".equals(session.getAttribute("foo"))) {
                        message.setBody("Old " + body);
                    } else {
                        session.setAttribute("foo", "bar");
                        message.setBody("New " + body);
                    }
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpSession(javax.servlet.http.HttpSession) HttpMessage(org.apache.camel.http.common.HttpMessage)

Example 3 with HttpSession

use of javax.servlet.http.HttpSession in project camel by apache.

the class SessionReflectionHandler method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    HttpSession session = request.getSession();
    OutputStream os = response.getOutputStream();
    baseRequest.setHandled(true);
    if (session.getAttribute("foo") == null) {
        session.setAttribute("foo", "bar");
        os.write("New ".getBytes());
    } else {
        os.write("Old ".getBytes());
    }
    IOHelper.copyAndCloseInput(request.getInputStream(), os);
    response.setStatus(HttpServletResponse.SC_OK);
}
Also used : HttpSession(javax.servlet.http.HttpSession) OutputStream(java.io.OutputStream)

Example 4 with HttpSession

use of javax.servlet.http.HttpSession in project camel by apache.

the class HttpRouteTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() {
            port1 = getPort();
            port2 = getNextPort();
            port3 = getNextPort();
            port4 = getNextPort();
            // enable stream cache
            context.setStreamCaching(true);
            from("jetty:http://localhost:" + port1 + "/test").to("mock:a");
            Processor proc = new Processor() {

                public void process(Exchange exchange) throws Exception {
                    try {
                        HttpMessage message = (HttpMessage) exchange.getIn();
                        HttpSession session = message.getRequest().getSession();
                        assertNotNull("we should get session here", session);
                    } catch (Exception e) {
                        exchange.getOut().setFault(true);
                        exchange.getOut().setBody(e);
                    }
                    exchange.getOut().setBody("<b>Hello World</b>");
                }
            };
            from("jetty:http://localhost:" + port1 + "/responseCode").setHeader(Exchange.HTTP_RESPONSE_CODE, simple("400"));
            Processor printProcessor = new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Message out = exchange.getOut();
                    out.copyFrom(exchange.getIn());
                    log.info("The body's object is " + exchange.getIn().getBody());
                    log.info("Process body = " + exchange.getIn().getBody(String.class));
                    InputStreamCache cache = out.getBody(InputStreamCache.class);
                    cache.reset();
                }
            };
            from("jetty:http://localhost:" + port2 + "/hello?sessionSupport=true").process(proc);
            from("jetty:http://localhost:" + port1 + "/echo").process(printProcessor).process(printProcessor);
            Processor procParameters = new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // As the request input stream is cached by DefaultHttpBinding,
                    // HttpServletRequest can't get the parameters of post message
                    String value = exchange.getIn().getHeader("request", String.class);
                    if (value != null) {
                        assertNotNull("The value of the parameter should not be null", value);
                        exchange.getOut().setBody(value);
                    } else {
                        exchange.getOut().setBody("Can't get a right parameter");
                    }
                }
            };
            from("jetty:http://localhost:" + port1 + "/parameter").process(procParameters);
            from("jetty:http://localhost:" + port1 + "/postxml").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String value = exchange.getIn().getBody(String.class);
                    assertEquals("The response message is wrong", value, POST_MESSAGE);
                    exchange.getOut().setBody("OK");
                }
            });
            from("jetty:http://localhost:" + port3 + "/noStreamCache?disableStreamCache=true").noStreamCaching().process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    InputStream is = (InputStream) exchange.getIn().getBody();
                    assertTrue("It should be a raw inputstream", is instanceof org.eclipse.jetty.server.HttpInput);
                    String request = exchange.getIn().getBody(String.class);
                    assertEquals("Got a wrong request", "This is a test", request);
                    exchange.getOut().setBody("OK");
                }
            });
            from("jetty:http://localhost:" + port4 + "/requestBufferSize").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String string = exchange.getIn().getBody(String.class);
                    exchange.getOut().setBody(string);
                }
            });
        }
    };
}
Also used : Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(org.apache.camel.Message) HttpMessage(org.apache.camel.http.common.HttpMessage) HttpSession(javax.servlet.http.HttpSession) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Exchange(org.apache.camel.Exchange) InputStreamCache(org.apache.camel.converter.stream.InputStreamCache) HttpMessage(org.apache.camel.http.common.HttpMessage)

Example 5 with HttpSession

use of javax.servlet.http.HttpSession in project tomcat by apache.

the class SecurityUtil method execute.

/**
     * 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 targetArguments <code>Object</code> array contains the
     *  runtime parameters instance.
     * @param principal the <code>Principal</code> to which the security
     *  privilege applies
     * @throws Exception an execution error occurred
     */
private static void execute(final Method method, final Object targetObject, final Object[] targetArguments, Principal principal) throws Exception {
    try {
        Subject subject = null;
        PrivilegedExceptionAction<Void> pea = new PrivilegedExceptionAction<Void>() {

            @Override
            public Void run() throws Exception {
                method.invoke(targetObject, targetArguments);
                return null;
            }
        };
        // The first argument is always the request object
        if (targetArguments != null && targetArguments[0] instanceof HttpServletRequest) {
            HttpServletRequest request = (HttpServletRequest) targetArguments[0];
            boolean hasSubject = false;
            HttpSession session = request.getSession(false);
            if (session != null) {
                subject = (Subject) session.getAttribute(Globals.SUBJECT_ATTR);
                hasSubject = (subject != null);
            }
            if (subject == null) {
                subject = new Subject();
                if (principal != null) {
                    subject.getPrincipals().add(principal);
                }
            }
            if (session != null && !hasSubject) {
                session.setAttribute(Globals.SUBJECT_ATTR, subject);
            }
        }
        Subject.doAsPrivileged(subject, pea, null);
    } catch (PrivilegedActionException pe) {
        Throwable e;
        if (pe.getException() instanceof InvocationTargetException) {
            e = pe.getException().getCause();
            ExceptionUtils.handleThrowable(e);
        } else {
            e = pe;
        }
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("SecurityUtil.doAsPrivilege"), e);
        }
        if (e instanceof UnavailableException)
            throw (UnavailableException) e;
        else if (e instanceof ServletException)
            throw (ServletException) e;
        else if (e instanceof IOException)
            throw (IOException) e;
        else if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new ServletException(e.getMessage(), e);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) HttpSession(javax.servlet.http.HttpSession) UnavailableException(javax.servlet.UnavailableException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) Subject(javax.security.auth.Subject) InvocationTargetException(java.lang.reflect.InvocationTargetException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException)

Aggregations

HttpSession (javax.servlet.http.HttpSession)730 HttpServletRequest (javax.servlet.http.HttpServletRequest)151 Test (org.junit.Test)110 IOException (java.io.IOException)80 HttpServletResponse (javax.servlet.http.HttpServletResponse)80 ServletException (javax.servlet.ServletException)75 ArrayList (java.util.ArrayList)65 RequestDispatcher (javax.servlet.RequestDispatcher)59 HashMap (java.util.HashMap)48 Map (java.util.Map)44 Locale (java.util.Locale)39 Properties (java.util.Properties)39 PrintWriter (java.io.PrintWriter)38 Cookie (javax.servlet.http.Cookie)27 List (java.util.List)24 SQLException (java.sql.SQLException)23 WebUser (org.compiere.util.WebUser)23 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)20 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 ModelAndView (org.springframework.web.servlet.ModelAndView)20