use of javax.servlet.ServletRequest in project wildfly by wildfly.
the class UndertowContextTestCase method addRequestListener.
@Test
public void addRequestListener() throws ServletException {
ServletRequestListener listener = mock(ServletRequestListener.class);
ServletContext context = mock(ServletContext.class);
ServletRequest request = mock(ServletRequest.class);
ApplicationListeners listeners = new ApplicationListeners(Collections.<ManagedListener>emptyList(), context);
ArgumentCaptor<ServletRequestEvent> event = ArgumentCaptor.forClass(ServletRequestEvent.class);
when(this.deployment.getApplicationListeners()).thenReturn(listeners);
this.context.addRequestListener(listener);
listeners.start();
listeners.requestInitialized(request);
verify(listener).requestInitialized(event.capture());
assertSame(request, event.getValue().getServletRequest());
assertSame(context, event.getValue().getServletContext());
event = ArgumentCaptor.forClass(ServletRequestEvent.class);
listeners.requestDestroyed(request);
verify(listener).requestDestroyed(event.capture());
assertSame(request, event.getValue().getServletRequest());
assertSame(context, event.getValue().getServletContext());
}
use of javax.servlet.ServletRequest in project wildfly by wildfly.
the class JASPICSecurityContext method buildAppContext.
/**
* <p>
* Builds the JASPIC application context.
* </p>
*
* @return a {@code String} representing the application context.
*/
private String buildAppContext() {
final ServletRequestContext requestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
ServletRequest servletRequest = requestContext.getServletRequest();
return servletRequest.getServletContext().getVirtualServerName() + " " + servletRequest.getServletContext().getContextPath();
}
use of javax.servlet.ServletRequest in project wildfly by wildfly.
the class AuditNotificationReceiver method handleNotification.
@Override
public void handleNotification(SecurityNotification notification) {
EventType event = notification.getEventType();
if (event == EventType.AUTHENTICATED || event == EventType.FAILED_AUTHENTICATION) {
AuditEvent auditEvent = new AuditEvent(event == EventType.AUTHENTICATED ? AuditLevel.SUCCESS : AuditLevel.FAILURE);
Map<String, Object> ctxMap = new HashMap<String, Object>();
Account account = notification.getAccount();
if (account != null) {
ctxMap.put("principal", account.getPrincipal().getName());
}
ctxMap.put("message", notification.getMessage());
ServletRequestContext src = notification.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY);
if (src != null) {
ServletRequest hsr = src.getServletRequest();
if (hsr instanceof HttpServletRequest) {
ctxMap.put("request", WebUtil.deriveUsefulInfo((HttpServletRequest) hsr));
}
}
ctxMap.put("Source", getClass().getCanonicalName());
auditEvent.setContextMap(ctxMap);
auditManager.audit(auditEvent);
}
}
use of javax.servlet.ServletRequest in project undertow by undertow-io.
the class RequestDispatcherImpl method includeImpl.
private void includeImpl(ServletRequest request, ServletResponse response, ServletRequestContext servletRequestContext, HttpServletRequestImpl requestImpl, HttpServletResponseImpl responseImpl) throws ServletException, IOException {
if (!servletContext.getDeployment().getDeploymentInfo().isAllowNonStandardWrappers()) {
if (servletRequestContext.getOriginalRequest() != request) {
if (!(request instanceof ServletRequestWrapper)) {
throw UndertowServletMessages.MESSAGES.requestWasNotOriginalOrWrapper(request);
}
}
if (servletRequestContext.getOriginalResponse() != response) {
if (!(response instanceof ServletResponseWrapper)) {
throw UndertowServletMessages.MESSAGES.responseWasNotOriginalOrWrapper(response);
}
}
}
final ServletRequest oldRequest = servletRequestContext.getServletRequest();
final ServletResponse oldResponse = servletRequestContext.getServletResponse();
Object requestUri = null;
Object contextPath = null;
Object servletPath = null;
Object pathInfo = null;
Object queryString = null;
Map<String, Deque<String>> queryParameters = requestImpl.getQueryParameters();
if (!named) {
requestUri = request.getAttribute(INCLUDE_REQUEST_URI);
contextPath = request.getAttribute(INCLUDE_CONTEXT_PATH);
servletPath = request.getAttribute(INCLUDE_SERVLET_PATH);
pathInfo = request.getAttribute(INCLUDE_PATH_INFO);
queryString = request.getAttribute(INCLUDE_QUERY_STRING);
int qsPos = path.indexOf("?");
String newServletPath = path;
if (qsPos != -1) {
String newQueryString = newServletPath.substring(qsPos + 1);
newServletPath = newServletPath.substring(0, qsPos);
String encoding = QueryParameterUtils.getQueryParamEncoding(servletRequestContext.getExchange());
Map<String, Deque<String>> newQueryParameters = QueryParameterUtils.mergeQueryParametersWithNewQueryString(queryParameters, newQueryString, encoding);
requestImpl.setQueryParameters(newQueryParameters);
requestImpl.setAttribute(INCLUDE_QUERY_STRING, newQueryString);
} else {
requestImpl.setAttribute(INCLUDE_QUERY_STRING, "");
}
String newRequestUri = servletContext.getContextPath() + newServletPath;
requestImpl.setAttribute(INCLUDE_REQUEST_URI, newRequestUri);
requestImpl.setAttribute(INCLUDE_CONTEXT_PATH, servletContext.getContextPath());
requestImpl.setAttribute(INCLUDE_SERVLET_PATH, pathMatch.getMatched());
requestImpl.setAttribute(INCLUDE_PATH_INFO, pathMatch.getRemaining());
}
boolean inInclude = responseImpl.isInsideInclude();
responseImpl.setInsideInclude(true);
DispatcherType oldDispatcherType = servletRequestContext.getDispatcherType();
ServletContextImpl oldContext = requestImpl.getServletContext();
try {
requestImpl.setServletContext(servletContext);
responseImpl.setServletContext(servletContext);
try {
servletRequestContext.setServletRequest(request);
servletRequestContext.setServletResponse(response);
servletContext.getDeployment().getServletDispatcher().dispatchToServlet(requestImpl.getExchange(), chain, DispatcherType.INCLUDE);
} catch (ServletException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
} finally {
responseImpl.setInsideInclude(inInclude);
requestImpl.setServletContext(oldContext);
responseImpl.setServletContext(oldContext);
servletRequestContext.setServletRequest(oldRequest);
servletRequestContext.setServletResponse(oldResponse);
servletRequestContext.setDispatcherType(oldDispatcherType);
if (!named) {
requestImpl.setAttribute(INCLUDE_REQUEST_URI, requestUri);
requestImpl.setAttribute(INCLUDE_CONTEXT_PATH, contextPath);
requestImpl.setAttribute(INCLUDE_SERVLET_PATH, servletPath);
requestImpl.setAttribute(INCLUDE_PATH_INFO, pathInfo);
requestImpl.setAttribute(INCLUDE_QUERY_STRING, queryString);
requestImpl.setQueryParameters(queryParameters);
}
}
}
use of javax.servlet.ServletRequest in project undertow by undertow-io.
the class RequestDispatcherImpl method forwardImpl.
private void forwardImpl(ServletRequest request, ServletResponse response, ServletRequestContext servletRequestContext) throws ServletException, IOException {
final HttpServletRequestImpl requestImpl = servletRequestContext.getOriginalRequest();
final HttpServletResponseImpl responseImpl = servletRequestContext.getOriginalResponse();
if (!servletContext.getDeployment().getDeploymentInfo().isAllowNonStandardWrappers()) {
if (servletRequestContext.getOriginalRequest() != request) {
if (!(request instanceof ServletRequestWrapper)) {
throw UndertowServletMessages.MESSAGES.requestWasNotOriginalOrWrapper(request);
}
}
if (servletRequestContext.getOriginalResponse() != response) {
if (!(response instanceof ServletResponseWrapper)) {
throw UndertowServletMessages.MESSAGES.responseWasNotOriginalOrWrapper(response);
}
}
}
response.resetBuffer();
final ServletRequest oldRequest = servletRequestContext.getServletRequest();
final ServletResponse oldResponse = servletRequestContext.getServletResponse();
Map<String, Deque<String>> queryParameters = requestImpl.getQueryParameters();
request.removeAttribute(INCLUDE_REQUEST_URI);
request.removeAttribute(INCLUDE_CONTEXT_PATH);
request.removeAttribute(INCLUDE_SERVLET_PATH);
request.removeAttribute(INCLUDE_PATH_INFO);
request.removeAttribute(INCLUDE_QUERY_STRING);
if (!named) {
//only update if this is the first forward
if (request.getAttribute(FORWARD_REQUEST_URI) == null) {
requestImpl.setAttribute(FORWARD_REQUEST_URI, requestImpl.getRequestURI());
requestImpl.setAttribute(FORWARD_CONTEXT_PATH, requestImpl.getContextPath());
requestImpl.setAttribute(FORWARD_SERVLET_PATH, requestImpl.getServletPath());
requestImpl.setAttribute(FORWARD_PATH_INFO, requestImpl.getPathInfo());
requestImpl.setAttribute(FORWARD_QUERY_STRING, requestImpl.getQueryString());
}
int qsPos = path.indexOf("?");
String newServletPath = path;
if (qsPos != -1) {
String newQueryString = newServletPath.substring(qsPos + 1);
newServletPath = newServletPath.substring(0, qsPos);
String encoding = QueryParameterUtils.getQueryParamEncoding(servletRequestContext.getExchange());
Map<String, Deque<String>> newQueryParameters = QueryParameterUtils.mergeQueryParametersWithNewQueryString(queryParameters, newQueryString, encoding);
requestImpl.getExchange().setQueryString(newQueryString);
requestImpl.setQueryParameters(newQueryParameters);
}
String newRequestUri = servletContext.getContextPath() + newServletPath;
requestImpl.getExchange().setRelativePath(newServletPath);
requestImpl.getExchange().setRequestPath(newRequestUri);
requestImpl.getExchange().setRequestURI(newRequestUri);
requestImpl.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY).setServletPathMatch(pathMatch);
requestImpl.setServletContext(servletContext);
responseImpl.setServletContext(servletContext);
}
try {
try {
servletRequestContext.setServletRequest(request);
servletRequestContext.setServletResponse(response);
if (named) {
servletContext.getDeployment().getServletDispatcher().dispatchToServlet(requestImpl.getExchange(), chain, DispatcherType.FORWARD);
} else {
servletContext.getDeployment().getServletDispatcher().dispatchToPath(requestImpl.getExchange(), pathMatch, DispatcherType.FORWARD);
}
//if we are not in an async or error dispatch then we close the response
if (!request.isAsyncStarted()) {
if (response instanceof HttpServletResponseImpl) {
responseImpl.closeStreamAndWriter();
} else {
try {
final PrintWriter writer = response.getWriter();
writer.flush();
writer.close();
} catch (IllegalStateException e) {
final ServletOutputStream outputStream = response.getOutputStream();
outputStream.flush();
outputStream.close();
}
}
}
} catch (ServletException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
} finally {
servletRequestContext.setServletRequest(oldRequest);
servletRequestContext.setServletResponse(oldResponse);
}
}
Aggregations