use of javax.servlet.DispatcherType in project jetty.project by eclipse.
the class WebFilterAnnotation method apply.
/**
* @see DiscoveredAnnotation#apply()
*/
public void apply() {
// TODO verify against rules for annotation v descriptor
Class clazz = getTargetClass();
if (clazz == null) {
LOG.warn(_className + " cannot be loaded");
return;
}
//Servlet Spec 8.1.2
if (!Filter.class.isAssignableFrom(clazz)) {
LOG.warn(clazz.getName() + " is not assignable from javax.servlet.Filter");
return;
}
MetaData metaData = _context.getMetaData();
WebFilter filterAnnotation = (WebFilter) clazz.getAnnotation(WebFilter.class);
if (filterAnnotation.value().length > 0 && filterAnnotation.urlPatterns().length > 0) {
LOG.warn(clazz.getName() + " defines both @WebFilter.value and @WebFilter.urlPatterns");
return;
}
String name = (filterAnnotation.filterName().equals("") ? clazz.getName() : filterAnnotation.filterName());
String[] urlPatterns = filterAnnotation.value();
if (urlPatterns.length == 0)
urlPatterns = filterAnnotation.urlPatterns();
FilterHolder holder = _context.getServletHandler().getFilter(name);
if (holder == null) {
//Filter with this name does not already exist, so add it
holder = _context.getServletHandler().newFilterHolder(new Source(Source.Origin.ANNOTATION, clazz.getName()));
holder.setName(name);
holder.setHeldClass(clazz);
metaData.setOrigin(name + ".filter.filter-class", filterAnnotation, clazz);
holder.setDisplayName(filterAnnotation.displayName());
metaData.setOrigin(name + ".filter.display-name", filterAnnotation, clazz);
for (WebInitParam ip : filterAnnotation.initParams()) {
holder.setInitParameter(ip.name(), ip.value());
metaData.setOrigin(name + ".filter.init-param." + ip.name(), ip, clazz);
}
FilterMapping mapping = new FilterMapping();
mapping.setFilterName(holder.getName());
if (urlPatterns.length > 0) {
ArrayList<String> paths = new ArrayList<String>();
for (String s : urlPatterns) {
paths.add(ServletPathSpec.normalize(s));
}
mapping.setPathSpecs(paths.toArray(new String[paths.size()]));
}
if (filterAnnotation.servletNames().length > 0) {
ArrayList<String> names = new ArrayList<String>();
for (String s : filterAnnotation.servletNames()) {
names.add(s);
}
mapping.setServletNames(names.toArray(new String[names.size()]));
}
EnumSet<DispatcherType> dispatcherSet = EnumSet.noneOf(DispatcherType.class);
for (DispatcherType d : filterAnnotation.dispatcherTypes()) {
dispatcherSet.add(d);
}
mapping.setDispatcherTypes(dispatcherSet);
metaData.setOrigin(name + ".filter.mappings", filterAnnotation, clazz);
holder.setAsyncSupported(filterAnnotation.asyncSupported());
metaData.setOrigin(name + ".filter.async-supported", filterAnnotation, clazz);
_context.getServletHandler().addFilter(holder);
_context.getServletHandler().addFilterMapping(mapping);
} else {
//init-params of the same name.
for (WebInitParam ip : filterAnnotation.initParams()) {
//if (holder.getInitParameter(ip.name()) == null)
if (metaData.getOrigin(name + ".filter.init-param." + ip.name()) == Origin.NotSet) {
holder.setInitParameter(ip.name(), ip.value());
metaData.setOrigin(name + ".filter.init-param." + ip.name(), ip, clazz);
}
}
FilterMapping[] mappings = _context.getServletHandler().getFilterMappings();
boolean mappingExists = false;
if (mappings != null) {
for (FilterMapping m : mappings) {
if (m.getFilterName().equals(name)) {
mappingExists = true;
break;
}
}
}
//from the annotation
if (!mappingExists) {
FilterMapping mapping = new FilterMapping();
mapping.setFilterName(holder.getName());
if (urlPatterns.length > 0) {
ArrayList<String> paths = new ArrayList<String>();
for (String s : urlPatterns) {
paths.add(ServletPathSpec.normalize(s));
}
mapping.setPathSpecs(paths.toArray(new String[paths.size()]));
}
if (filterAnnotation.servletNames().length > 0) {
ArrayList<String> names = new ArrayList<String>();
for (String s : filterAnnotation.servletNames()) {
names.add(s);
}
mapping.setServletNames(names.toArray(new String[names.size()]));
}
EnumSet<DispatcherType> dispatcherSet = EnumSet.noneOf(DispatcherType.class);
for (DispatcherType d : filterAnnotation.dispatcherTypes()) {
dispatcherSet.add(d);
}
mapping.setDispatcherTypes(dispatcherSet);
_context.getServletHandler().addFilterMapping(mapping);
metaData.setOrigin(name + ".filter.mappings", filterAnnotation, clazz);
}
}
}
use of javax.servlet.DispatcherType in project jetty.project by eclipse.
the class ServletHandler method doScope.
@Override
public void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Get the base requests
final String old_servlet_path = baseRequest.getServletPath();
final String old_path_info = baseRequest.getPathInfo();
DispatcherType type = baseRequest.getDispatcherType();
ServletHolder servlet_holder = null;
UserIdentity.Scope old_scope = null;
// find the servlet
if (target.startsWith("/")) {
// Look for the servlet by path
MappedResource<ServletHolder> entry = getHolderEntry(target);
if (entry != null) {
PathSpec pathSpec = entry.getPathSpec();
servlet_holder = entry.getResource();
String servlet_path = pathSpec.getPathMatch(target);
String path_info = pathSpec.getPathInfo(target);
if (DispatcherType.INCLUDE.equals(type)) {
baseRequest.setAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH, servlet_path);
baseRequest.setAttribute(RequestDispatcher.INCLUDE_PATH_INFO, path_info);
} else {
baseRequest.setServletPath(servlet_path);
baseRequest.setPathInfo(path_info);
}
}
} else {
// look for a servlet by name!
servlet_holder = _servletNameMap.get(target);
}
if (LOG.isDebugEnabled())
LOG.debug("servlet {}|{}|{} -> {}", baseRequest.getContextPath(), baseRequest.getServletPath(), baseRequest.getPathInfo(), servlet_holder);
try {
// Do the filter/handling thang
old_scope = baseRequest.getUserIdentityScope();
baseRequest.setUserIdentityScope(servlet_holder);
nextScope(target, baseRequest, request, response);
} finally {
if (old_scope != null)
baseRequest.setUserIdentityScope(old_scope);
if (!(DispatcherType.INCLUDE.equals(type))) {
baseRequest.setServletPath(old_servlet_path);
baseRequest.setPathInfo(old_path_info);
}
}
}
use of javax.servlet.DispatcherType in project jetty.project by eclipse.
the class Dispatcher method include.
@Override
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
Request baseRequest = Request.getBaseRequest(request);
if (!(request instanceof HttpServletRequest))
request = new ServletRequestHttpWrapper(request);
if (!(response instanceof HttpServletResponse))
response = new ServletResponseHttpWrapper(response);
final DispatcherType old_type = baseRequest.getDispatcherType();
final Attributes old_attr = baseRequest.getAttributes();
final MultiMap<String> old_query_params = baseRequest.getQueryParameters();
try {
baseRequest.setDispatcherType(DispatcherType.INCLUDE);
baseRequest.getResponse().include();
if (_named != null) {
_contextHandler.handle(_named, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
} else {
IncludeAttributes attr = new IncludeAttributes(old_attr);
attr._requestURI = _uri.getPath();
attr._contextPath = _contextHandler.getContextPath();
// set by ServletHandler
attr._servletPath = null;
attr._pathInfo = _pathInContext;
attr._query = _uri.getQuery();
if (attr._query != null)
baseRequest.mergeQueryParameters(baseRequest.getQueryString(), attr._query, false);
baseRequest.setAttributes(attr);
_contextHandler.handle(_pathInContext, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
}
} finally {
baseRequest.setAttributes(old_attr);
baseRequest.getResponse().included();
baseRequest.setQueryParameters(old_query_params);
baseRequest.resetParameters();
baseRequest.setDispatcherType(old_type);
}
}
use of javax.servlet.DispatcherType in project jetty.project by eclipse.
the class Dispatcher method forward.
protected void forward(ServletRequest request, ServletResponse response, DispatcherType dispatch) throws ServletException, IOException {
Request baseRequest = Request.getBaseRequest(request);
Response base_response = baseRequest.getResponse();
base_response.resetForForward();
if (!(request instanceof HttpServletRequest))
request = new ServletRequestHttpWrapper(request);
if (!(response instanceof HttpServletResponse))
response = new ServletResponseHttpWrapper(response);
final HttpURI old_uri = baseRequest.getHttpURI();
final String old_context_path = baseRequest.getContextPath();
final String old_servlet_path = baseRequest.getServletPath();
final String old_path_info = baseRequest.getPathInfo();
final MultiMap<String> old_query_params = baseRequest.getQueryParameters();
final Attributes old_attr = baseRequest.getAttributes();
final DispatcherType old_type = baseRequest.getDispatcherType();
try {
baseRequest.setDispatcherType(dispatch);
if (_named != null) {
_contextHandler.handle(_named, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
} else {
ForwardAttributes attr = new ForwardAttributes(old_attr);
//for queryString is allowed to be null, but cannot be null for the other values.
if (old_attr.getAttribute(FORWARD_REQUEST_URI) != null) {
attr._pathInfo = (String) old_attr.getAttribute(FORWARD_PATH_INFO);
attr._query = (String) old_attr.getAttribute(FORWARD_QUERY_STRING);
attr._requestURI = (String) old_attr.getAttribute(FORWARD_REQUEST_URI);
attr._contextPath = (String) old_attr.getAttribute(FORWARD_CONTEXT_PATH);
attr._servletPath = (String) old_attr.getAttribute(FORWARD_SERVLET_PATH);
} else {
attr._pathInfo = old_path_info;
attr._query = old_uri.getQuery();
attr._requestURI = old_uri.getPath();
attr._contextPath = old_context_path;
attr._servletPath = old_servlet_path;
}
HttpURI uri = new HttpURI(old_uri.getScheme(), old_uri.getHost(), old_uri.getPort(), _uri.getPath(), _uri.getParam(), _uri.getQuery(), _uri.getFragment());
baseRequest.setHttpURI(uri);
baseRequest.setContextPath(_contextHandler.getContextPath());
baseRequest.setServletPath(null);
baseRequest.setPathInfo(_pathInContext);
if (_uri.getQuery() != null || old_uri.getQuery() != null)
baseRequest.mergeQueryParameters(old_uri.getQuery(), _uri.getQuery(), true);
baseRequest.setAttributes(attr);
_contextHandler.handle(_pathInContext, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
if (!baseRequest.getHttpChannelState().isAsync())
commitResponse(response, baseRequest);
}
} finally {
baseRequest.setHttpURI(old_uri);
baseRequest.setContextPath(old_context_path);
baseRequest.setServletPath(old_servlet_path);
baseRequest.setPathInfo(old_path_info);
baseRequest.setQueryParameters(old_query_params);
baseRequest.resetParameters();
baseRequest.setAttributes(old_attr);
baseRequest.setDispatcherType(old_type);
}
}
use of javax.servlet.DispatcherType in project jetty.project by eclipse.
the class AsyncDelayHandler method handle.
/* ------------------------------------------------------------ */
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!isStarted() || _handler == null)
return;
// Get the dispatcher types
DispatcherType ctype = baseRequest.getDispatcherType();
DispatcherType dtype = (DispatcherType) baseRequest.getAttribute(AHW_ATTR);
Object async_context_path = null;
Object async_path_info = null;
Object async_query_string = null;
Object async_request_uri = null;
Object async_servlet_path = null;
// Is this request a restarted one?
boolean restart = false;
if (dtype != null) {
// fake the dispatch type to the original
baseRequest.setAttribute(AHW_ATTR, null);
baseRequest.setDispatcherType(dtype);
restart = true;
async_context_path = baseRequest.getAttribute(AsyncContext.ASYNC_CONTEXT_PATH);
baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH, null);
async_path_info = baseRequest.getAttribute(AsyncContext.ASYNC_PATH_INFO);
baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO, null);
async_query_string = baseRequest.getAttribute(AsyncContext.ASYNC_QUERY_STRING);
baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING, null);
async_request_uri = baseRequest.getAttribute(AsyncContext.ASYNC_REQUEST_URI);
baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI, null);
async_servlet_path = baseRequest.getAttribute(AsyncContext.ASYNC_SERVLET_PATH);
baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH, null);
}
// Should we handle this request now?
if (!startHandling(baseRequest, restart)) {
// No, so go async and remember dispatch type
AsyncContext context = baseRequest.startAsync();
baseRequest.setAttribute(AHW_ATTR, ctype);
delayHandling(baseRequest, context);
return;
}
// Handle the request
try {
_handler.handle(target, baseRequest, request, response);
} finally {
if (restart) {
// reset the request
baseRequest.setDispatcherType(ctype);
baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH, async_context_path);
baseRequest.setAttribute(AsyncContext.ASYNC_PATH_INFO, async_path_info);
baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING, async_query_string);
baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI, async_request_uri);
baseRequest.setAttribute(AsyncContext.ASYNC_SERVLET_PATH, async_servlet_path);
}
// signal the request is leaving the handler
endHandling(baseRequest);
}
}
Aggregations