/** * Return this previously allocated servlet to the pool of available * instances. If this servlet class does not implement SingleThreadModel, no * action is actually required. * * @param servlet * The servlet to be returned * * @exception ServletException * if a deallocation error occurs */ @Override public void deallocate(Servlet servlet) throws ServletException { // If not SingleThreadModel, no action is required if (!singleThreadModel) { countAllocated.decrementAndGet(); return; } // Unlock and free this instance synchronized (instancePool) { countAllocated.decrementAndGet(); instancePool.push(servlet); instancePool.notify(); } }
/** * onServletStop * * @param args */ @Override public void onServletStop(Object... args) { StandardWrapper sw = (StandardWrapper) args[0]; Servlet servlet = (Servlet) args[1]; InterceptSupport iSupport = InterceptSupport.instance(); InterceptContext context = iSupport.createInterceptContext(Event.BEFORE_SERVLET_DESTROY); context.put(InterceptConstants.SERVLET_INSTANCE, servlet); /** * NOTE: spring boot rewrite the tomcat webappclassloader, makes the addURL for nothing, then we can't do * anything on this we may use its webappclassloader's parent as the classloader */ context.put(InterceptConstants.WEBAPPLOADER, Thread.currentThread().getContextClassLoader().getParent()); context.put(InterceptConstants.CONTEXTPATH, sw.getServletContext().getContextPath()); iSupport.doIntercept(context); }
/** * @deprecated As of Java Servlet API 2.1, with no direct replacement. */ @Override @Deprecated public Servlet getServlet(String name) throws ServletException { if (SecurityUtil.isPackageProtectionEnabled()) { try { return (Servlet) invokeMethod(context, "getServlet", new Object[]{name}); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); if (t instanceof ServletException) { throw (ServletException) t; } return null; } } else { return context.getServlet(name); } }
@Override @SuppressWarnings("unchecked") // doPrivileged() returns the correct type public <T extends Servlet> T createServlet(Class<T> c) throws ServletException { if (SecurityUtil.isPackageProtectionEnabled()) { try { return (T) invokeMethod(context, "createServlet", new Object[] { c }); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); if (t instanceof ServletException) { throw (ServletException) t; } return null; } } else { return context.createServlet(c); } }
/** * Return this previously allocated servlet to the pool of available * instances. If this servlet class does not implement SingleThreadModel, * no action is actually required. * * @param servlet The servlet to be returned * * @exception ServletException if a deallocation error occurs */ @Override public void deallocate(Servlet servlet) throws ServletException { // If not SingleThreadModel, no action is required if (!singleThreadModel) { countAllocated.decrementAndGet(); return; } // Unlock and free this instance synchronized (instancePool) { countAllocated.decrementAndGet(); instancePool.push(servlet); instancePool.notify(); } }
/** * Notify all lifecycle event listeners that a particular event has * occurred for this Container. The default implementation performs * this notification synchronously using the calling thread. * * @param type Event type * @param servlet The relevant Servlet for this event * @param request The servlet request we are processing * @param response The servlet response we are processing * @param exception Exception that occurred */ public void fireInstanceEvent(String type, Servlet servlet, ServletRequest request, ServletResponse response, Throwable exception) { if (listeners.length == 0) return; InstanceEvent event = new InstanceEvent(wrapper, servlet, type, request, response, exception); InstanceListener interested[] = listeners; for (int i = 0; i < interested.length; i++) interested[i].instanceEvent(event); }
/** * Construct a new InstanceEvent with the specified parameters. This * constructor is used for processing servlet lifecycle events. * * @param wrapper Wrapper managing this servlet instance * @param servlet Servlet instance for which this event occurred * @param type Event type (required) * @param exception Exception that occurred */ public InstanceEvent(Wrapper wrapper, Servlet servlet, String type, Throwable exception) { super(wrapper); this.wrapper = wrapper; this.filter = null; this.servlet = servlet; this.type = type; this.exception = exception; }
public ServletInfo(final String name, final Class<? extends Servlet> servletClass) { if (name == null) { throw UndertowServletMessages.MESSAGES.paramCannotBeNull("name"); } if (servletClass == null) { throw UndertowServletMessages.MESSAGES.paramCannotBeNull("servletClass", "Servlet", name); } if (!Servlet.class.isAssignableFrom(servletClass)) { throw UndertowServletMessages.MESSAGES.servletMustImplementServlet(name, servletClass); } try { final Constructor<? extends Servlet> ctor = servletClass.getDeclaredConstructor(); ctor.setAccessible(true); this.instanceFactory = new ConstructorInstanceFactory(ctor); this.name = name; this.servletClass = servletClass; } catch (NoSuchMethodException e) { throw UndertowServletMessages.MESSAGES.componentMustHaveDefaultConstructor("Servlet", servletClass); } }
public static void addService(Class<? extends Servlet> service) { try { APIHandler handler = (APIHandler) service.newInstance(); services.add(service); serviceMap.put(handler.getAPIName(), handler); } catch (InstantiationException | IllegalAccessException e) { Data.logger.warn("", e); } }
@Override public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) { if (SecurityUtil.isPackageProtectionEnabled()) { return (ServletRegistration.Dynamic) doPrivileged("addServlet", new Class[]{String.class, Class.class}, new Object[]{servletName, servletClass}); } else { return context.addServlet(servletName, servletClass); } }
@Bean Servlet thrift(ThriftCodecManager thriftCodecManager, TProtocolFactory protocolFactory, TCalculatorService calculatorService) { ThriftServiceProcessor processor = new ThriftServiceProcessor(thriftCodecManager, Arrays.<ThriftEventHandler>asList(), calculatorService); return new TServlet( NiftyProcessorAdapters.processorToTProcessor(processor), protocolFactory, protocolFactory ); }
/** * Construct a new InstanceEvent with the specified parameters. This * constructor is used for processing servlet processing events. * * @param wrapper Wrapper managing this servlet instance * @param servlet Servlet instance for which this event occurred * @param type Event type (required) * @param request Servlet request we are processing * @param response Servlet response we are processing * @param exception Exception that occurred */ public InstanceEvent(Wrapper wrapper, Servlet servlet, String type, ServletRequest request, ServletResponse response, Throwable exception) { super(wrapper); this.filter = null; this.servlet = servlet; this.type = type; this.request = request; this.response = response; this.exception = exception; }
private PageContext internalGetPageContext(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoflush) { try { PageContext pc; if (USE_POOL) { PageContextPool pool = localPool.get(); if (pool == null) { pool = new PageContextPool(); localPool.set(pool); } pc = pool.get(); if (pc == null) { pc = new PageContextImpl(); } } else { pc = new PageContextImpl(); } pc.initialize(servlet, request, response, errorPageURL, needsSession, bufferSize, autoflush); return pc; } catch (Throwable ex) { ExceptionUtils.handleThrowable(ex); if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } log.fatal("Exception initializing page context", ex); return null; } }
/** * Perform work as a particular </code>Subject</code>. Here the work * will be granted to a <code>null</code> subject. * * @param methodName the method to apply the security restriction * @param targetObject the <code>Servlet</code> on which the method will * be called. * @param targetParameterTypes <code>Class</code> array used to instantiate a * <code>Method</code> object. * @param targetArguments <code>Object</code> array contains the * runtime parameters instance. * @param principal the <code>Principal</code> to which the security * privilege apply.. */ public static void doAsPrivilege(final String methodName, final Servlet targetObject, final Class<?>[] targetParameterTypes, final Object[] targetArguments, Principal principal) throws java.lang.Exception{ // CometProcessor instances must not be cached as Servlet or // NoSuchMethodException will be thrown. Class<? extends Servlet> targetType = targetObject instanceof CometProcessor ? CometProcessor.class : Servlet.class; Method method = null; Method[] methodsCache = classCache.get(Servlet.class); if(methodsCache == null) { method = createMethodAndCacheIt(methodsCache, targetType, methodName, targetParameterTypes); } else { method = findMethod(methodsCache, methodName); if (method == null) { method = createMethodAndCacheIt(methodsCache, targetType, methodName, targetParameterTypes); } } execute(method, targetObject, targetArguments, principal); }
@SuppressWarnings("deprecation") public ExistingStandardWrapper( Servlet existing ) { this.existing = existing; if (existing instanceof javax.servlet.SingleThreadModel) { singleThreadModel = true; instancePool = new Stack<Servlet>(); } }
/** * Construct a new InstanceEvent with the specified parameters. This * constructor is used for processing servlet processing events. * * @param wrapper Wrapper managing this servlet instance * @param servlet Servlet instance for which this event occurred * @param type Event type (required) * @param request Servlet request we are processing * @param response Servlet response we are processing */ public InstanceEvent(Wrapper wrapper, Servlet servlet, String type, ServletRequest request, ServletResponse response) { super(wrapper); this.filter = null; this.servlet = servlet; this.type = type; this.request = request; this.response = response; }
/** * @since servlet-api-3.0 */ @Override public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) { if (!isStarting()) throw new IllegalStateException(); if (servletName == null || "".equals(servletName.trim())) throw new IllegalStateException("Missing servlet name"); if (!_enabled) throw new UnsupportedOperationException(); final ServletHandler handler = ServletContextHandler.this.getServletHandler(); ServletHolder holder = handler.getServlet(servletName); if (holder == null) { holder = handler.newServletHolder(Source.JAVAX_API); holder.setName(servletName); holder.setServlet(servlet); handler.addServlet(holder); return dynamicHolderAdded(holder); } //complete a partial registration if (holder.getClassName()==null && holder.getHeldClass()==null) { holder.setServlet(servlet); return holder.getRegistration(); } else return null; //existing completed registration for servlet name }
private void checkAccess(Class<?> clazz) { if (privileged) return; if (Filter.class.isAssignableFrom(clazz)) { checkAccess(clazz, restrictedFilters); } else if (Servlet.class.isAssignableFrom(clazz)) { checkAccess(clazz, restrictedServlets); } else { checkAccess(clazz, restrictedListeners); } }
/** * Notify all lifecycle event listeners that a particular event has * occurred for this Container. The default implementation performs * this notification synchronously using the calling thread. * * @param type Event type * @param servlet The relevant Servlet for this event * @param exception Exception that occurred */ public void fireInstanceEvent(String type, Servlet servlet, Throwable exception) { if (listeners.length == 0) return; InstanceEvent event = new InstanceEvent(wrapper, servlet, type, exception); InstanceListener interested[] = listeners; for (int i = 0; i < interested.length; i++) interested[i].instanceEvent(event); }
/** * Notify all lifecycle event listeners that a particular event has * occurred for this Container. The default implementation performs * this notification synchronously using the calling thread. * * @param type Event type * @param servlet The relevant Servlet for this event * @param request The servlet request we are processing * @param response The servlet response we are processing */ public void fireInstanceEvent(String type, Servlet servlet, ServletRequest request, ServletResponse response) { if (listeners.length == 0) return; InstanceEvent event = new InstanceEvent(wrapper, servlet, type, request, response); InstanceListener interested[] = listeners; for (int i = 0; i < interested.length; i++) interested[i].instanceEvent(event); }
/** * @deprecated As of Java Servlet API 2.1, with no direct replacement. */ @Override @SuppressWarnings("unchecked") // doPrivileged() returns the correct type @Deprecated public Enumeration<Servlet> getServlets() { if (SecurityUtil.isPackageProtectionEnabled()) { return (Enumeration<Servlet>) doPrivileged("getServlets", null); } else { return context.getServlets(); } }
/** * onServletStart * * @param args */ public void onServletStart(Object... args) { StandardWrapper sw = (StandardWrapper) args[0]; Servlet servlet = (Servlet) args[1]; InterceptSupport iSupport = InterceptSupport.instance(); InterceptContext context = iSupport.createInterceptContext(Event.AFTER_SERVET_INIT); context.put(InterceptConstants.SERVLET_INSTANCE, servlet); context.put(InterceptConstants.WEBAPPLOADER, Thread.currentThread().getContextClassLoader()); context.put(InterceptConstants.CONTEXTPATH, sw.getServletContext().getContextPath()); iSupport.doIntercept(context); }
@Test public void testAsyncContextListenerClearing() throws Exception { resetTracker(); // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); Servlet stage1 = new DispatchingServletTracking("/stage2", true); Wrapper wrapper1 = Tomcat.addServlet(ctx, "stage1", stage1); wrapper1.setAsyncSupported(true); ctx.addServletMapping("/stage1", "stage1"); Servlet stage2 = new DispatchingServletTracking("/stage3", false); Wrapper wrapper2 = Tomcat.addServlet(ctx, "stage2", stage2); wrapper2.setAsyncSupported(true); ctx.addServletMapping("/stage2", "stage2"); Servlet stage3 = new NonAsyncServlet(); Tomcat.addServlet(ctx, "stage3", stage3); ctx.addServletMapping("/stage3", "stage3"); TesterAccessLogValve alv = new TesterAccessLogValve(); ctx.getPipeline().addValve(alv); tomcat.start(); getUrl("http://localhost:" + getPort()+ "/stage1"); assertEquals("doGet-startAsync-doGet-startAsync-onStartAsync-NonAsyncServletGet-onComplete-", getTrack()); // Check the access log alv.validateAccessLog(1, 200, 0, REQUEST_TIME); }
/** * @deprecated As of Java Servlet API 2.1, with no direct replacement. */ @Override @Deprecated public Servlet getServlet(String name) { return (null); }
/** * hook to register that we need to scan for security annotations. * @param wrapper The wrapper for the Servlet that was added */ public ServletRegistration.Dynamic dynamicServletAdded(Wrapper wrapper) { Servlet s = wrapper.getServlet(); if (s != null && createdServlets.contains(s)) { // Mark the wrapper to indicate annotations need to be scanned wrapper.setServletSecurityAnnotationScanRequired(true); } return new ApplicationServletRegistration(wrapper, this); }
@Override public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) { if (SecurityUtil.isPackageProtectionEnabled()) { return (ServletRegistration.Dynamic) doPrivileged("addServlet", new Class[] { String.class, Servlet.class }, new Object[] { servletName, servlet }); } else { return context.addServlet(servletName, servlet); } }
/** * Construct a new InstanceEvent with the specified parameters. This * constructor is used for processing servlet lifecycle events. * * @param wrapper Wrapper managing this servlet instance * @param servlet Servlet instance for which this event occurred * @param type Event type (required) */ public InstanceEvent(Wrapper wrapper, Servlet servlet, String type) { super(wrapper); this.wrapper = wrapper; this.filter = null; this.servlet = servlet; this.type = type; }
/** * Gets the names of the methods supported by the underlying servlet. * * This is the same set of methods included in the Allow response header * in response to an OPTIONS request method processed by the underlying * servlet. * * @return Array of names of the methods supported by the underlying * servlet */ @Override public String[] getServletMethods() throws ServletException { instance = loadServlet(); Class<? extends Servlet> servletClazz = instance.getClass(); if (!javax.servlet.http.HttpServlet.class.isAssignableFrom( servletClazz)) { return DEFAULT_SERVLET_METHODS; } HashSet<String> allow = new HashSet<String>(); allow.add("TRACE"); allow.add("OPTIONS"); Method[] methods = getAllDeclaredMethods(servletClazz); for (int i=0; methods != null && i<methods.length; i++) { Method m = methods[i]; if (m.getName().equals("doGet")) { allow.add("GET"); allow.add("HEAD"); } else if (m.getName().equals("doPost")) { allow.add("POST"); } else if (m.getName().equals("doPut")) { allow.add("PUT"); } else if (m.getName().equals("doDelete")) { allow.add("DELETE"); } } String[] methodNames = new String[allow.size()]; return allow.toArray(methodNames); }
/** * Construct a new InstanceEvent with the specified parameters. This * constructor is used for processing servlet processing events. * * @param wrapper Wrapper managing this servlet instance * @param servlet Servlet instance for which this event occurred * @param type Event type (required) * @param request Servlet request we are processing * @param response Servlet response we are processing * @param exception Exception that occurred */ public InstanceEvent(Wrapper wrapper, Servlet servlet, String type, ServletRequest request, ServletResponse response, Throwable exception) { super(wrapper); this.wrapper = wrapper; this.filter = null; this.servlet = servlet; this.type = type; this.request = request; this.response = response; this.exception = exception; }
/** * Return a null reference for the specified servlet name. * * @param name Name of the requested servlet * * @deprecated This method has been deprecated with no replacement */ @Override @Deprecated public Servlet getServlet(String name) throws ServletException { return (null); }
/** * Perform work as a particular </code>Subject</code>. Here the work * will be granted to a <code>null</code> subject. * * @param methodName the method to apply the security restriction * @param targetObject the <code>Servlet</code> on which the method will * be called. * @param targetType <code>Class</code> array used to instantiate a * <code>Method</code> object. * @param targetArguments <code>Object</code> array contains the runtime * parameters instance. */ public static void doAsPrivilege(final String methodName, final Servlet targetObject, final Class<?>[] targetType, final Object[] targetArguments) throws java.lang.Exception{ doAsPrivilege(methodName, targetObject, targetType, targetArguments, null); }
/** * Construct a new InstanceEvent with the specified parameters. This * constructor is used for processing servlet lifecycle events. * * @param wrapper Wrapper managing this servlet instance * @param servlet Servlet instance for which this event occurred * @param type Event type (required) */ public InstanceEvent(Wrapper wrapper, Servlet servlet, String type) { super(wrapper); this.filter = null; this.servlet = servlet; this.type = type; }
public PageContext getPageContext(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoflush) { if( Constants.IS_SECURITY_ENABLED ) { PrivilegedGetPageContext dp = new PrivilegedGetPageContext( (JspFactoryImpl)this, servlet, request, response, errorPageURL, needsSession, bufferSize, autoflush); return (PageContext)AccessController.doPrivileged(dp); } else { return internalGetPageContext(servlet, request, response, errorPageURL, needsSession, bufferSize, autoflush); } }
PrivilegedGetPageContext(JspFactoryImpl factory, Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoflush) { this.factory = factory; this.servlet = servlet; this.request = request; this.response = response; this.errorPageURL = errorPageURL; this.needsSession = needsSession; this.bufferSize = bufferSize; this.autoflush = autoflush; }
public void setServletMap(Map<String, Servlet> urlPatternMap) { servletMap = new LinkedHashMap<UrlPatternMatcher, Servlet>(); for (Map.Entry<String, Servlet> entry : urlPatternMap.entrySet()) { UrlPatternMatcher urlPatternMatcher = UrlPatternMatcher .create(entry.getKey()); servletMap.put(urlPatternMatcher, entry.getValue()); } }