Search in sources :

Example 6 with DataChunk

use of org.glassfish.grizzly.http.util.DataChunk in project Payara by payara.

the class WebContainer method createHttpListener.

protected WebConnector createHttpListener(NetworkListener listener, HttpService httpService, Mapper mapper) {
    if (!Boolean.valueOf(listener.getEnabled())) {
        return null;
    }
    int port = 8080;
    WebConnector connector;
    checkHostnameUniqueness(listener.getName(), httpService);
    try {
        port = Integer.parseInt(listener.getPort());
    } catch (NumberFormatException nfe) {
        String msg = rb.getString(LogFacade.HTTP_LISTENER_INVALID_PORT);
        msg = MessageFormat.format(msg, listener.getPort(), listener.getName());
        throw new IllegalArgumentException(msg);
    }
    if (mapper == null) {
        for (Mapper m : habitat.<Mapper>getAllServices(Mapper.class)) {
            if (m.getPort() == port && m instanceof ContextMapper) {
                ContextMapper cm = (ContextMapper) m;
                if (listener.getName().equals(cm.getId())) {
                    mapper = m;
                    break;
                }
            }
        }
    }
    String defaultVS = listener.findHttpProtocol().getHttp().getDefaultVirtualServer();
    if (!defaultVS.equals(org.glassfish.api.web.Constants.ADMIN_VS)) {
        // Before we start a WebConnector, let's makes sure there is
        // not another Container already listening on that port
        DataChunk host = DataChunk.newInstance();
        char[] c = defaultVS.toCharArray();
        host.setChars(c, 0, c.length);
        DataChunk mb = DataChunk.newInstance();
        mb.setChars(new char[] { '/' }, 0, 1);
        MappingData md = new MappingData();
        try {
            mapper.map(host, mb, md);
        } catch (Exception e) {
            if (logger.isLoggable(Level.FINE)) {
                logger.log(Level.FINE, "", e);
            }
        }
        if (md.context != null && md.context instanceof ContextRootInfo) {
            ContextRootInfo r = (ContextRootInfo) md.context;
            if (!(r.getHttpHandler() instanceof ContainerMapper)) {
                new BindException("Port " + port + " is already used by Container: " + r.getHttpHandler() + " and will not get started.").printStackTrace();
                return null;
            }
        }
    }
    /*
         * Create Connector. Connector is SSL-enabled if
         * 'security-enabled' attribute in <http-listener>
         * element is set to TRUE.
         */
    boolean isSecure = Boolean.valueOf(listener.findHttpProtocol().getSecurityEnabled());
    if (isSecure && defaultRedirectPort == -1) {
        defaultRedirectPort = port;
    }
    String address = listener.getAddress();
    if ("any".equals(address) || "ANY".equals(address) || "INADDR_ANY".equals(address)) {
        address = null;
    /*
             * Setting 'address' to NULL will cause Tomcat to pass a
             * NULL InetAddress argument to the java.net.ServerSocket
             * constructor, meaning that the server socket will accept
             * connections on any/all local addresses.
             */
    }
    connector = (WebConnector) _embedded.createConnector(address, port, isSecure);
    connector.setMapper(mapper);
    connector.setJvmRoute(engine.getJvmRoute());
    if (logger.isLoggable(Level.INFO)) {
        logger.log(Level.INFO, LogFacade.HTTP_LISTENER_CREATED, new Object[] { listener.getName(), listener.getAddress(), listener.getPort() });
    }
    connector.setDefaultHost(listener.findHttpProtocol().getHttp().getDefaultVirtualServer());
    connector.setName(listener.getName());
    connector.setInstanceName(instanceName);
    connector.configure(listener, isSecure, httpService);
    _embedded.addConnector(connector);
    connectorMap.put(listener.getName(), connector);
    // If we already know the redirect port, then set it now
    // This situation will occurs when dynamic reconfiguration occurs
    String redirectPort = listener.findHttpProtocol().getHttp().getRedirectPort();
    if (redirectPort != null) {
        connector.setRedirectPort(Integer.parseInt(redirectPort));
    } else if (defaultRedirectPort != -1) {
        connector.setRedirectPort(defaultRedirectPort);
    }
    ObservableBean httpListenerBean = (ObservableBean) ConfigSupport.getImpl(listener);
    httpListenerBean.addListener(configListener);
    return connector;
}
Also used : BindException(java.net.BindException) LifecycleException(org.apache.catalina.LifecycleException) NamingException(javax.naming.NamingException) BindException(java.net.BindException) MalformedURLException(java.net.MalformedURLException) ContextRootInfo(org.glassfish.grizzly.config.ContextRootInfo) Mapper(org.glassfish.grizzly.http.server.util.Mapper) ContextMapper(org.glassfish.internal.grizzly.ContextMapper) ContainerMapper(com.sun.enterprise.v3.services.impl.ContainerMapper) MappingData(org.glassfish.grizzly.http.server.util.MappingData) DataChunk(org.glassfish.grizzly.http.util.DataChunk) ObservableBean(org.jvnet.hk2.config.ObservableBean) ContextMapper(org.glassfish.internal.grizzly.ContextMapper) ContainerMapper(com.sun.enterprise.v3.services.impl.ContainerMapper)

Example 7 with DataChunk

use of org.glassfish.grizzly.http.util.DataChunk in project Payara by payara.

the class MapperListener method unregisterContext.

/**
 * Unregister context.
 */
