Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.coyote.http11;
 
 
 
Abstract the protocol implementation, including threading, etc. Processor is single threaded and specific to stream-based protocols, will not fit Jk protocols like JNI.

Author(s):
Remy Maucherat
Costin Manolache
 
 public class Http11Protocol 
     implements ProtocolHandlerMBeanRegistration {
 
 
     // ------------------------------------------------------------ Constructor
 
 
     public Http11Protocol() {
         //setServerSoTimeout(Constants.DEFAULT_SERVER_SOCKET_TIMEOUT);
     }
 
     
     // ----------------------------------------------------------------- Fields
 
 
     protected Http11ConnectionHandler cHandler = new Http11ConnectionHandler(this);
     protected JIoEndpoint endpoint = new JIoEndpoint();
 
 
     // *
     protected ObjectName tpOname = null;
     // *
     protected ObjectName rgOname = null;
 
 
     protected ServerSocketFactory socketFactory = null;
     protected JSSEImplementation sslImplementation = null;
 
 
     // ----------------------------------------- ProtocolHandler Implementation
     // *
 
 
     protected HashMap<StringObjectattributes = new HashMap<StringObject>();

    
    
Pass config info
    public void setAttribute(String nameObject value) {
        .put(namevalue);
    }
    public Object getAttribute(String key) {
        return .get(key);
    }
    public Iterator getAttributeNames() {
        return .keySet().iterator();
    }

    
Set a property.
    public void setProperty(String nameString value) {
        setAttribute(namevalue);
    }

    
Get a property
    public String getProperty(String name) {
        return (String)getAttribute(name);
    }

    
The adapter, used to call the connector.
    protected Adapter adapter;
    public void setAdapter(Adapter adapter) { this. = adapter; }
    public Adapter getAdapter() { return ; }
    public boolean hasIoEvents() {
        return false;
    }
        return .;
    }
    public void init() throws Exception {
        .setName(getName());
        .setHandler();
        // Verify the validity of the configured socket factory
        try {
            if (isSSLEnabled()) {
                 =
                        (JSSEImplementation) SSLImplementation.getInstance();
                 = .getServerSocketFactory();
                .setServerSocketFactory();
            } else if ( != null) {
                 = (ServerSocketFactory) Class.forName().newInstance();
                .setServerSocketFactory();
            }
        } catch (Exception ex) {
            throw ex;
        }
        if (!=null) {
            Iterator<StringattE = .keySet().iterator();
            whileattE.hasNext() ) {
                String key = attE.next();
                Object v=.get(key);
                .setAttribute(keyv);
            }
        }
        
        try {
            .init();
        } catch (Exception ex) {
            throw ex;
        }
    }
    public void start() throws Exception {
        if (.....) {
            if (this. != null) {
                try {
                     = new ObjectName
                    ( + ":" + "type=ThreadPool,name=" + getJmxName());
                    Registry.getRegistry(nullnull)
                    .registerComponent(null );
                } catch (Exception e) {
                    ..errorRegisteringPool(e);
                }
                =new ObjectName
                ( + ":type=GlobalRequestProcessor,name=" + getJmxName());
                Registry.getRegistry(nullnull).registerComponent
                ( .null );
            }
        }
        try {
            .start();
        } catch (Exception ex) {
            throw ex;
        }
    }
    public void pause() throws Exception {
        try {
            .pause();
        } catch (Exception ex) {
            throw ex;
        }
        // Wait for a while until all the processors are no longer processing requests
        RequestInfo[] states = ..getRequestProcessors();
        int retry = 0;
        boolean done = false;
        while (!done && retry < ....) {
            retry++;
            done = true;
            for (int i = 0; i < states.lengthi++) {
                if (states[i].getStage() == ....) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        ;
                    }
                    done = false;
                    break;
                }
            }
        }
    }
    public void resume() throws Exception {
        try {
            .resume();
        } catch (Exception ex) {
            throw ex;
        }
    }
    public void destroy() throws Exception {
        .destroy();
        if (.....) {
            if (!=null)
                Registry.getRegistry(nullnull).unregisterComponent();
            if ( != null)
                Registry.getRegistry(nullnull).unregisterComponent();
        }
    }
    public String getJmxName() {
        String encodedAddr = "";
        if (getAddress() != null) {
            encodedAddr = "" + getAddress();
            encodedAddr = URLEncoder.encode(encodedAddr.replace('/''-').replace(':''_').replace('%''-')) + "-";
        }
        return ("http-" + encodedAddr + .getPort());
    }
    public String getName() {
        String encodedAddr = "";
        if (getAddress() != null) {
            encodedAddr = getAddress() + ":";
        }
        return ("http-" + encodedAddr + .getPort());
    }
    // ------------------------------------------------------------- Properties

    
    
