org.apache.catalina.core.StandardWrapper Java Examples

The following examples show how to use org.apache.catalina.core.StandardWrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringBootTomcatPlusIT.java    From uavstack with Apache License 2.0 6 votes vote down vote up
/**
 * onServletStart
 * 
 * @param args
 */
@Override
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);
    /**
     * 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);
}
 
Example #2
Source File: SpringBootTomcatPlusIT.java    From uavstack with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: TomcatEmbeddedContext.java    From spring-graalvm-native with Apache License 2.0 5 votes vote down vote up
private void load(Wrapper wrapper) {
	try {
		wrapper.load();
	}
	catch (ServletException ex) {
		String message = sm.getString("standardContext.loadOnStartup.loadException", getName(), wrapper.getName());
		if (getComputedFailCtxIfServletStartFails()) {
			throw new RuntimeException(message, ex);
		}
		getLogger().error(message, StandardWrapper.getRootCause(ex));
	}
}
 
Example #4
Source File: TomcatPlusIT.java    From uavstack with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: TomcatPlusIT.java    From uavstack with Apache License 2.0 5 votes vote down vote up
/**
 * onServletStop
 * 
 * @param args
 */
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);
    context.put(InterceptConstants.WEBAPPLOADER, Thread.currentThread().getContextClassLoader());
    context.put(InterceptConstants.CONTEXTPATH, sw.getServletContext().getContextPath());
    iSupport.doIntercept(context);
}
 
Example #6
Source File: PresentationConfiguration.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public static Gson gson() {
    GsonBuilder gsonBuilder = new GsonBuilder()
            .disableHtmlEscaping()
            .registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter())
            .registerTypeAdapter(PlatformIO.class, new PlatformIO.Serializer()) // Exclusion de hasPasswords lorsqu'il est null
            .registerTypeAdapter(PropertyOutput.class, new PropertyOutput.Serializer()) // Exclusion et récursivité
            .registerTypeAdapter(AbstractValuedPropertyIO.class, new AbstractValuedPropertyIO.Adapter()) // Classe abstraite
            .registerTypeAdapter(AbstractDifferingPropertyOutput.class, new AbstractDifferingPropertyOutput.Adapter()) // Classe abstraite
            .registerTypeAdapter(PlatformChangeOutput.class, new PlatformChangeOutput.Adapter()) // Classe abstraite
            .serializeNulls()
            .addSerializationExclusionStrategy(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes field) {
                    // Ceci est nécessaire pour éviter des erreurs 500 lorsqu'on requête /rest/manage/beans:
                    // "Could not write JSON" "Attempted to serialize java.lang.Class" "Forgot to register a type adapter?"
                    // à cause de org.springframework.boot.actuate.beans.BeansEndpoint.BeanDescriptor
                    // Plus de doc sur le sujet: https://www.baeldung.com/gson-exclude-fields-serialization
                    return field.getDeclaredType().getTypeName().equals("java.lang.Class<?>");
                }

                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            })
            // On doit exclure ces classes de la désérialization pour éviter une boucle circulaire infinie
            // lorsqu'on requête /rest/manage/mappings (cf. #414)
            // et dans ce cas une ExclusionStrategy ne fonctionne pas (bug connu de Gson) :
            .registerTypeAdapter(StandardWrapper.class, (JsonSerializer<StandardWrapper>) (src, typeOfSrc, context) -> null);
    try {
        // idem, mais comme cette classe est package-private, impossible de l'importer directement :
        gsonBuilder.registerTypeAdapter(Class.forName("org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedContext"), (JsonSerializer) (src, typeOfSrc, context) -> null);
    } catch (ClassNotFoundException ignored) {
    }
    return gsonBuilder.create();
}
 
Example #7
Source File: TestMapper.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private Wrapper createWrapper(String name) {
    Wrapper wrapper = new StandardWrapper();
    wrapper.setName(name);
    return wrapper;
}
 
Example #8
Source File: TomcatWsRegistry.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public List<String> setWsContainer(final HttpListener httpListener,
                                   final ClassLoader classLoader,
                                   String contextRoot, String virtualHost, final ServletInfo servletInfo,
                                   final String realmName, final String transportGuarantee, final String authMethod,
                                   final String moduleId) throws Exception {

    if (virtualHost == null) {
        virtualHost = engine.getDefaultHost();
    }

    final Container host = engine.findChild(virtualHost);
    if (host == null) {
        throw new IllegalArgumentException("Invalid virtual host '" + virtualHost + "'.  Do you have a matchiing Host entry in the server.xml?");
    }

    if ("ROOT".equals(contextRoot)) { // doesn't happen in tomee itself but with all our tooling around
        contextRoot = "";
    }
    if (!contextRoot.startsWith("/") && !contextRoot.isEmpty()) {
        contextRoot = "/" + contextRoot;
    }

    final Context context = (Context) host.findChild(contextRoot);
    if (context == null) {
        throw new IllegalArgumentException("Could not find web application context " + contextRoot + " in host " + host.getName());
    }

    final Wrapper wrapper = (Wrapper) context.findChild(servletInfo.servletName);
    if (wrapper == null) {
        throw new IllegalArgumentException("Could not find servlet " + servletInfo.servletName + " in web application context " + context.getName());
    }

    // for Pojo web services, we need to change the servlet class which is the service implementation
    // by the WsServler class
    wrapper.setServletClass(WsServlet.class.getName());
    if (wrapper.getServlet() != null) {
        wrapper.unload(); // deallocate previous one
        wrapper.load(); // reload this one withuot unloading it to keep the instance - unload is called during stop()
        // boolean controlling this method call can't be set to false through API so let do it ourself
        wrapper.getServlet().init(StandardWrapper.class.cast(wrapper)); // or Reflections.set(wrapper, "instanceInitialized", false);
    }

    setWsContainer(context, wrapper, httpListener);

    // add service locations
    final List<String> addresses = new ArrayList<>();
    for (final Connector connector : connectors) {
        for (final String mapping : wrapper.findMappings()) {
            final URI address = new URI(connector.getScheme(), null, host.getName(), connector.getPort(), (contextRoot.startsWith("/") ? "" : "/") + contextRoot + mapping, null, null);
            addresses.add(address.toString());
        }
    }
    return addresses;
}