Search in sources :

Example 1 with HttpListener

use of org.apache.openejb.server.httpd.HttpListener in project tomee by apache.

the class WsService method afterApplicationCreated.

public void afterApplicationCreated(final AppInfo appInfo, final WebAppInfo webApp) {
    final WebContext webContext = containerSystem.getWebContextByHost(webApp.moduleId, webApp.host != null ? webApp.host : virtualHost);
    if (webContext == null)
        return;
    // if already deployed skip this webapp
    if (!deployedWebApps.add(webApp))
        return;
    final Map<String, PortInfo> ports = new TreeMap<>();
    for (final PortInfo port : webApp.portInfos) {
        ports.put(port.serviceLink, port);
    }
    URL moduleBaseUrl = null;
    try {
        moduleBaseUrl = new File(webApp.path).toURI().toURL();
    } catch (final MalformedURLException e) {
        LOGGER.error("Invalid ejb jar location " + webApp.path, e);
    }
    // lazy init
    Collection<IdPropertiesInfo> pojoConfiguration = null;
    for (final ServletInfo servlet : webApp.servlets) {
        if (servlet.servletName == null) {
            continue;
        }
        final PortInfo portInfo = ports.get(servlet.servletName);
        if (portInfo == null) {
            continue;
        }
        final ClassLoader old = Thread.currentThread().getContextClassLoader();
        final ClassLoader classLoader = webContext.getClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);
        try {
            final Collection<Injection> injections = webContext.getInjections();
            final Context context = webContext.getJndiEnc();
            final Class target = classLoader.loadClass(servlet.servletClass);
            final Map<String, Object> bindings = webContext.getBindings();
            final PortData port = WsBuilder.toPortData(portInfo, injections, moduleBaseUrl, classLoader);
            pojoConfiguration = PojoUtil.findPojoConfig(pojoConfiguration, appInfo, webApp);
            final HttpListener container = createPojoWsContainer(classLoader, moduleBaseUrl, port, portInfo.serviceLink, target, context, webApp.contextRoot, bindings, new ServiceConfiguration(PojoUtil.findConfiguration(pojoConfiguration, target.getName()), appInfo.services));
            if (wsRegistry != null) {
                String auth = authMethod;
                String realm = realmName;
                String transport = transportGuarantee;
                if ("BASIC".equals(portInfo.authMethod) || "DIGEST".equals(portInfo.authMethod) || "CLIENT-CERT".equals(portInfo.authMethod)) {
                    auth = portInfo.authMethod;
                    realm = portInfo.realmName;
                    transport = portInfo.transportGuarantee;
                }
                // give servlet a reference to the webservice container
                final List<String> addresses = wsRegistry.setWsContainer(container, classLoader, webApp.contextRoot, host(webApp), servlet, realm, transport, auth, webApp.moduleId);
                // one of the registered addresses to be the connonical address
                final String address = HttpUtil.selectSingleAddress(addresses);
                if (address != null) {
                    // add address to global registry
                    portAddressRegistry.addPort(portInfo.serviceId, portInfo.wsdlService, portInfo.portId, portInfo.wsdlPort, portInfo.seiInterfaceName, address);
                    setWsdl(container, address);
                    LOGGER.info("Webservice(wsdl=" + address + ", qname=" + port.getWsdlService() + ") --> Pojo(id=" + portInfo.portId + ")");
                    /*
                        In POJO webservices it is very common to have the same JAW-WS endpoint published under different URLs
                        using url-pattern for the servlet. We only deploy one instance of the WsServlet and keep the same
                        set of url-pattern the user defined. At the moment we can't add additional ports, with different
                        addresses. So at least we should list other URLs so the user knows they are successfully deployed
                        and is aware of the different URLs
                         */
                    for (String urlAddress : addresses) {
                        if (address.equals(urlAddress)) {
                            continue;
                        }
                        LOGGER.info("Webservice(wsdl=" + urlAddress + ", qname=" + port.getWsdlService() + ") --> Pojo(id=" + portInfo.portId + ")");
                    }
                    servletAddresses.put(webApp.moduleId + "." + servlet.servletName, address);
                    addressesForApp(webApp.moduleId).add(new EndpointInfo(address, port.getWsdlService(), target.getName()));
                }
            }
        } catch (final Throwable e) {
            LOGGER.error("Error deploying CXF webservice for servlet " + portInfo.serviceLink, e);
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) MalformedURLException(java.net.MalformedURLException) WebContext(org.apache.openejb.core.WebContext) Injection(org.apache.openejb.Injection) TreeMap(java.util.TreeMap) URL(java.net.URL) PortInfo(org.apache.openejb.assembler.classic.PortInfo) ServletInfo(org.apache.openejb.assembler.classic.ServletInfo) ServiceConfiguration(org.apache.openejb.assembler.classic.util.ServiceConfiguration) PortData(org.apache.openejb.core.webservices.PortData) IdPropertiesInfo(org.apache.openejb.assembler.classic.IdPropertiesInfo) HttpListener(org.apache.openejb.server.httpd.HttpListener) File(java.io.File)

Example 2 with HttpListener

use of org.apache.openejb.server.httpd.HttpListener in project tomee by apache.

the class WsService method deployApp.

private void deployApp(final AppInfo appInfo, final Collection<BeanContext> ejbs) {
    final Collection<BeanContext> alreadyDeployed = deployedApplications.get(appInfo);
    final Map<String, WebAppInfo> webContextByEjb = new HashMap<>();
    for (final WebAppInfo webApp : appInfo.webApps) {
        for (final String ejb : webApp.ejbWebServices) {
            webContextByEjb.put(ejb, webApp);
        }
    }
    final Map<String, String> contextData = new HashMap<>();
    contextData.put("appId", appInfo.path);
    for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
        final Map<String, PortInfo> ports = new TreeMap<>();
        for (final PortInfo port : ejbJar.portInfos) {
            ports.put(port.serviceLink, port);
        }
        URL moduleBaseUrl = null;
        if (ejbJar.path != null) {
            try {
                moduleBaseUrl = new File(ejbJar.path).toURI().toURL();
            } catch (final MalformedURLException e) {
                LOGGER.error("Invalid ejb jar location " + ejbJar.path, e);
            }
        }
        StringTemplate deploymentIdTemplate = this.wsAddressTemplate;
        if (ejbJar.properties.containsKey(WS_ADDRESS_FORMAT)) {
            final String format = ejbJar.properties.getProperty(WS_ADDRESS_FORMAT);
            LOGGER.info("Using " + WS_ADDRESS_FORMAT + " '" + format + "'");
            deploymentIdTemplate = new StringTemplate(format);
        }
        contextData.put("ejbJarId", ejbJar.moduleName);
        final String host = host(ejbJar, appInfo);
        for (final EnterpriseBeanInfo bean : ejbJar.enterpriseBeans) {
            if (bean instanceof StatelessBeanInfo || bean instanceof SingletonBeanInfo) {
                final BeanContext beanContext = containerSystem.getBeanContext(bean.ejbDeploymentId);
                if (beanContext == null || (ejbs != null && !ejbs.contains(beanContext))) {
                    continue;
                }
                final PortInfo portInfo = ports.get(bean.ejbName);
                if (portInfo == null || alreadyDeployed.contains(beanContext))
                    continue;
                final ClassLoader old = Thread.currentThread().getContextClassLoader();
                Thread.currentThread().setContextClassLoader(beanContext.getClassLoader());
                try {
                    final PortData port = WsBuilder.toPortData(portInfo, beanContext.getInjections(), moduleBaseUrl, beanContext.getClassLoader());
                    final HttpListener container = createEjbWsContainer(moduleBaseUrl, port, beanContext, new ServiceConfiguration(beanContext.getProperties(), appInfo.services));
                    // generate a location if one was not assigned
                    String location = port.getLocation();
                    if (location == null) {
                        location = autoAssignWsLocation(bean, port, contextData, deploymentIdTemplate);
                    }
                    if (!location.startsWith("/"))
                        location = "/" + location;
                    ejbLocations.put(bean.ejbDeploymentId, location);
                    final ClassLoader classLoader = beanContext.getClassLoader();
                    if (wsRegistry != null) {
                        String auth = authMethod;
                        String realm = realmName;
                        String transport = transportGuarantee;
                        if ("BASIC".equals(portInfo.authMethod) || "DIGEST".equals(portInfo.authMethod) || "CLIENT-CERT".equals(portInfo.authMethod)) {
                            auth = portInfo.authMethod;
                            realm = portInfo.realmName;
                            transport = portInfo.transportGuarantee;
                        }
                        final WebAppInfo webAppInfo = webContextByEjb.get(bean.ejbClass);
                        String context = webAppInfo != null ? webAppInfo.contextRoot : null;
                        String moduleId = webAppInfo != null ? webAppInfo.moduleId : null;
                        if (context == null && !OLD_WEBSERVICE_DEPLOYMENT) {
                            context = ejbJar.moduleName;
                        }
                        final List<String> addresses = wsRegistry.addWsContainer(container, classLoader, context, host, location, realm, transport, auth, moduleId);
                        alreadyDeployed.add(beanContext);
                        // one of the registered addresses to be the canonical address
                        final String address = HttpUtil.selectSingleAddress(addresses);
                        if (address != null) {
                            // register wsdl location
                            portAddressRegistry.addPort(portInfo.serviceId, portInfo.wsdlService, portInfo.portId, portInfo.wsdlPort, portInfo.seiInterfaceName, address);
                            setWsdl(container, address);
                            LOGGER.info("Webservice(wsdl=" + address + ", qname=" + port.getWsdlService() + ") --> Ejb(id=" + portInfo.portId + ")");
                            ejbAddresses.put(bean.ejbDeploymentId, address);
                            addressesForApp(appInfo.appId).add(new EndpointInfo(address, port.getWsdlService(), beanContext.getBeanClass().getName()));
                        }
                    }
                } catch (final Throwable e) {
                    LOGGER.error("Error deploying JAX-WS Web Service for EJB " + beanContext.getDeploymentID(), e);
                } finally {
                    Thread.currentThread().setContextClassLoader(old);
                }
            }
        }
    }
    if (ejbs == null || appInfo.webAppAlone) {
        for (final WebAppInfo webApp : appInfo.webApps) {
            afterApplicationCreated(appInfo, webApp);
        }
    }
// else called because of ear case where new ejbs are deployed in webapps
}
Also used : MalformedURLException(java.net.MalformedURLException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SingletonBeanInfo(org.apache.openejb.assembler.classic.SingletonBeanInfo) URL(java.net.URL) PortInfo(org.apache.openejb.assembler.classic.PortInfo) EnterpriseBeanInfo(org.apache.openejb.assembler.classic.EnterpriseBeanInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) ServiceConfiguration(org.apache.openejb.assembler.classic.util.ServiceConfiguration) PortData(org.apache.openejb.core.webservices.PortData) StringTemplate(org.apache.openejb.util.StringTemplate) HttpListener(org.apache.openejb.server.httpd.HttpListener) StatelessBeanInfo(org.apache.openejb.assembler.classic.StatelessBeanInfo) TreeMap(java.util.TreeMap) BeanContext(org.apache.openejb.BeanContext) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo) File(java.io.File)

