View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.handler.codec.http.websocketx;
    17
    18  import org.jboss.netty.buffer.ChannelBuffer;
    19  import org.jboss.netty.buffer.ChannelBuffers;
    20  import org.jboss.netty.util.CharsetUtil;
    21
    22  /**
    23   * Web Socket continuation frame containing continuation text or binary data. This is used for
    24   * fragmented messages where the contents of a messages is contained more than 1 frame.
    25   */
    26  public class ContinuationWebSocketFrame extends WebSocketFrame {
    27
    28      private String aggregatedText;
    29
    30      /**
    31       * Creates a new empty continuation frame.
    32       */
    33      public ContinuationWebSocketFrame() {
    34          setBinaryData(ChannelBuffers.EMPTY_BUFFER);
    35      }
    36
    37      /**
    38       * Creates a new continuation frame with the specified binary data. The final fragment flag is
    39       * set to true.
    40       *
    41       * @param binaryData
    42       *            the content of the frame.
    43       */
    44      public ContinuationWebSocketFrame(ChannelBuffer binaryData) {
    45          setBinaryData(binaryData);
    46      }
    47
    48      /**
    49       * Creates a new continuation frame with the specified binary data
    50       *
    51       * @param finalFragment
    52       *            flag indicating if this frame is the final fragment
    53       * @param rsv
    54       *            reserved bits used for protocol extensions
    55       * @param binaryData
    56       *            the content of the frame.
    57       */
    58      public ContinuationWebSocketFrame(boolean finalFragment, int rsv, ChannelBuffer binaryData) {
    59          setFinalFragment(finalFragment);
    60          setRsv(rsv);
    61          setBinaryData(binaryData);
    62      }
    63
    64      /**
    65       * Creates a new continuation frame with the specified binary data
    66       *
    67       * @param finalFragment
    68       *            flag indicating if this frame is the final fragment
    69       * @param rsv
    70       *            reserved bits used for protocol extensions
    71       * @param binaryData
    72       *            the content of the frame.
    73       * @param aggregatedText
    74       *            Aggregated text set by decoder on the final continuation frame of a fragmented text message
    75       */
    76      public ContinuationWebSocketFrame(
    77              boolean finalFragment, int rsv, ChannelBuffer binaryData, String aggregatedText) {
    78          setFinalFragment(finalFragment);
    79          setRsv(rsv);
    80          setBinaryData(binaryData);
    81          this.aggregatedText = aggregatedText;
    82      }
    83
    84      /**
    85       * Creates a new continuation frame with the specified text data
    86       *
    87       * @param finalFragment
    88       *            flag indicating if this frame is the final fragment
    89       * @param rsv
    90       *            reserved bits used for protocol extensions
    91       * @param text
    92       *            text content of the frame.
    93       */
    94      public ContinuationWebSocketFrame(boolean finalFragment, int rsv, String text) {
    95          setFinalFragment(finalFragment);
    96          setRsv(rsv);
    97          setText(text);
    98      }
    99
    100     /**
    101      * Returns the text data in this frame
    102      */
    103     public String getText() {
    104         if (getBinaryData() == null) {
    105             return null;
    106         }
    107         return getBinaryData().toString(CharsetUtil.UTF_8);
    108     }
    109
    110     /**
    111      * Sets the string for this frame
    112      *
    113      * @param text
    114      *            text to store
    115      */
    116     public void setText(String text) {
    117         if (text == null || text.length() == 0) {
    118             setBinaryData(ChannelBuffers.EMPTY_BUFFER);
    119         } else {
    120             setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8));
    121         }
    122     }
    123
    124     @Override
    125     public String toString() {
    126         return getClass().getSimpleName() + "(data: " + getBinaryData() + ')';
    127     }
    128
    129     /**
    130      * Aggregated text returned by decoder on the final continuation frame of a fragmented text message
    131      */
    132     public String getAggregatedText() {
    133         return aggregatedText;
    134     }
    135
    136     public void setAggregatedText(String aggregatedText) {
    137         this.aggregatedText = aggregatedText;
    138     }
    139
    140 }