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     */
017    package org.apache.activemq.transport.stomp;
018    
019    import java.io.ByteArrayInputStream;
020    import java.io.IOException;
021    import java.net.Socket;
022    import java.net.URI;
023    import java.net.UnknownHostException;
024    import java.nio.ByteBuffer;
025    import java.security.cert.X509Certificate;
026    
027    import javax.net.SocketFactory;
028    
029    import org.apache.activemq.transport.nio.NIOSSLTransport;
030    import org.apache.activemq.wireformat.WireFormat;
031    
032    public class StompNIOSSLTransport extends NIOSSLTransport {
033    
034        StompCodec codec;
035    
036        private X509Certificate[] cachedPeerCerts;
037    
038        public StompNIOSSLTransport(WireFormat wireFormat, SocketFactory socketFactory, URI remoteLocation, URI localLocation) throws UnknownHostException, IOException {
039            super(wireFormat, socketFactory, remoteLocation, localLocation);
040        }
041    
042        public StompNIOSSLTransport(WireFormat wireFormat, Socket socket) throws IOException {
043            super(wireFormat, socket);
044        }
045    
046        @Override
047        protected void initializeStreams() throws IOException {
048            codec = new StompCodec(this);
049            super.initializeStreams();
050            if (inputBuffer.position() != 0 && inputBuffer.hasRemaining()) {
051                serviceRead();
052            }
053        }
054    
055        @Override
056        protected void processCommand(ByteBuffer plain) throws Exception {
057            byte[] fill = new byte[plain.remaining()];
058            plain.get(fill);
059            ByteArrayInputStream input = new ByteArrayInputStream(fill);
060            codec.parse(input, fill.length);
061        }
062    
063        @Override
064        public void doConsume(Object command) {
065            StompFrame frame = (StompFrame) command;
066    
067            if (cachedPeerCerts == null) {
068                cachedPeerCerts = getPeerCertificates();
069            }
070            frame.setTransportContext(cachedPeerCerts);
071    
072            super.doConsume(command);
073        }
074    }