"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(
"enterprise.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(
"enterprise.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());
}
}