Example 3 with HttpListener

use of org.apache.openejb.server.httpd.HttpListener in project tomee by apache.

the class OpenEJBHttpWsRegistry method addWsContainer.

@Override
public List<String> addWsContainer(final HttpListener inputListener, final ClassLoader classLoader, final String context, final String virtualHost, final String path, final String realmName, // ignored
final String transportGuarantee, final String authMethod, final String moduleId) throws Exception {
    if (path == null)
        throw new NullPointerException("contextRoot is null");
    HttpListener httpListener = inputListener;
    if (httpListener == null)
        throw new NullPointerException("httpListener is null");
    if ("BASIC".equals(authMethod)) {
        httpListener = new BasicAuthHttpListenerWrapper(httpListener, realmName);
    }
    final StringBuilder deployedPath = new StringBuilder("");
    if (context != null) {
        if (!context.startsWith("/")) {
            deployedPath.append("/");
        }
        deployedPath.append(context);
        if (!context.endsWith("/")) {
            deployedPath.append("/");
        }
    } else {
        deployedPath.append("/");
    }
    if (path.startsWith("/") && path.length() > 1) {
        deployedPath.append(path.substring(1));
    } else if (path.length() > 1) {
        deployedPath.append(path);
    }
    addWrappedHttpListener(httpListener, classLoader, deployedPath.toString());
    // register wsdl locations for service-ref resolution
    return getResolvedAddresses(deployedPath.toString());
}
Also used : BasicAuthHttpListenerWrapper(org.apache.openejb.server.httpd.BasicAuthHttpListenerWrapper) HttpListener(org.apache.openejb.server.httpd.HttpListener)