Processor cache.
    protected int processorCache = -1;
    public int getProcessorCache() { return this.; }
    public void setProcessorCache(int processorCache) { this. = processorCache; }
    protected int socketBuffer = 9000;
    public int getSocketBuffer() { return ; }
    public void setSocketBuffer(int socketBuffer) { this. = socketBuffer; }

    
This field indicates if the protocol is secure from the perspective of the client (= https is used).
    protected boolean secure;
    public boolean getSecure() { return ; }
    public void setSecure(boolean b) {  = b; }
    protected boolean SSLEnabled = false;
    public boolean isSSLEnabled() { return ;}
    public void setSSLEnabled(boolean SSLEnabled) {this. = SSLEnabled;}    
    
    
Name of the socket factory.
    protected String socketFactoryName = null;
    public String getSocketFactory() { return ; }
    public void setSocketFactory(String valueS) {  = valueS; }
    
    
Name of the SSL implementation.
    protected String sslImplementationName=null;
    public String getSSLImplementation() { return ; }
    public void setSSLImplementationString valueS) {
         = valueS;
        setSecure(true);
    }
    
    
    // HTTP
    
Maximum number of requests which can be performed over a keepalive connection. The default is the same as for Apache HTTP Server.
    protected int maxKeepAliveRequests = (.....) ? 1 : 
        Integer.valueOf(System.getProperty("org.apache.coyote.http11.Http11Protocol.MAX_KEEP_ALIVE_REQUESTS""-1")).intValue();
    public int getMaxKeepAliveRequests() { return ; }
    public void setMaxKeepAliveRequests(int mkar) {  = mkar; }
    // HTTP
    
The number of seconds Tomcat will wait for a subsequent request before closing the connection. The default is the same as for Apache HTTP Server (15 000 milliseconds).
    protected int keepAliveTimeout = -1;
    public int getKeepAliveTimeout() { return ; }
    public void setKeepAliveTimeout(int timeout) {  = timeout; }
    // HTTP
    
This timeout represents the socket timeout which will be used while the adapter execution is in progress, unless disableUploadTimeout is set to true. The default is the same as for Apache HTTP Server (300 000 milliseconds).
    protected int timeout = 300000;
    public int getTimeout() { return ; }
    public void setTimeout(int timeout) { this. = timeout; }
    // *
    
Maximum size of the post which will be saved when processing certain requests, such as a POST.
    protected int maxSavePostSize = 4 * 1024;
    public int getMaxSavePostSize() { return ; }
    public void setMaxSavePostSize(int valueI) {  = valueI; }
    // HTTP
    
Maximum size of the HTTP message header.
    protected int maxHttpHeaderSize = Integer.valueOf(System.getProperty("org.apache.coyote.http11.Http11Protocol.MAX_HEADER_SIZE""8192")).intValue();
    public int getMaxHttpHeaderSize() { return ; }
    public void setMaxHttpHeaderSize(int valueI) {  = valueI; }
    // HTTP
    
If true, the regular socket timeout will be used for the full duration of the connection.
    public boolean getDisableUploadTimeout() { return ; }
    public void setDisableUploadTimeout(boolean isDisabled) {  = isDisabled; }
    // HTTP
    
Integrated compression support.
    protected String compression = System.getProperty("org.apache.coyote.http11.Http11Protocol.COMPRESSION""off");
    public String getCompression() { return ; }
    public void setCompression(String valueS) {  = valueS; }
    
    
    // HTTP
    protected String noCompressionUserAgents = System.getProperty("org.apache.coyote.http11.Http11Protocol.COMPRESSION_RESTRICTED_UA");
    public void setNoCompressionUserAgents(String valueS) {  = valueS; }
    
    // HTTP
    protected String compressableMimeTypes = System.getProperty("org.apache.coyote.http11.Http11Protocol.COMPRESSION_MIME_TYPES""text/html,text/xml,text/plain");
    public String getCompressableMimeType() { return ; }
    public void setCompressableMimeType(String valueS) {  = valueS; }
    
    
    // HTTP
    protected int compressionMinSize = Integer.valueOf(System.getProperty("org.apache.coyote.http11.Http11Protocol.COMPRESSION_MIN_SIZE""2048")).intValue();
    public int getCompressionMinSize() { return ; }
    public void setCompressionMinSize(int valueI) {  = valueI; }
    // HTTP
    
User agents regular expressions which should be restricted to HTTP/1.0 support.
    protected String restrictedUserAgents = null;
    public String getRestrictedUserAgents() { return ; }
    public void setRestrictedUserAgents(String valueS) {  = valueS; }
    
    // HTTP
    
