Search in sources :

Example 41 with Wrapper

use of org.apache.catalina.Wrapper in project tomee by apache.

the class ManualDeploymentTest method run.

@Test
public void run() throws IOException {
    final Configuration configuration = new Configuration().randomHttpPort();
    configuration.setDir(Files.mkdirs(new File("target/" + getClass().getSimpleName() + "-tomcat")).getAbsolutePath());
    try (final Container container = new Container(configuration)) {
        // tomee-embedded (this "container url" is filtered: name prefix + it is a directory (target/test-classes)
        final File parent = Files.mkdirs(new File("target/" + getClass().getSimpleName()));
        final File war = ShrinkWrap.create(WebArchive.class, "the-webapp").addClass(Foo.class).addAsWebInfResource(EmptyAsset.INSTANCE, // activate CDI
        "beans.xml").as(ExplodedExporter.class).exportExploded(parent);
        final Context ctx = container.addContext("", war.getAbsolutePath());
        final Wrapper wrapper = Tomcat.addServlet(ctx, "awesome", AServlet.class.getName());
        ctx.addServletMappingDecoded("/awesome", wrapper.getName());
        assertEquals("Awesome", IO.slurp(new URL("http://localhost:" + configuration.getHttpPort() + "/awesome")).trim());
    } catch (final Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) ExplodedExporter(org.jboss.shrinkwrap.api.exporter.ExplodedExporter) File(java.io.File) URL(java.net.URL) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Test(org.junit.Test)

Example 42 with Wrapper

use of org.apache.catalina.Wrapper in project tomee by apache.

the class CXFJAXRSFilter method servletMappingIsUnderRestPath.

private boolean servletMappingIsUnderRestPath(final HttpServletRequest request) {
    final HttpServletRequest unwrapped = unwrap(request);
    if (!RequestFacade.class.isInstance(unwrapped)) {
        return false;
    }
    final Request tr;
    try {
        tr = Request.class.cast(REQUEST.get(unwrapped));
    } catch (final IllegalAccessException e) {
        return false;
    }
    final Wrapper wrapper = tr.getWrapper();
    if (wrapper == null || mapping == null) {
        return false;
    }
    Boolean accept = mappingByServlet.get(wrapper);
    if (accept == null) {
        accept = false;
        if (!"org.apache.catalina.servlets.DefaultServlet".equals(wrapper.getServletClass())) {
            for (final String mapping : wrapper.findMappings()) {
                if (!mapping.isEmpty() && !"/*".equals(mapping) && !"/".equals(mapping) && !mapping.startsWith("*") && mapping.startsWith(this.mapping)) {
                    accept = true;
                    break;
                }
            }
        }
        // else will be handed by getResourceAsStream()
        mappingByServlet.putIfAbsent(wrapper, accept);
        return accept;
    }
    return accept;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) Wrapper(org.apache.catalina.Wrapper) Request(org.apache.catalina.connector.Request) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestFacade(org.apache.catalina.connector.RequestFacade)

Example 43 with Wrapper

use of org.apache.catalina.Wrapper in project tomee by apache.

the class TomcatHessianRegistry method deploy.

@Override
public String deploy(final ClassLoader loader, final HessianServer listener, final String hostname, final String app, final String authMethod, final String transportGuarantee, final String realmName, final String name) throws URISyntaxException {
    Container host = engine.findChild(hostname);
    if (host == null) {
        host = engine.findChild(engine.getDefaultHost());
        if (host == null) {
            throw new IllegalArgumentException("Invalid virtual host '" + engine.getDefaultHost() + "'.  Do you have a matchiing Host entry in the server.xml?");
        }
    }
    final String contextRoot = contextName(app);
    Context context = Context.class.cast(host.findChild(contextRoot));
    if (context == null) {
        Pair<Context, Integer> fakeContext = fakeContexts.get(contextRoot);
        if (fakeContext != null) {
            context = fakeContext.getLeft();
            fakeContext.setValue(fakeContext.getValue() + 1);
        } else {
            context = Context.class.cast(host.findChild(contextRoot));
            if (context == null) {
                fakeContext = fakeContexts.get(contextRoot);
                if (fakeContext == null) {
                    context = createNewContext(loader, authMethod, transportGuarantee, realmName, app);
                    fakeContext = new MutablePair<>(context, 1);
                    fakeContexts.put(contextRoot, fakeContext);
                } else {
                    context = fakeContext.getLeft();
                    fakeContext.setValue(fakeContext.getValue() + 1);
                }
            }
        }
    }
    final String servletMapping = generateServletPath(name);
    Wrapper wrapper = Wrapper.class.cast(context.findChild(servletMapping));
    if (wrapper != null) {
        throw new IllegalArgumentException("Servlet " + servletMapping + " in web application context " + context.getName() + " already exists");
    }
    wrapper = context.createWrapper();
    wrapper.setName(HESSIAN.replace("/", "") + "_" + name);
    wrapper.setServlet(new OpenEJBHessianServlet(listener));
    context.addChild(wrapper);
    context.addServletMappingDecoded(servletMapping, wrapper.getName());
    if ("BASIC".equals(authMethod) && StandardContext.class.isInstance(context)) {
        final StandardContext standardContext = StandardContext.class.cast(context);
        boolean found = false;
        for (final Valve v : standardContext.getPipeline().getValves()) {
            if (LimitedBasicValve.class.isInstance(v) || BasicAuthenticator.class.isInstance(v)) {
                found = true;
                break;
            }
        }
        if (!found) {
            standardContext.addValve(new LimitedBasicValve());
        }
    }
    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 + mapping, null, null);
            addresses.add(address.toString());
        }
    }
    return HttpUtil.selectSingleAddress(addresses);
}
Also used : Context(org.apache.catalina.Context) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) Wrapper(org.apache.catalina.Wrapper) Connector(org.apache.catalina.connector.Connector) ArrayList(java.util.ArrayList) URI(java.net.URI) Container(org.apache.catalina.Container) BasicAuthenticator(org.apache.catalina.authenticator.BasicAuthenticator) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) OpenEJBValve(org.apache.tomee.catalina.OpenEJBValve) Valve(org.apache.catalina.Valve)