private void unregisterContext(ObjectName objectName) throws Exception {
    String name = objectName.getKeyProperty("name");
    String hostName = null;
    String contextName = null;
    if (name.startsWith("//")) {
        name = name.substring(2);
    }
    int slash = name.indexOf("/");
    if (slash != -1) {
        hostName = name.substring(0, slash);
        contextName = name.substring(slash);
        contextName = RequestUtil.urlDecode(contextName, "UTF-8");
    } else {
        return;
    }
    // Special case for the root context
    if (contextName.equals("/")) {
        contextName = "";
    }
    // Don't un-map a context that is paused
    DataChunk hostDC = DataChunk.newInstance();
    hostDC.setString(hostName);
    DataChunk contextDC = DataChunk.newInstance();
    contextDC.setString(contextName);
    MappingData mappingData = new MappingData();
    mapper.map(hostDC, contextDC, mappingData);
    if (mappingData.context instanceof StandardContext && ((StandardContext) mappingData.context).getPaused()) {
        return;
    }
    if (logger.isLoggable(Level.FINE)) {
        logger.log(Level.FINE, LogFacade.REGISTER_CONTEXT, contextName);
    }
    mapper.removeContext(hostName, contextName);
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) MappingData(org.glassfish.grizzly.http.server.util.MappingData) DataChunk(org.glassfish.grizzly.http.util.DataChunk) String(java.lang.String)

Example 8 with DataChunk

use of org.glassfish.grizzly.http.util.DataChunk in project Payara by payara.

the class SnifferAdapter method service.

// I could synchronize this method since I only start one container and do it
// synchronously but that seems like an overkill and I would still need to handle
// pending requests.
@Override
public void service(Request req, Response resp) throws Exception {
    if (adapter != null) {
        // this is not supposed to happen, however due to multiple requests coming in, I would
        // not be surprised...
        adapter.service(req, resp);
        return;
    }
    // different threads.
    synchronized (containerRegistry) {
        if (adapter != null) {
            // I got started in the meantime
            adapter.service(req, resp);
            return;
        }
        if (containerRegistry.getContainer(sniffer.getContainersNames()[0]) != null) {
            LOGGER.fine("Container is claimed to be started...");
            containerRegistry.getContainer(sniffer.getContainersNames()[0]).getContainer();
        } else {
            final long startTime = System.currentTimeMillis();
            LOGGER.log(Level.INFO, KernelLoggerInfo.snifferAdapterStartingContainer, sniffer.getModuleType());
            try {
                Collection<EngineInfo> containersInfo = containerStarter.startContainer(sniffer);
                if (containersInfo != null && !containersInfo.isEmpty()) {
                    // force the start on each container
                    for (EngineInfo info : containersInfo) {
                        if (LOGGER.isLoggable(Level.FINE)) {
                            LOGGER.log(Level.FINE, "Got container, deployer is {0}", info.getDeployer());
                        }
                        info.getContainer();
                        LOGGER.log(Level.INFO, KernelLoggerInfo.snifferAdapterContainerStarted, new Object[] { sniffer.getModuleType(), System.currentTimeMillis() - startTime });
                    }
                } else {
                    LOGGER.severe(KernelLoggerInfo.snifferAdapterNoContainer);
                }
            } catch (Exception e) {
                LOGGER.log(Level.SEVERE, KernelLoggerInfo.snifferAdapterExceptionStarting, new Object[] { sniffer.getContainersNames()[0], e });
            }
        }
        // at this point the post construct should have been called.
        // seems like there is some possibility that the container is not synchronously started
        // preventing the calls below to succeed...
        DataChunk decodedURI = req.getRequest().getRequestURIRef().getDecodedRequestURIBC();
        try {
            // Clear the previous mapped information.
            MappingData mappingData = (MappingData) req.getNote(ContainerMapper.MAPPING_DATA);
            mappingData.recycle();
            adapter = mapper.mapUriWithSemicolon(req, decodedURI, 0, null);
            // and throw a Runtime exception.
            if (adapter.equals(this)) {
                adapter = null;
                throw new RuntimeException("SnifferAdapter cannot map themself.");
            }
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, KernelLoggerInfo.snifferAdapterExceptionMapping, e);
            throw e;
        }
        // pass on,,,
        if (adapter != null) {
            adapter.service(req, resp);
        } else {
            throw new RuntimeException("No Adapter found.");
        }
    }
}
Also used : EngineInfo(org.glassfish.internal.data.EngineInfo) MappingData(org.glassfish.grizzly.http.server.util.MappingData) DataChunk(org.glassfish.grizzly.http.util.DataChunk)

Aggregations

DataChunk (org.glassfish.grizzly.http.util.DataChunk)8 MappingData (org.glassfish.grizzly.http.server.util.MappingData)5 CharChunk (org.glassfish.grizzly.http.util.CharChunk)3 Context (org.apache.catalina.Context)2 ProxyHandler (com.sun.appserv.ProxyHandler)1 ContainerMapper (com.sun.enterprise.v3.services.impl.ContainerMapper)1 CharConversionException (java.io.CharConversionException)1 IOException (java.io.IOException)1 String (java.lang.String)1 BindException (java.net.BindException)1 MalformedURLException (java.net.MalformedURLException)1 NamingException (javax.naming.NamingException)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 UnavailableException (javax.servlet.UnavailableException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpRequest (org.apache.catalina.HttpRequest)1 LifecycleException (org.apache.catalina.LifecycleException)1 Wrapper (org.apache.catalina.Wrapper)1