Example 4 with HttpListener

use of org.apache.openejb.server.httpd.HttpListener in project tomee by apache.

the class WsServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    HttpListener service = getService();
    if (service == null)
        throw new ServletException("WebServiceContainer has not been set");
    ServletEndpointContext context = getContext();
    ENDPOINT_CONTENT.set(new InvocationContext((HttpServletRequest) req));
    try {
        res.setContentType("text/xml");
        HttpRequest httpRequest = new ServletRequestAdapter((HttpServletRequest) req, (HttpServletResponse) res, config.getServletContext());
        HttpResponse httpResponse = new ServletResponseAdapter((HttpServletResponse) res);
        if (pojo != null) {
            req.setAttribute(WsConstants.POJO_INSTANCE, pojo);
        }
        try {
            service.onMessage(httpRequest, httpResponse);
        } catch (IOException | ServletException e) {
            throw e;
        } catch (Exception e) {
            throw new ServletException("Error processing webservice request", e);
        }
    } finally {
        ENDPOINT_CONTENT.set(context);
    }
}
Also used : ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.apache.openejb.server.httpd.HttpRequest) ServletEndpointContext(javax.xml.rpc.server.ServletEndpointContext) ServletRequestAdapter(org.apache.openejb.server.httpd.ServletRequestAdapter) HttpResponse(org.apache.openejb.server.httpd.HttpResponse) HttpListener(org.apache.openejb.server.httpd.HttpListener) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServiceException(javax.xml.rpc.ServiceException) ServletResponseAdapter(org.apache.openejb.server.httpd.ServletResponseAdapter)

