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.stomp;
018
019import java.io.ByteArrayInputStream;
020import java.io.IOException;
021import java.net.Socket;
022import java.net.URI;
023import java.net.UnknownHostException;
024import java.nio.ByteBuffer;
025import java.security.cert.X509Certificate;
026
027import javax.net.SocketFactory;
028import javax.net.ssl.SSLEngine;
029
030import org.apache.activemq.transport.nio.NIOSSLTransport;
031import org.apache.activemq.wireformat.WireFormat;
032
033public class StompNIOSSLTransport extends NIOSSLTransport {
034
035    StompCodec codec;
036
037    private X509Certificate[] cachedPeerCerts;
038
039    public StompNIOSSLTransport(WireFormat wireFormat, SocketFactory socketFactory, URI remoteLocation, URI localLocation) throws UnknownHostException, IOException {
040        super(wireFormat, socketFactory, remoteLocation, localLocation);
041    }
042
043    public StompNIOSSLTransport(WireFormat wireFormat, Socket socket) throws IOException {
044        super(wireFormat, socket, null, null, null);
045    }
046
047
048
049    public StompNIOSSLTransport(WireFormat wireFormat, Socket socket,
050            SSLEngine engine, InitBuffer initBuffer, ByteBuffer inputBuffer) throws IOException {
051        super(wireFormat, socket, engine, initBuffer, inputBuffer);
052    }
053
054    @Override
055    protected void initializeStreams() throws IOException {
056        codec = new StompCodec(this);
057        super.initializeStreams();
058        if (inputBuffer.position() != 0 && inputBuffer.hasRemaining()) {
059            serviceRead();
060        }
061    }
062
063    @Override
064    protected void processCommand(ByteBuffer plain) throws Exception {
065        byte[] fill = new byte[plain.remaining()];
066        plain.get(fill);
067        ByteArrayInputStream input = new ByteArrayInputStream(fill);
068        codec.parse(input, fill.length);
069    }
070
071    @Override
072    public void doConsume(Object command) {
073        StompFrame frame = (StompFrame) command;
074
075        if (cachedPeerCerts == null) {
076            cachedPeerCerts = getPeerCertificates();
077        }
078        frame.setTransportContext(cachedPeerCerts);
079
080        super.doConsume(command);
081    }
082
083    /* (non-Javadoc)
084     * @see org.apache.activemq.transport.nio.NIOSSLTransport#doInit()
085     */
086    @Override
087    protected void doInit() throws Exception {
088        if (initBuffer != null) {
089            nextFrameSize = -1;
090           // System.out.println("length1: " + initBuffer.array().length);
091            receiveCounter += initBuffer.readSize;
092            initBuffer.buffer.flip();
093            processCommand(initBuffer.buffer);
094        }
095    }
096}