Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved. 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.kaazing.gateway.transport.wseb.filter;
 
 import static org.kaazing.gateway.transport.bridge.CachingMessageEncoder.IO_MESSAGE_ENCODER;
 
 
 
 public class WsebFrameEncoder extends AbstractWsFrameEncoder {
 
     private static final byte[] TO_HEX = {'0''1''2''3''4''5''6''7''8''9''A''B''C''D''E''F'};
 
     private static final byte COMMAND_TYPE_BYTE  = (byte)0x01;
     private static final byte[] EMPTY_PING_BYTES = new byte[]{(byte)0x89, (byte)0x00};
     private static final byte[] EMPTY_PONG_BYTES = new byte[]{(byte)0x8A, (byte)0x00};
 
 	/* Block Padding is used in cases where we need to guarantee that at least 4K of data arrives
 	 * to ensure timely delivery in buffering clients (e.g. Android)
 	 */
 	public static final WsMessage BLOCK_PADDING_MESSAGE;
 	static {
     	// each command uses 2 bytes on the wire so we need half as many commands as padding bytes
     	int len = 4096 / 2 - 2;
     	Command[] commands = new Command[len];
     	for (int i = 0; i < leni++) {
     		commands[i] = Command.noop();
     	}
          = new WsCommandMessage(commands);
 	}
 
 
     public WsebFrameEncoder(IoBufferAllocatorEx<?> allocator) {
         this(allocator);
     }
 
     public WsebFrameEncoder(CachingMessageEncoder cachingEncoderIoBufferAllocatorEx<?> allocator) {
         super(cachingEncoderallocator);
     }
 
     @Override
     protected IoBufferEx doEncode(IoBufferAllocatorEx<?> allocatorint flagsWsMessage message) {
         IoBufferEx buf = message.getBytes();
         byte[] escapeBytes = null;
         boolean escaping = false;
 
         // TODO: fix WsMessage inheritance hierarchy so WsCommandMessage doesn't have getBytes method
         if (buf != null &&  != null ) {
             EscapeSequencer escapeSequencer = .getEscapeSequencer(message.getKind());
             if (escapeSequencer != null) {
                 escapeBytes = escapeSequencer.getEscapeBytes(buf);
                 escaping = escapeBytes.length > 0;
             }
         }
 
         switch (message.getKind()) {
             case : {
                 if (escaping) {
                     return doBinaryEscapedEncode(allocatorflagsmessageescapeBytes);
                 } else {
                     return doBinaryEncode(allocatorflagsmessage);
                 }
             }
             case : {
                 if (escaping) {
                     return doTextEscapedEncode(allocatorflagsmessageescapeBytes);
                 } else {
                    return doTextEncode(allocatorflagsmessage);
                }
            }
            case : {
                return doCommandEncode(allocatorflagsmessage);
            }
            case : {
                return doPingEncode(allocatorflagsmessage);
            }
            case : {
                return doPongEncode(allocatorflagsmessage);
            }
            default:
                throw new IllegalStateException("Unrecognized frame type: " + message.getKind());
        }
    }
    private IoBufferEx doCommandEncode(IoBufferAllocatorEx<?> allocatorint flagsWsMessage message) {
        // check for special block padding message
        if (message == ) {
            // wrap using allocator (OK) since we need an exact == match in HttpGzipEncoder later
            return allocator.wrap(ByteBuffer.wrap(.), flags);
        }
        WsCommandMessage wsebCommand = (WsCommandMessage)message;
        Command[] commands = wsebCommand.getCommands();
        ByteBuffer text = allocator.allocate(2 * commands.length + 2, flags);
        int offset = text.position();
        text.put();
        for (int i=0; i < commands.lengthi++) {
            int value = commands[i].value();
            text.put([(value >> 4) & 0x000F]);
            text.put([value & 0x000F]);
        }
        text.put();
        text.flip();
        text.position(offset);
        return allocator.wrap(textflags);
    }
    protected IoBufferEx doPingEncode(IoBufferAllocatorEx<?> allocatorint flagsWsMessage message) {
        WsPingMessage ping = (WsPingMessage)message;
        assert ping.getBytes().remaining() == 0 : "PING with payload not supported";
        ByteBuffer text = allocator.allocate(.flags);
        int offset = text.position();
        text.put();
        text.flip();
        text.position(offset);
        return allocator.wrap(textflags);
    }
    protected IoBufferEx doPongEncode(IoBufferAllocatorEx<?> allocatorint flagsWsMessage message) {
        WsPongMessage ping = (WsPongMessage)message;
        assert ping.getBytes().remaining() == 0 : "PONG with payload not supported";
        ByteBuffer text = allocator.allocate(.flags);
        int offset = text.position();
        text.put();
        text.flip();
        text.position(offset);
        return allocator.wrap(textflags);
    }
    protected IoBufferEx doTextEscapedEncode(IoBufferAllocatorEx<?> allocatorint flagsWsMessage messagebyte[] escapedBytes) {
        return WsDraftHixieFrameEncodingSupport.doSpecifiedLengthTextEscapedEncode(allocatorflagsmessageescapedBytes);
    }
    @Override
    protected IoBufferEx doContinuationEscapedEncode(IoBufferAllocatorEx<?> ioBufferAllocatorExint iWsMessage wsMessagebyte[] bytes) {
        throw new UnsupportedOperationException();
    }
    protected IoBufferEx doBinaryEscapedEncode(IoBufferAllocatorEx<?> allocatorint flagsWsMessage messagebyte[] escapedBytes) {
        return WsDraftHixieFrameEncodingSupport.doBinaryEscapedEncode(allocatorflagsmessageescapedBytes);
    }
    protected IoBufferEx doTextEncode(IoBufferAllocatorEx<?> allocatorint flagsWsMessage message) {
        return WsDraftHixieFrameEncodingSupport.doSpecifiedLengthTextEncode(allocatorflagsmessage);
    }
    @Override
    protected IoBufferEx doContinuationEncode(IoBufferAllocatorEx<?> ioBufferAllocatorExint iWsMessage wsMessage) {
        throw new UnsupportedOperationException();
    }
    protected IoBufferEx doBinaryEncode(IoBufferAllocatorEx<?> allocatorint flagsWsMessage message) {
        return WsDraftHixieFrameEncodingSupport.doBinaryEncode(allocatorflagsmessage);
    }
    protected IoBufferEx doCloseEncode(IoBufferAllocatorEx<?> allocatorint flagsWsCloseMessage message) {
        return WsDraftHixieFrameEncodingSupport.doCloseEncode(allocatorflags);
    }
New to GrepCode? Check out our FAQ X