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.catalina.websocket;
 
 import java.io.Reader;
 
Base implementation of the class used to process WebSocket connections based on streams. Applications should extend this class to provide application specific functionality. Applications that wish to operate on a message basis rather than a stream basis should use MessageInbound.
 
 public abstract class StreamInbound {
 
     private final ClassLoader applicationClassLoader;
     private HttpEvent event = null;
     private WsOutbound outbound;
 
 
     public StreamInbound() {
     }
 
 
     public int getOutboundByteBufferSize() {
         return ;
     }
 
     public HttpEvent getEvent() {
         return ;
     }
 
     public void setEvent(HttpEvent event) {
         this. = event;
     }

    
This only applies to the WsOutbound instance returned from getWsOutbound() created by a subsequent call to #setUpgradeOutbound(UpgradeOutbound ). The current WsOutbound instance, if any, is not affected.

Parameters:
outboundByteBufferSize
 
     public void setOutboundByteBufferSize(int outboundByteBufferSize) {
         this. = outboundByteBufferSize;
     }
 
 
     public int getOutboundCharBufferSize() {
         return ;
     }


    
This only applies to the WsOutbound instance returned from getWsOutbound() created by a subsequent call to #setUpgradeOutbound(UpgradeOutbound ). The current WsOutbound instance, if any, is not affected.

Parameters:
outboundCharBufferSize
 
     public void setOutboundCharBufferSize(int outboundCharBufferSize) {
         this. = outboundCharBufferSize;
     }


    
Obtain the outbound side of this WebSocket connection used for writing data to the client.
 
     public final WsOutbound getWsOutbound() {
         return ;
     }
    public final void onData() throws IOException {
        // Must be start the start of a message (which may consist of multiple
        // frames)
        WsInputStream wsIs = new WsInputStream(getWsOutbound());
        try {
            WsFrame frame = wsIs.nextFrame(false);
            while (frame != null) {
                // TODO User defined extensions may define values for rsv
                if (frame.getRsv() > 0) {
                    closeOutboundConnection(
                            .null);
                    .close();
                }
                byte opCode = frame.getOpCode();
                if (opCode == .) {
                    onBinaryData(wsIs);
                } else if (opCode == .) {
                    InputStreamReader r =
                            new InputStreamReader(wsIsnew Utf8Decoder());
                    onTextData(r);
                } else if (opCode == .){
                    closeOutboundConnection(frame);
                    .close();
                } else if (opCode == .) {
                    getWsOutbound().pong(frame.getPayLoad());
                } else if (opCode == .) {
                    onPong(frame.getPayLoad());
                } else {
                    // Unknown OpCode
                    closeOutboundConnection(
                            .null);
                    .close();
                }
                frame = wsIs.nextFrame(false);
            }
        } catch (MalformedInputException mie) {
            // Invalid UTF-8
            closeOutboundConnection(.null);
            .close();
        } catch (UnmappableCharacterException uce) {
            // Invalid UTF-8
            closeOutboundConnection(.null);
            .close();
        } catch (IOException ioe) {
            // Given something must have gone to reach this point, this
            // might not work but try it anyway.
            .close();
        }
    }
    private void closeOutboundConnection(int statusByteBuffer datathrows IOException {
        try {
            getWsOutbound().close(statusdata);
        } finally {
            onClose(status);
        }
    }
    private void closeOutboundConnection(WsFrame framethrows IOException {
        try {
            getWsOutbound().close(frame);
        } finally {
            onClose(.);
        }
    }

    
Package private so the outbound connection can signal that the connection has been closed - usually due to an error.

Parameters:
status The WebSocket status code to report to the application
    void doOnClose(int status) {
        // Need to call onClose using the web application's class loader
        Thread t = Thread.currentThread();
        ClassLoader cl = t.getContextClassLoader();
        try {
            onClose(status);
        } finally {
            t.setContextClassLoader(cl);
        }
    }
    public final void onUpgradeComplete() {
         = new WsOutbound(this,
                );
        onOpen();
    }

    
Intended to be overridden by sub-classes that wish to be notified when the outbound connection is established. The default implementation is a NO-OP.

Parameters:
outbound The outbound WebSocket connection.
    protected void onOpen(WsOutbound outbound) {
        // NO-OP
    }

    
Intended to be overridden by sub-classes that wish to be notified when the outbound connection is closed. The default implementation is a NO-OP.

Parameters:
status The status code of the close reason.
    protected void onClose(int status) {
        // NO-OP
    }

    
Intended to be overridden by sub-classes that wish to be notified when a pong is received. The default implementation is a NO-OP.

Parameters:
payload The payload included in the pong.
    protected void onPong(ByteBuffer payload) {
        // NO-OP
    }

    
This method is called when there is a binary WebSocket message available to process. The message is presented via a stream and may be formed from one or more frames. The number of frames used to transmit the message is not made visible to the application.

Parameters:
is The WebSocket message
Throws:
java.io.IOException If a problem occurs processing the message. Any exception will trigger the closing of the WebSocket connection.
    protected abstract void onBinaryData(InputStream isthrows IOException;


    
This method is called when there is a textual WebSocket message available to process. The message is presented via a reader and may be formed from one or more frames. The number of frames used to transmit the message is not made visible to the application.

Parameters:
r The WebSocket message
Throws:
java.io.IOException If a problem occurs processing the message. Any exception will trigger the closing of the WebSocket connection.
    protected abstract void onTextData(Reader rthrows IOException;

    
This default implementation sets the read timeout to infinite and expects the WebSocket application to close the connection when it is no longer required. Applications wishing to set an explicit timeout may override this method and return a value of their choice.

Returns:
The read timeout in milliseconds or -1 for infinite
    public int getReadTimeout() {
        return -1;
    }
New to GrepCode? Check out our FAQ X