Example 5 with HttpListener

use of org.apache.openejb.server.httpd.HttpListener in project tomee by apache.

the class HttpUtil method removeFilter.

public static void removeFilter(final String mapping, final WebContext wc) {
    final HttpListenerRegistry registry = SystemInstance.get().getComponent(HttpListenerRegistry.class);
    if (registry == null || mapping == null) {
        return;
    }
    final Collection<HttpListener> filters = registry.removeHttpFilter(pattern(wc.getContextRoot(), mapping));
    for (HttpListener listener : filters) {
        final Filter filter = ((FilterListener) listener).getDelegate();
        filter.destroy();
        wc.destroy(filter);
    }
    filters.clear();
}
Also used : FilterListener(org.apache.openejb.server.httpd.FilterListener) Filter(javax.servlet.Filter) HttpListener(org.apache.openejb.server.httpd.HttpListener) HttpListenerRegistry(org.apache.openejb.server.httpd.HttpListenerRegistry)

Aggregations

HttpListener (org.apache.openejb.server.httpd.HttpListener)5 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 TreeMap (java.util.TreeMap)2 BeanContext (org.apache.openejb.BeanContext)2 PortInfo (org.apache.openejb.assembler.classic.PortInfo)2 ServiceConfiguration (org.apache.openejb.assembler.classic.util.ServiceConfiguration)2 PortData (org.apache.openejb.core.webservices.PortData)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Context (javax.naming.Context)1 Filter (javax.servlet.Filter)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ServiceException (javax.xml.rpc.ServiceException)1 ServletEndpointContext (javax.xml.rpc.server.ServletEndpointContext)1 AppContext (org.apache.openejb.AppContext)1 Injection (org.apache.openejb.Injection)1