Example 44 with Wrapper

use of org.apache.catalina.Wrapper in project tomee by apache.

the class OpenEJBContextConfig method addAddedJAXWsServices.

private void addAddedJAXWsServices() {
    final WebAppInfo webAppInfo = info.get();
    final AppInfo appInfo = info.app();
    if (webAppInfo == null || appInfo == null || "false".equalsIgnoreCase(appInfo.properties.getProperty("openejb.jaxws.add-missing-servlets", "true"))) {
        return;
    }
    try {
        // if no jaxws classes are here don't try anything
        OpenEJBContextConfig.class.getClassLoader().loadClass("org.apache.openejb.server.webservices.WsServlet");
    } catch (final ClassNotFoundException | NoClassDefFoundError e) {
        return;
    }
    for (final ServletInfo servlet : webAppInfo.servlets) {
        if (!servlet.mappings.iterator().hasNext()) {
            // no need to do anything
            continue;
        }
        for (final ParamValueInfo pv : servlet.initParams) {
            if ("openejb-internal".equals(pv.name) && "true".equals(pv.value)) {
                if (context.findChild(servlet.servletName) == null) {
                    final Wrapper wrapper = context.createWrapper();
                    wrapper.setName(servlet.servletName);
                    wrapper.setServletClass("org.apache.openejb.server.webservices.WsServlet");
                    // add servlet to context
                    context.addChild(wrapper);
                    context.addServletMappingDecoded(servlet.mappings.iterator().next(), wrapper.getName());
                }
                break;
            }
        }
    }
}
Also used : ServletInfo(org.apache.openejb.assembler.classic.ServletInfo) Wrapper(org.apache.catalina.Wrapper) StandardWrapper(org.apache.catalina.core.StandardWrapper) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) ParamValueInfo(org.apache.openejb.assembler.classic.ParamValueInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Example 45 with Wrapper

use of org.apache.catalina.Wrapper in project tomee by apache.

the class OpenEJBContextConfig method webConfig.

@Override
protected void webConfig() {
    TomcatHelper.configureJarScanner(context);
    // read the real config
    super.webConfig();
    if (IgnoredStandardContext.class.isInstance(context)) {
        // no need of jsf
        return;
    }
    if (AppFinder.findAppContextOrWeb(context.getLoader().getClassLoader(), AppFinder.WebBeansContextTransformer.INSTANCE) != null) {
        final FilterDef asyncOwbFilter = new FilterDef();
        asyncOwbFilter.setAsyncSupported("true");
        asyncOwbFilter.setDescription("OpenEJB CDI Filter - to propagate @RequestScoped in async tasks");
        asyncOwbFilter.setDisplayName("OpenEJB CDI");
        asyncOwbFilter.setFilterClass(EEFilter.class.getName());
        asyncOwbFilter.setFilterName(EEFilter.class.getName());
        context.addFilterDef(asyncOwbFilter);
        final FilterMap asyncOwbMapping = new FilterMap();
        asyncOwbMapping.setFilterName(asyncOwbFilter.getFilterName());
        asyncOwbMapping.addURLPattern("/*");
        context.addFilterMap(asyncOwbMapping);
    }
    if ("true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.jsp-development", "false"))) {
        for (final Container c : context.findChildren()) {
            if (Wrapper.class.isInstance(c)) {
                final Wrapper servlet = Wrapper.class.cast(c);
                if ("org.apache.jasper.servlet.JspServlet".equals(servlet.getServletClass())) {
                    servlet.addInitParameter("development", "true");
                }
            }
        }
    }
    final ClassLoader classLoader = context.getLoader().getClassLoader();
    // add myfaces auto-initializer if mojarra is not present
    try {
        classLoader.loadClass("com.sun.faces.context.SessionMap");
        return;
    } catch (final Throwable ignored) {
    // no-op
    }
    try {
        final Class<?> myfacesInitializer = Class.forName(MYFACES_TOMEEM_CONTAINER_INITIALIZER, true, classLoader);
        final ServletContainerInitializer instance = (ServletContainerInitializer) myfacesInitializer.newInstance();
        context.addServletContainerInitializer(instance, getJsfClasses(context));
        // cleanup listener
        context.addApplicationListener(TOMEE_MYFACES_CONTEXT_LISTENER);
    } catch (final Exception | NoClassDefFoundError ignored) {
    // no-op
    }
}
Also used : ServletContainerInitializer(javax.servlet.ServletContainerInitializer) Wrapper(org.apache.catalina.Wrapper) StandardWrapper(org.apache.catalina.core.StandardWrapper) Container(org.apache.catalina.Container) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) EEFilter(org.apache.openejb.server.httpd.EEFilter) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException)

Aggregations

Wrapper (org.apache.catalina.Wrapper)109 Context (org.apache.catalina.Context)57 Tomcat (org.apache.catalina.startup.Tomcat)48 AsyncContext (javax.servlet.AsyncContext)33 Test (org.junit.Test)31 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)28 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)24 TesterContext (org.apache.tomcat.unittest.TesterContext)24 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)22 IOException (java.io.IOException)18 StandardWrapper (org.apache.catalina.core.StandardWrapper)16 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)14 File (java.io.File)13 Container (org.apache.catalina.Container)13 ServletException (javax.servlet.ServletException)9 HashMap (java.util.HashMap)7 StandardContext (org.apache.catalina.core.StandardContext)7 Servlet (javax.servlet.Servlet)6 ArrayList (java.util.ArrayList)5