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;
}
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);
}
}
});
}
};
}
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);
}
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);
}
});
}
};
}
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);
}
}
Aggregations