Server header.
    protected String server = System.getProperty("org.apache.coyote.http11.Http11Protocol.SERVER");
    public void setServerString server ) { this. = server; }
    public String getServer() { return ; }
    public Executor getExecutor() { return .getExecutor(); }
    public void setExecutor(Executor executor) { .setExecutor(executor); }
    
    public int getMaxThreads() { return .getMaxThreads(); }
    public void setMaxThreads(int maxThreads) { .setMaxThreads(maxThreads); }
    public int getThreadPriority() { return .getThreadPriority(); }
    public void setThreadPriority(int threadPriority) { .setThreadPriority(threadPriority); }
    public int getBacklog() { return .getBacklog(); }
    public void setBacklog(int backlog) { .setBacklog(backlog); }
    public int getPort() { return .getPort(); }
    public void setPort(int port) { .setPort(port); }
    public InetAddress getAddress() { return .getAddress(); }
    public void setAddress(InetAddress ia) { .setAddress(ia); }
    public boolean getTcpNoDelay() { return .getTcpNoDelay(); }
    public void setTcpNoDelay(boolean tcpNoDelay) { .setTcpNoDelay(tcpNoDelay); }
    public int getSoLinger() { return .getSoLinger(); }
    public void setSoLinger(int soLinger) { .setSoLinger(soLinger); }
    public int getSoTimeout() { return .getSoTimeout(); }
    public void setSoTimeout(int soTimeout) { .setSoTimeout(soTimeout); }
    public void setPollerSize(int pollerSize) { .setPollerSize(pollerSize); }
    public int getPollerSize() { return .getPollerSize(); }
    // HTTP
    
Return the Keep-Alive policy for the connection.
    public boolean getKeepAlive() {
        return (( != 0) && ( != 1));
    }
    // HTTP
    
