001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.transport.amqp;
018
019import java.io.IOException;
020import java.net.Socket;
021import java.net.URI;
022import java.net.UnknownHostException;
023import java.nio.ByteBuffer;
024
025import javax.net.SocketFactory;
026import javax.net.ssl.SSLEngine;
027
028import org.apache.activemq.transport.nio.NIOSSLTransport;
029import org.apache.activemq.wireformat.WireFormat;
030
031public class AmqpNioSslTransport extends NIOSSLTransport {
032
033    private final AmqpFrameParser frameReader = new AmqpFrameParser(this);
034
035    public AmqpNioSslTransport(WireFormat wireFormat, SocketFactory socketFactory, URI remoteLocation, URI localLocation) throws UnknownHostException, IOException {
036        super(wireFormat, socketFactory, remoteLocation, localLocation);
037
038        frameReader.setWireFormat((AmqpWireFormat) wireFormat);
039    }
040
041    public AmqpNioSslTransport(WireFormat wireFormat, Socket socket) throws IOException {
042        super(wireFormat, socket, null, null, null);
043
044        frameReader.setWireFormat((AmqpWireFormat) wireFormat);
045    }
046
047    public AmqpNioSslTransport(WireFormat wireFormat, Socket socket,
048            SSLEngine engine, InitBuffer initBuffer, ByteBuffer inputBuffer) throws IOException {
049        super(wireFormat, socket, engine, initBuffer, inputBuffer);
050
051        frameReader.setWireFormat((AmqpWireFormat) wireFormat);
052    }
053
054    @Override
055    protected void initializeStreams() throws IOException {
056        super.initializeStreams();
057        if (inputBuffer.position() != 0 && inputBuffer.hasRemaining()) {
058            serviceRead();
059        }
060    }
061
062    @Override
063    protected void processCommand(ByteBuffer plain) throws Exception {
064        frameReader.parse(plain);
065    }
066
067    /* (non-Javadoc)
068     * @see org.apache.activemq.transport.nio.NIOSSLTransport#secureRead(java.nio.ByteBuffer)
069     */
070
071    @Override
072    protected void doInit() {
073        if (initBuffer != null) {
074            nextFrameSize = -1;
075            serviceRead();
076
077        }
078    }
079
080    @Override
081    protected int secureRead(ByteBuffer plain) throws Exception {
082        if (initBuffer != null) {
083            initBuffer.buffer.flip();
084            if (initBuffer.buffer.hasRemaining()) {
085                plain.flip();
086                for (int i =0; i < 8; i++) {
087                    plain.put(initBuffer.buffer.get());
088                }
089                plain.flip();
090                processCommand(plain);
091                initBuffer.buffer.clear();
092                return 8;
093            }
094        }
095        return super.secureRead(plain);
096    }
097
098
099}