void registerServlets(ServletContextHandler context, Class<? extends HttpServlet> type) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { WebServlet info = type.getAnnotation(WebServlet.class); for (String pattern : info.urlPatterns()) { HttpServlet servlet = type.getConstructor().newInstance(); context.addServlet(new ServletHolder(servlet), pattern); } }
protected void printServletInfo() { Class<?> clazz = this.getClass(); WebServlet servlet = clazz.getAnnotation(WebServlet.class); if (servlet == null) { return; } String name = servlet.name(); String uri = StringUtils.arrayToDelimitedString(servlet.urlPatterns(), ","); System.err.println("WebServlet name:" + name + "\turi:" + uri + "\tclass:" + clazz.getName()); }
PwmServletDefinition( final Class<? extends PwmServlet> pwmServletClass, final Class<? extends PwmSessionBean> pwmSessionBeanClass, final Flag... flags ) { this.pwmServletClass = pwmServletClass; this.pwmSessionBeanClass = pwmSessionBeanClass; final EnumSet flagSet = EnumSet.noneOf( Flag.class ); Collections.addAll( flagSet, flags ); this.flags = Collections.unmodifiableSet( flagSet ); try { this.patterns = getWebServletAnnotation( pwmServletClass ).urlPatterns(); } catch ( Exception e ) { throw new IllegalStateException( "error initializing PwmServletInfo value " + this.toString() + ", error: " + e.getMessage() ); } final String firstPattern = patterns[ 0 ]; final int lastSlash = firstPattern.lastIndexOf( "/" ); servletUrl = firstPattern.substring( lastSlash + 1 ); }
/** * Registers a servlet in this application context * @param servletClass servlet class to be registered. Given class must be annotated with {@linkplain WebServlet} annotation. * @throws IllegalArgumentException if given class isn't annotated with a {@linkplain WebServlet} annotation and * does not have URL mapping */ public void registerServlet(Class<? extends Servlet> servletClass) throws IllegalArgumentException { WebServlet[] annotations = servletClass.getAnnotationsByType(WebServlet.class); if (annotations.length == 0) throw new IllegalArgumentException("Servlet class does not have a WebServlet annotation"); for (WebServlet annotation : annotations) { String[] urlPatterns = annotation.value(); if (urlPatterns.length == 0) urlPatterns = annotation.urlPatterns(); if (urlPatterns.length == 0) throw new IllegalArgumentException("Missing urlPatterns"); for (String urlPattern : urlPatterns) { registerServlet(servletClass, urlPattern); } } }
.filter(ws -> ws.urlPatterns().length > 0) .count();
public ServletAnnotationScanner(Class<?> clazz) { WebServlet annotation = (WebServlet) clazz.getAnnotation(WebServlet.class); if (annotation == null) { return; } scanned = true; multiPartConfigAnnotation = (MultipartConfig) clazz.getAnnotation(MultipartConfig.class); if (annotation.urlPatterns().length > 0 && annotation.value().length > 0) { log.warn(clazz.getName() + " defines both @WebServlet.value and @WebServlet.urlPatterns"); return; } 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; } servletName = (annotation.name().equals("") ? clazz .getName() : annotation.name()); webInitParams = annotation.initParams(); asyncSupported = annotation.asyncSupported(); loadOnStartup = annotation.loadOnStartup(); }
public ServletAnnotationScanner(Class<?> clazz) { WebServlet annotation = (WebServlet) clazz.getAnnotation(WebServlet.class); if (annotation == null) { return; } scanned = true; multiPartConfigAnnotation = (MultipartConfig) clazz.getAnnotation(MultipartConfig.class); if (annotation.urlPatterns().length > 0 && annotation.value().length > 0) { log.warn(clazz.getName() + " defines both @WebServlet.value and @WebServlet.urlPatterns"); return; } 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; } servletName = (annotation.name().equals("") ? clazz .getName() : annotation.name()); webInitParams = annotation.initParams(); asyncSupported = annotation.asyncSupported(); loadOnStartup = annotation.loadOnStartup(); }
private void configure(final ServletEnvironment environment, final HttpServlet servlet, final Class<? extends HttpServlet> type, final String name, final WebServlet annotation) { final ServletRegistration.Dynamic mapping = environment.addServlet(name, servlet); final Set<String> clash = mapping .addMapping(annotation.urlPatterns().length > 0 ? annotation.urlPatterns() : annotation.value()); if (clash != null && !clash.isEmpty()) { final String msg = String.format( "Servlet registration %s clash with already installed servlets on paths: %s", type.getSimpleName(), Joiner.on(',').join(clash)); if (option(DenyServletRegistrationWithClash)) { throw new IllegalStateException(msg); } else { logger.warn(msg); } } if (annotation.initParams().length > 0) { for (WebInitParam param : annotation.initParams()) { mapping.setInitParameter(param.name(), param.value()); } } mapping.setAsyncSupported(annotation.asyncSupported()); } }
private void configure(final ServletEnvironment environment, final HttpServlet servlet, final Class<? extends HttpServlet> type, final String name, final WebServlet annotation) { final ServletRegistration.Dynamic mapping = environment.addServlet(name, servlet); final Set<String> clash = mapping .addMapping(annotation.urlPatterns().length > 0 ? annotation.urlPatterns() : annotation.value()); if (clash != null && !clash.isEmpty()) { final String msg = String.format( "Servlet registration %s clash with already installed servlets on paths: %s", type.getSimpleName(), Joiner.on(',').join(clash)); if (option(DenyServletRegistrationWithClash)) { throw new IllegalStateException(msg); } else { logger.warn(msg); } } if (annotation.initParams().length > 0) { for (WebInitParam param : annotation.initParams()) { mapping.setInitParameter(param.name(), param.value()); } } mapping.setAsyncSupported(annotation.asyncSupported()); } }
@Override public void install(final Environment environment, final HttpServlet instance) { final Class<? extends HttpServlet> extType = FeatureUtils.getInstanceClass(instance); final WebServlet annotation = FeatureUtils.getAnnotation(extType, WebServlet.class); final String[] patterns = annotation.urlPatterns().length > 0 ? annotation.urlPatterns() : annotation.value(); Preconditions.checkArgument(patterns.length > 0, "Servlet %s not specified url pattern for mapping", extType.getName()); final AdminContext context = FeatureUtils.getAnnotation(extType, AdminContext.class); final String name = WebUtils.getServletName(annotation, extType); reporter.line("%-15s %-5s %-2s (%s) %s", Joiner.on(",").join(patterns), WebUtils.getAsyncMarker(annotation), WebUtils.getContextMarkers(context), extType.getName(), name); if (WebUtils.isForMain(context)) { configure(environment.servlets(), instance, extType, name, annotation); } if (WebUtils.isForAdmin(context)) { configure(environment.admin(), instance, extType, name, annotation); } }
@Override public void install(final Environment environment, final HttpServlet instance) { final Class<? extends HttpServlet> extType = FeatureUtils.getInstanceClass(instance); final WebServlet annotation = FeatureUtils.getAnnotation(extType, WebServlet.class); final String[] patterns = annotation.urlPatterns().length > 0 ? annotation.urlPatterns() : annotation.value(); Preconditions.checkArgument(patterns.length > 0, "Servlet %s not specified url pattern for mapping", extType.getName()); final AdminContext context = FeatureUtils.getAnnotation(extType, AdminContext.class); final String name = WebUtils.getServletName(annotation, extType); reporter.line("%-15s %-5s %-2s (%s) %s", Joiner.on(",").join(patterns), WebUtils.getAsyncMarker(annotation), WebUtils.getContextMarkers(context), extType.getName(), name); if (WebUtils.isForMain(context)) { configure(environment.servlets(), instance, extType, name, annotation); } if (WebUtils.isForAdmin(context)) { configure(environment.admin(), instance, extType, name, annotation); } }
@SuppressWarnings("unchecked") private List<ServletDefinition> detectServlets(Collection<Class<?>> servletClasses) { List<ServletDefinition> servletDefinitions = new ArrayList<>(); for (Class<?> candidate : servletClasses) { if (Servlet.class.isAssignableFrom(candidate)) { Class<? extends Servlet> servletClass = (Class<? extends Servlet>) candidate; WebServlet annotation = servletClass.getAnnotation(WebServlet.class); ServletDefinition servletDefinition = new ServletDefinition( Strings.isNullOrEmpty(annotation.name()) ? servletClass.getCanonicalName() : annotation.name(), servletClass ); servletDefinition.setAsyncSupported(annotation.asyncSupported()); if (annotation.value().length > 0) { servletDefinition.addMappings(annotation.value()); } if (annotation.urlPatterns().length > 0) { servletDefinition.addMappings(annotation.urlPatterns()); } servletDefinition.setLoadOnStartup(annotation.loadOnStartup()); servletDefinition.addInitParameters(convert(annotation.initParams())); servletDefinitions.add(servletDefinition); } } return servletDefinitions; }
final String[] urlPatterns = annotation.urlPatterns(); for (String urlPattern : urlPatterns) { servletInfo.addMapping(urlPattern);
String[] urls = annotation.value(); if (urls.length == 0) { urls = annotation.urlPatterns();
public void create(WebServlet webServlet) { if (webServlet.value().length > 0 && webServlet.urlPatterns().length == 0) { for (String url : webServlet.value()) { addURLPattern(url); // XXX: support addURLRegexp? } } else if (webServlet.urlPatterns().length > 0 && webServlet.value().length == 0) { for (String url : webServlet.urlPatterns()) { addURLPattern(url); // XXX: support addURLRegexp? } } else { throw new ConfigException(L.l("Annotation @WebServlet at '{0}' must specify either value or urlPatterns", getServletClassName())); } for (WebInitParam initParam : webServlet.initParams()) { setInitParam(initParam.name(), initParam.value()); //omit description } setLoadOnStartup(webServlet.loadOnStartup()); setAsyncSupported(webServlet.asyncSupported()); }
servlets.add(servlet); metaData.setServlets(servlets); if (webServlet.urlPatterns() != null || webServlet.value() != null) servletMapping.setServletName(servletName); List<String> urlPatterns = new ArrayList<String>(); if (webServlet.urlPatterns() != null) for (String urlPattern : webServlet.urlPatterns())
private void procesServlets() { Consumer<Class<? extends HttpServlet>> c = servlet -> { WebServlet webServlet = ClassUtils.getAnnotation(servlet, WebServlet.class); if(webServlet != null) { ServletDescriptor servletDescriptor = new ServletDescriptor(webServlet.name(), webServlet.value(), mapUrls(webServlet.urlPatterns()), webServlet.loadOnStartup(), webServlet.initParams(),webServlet.asyncSupported(),servlet); webServer.addServlet(servletDescriptor); } }; extension.processServlets(c); }
private void procesServlets() { Consumer<Class<? extends HttpServlet>> c = servlet -> { WebServlet webServlet = ClassUtils.getAnnotation(servlet, WebServlet.class); if(webServlet != null) { ServletDescriptor servletDescriptor = new ServletDescriptor(webServlet.name(), webServlet.value(), mapUrls(webServlet.urlPatterns()), webServlet.loadOnStartup(), webServlet.initParams(),webServlet.asyncSupported(),servlet); webServer.addServlet(servletDescriptor); } }; extension.processServlets(c); }
public void loadAnnotation(WebServlet ws) { this.addMapping(ws.value()); this.addMapping(ws.urlPatterns()); this.setLoadOnStartup(ws.loadOnStartup()); this.setAsyncSupported(ws.asyncSupported()); for (WebInitParam initParam : ws.initParams()) { this.setInitParameter(initParam.name(), initParam.value()); } }
servletInfo.addMappings(webServlet.urlPatterns());