/** * 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); } } }
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(); }
patterns = servlet.value();
patterns = servlet.value();
patterns = servlet.value();
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()); } }
@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; }
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); } }
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()); }
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()); } }
boolean valueAttributeConfigured = webServlet.value().length > 0; boolean urlPatternsAttributeConfigured = webServlet.urlPatterns().length > 0; if (valueAttributeConfigured && urlPatternsAttributeConfigured) { String[] urlPatterns = valueAttributeConfigured ? webServlet.value() : webServlet.urlPatterns(); if (ServletMergeHandler.isServletConfigured(servletName, mergeContext)) { Servlet targetServlet = ServletMergeHandler.getServlet(servletName, mergeContext);
String[] urlPatterns = webServletAn.urlPatterns(); if (urlPatterns == null || urlPatterns.length == 0) { urlPatterns = webServletAn.value();
servletInfo.setAsyncSupported(webServlet.asyncSupported()); servletInfo.addMappings(webServlet.value()); servletInfo.addMappings(webServlet.urlPatterns());