Set the keep-alive policy for this connection.
    public void setKeepAlive(boolean keepAlive) {
        if (!keepAlive) {
            setMaxKeepAliveRequests(1);
        }
    }
    /*
     * Note: All the following are JSSE/java.io specific attributes.
     */
    
    public String getKeystore() {
        return (StringgetAttribute("keystore");
    }
    public void setKeystoreString k ) {
        setAttribute("keystore"k);
    }
    public String getKeypass() {
        return (StringgetAttribute("keypass");
    }
    public void setKeypassString k ) {
        .put("keypass"k);
        //setAttribute("keypass", k);
    }
    public String getKeytype() {
        return (StringgetAttribute("keystoreType");
    }
    public void setKeytypeString k ) {
        setAttribute("keystoreType"k);
    }
    public String getClientauth() {
        return (StringgetAttribute("clientauth");
    }
    public void setClientauthString k ) {
        setAttribute("clientauth"k);
    }
    public String getProtocols() {
        return (StringgetAttribute("protocols");
    }
    public void setProtocols(String k) {
        setAttribute("protocols"k);
    }
    public String getAlgorithm() {
        return (StringgetAttribute("algorithm");
    }
    public void setAlgorithmString k ) {
        setAttribute("algorithm"k);
    }
    public String getCiphers() {
        return (StringgetAttribute("ciphers");
    }
    public void setCiphers(String ciphers) {
        setAttribute("ciphers"ciphers);
    }
    public String getKeyAlias() {
        return (StringgetAttribute("keyAlias");
    }
    public void setKeyAlias(String keyAlias) {
        setAttribute("keyAlias"keyAlias);
    }
    public SSLContext getSSLContext() {
        return (SSLContextgetAttribute("SSLContext");
    }
    public void setSSLContext(SSLContext sslContext) {
        setAttribute("SSLContext"sslContext);
    }
    // -----------------------------------  Http11ConnectionHandler Inner Class
    protected static class Http11ConnectionHandler implements Handler {
        protected Http11Protocol proto;
        protected AtomicLong registerCount = new AtomicLong(0);
        protected RequestGroupInfo global = new RequestGroupInfo();
        protected ConcurrentHashMap<SocketHttp11Processorconnections =
            new ConcurrentHashMap<SocketHttp11Processor>();
            new ConcurrentLinkedQueue<Http11Processor>() {
            protected AtomicInteger size = new AtomicInteger(0);
            public boolean offer(Http11Processor processor) {
                boolean offer = (. == -1) ? true : (.get() < .);
                //avoid over growing our cache or add after we have stopped
                boolean result = false;
                if ( offer ) {
                    result = super.offer(processor);
                    if ( result ) {
                        .incrementAndGet();
                    }
                }
                if (!resultunregister(processor);
                return result;
            }
            
            public Http11Processor poll() {
                Http11Processor result = super.poll();
                if ( result != null ) {
                    .decrementAndGet();
                }
                return result;
            }
            
            public void clear() {
                Http11Processor next = poll();
                while ( next != null ) {
                    unregister(next);
                    next = poll();
                }
                super.clear();
                .set(0);
            }
        };
        Http11ConnectionHandler(Http11Protocol proto) {
            this. = proto;
        }
        public SocketState event(Socket socketSocketStatus status) {
            Http11Processor result = .get(socket);
            SocketState state = .
            if (result != null) {
                result.startProcessing();
                // Call the appropriate event
                try {
                    state = result.event(status);
                } catch (java.net.SocketException e) {
                    // SocketExceptions are normal
                    ..socketException(e);
                } catch (java.io.IOException e) {
                    // IOExceptions are normal
                    ..socketException(e);
                }
                // Future developers: if you discover any other
                // rare-but-nonfatal exceptions, catch them here, and log as
                // above.
                catch (Throwable e) {
                    // any other exception or error is odd. Here we log it
                    // with "ERROR" level, so it will show up even on
                    // less-than-verbose logs.
                    ..socketError(e);
                } finally {
                    if (state != .) {
                        .remove(socket);
                        .offer(result);
                    } else {
                        if (..isRunning()) {
                            ..getEventPoller().add(socketresult.getTimeout(), 
                                    result.getResumeNotification(), false);
                        }
                    }
                    result.endProcessing();
                }
            }
            return state;
        }
        
        public SocketState process(Socket socket) {
            Http11Processor processor = .poll();
            try {
                if (processor == null) {
                    processor = createProcessor();
                }
                if (. && (. != null)) {
                    processor.setSSLSupport
                        (..getSSLSupport(socket));
                } else {
                    processor.setSSLSupport(null);
                }
                
                SocketState state = processor.process(socket);
                if (state == .) {
                    // Associate the connection with the processor. The next request 
                    // processed by this thread will use either a new or a recycled
                    // processor.
                    .put(socketprocessor);
                    ..getEventPoller().add(socketprocessor.getTimeout(), 
                            processor.getResumeNotification(), false);
                } else {
                    .offer(processor);
                }
                return state;
            } catch(java.net.SocketException e) {
                // SocketExceptions are normal
                ..socketException(e);
            } catch (java.io.IOException e) {
                // IOExceptions are normal
                ..socketException(e);
            }
            // Future developers: if you discover any other
            // rare-but-nonfatal exceptions, catch them here, and log as
            // above.
            catch (Throwable e) {
                // any other exception or error is odd. Here we log it
                // with "ERROR" level, so it will show up even on
                // less-than-verbose logs.
                ..socketError(e);
            }
            .offer(processor);
            return .;
        }
        
        protected Http11Processor createProcessor() {
            Http11Processor processor =
                new Http11Processor(..);
            processor.setAdapter(.);
            processor.setMaxKeepAliveRequests(.);
            processor.setKeepAliveTimeout(.);
            processor.setTimeout(.);
            processor.setDisableUploadTimeout(.);
            processor.setCompressionMinSize(.);
            processor.setCompression(.);
            processor.setNoCompressionUserAgents(.);
            processor.setCompressableMimeTypes(.);
            processor.setRestrictedUserAgents(.);
            processor.setSocketBuffer(.);
            processor.setMaxSavePostSize(.);
            processor.setServer(.);
            register(processor);
            return processor;
        }
        
        protected void register(Http11Processor processor) {
            RequestInfo rp = processor.getRequest().getRequestProcessor();
            rp.setGlobalProcessor();
            if (..... && .getDomain() != null) {
                synchronized (this) {
                    try {
                        long count = .incrementAndGet();
                        ObjectName rpName = new ObjectName
                            (.getDomain() + ":type=RequestProcessor,worker="
                                + .getJmxName() + ",name=HttpRequest" + count);
                        Registry.getRegistry(nullnull).registerComponent(rprpNamenull);
                        rp.setRpName(rpName);
                    } catch (Exception e) {
                        ..errorRegisteringRequest(e);
                    }
                }
            }
        }
        protected void unregister(Http11Processor processor) {
            RequestInfo rp = processor.getRequest().getRequestProcessor();
            rp.setGlobalProcessor(null);
            if (..... && .getDomain() != null) {
                synchronized (this) {
                    try {
                        ObjectName rpName = rp.getRpName();
                        Registry.getRegistry(nullnull).unregisterComponent(rpName);
                        rp.setRpName(null);
                    } catch (Exception e) {
                        ..errorUnregisteringRequest(e);
                    }
                }
            }
        }
    }
    // -------------------- JMX related methods --------------------
    // *
    protected String domain;
    protected ObjectName oname;
    protected MBeanServer mserver;
    public ObjectName getObjectName() {
        return ;
    }
    public String getDomain() {
        return ;
    }
    public ObjectName preRegister(MBeanServer server,
                                  ObjectName namethrows Exception {
        =name;
        =server;
        =name.getDomain();
        return name;
    }
    public void postRegister(Boolean registrationDone) {
    }
    public void preDeregister() throws Exception {
    }
    public void postDeregister() {
    }
New to GrepCode? Check out our FAQ X