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;
 
 import static java.lang.String.format;
 import static org.kaazing.gateway.transport.http.HttpHeaders.HEADER_CONTENT_LENGTH;
 
 
     private static final String HEADER_CONTENT_TYPE = "Content-Type";
     private static final String CONTENT_TYPE_TEXT_PLAIN_CHARSET_UTF_8 = "text/plain; charset=utf-8";
 
     private static final String CODEC_FILTER = . + "#codec";
     private static final String UTF8_FILTER = . + "#utf8";
 
     private static final String LOGGER_NAME = String.format("transport.%s.accept".);
     private final Logger logger = LoggerFactory.getLogger();
 
     private final ResourceAddress nextProtocolAddress;
     private final WsebSession wsebSession;
     private final IoFilter codec;
     private final IoFilter utf8;
 
     public WsebUpstreamHandler(ResourceAddress nextProtocolAddress,  WsebSession wsebSessionint wsMaxMessageSize) {
         this(nextProtocolAddresswsebSessionnullwsMaxMessageSize);
     }
 
     public WsebUpstreamHandler(ResourceAddress nextProtocolAddressWsebSession wsebSessionEncoding utf8Encoding
                                int wsMaxMessageSize) {
         this. = nextProtocolAddress;
         this. = wsebSession;
         this. = new WsebDecodingCodecFilter(wsMaxMessageSize);
         this. = (utf8Encoding != null) ? new EncodingFilter(utf8Encoding) : null;
     }
 
     @Override
     protected void doSessionCreated(HttpAcceptSession sessionthrows Exception {
         // session create/open will be called in doSessionOpen
     }
 
     @Override
     protected void doSessionOpened(final HttpAcceptSession sessionthrows Exception {
         WsebSession wsebSession = getSession(session);
         if (wsebSession == null || wsebSession.isClosing()) {
             session.close(false);
             return;
         }
 
         IoFilterChain filterChain = session.getFilterChain();
 
         filterChain.addLast();
 
         // only supported for non-binary upstream
         if ( != null) {
             // Note: encoding filter needs to be closer to the network than the codec filter
             String contentType = session.getReadHeader();
             if (.equalsIgnoreCase(contentType)) {
                 filterChain.addBefore();
             }
         }
 
        final CloseFuture wsebCloseFuture = wsebSession.getCloseFuture();
        final IoFutureListener<CloseFuturelistener = new IoFutureListener<CloseFuture>() {
            @Override
            public void operationComplete(CloseFuture future) {
                // Note: this reference to HTTP session is pinned by listener
                //       and must be removed to avoid a memory leak (see below)
                session.close(false);
            }
        };
        // detect when emulated session is closed to force upstream to close
        wsebCloseFuture.addListener(listener);
        // detect when upstream is closed to remove upstream reference from emulated session
        session.getCloseFuture().addListener(new IoFutureListener<CloseFuture>() {
            @Override
            public void operationComplete(CloseFuture future) {
                // Note: a reference to the HTTP upstream session is pinned by listener
                //       and must be removed to avoid a memory leak (see above)
                wsebCloseFuture.removeListener(listener);
            }
        });
        wsebSession.attachReader(session);
    }
    @Override
    protected void doMessageReceived(HttpAcceptSession sessionObject messagethrows Exception {
        // this can happen if there is an error
        if (!(message instanceof WsMessage)) {
            return;
        }
        WsebSession wsebSession = getSession(session);
        WsMessage wsebMessage = (WsMessage)message;
        IoBufferEx data = wsebMessage.getBytes();
        
        if (wsebSession.getInactivityTimeout() > 0) {
            WsebInactivityTracker.messageReceived(wsebSessionwsebMessage);
        }
        switch (wsebMessage.getKind()) {
        case :
            IoFilterChain filterChain = wsebSession.getFilterChain();
            WsebBufferAllocator allocator = (WsebBufferAllocatorwsebSession.getBufferAllocator();
            WsBuffer wsBinaryBuffer = allocator.wrap(data.buf());
            filterChain.fireMessageReceived(wsBinaryBuffer);
            break;
        case :
            filterChain = wsebSession.getFilterChain();
            allocator = (WsebBufferAllocatorwsebSession.getBufferAllocator();
            WsBuffer wsTextBuffer = allocator.wrap(data.buf());
            wsTextBuffer.setKind(..);
            filterChain.fireMessageReceived(wsTextBuffer);
            break;
        case :
            for (Command command : ((WsCommandMessage)wsebMessage).getCommands()) {
                if (command == Command.close()) {
                    session.setWriteHeader("0");
                    session.close(false);
                    wsebSession.close(false);
                    break;
                }
                else if (command == Command.reconnect()) {
                    session.setWriteHeader("0");
                    session.close(false);
                    break;
                }
                // no-op (0x00) - continue reading commands
            }
            break;
        case :
            wsebSession.issuePongRequest();
            break;
        }
    }
    @Override
    protected void doExceptionCaught(HttpAcceptSession sessionThrowable causethrows Exception {
        WsebSession wseSession = getSession(session);
        if (wseSession != null && !wseSession.isClosing()) {
            wseSession.reset(cause);
        }
        else {
            if (.isDebugEnabled()) {
                String message = format("Exception while handling HTTP upstream for WsebSession: %s"cause);
                if (.isTraceEnabled()) {
                    // note: still debug level, but with extra detail about the exception
                    .debug(messagecause);
                }
                else {
                    .debug(message);
                }
            }
            session.close(true);
        }
    }
    @Override
    protected void doSessionClosed(HttpAcceptSession sessionthrows Exception {
        // session is long lived so we do not want to close it when the http session is closed
    }
    @Override
    protected void doSessionIdle(HttpAcceptSession sessionIdleStatus statusthrows Exception {
        // do not percolate idle
    }
    private WsebSession getSession(HttpAcceptSession sessionthrows Exception {
        boolean traceEnabled = .isTraceEnabled();
        if (traceEnabled) {
            .trace("Remote address resource = '"+session.getRemoteAddress().getResource()+"'");
        }
        return ;
    }
    protected final void removeFilter(IoFilterChain filterChainString name) {
        if (filterChain.contains(name)) {
            filterChain.remove(name);
        }
    }
    protected final void removeFilter(IoFilterChain filterChainIoFilter filter) {
        if (filterChain.contains(filter)) {
            filterChain.remove(filter);
        }
    }
New to GrepCode? Check out our FAQ X