use of javax.servlet.annotation.WebInitParam in project jetty.project by eclipse.
the class WebServletAnnotation method apply.
/**
* @see DiscoveredAnnotation#apply()
*/
public void apply() {
//TODO check this algorithm with new rules for applying descriptors and annotations in order
Class<? extends Servlet> clazz = (Class<? extends Servlet>) getTargetClass();
if (clazz == null) {
LOG.warn(_className + " cannot be loaded");
return;
}
//Servlet Spec 8.1.1
if (!HttpServlet.class.isAssignableFrom(clazz)) {
LOG.warn(clazz.getName() + " is not assignable from javax.servlet.http.HttpServlet");
return;
}
WebServlet annotation = (WebServlet) clazz.getAnnotation(WebServlet.class);
if (annotation.urlPatterns().length > 0 && annotation.value().length > 0) {
LOG.warn(clazz.getName() + " defines both @WebServlet.value and @WebServlet.urlPatterns");
return;
}
String[] urlPatterns = annotation.value();
if (urlPatterns.length == 0)
urlPatterns = annotation.urlPatterns();
if (urlPatterns.length == 0) {
LOG.warn(clazz.getName() + " defines neither @WebServlet.value nor @WebServlet.urlPatterns");
return;
}
//canonicalize the patterns
ArrayList<String> urlPatternList = new ArrayList<String>();
for (String p : urlPatterns) urlPatternList.add(ServletPathSpec.normalize(p));
String servletName = (annotation.name().equals("") ? clazz.getName() : annotation.name());
MetaData metaData = _context.getMetaData();
//the new mapping
ServletMapping mapping = null;
//Find out if a <servlet> already exists with this name
ServletHolder[] holders = _context.getServletHandler().getServlets();
ServletHolder holder = null;
if (holders != null) {
for (ServletHolder h : holders) {
if (h.getName() != null && servletName.equals(h.getName())) {
holder = h;
break;
}
}
}
//handle creation/completion of a servlet
if (holder == null) {
//No servlet of this name has already been defined, either by a descriptor
//or another annotation (which would be impossible).
Source source = new Source(Source.Origin.ANNOTATION, clazz.getName());
holder = _context.getServletHandler().newServletHolder(source);
holder.setHeldClass(clazz);
metaData.setOrigin(servletName + ".servlet.servlet-class", annotation, clazz);
holder.setName(servletName);
holder.setDisplayName(annotation.displayName());
metaData.setOrigin(servletName + ".servlet.display-name", annotation, clazz);
holder.setInitOrder(annotation.loadOnStartup());
metaData.setOrigin(servletName + ".servlet.load-on-startup", annotation, clazz);
holder.setAsyncSupported(annotation.asyncSupported());
metaData.setOrigin(servletName + ".servlet.async-supported", annotation, clazz);
for (WebInitParam ip : annotation.initParams()) {
holder.setInitParameter(ip.name(), ip.value());
metaData.setOrigin(servletName + ".servlet.init-param." + ip.name(), ip, clazz);
}
_context.getServletHandler().addServlet(holder);
mapping = new ServletMapping(source);
mapping.setServletName(holder.getName());
mapping.setPathSpecs(LazyList.toStringArray(urlPatternList));
} else {
//can complete it, see http://java.net/jira/browse/SERVLET_SPEC-42
if (holder.getClassName() == null)
holder.setClassName(clazz.getName());
if (holder.getHeldClass() == null)
holder.setHeldClass(clazz);
//if not, add it
for (WebInitParam ip : annotation.initParams()) {
if (metaData.getOrigin(servletName + ".servlet.init-param." + ip.name()) == Origin.NotSet) {
holder.setInitParameter(ip.name(), ip.value());
metaData.setOrigin(servletName + ".servlet.init-param." + ip.name(), ip, clazz);
}
}
//check the url-patterns
//ServletSpec 3.0 p81 If a servlet already has url mappings from a
//webxml or fragment descriptor the annotation is ignored.
//However, we want to be able to replace mappings that were given in webdefault.xml
List<ServletMapping> existingMappings = getServletMappingsForServlet(servletName);
//about processing these url mappings
if (existingMappings.isEmpty() || !containsNonDefaultMappings(existingMappings)) {
mapping = new ServletMapping(new Source(Source.Origin.ANNOTATION, clazz.getName()));
mapping.setServletName(servletName);
mapping.setPathSpecs(LazyList.toStringArray(urlPatternList));
}
}
//servlet
if (mapping != null) {
//url mapping was permitted by annotation processing rules
//take a copy of the existing servlet mappings that we can iterate over and remove from. This is
//because the ServletHandler interface does not support removal of individual mappings.
List<ServletMapping> allMappings = ArrayUtil.asMutableList(_context.getServletHandler().getServletMappings());
// guard against duplicate path mapping here: that is the job of the ServletHandler
for (String p : urlPatternList) {
ServletMapping existingMapping = _context.getServletHandler().getServletMapping(p);
if (existingMapping != null && existingMapping.isDefault()) {
String[] updatedPaths = ArrayUtil.removeFromArray(existingMapping.getPathSpecs(), p);
//if we removed the last path from a servletmapping, delete the servletmapping
if (updatedPaths == null || updatedPaths.length == 0) {
boolean success = allMappings.remove(existingMapping);
if (LOG.isDebugEnabled())
LOG.debug("Removed empty mapping {} from defaults descriptor success:{}", existingMapping, success);
} else {
existingMapping.setPathSpecs(updatedPaths);
if (LOG.isDebugEnabled())
LOG.debug("Removed path {} from mapping {} from defaults descriptor ", p, existingMapping);
}
}
_context.getMetaData().setOrigin(servletName + ".servlet.mapping." + p, annotation, clazz);
}
allMappings.add(mapping);
_context.getServletHandler().setServletMappings(allMappings.toArray(new ServletMapping[allMappings.size()]));
}
}
use of javax.servlet.annotation.WebInitParam 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.annotation.WebInitParam in project dropwizard-guicey by xvik.
the class WebFilterInstaller method configure.
private void configure(final ServletEnvironment environment, final Filter filter, final String name, final WebFilter annotation) {
final FilterRegistration.Dynamic mapping = environment.addFilter(name, filter);
final EnumSet<DispatcherType> dispatcherTypes = EnumSet.copyOf(Arrays.asList(annotation.dispatcherTypes()));
if (annotation.servletNames().length > 0) {
mapping.addMappingForServletNames(dispatcherTypes, false, annotation.servletNames());
} else {
final String[] urlPatterns = annotation.urlPatterns().length > 0 ? annotation.urlPatterns() : annotation.value();
mapping.addMappingForUrlPatterns(dispatcherTypes, false, urlPatterns);
}
if (annotation.initParams().length > 0) {
for (WebInitParam param : annotation.initParams()) {
mapping.setInitParameter(param.name(), param.value());
}
}
mapping.setAsyncSupported(annotation.asyncSupported());
}
use of javax.servlet.annotation.WebInitParam in project Payara by payara.
the class WebFilterHandler method processAnnotation.
private HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, WebBundleDescriptor webBundleDesc) throws AnnotationProcessorException {
Class filterClass = (Class) ainfo.getAnnotatedElement();
if (!javax.servlet.Filter.class.isAssignableFrom(filterClass)) {
log(Level.SEVERE, ainfo, localStrings.getLocalString("web.deployment.annotation.handlers.needtoimpl", "The Class {0} having annotation {1} need to implement the interface {2}.", new Object[] { filterClass.getName(), WebFilter.class.getName(), javax.servlet.Filter.class.getName() }));
return getDefaultFailedResult();
}
WebFilter webFilterAn = (WebFilter) ainfo.getAnnotation();
String filterName = webFilterAn.filterName();
if (filterName == null || filterName.length() == 0) {
filterName = filterClass.getName();
}
ServletFilterDescriptor servletFilterDesc = null;
for (ServletFilter sfDesc : webBundleDesc.getServletFilters()) {
if (filterName.equals(sfDesc.getName())) {
servletFilterDesc = (ServletFilterDescriptor) sfDesc;
break;
}
}
if (servletFilterDesc == null) {
servletFilterDesc = new ServletFilterDescriptor();
servletFilterDesc.setName(filterName);
webBundleDesc.addServletFilter(servletFilterDesc);
} else {
String filterImpl = servletFilterDesc.getClassName();
if (filterImpl != null && filterImpl.length() > 0 && !filterImpl.equals(filterClass.getName())) {
log(Level.SEVERE, ainfo, localStrings.getLocalString("web.deployment.annotation.handlers.filternamedontmatch", "The filter ''{0}'' has implementation ''{1}'' in xml. It does not match with ''{2}'' from annotation @{3}.", new Object[] { filterName, filterImpl, filterClass.getName(), WebFilter.class.getName() }));
return getDefaultFailedResult();
}
}
servletFilterDesc.setClassName(filterClass.getName());
if (servletFilterDesc.getDescription() == null || servletFilterDesc.getDescription().length() == 0) {
servletFilterDesc.setDescription(webFilterAn.description());
}
if (servletFilterDesc.hasSetDisplayName()) {
servletFilterDesc.setDisplayName(webFilterAn.displayName());
}
if (servletFilterDesc.getInitializationParameters().size() == 0) {
WebInitParam[] initParams = webFilterAn.initParams();
if (initParams != null && initParams.length > 0) {
for (WebInitParam initParam : initParams) {
servletFilterDesc.addInitializationParameter(new EnvironmentProperty(initParam.name(), initParam.value(), initParam.description()));
}
}
}
if (servletFilterDesc.getSmallIconUri() == null) {
servletFilterDesc.setSmallIconUri(webFilterAn.smallIcon());
}
if (servletFilterDesc.getLargeIconUri() == null) {
servletFilterDesc.setLargeIconUri(webFilterAn.largeIcon());
}
if (servletFilterDesc.isAsyncSupported() == null) {
servletFilterDesc.setAsyncSupported(webFilterAn.asyncSupported());
}
ServletFilterMapping servletFilterMappingDesc = null;
boolean hasUrlPattern = false;
boolean hasServletName = false;
for (ServletFilterMapping sfm : webBundleDesc.getServletFilterMappings()) {
if (filterName.equals(sfm.getName())) {
servletFilterMappingDesc = sfm;
hasUrlPattern = hasUrlPattern || (sfm.getUrlPatterns().size() > 0);
hasServletName = hasServletName || (sfm.getServletNames().size() > 0);
}
}
if (servletFilterMappingDesc == null) {
servletFilterMappingDesc = new ServletFilterMappingDescriptor();
servletFilterMappingDesc.setName(filterName);
webBundleDesc.addServletFilterMapping(servletFilterMappingDesc);
}
if (!hasUrlPattern) {
String[] urlPatterns = webFilterAn.urlPatterns();
if (urlPatterns == null || urlPatterns.length == 0) {
urlPatterns = webFilterAn.value();
}
// accept here as url patterns may be defined in top level xml
boolean validUrlPatterns = true;
if (urlPatterns != null && urlPatterns.length > 0) {
for (String up : urlPatterns) {
if (!URLPattern.isValid(up)) {
validUrlPatterns = false;
break;
}
servletFilterMappingDesc.addURLPattern(up);
}
}
if (!validUrlPatterns) {
String urlPatternString = (urlPatterns != null) ? Arrays.toString(urlPatterns) : "";
throw new IllegalArgumentException(localStrings.getLocalString("web.deployment.annotation.handlers.invalidUrlPatterns", "Invalid url patterns: {0}.", urlPatternString));
}
}
if (!hasServletName) {
String[] servletNames = webFilterAn.servletNames();
if (servletNames != null && servletNames.length > 0) {
for (String sn : servletNames) {
servletFilterMappingDesc.addServletName(sn);
}
}
}
if (servletFilterMappingDesc.getDispatchers().size() == 0) {
DispatcherType[] dispatcherTypes = webFilterAn.dispatcherTypes();
if (dispatcherTypes != null && dispatcherTypes.length > 0) {
for (DispatcherType dType : dispatcherTypes) {
servletFilterMappingDesc.addDispatcher(dType.name());
}
}
}
return getDefaultProcessedResult();
}
use of javax.servlet.annotation.WebInitParam in project Payara by payara.
the class WebServletHandler method processAnnotation.
private HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, WebComponentDescriptor webCompDesc) throws AnnotationProcessorException {
Class webCompClass = (Class) ainfo.getAnnotatedElement();
if (!HttpServlet.class.isAssignableFrom(webCompClass)) {
log(Level.SEVERE, ainfo, localStrings.getLocalString("web.deployment.annotation.handlers.needtoextend", "The Class {0} having annotation {1} need to be a derived class of {2}.", new Object[] { webCompClass.getName(), WebServlet.class.getName(), HttpServlet.class.getName() }));
return getDefaultFailedResult();
}
WebServlet webServletAn = (WebServlet) ainfo.getAnnotation();
String servletName = getServletName(webServletAn, webCompClass);
if (!servletName.equals(webCompDesc.getCanonicalName())) {
// skip the processing as it is not for given webCompDesc
return getDefaultProcessedResult();
}
String webCompImpl = webCompDesc.getWebComponentImplementation();
if (webCompImpl != null && webCompImpl.length() > 0 && (!webCompImpl.equals(webCompClass.getName()) || !webCompDesc.isServlet())) {
String messageKey = null;
String defaultMessage = null;
if (webCompDesc.isServlet()) {
messageKey = "web.deployment.annotation.handlers.servletimpldontmatch";
defaultMessage = "The servlet ''{0}'' has implementation ''{1}'' in xml. It does not match with ''{2}'' from annotation @{3}.";
} else {
messageKey = "web.deployment.annotation.handlers.servletimpljspdontmatch";
defaultMessage = "The servlet ''{0}'' is a jsp ''{1}'' in xml. It does not match with ''{2}'' from annotation @{3}.";
}
log(Level.SEVERE, ainfo, localStrings.getLocalString(messageKey, defaultMessage, new Object[] { webCompDesc.getCanonicalName(), webCompImpl, webCompClass.getName(), WebServlet.class.getName() }));
return getDefaultFailedResult();
}
webCompDesc.setServlet(true);
webCompDesc.setWebComponentImplementation(webCompClass.getName());
if (webCompDesc.getUrlPatternsSet().isEmpty()) {
String[] urlPatterns = webServletAn.urlPatterns();
if (urlPatterns == null || urlPatterns.length == 0) {
urlPatterns = webServletAn.value();
}
// no url patterns is accepted as it may be defined in top level xml
boolean validUrlPatterns = true;
if (urlPatterns != null && urlPatterns.length > 0) {
for (String up : urlPatterns) {
if (!URLPattern.isValid(up)) {
validUrlPatterns = false;
break;
}
webCompDesc.addUrlPattern(TranslatedConfigView.expandValue(up));
}
}
if (!validUrlPatterns) {
String urlPatternString = (urlPatterns != null) ? Arrays.toString(urlPatterns) : "";
throw new IllegalArgumentException(localStrings.getLocalString("web.deployment.annotation.handlers.invalidUrlPatterns", "Invalid url patterns for {0}: {1}.", new Object[] { webCompClass, urlPatternString }));
}
}
if (webCompDesc.getLoadOnStartUp() == null) {
webCompDesc.setLoadOnStartUp(webServletAn.loadOnStartup());
}
WebInitParam[] initParams = webServletAn.initParams();
if (initParams != null && initParams.length > 0) {
for (WebInitParam initParam : initParams) {
webCompDesc.addInitializationParameter(new EnvironmentProperty(initParam.name(), TranslatedConfigView.expandValue(initParam.value()), initParam.description()));
}
}
if (webCompDesc.getSmallIconUri() == null) {
webCompDesc.setSmallIconUri(webServletAn.smallIcon());
}
if (webCompDesc.getLargeIconUri() == null) {
webCompDesc.setLargeIconUri(webServletAn.largeIcon());
}
if (webCompDesc.getDescription() == null || webCompDesc.getDescription().length() == 0) {
webCompDesc.setDescription(webServletAn.description());
}
if (webCompDesc.getDisplayName() == null || webCompDesc.getDisplayName().length() == 0) {
webCompDesc.setDisplayName(webServletAn.displayName());
}
if (webCompDesc.isAsyncSupported() == null) {
webCompDesc.setAsyncSupported(webServletAn.asyncSupported());
}
return getDefaultProcessedResult();
}
Aggregations