Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed to the Apache Software Foundation (ASF) under one or more
   *  contributor license agreements.  See the NOTICE file distributed with
   *  this work for additional information regarding copyright ownership.
   *  The ASF licenses this file to You 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.tomcat.websocket.server;
 
 import static org.jboss.web.WebsocketsMessages.MESSAGES;
 
 import java.util.Map;
 
 
Servlet 3.1 HTTP upgrade handler for WebSocket connections.
 
 public class WsHttpUpgradeHandler implements HttpUpgradeHandler {
 
     private final ClassLoader applicationClassLoader;
 
     private Endpoint ep;
     private EndpointConfig endpointConfig;
     private String subProtocol;
     private Map<String,StringpathParameters;
     private boolean secure;
     private WebConnection connection;
 
     private WsSession wsSession;
 
 
     public WsHttpUpgradeHandler() {
     }
 
 
     public void preInit(Endpoint epEndpointConfig endpointConfig,
             WsServerContainer wscWsHandshakeRequest handshakeRequest,
             String subProtocolMap<String,StringpathParameters,
             boolean secure) {
         this. = ep;
         this. = endpointConfig;
         this. = wsc;
         this. = handshakeRequest;
         this. = subProtocol;
         this. = pathParameters;
         this. = secure;
     }
 
 
     @Override
     public void init(WebConnection connection) {
         if ( == null) {
             throw .noPreInit();
         }
 
         this. = connection;
 
         AbstractServletInputStream sis;
         AbstractServletOutputStream sos;
         try {
             sis = connection.getInputStream();
             sos = connection.getOutputStream();
         } catch (IOException e) {
             throw new IllegalStateException(e);
         }
 
         String httpSessionId = null;
         Object session = .getHttpSession();
         if (session != null ) {
            httpSessionId = ((HttpSessionsession).getId();
        }
        try {
            WsRemoteEndpointImplServer wsRemoteEndpointServer =
                    new WsRemoteEndpointImplServer(sos);
             = new WsSession(wsRemoteEndpointServer,
                    .getRequestURI(),
                    .getParameterMap(),
                    .getQueryString(),
                    .getUserPrincipal(), httpSessionId,
                    );
            WsFrameServer wsFrame = new WsFrameServer(
                    sis,
                    );
            sos.setWriteListener(
                    new WsWriteListener(thiswsRemoteEndpointServer));
            .onOpen();
            sis.setReadListener(new WsReadListener(thiswsFrame));
        } catch (DeploymentException e) {
            throw new IllegalArgumentException(e);
        }
    }
    @Override
    public void destroy() {
        if ( != null) {
            try {
                .close();
            } catch (Exception e) {
                ..destroyFailed(e);
            }
        }
    }
    private void onError(Throwable throwable) {
        .onError(throwable);
    }
    private void close(CloseReason cr) {
        /*
         * Any call to this method is a result of a problem reading from the
         * client. At this point that state of the connection is unknown.
         * Attempt to send a close frame to the client and then close the socket
         * immediately. There is no point in waiting for a close frame from the
         * client because there is no guarantee that we can recover from
         * whatever messed up state the client put the connection into.
         */
        .onClose(cr);
    }
    private static class WsReadListener implements ReadListener {
        private final WsHttpUpgradeHandler wsProtocolHandler;
        private final WsFrameServer wsFrame;
        private WsReadListener(WsHttpUpgradeHandler wsProtocolHandler,
                WsFrameServer wsFrame) {
            this. = wsProtocolHandler;
            this. = wsFrame;
        }
        @Override
        public void onDataAvailable() {
            try {
                .onDataAvailable();
            } catch (WsIOException ws) {
                .close(ws.getCloseReason());
            } catch (EOFException eof) {
                CloseReason cr = new CloseReason(
                        .eof.getMessage());
                .close(cr);
            } catch (IOException ioe) {
                onError(ioe);
                CloseReason cr = new CloseReason(
                        .ioe.getMessage());
                .close(cr);
            }
        }
        @Override
        public void onAllDataRead() {
            // Will never happen with WebSocket
            throw new IllegalStateException();
        }
        @Override
        public void onError(Throwable throwable) {
            .onError(throwable);
        }
    }
    private static class WsWriteListener implements WriteListener {
        private final WsHttpUpgradeHandler wsProtocolHandler;
        private final WsRemoteEndpointImplServer wsRemoteEndpointServer;
        private WsWriteListener(WsHttpUpgradeHandler wsProtocolHandler,
                WsRemoteEndpointImplServer wsRemoteEndpointServer) {
            this. = wsProtocolHandler;
            this. = wsRemoteEndpointServer;
        }
        @Override
        public void onWritePossible() {
            // Triggered by the poller so this isn't the same thread that
            // triggered the write so no need for a dispatch
            .onWritePossible(false);
        }
        @Override
        public void onError(Throwable throwable) {
            .onError(throwable);
            .close();
        }
    }
New to GrepCode? Check out our FAQ X