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.IOException;
020    import java.security.cert.X509Certificate;
021    
022    import javax.jms.JMSException;
023    
024    import org.apache.activemq.command.Command;
025    
026    import org.apache.activemq.transport.Transport;
027    import org.apache.activemq.transport.TransportFilter;
028    import org.apache.activemq.transport.TransportListener;
029    import org.apache.activemq.transport.tcp.SslTransport;
030    import org.apache.activemq.util.IOExceptionSupport;
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.springframework.context.ApplicationContext;
034    
035    /**
036     * The StompTransportFilter normally sits on top of a TcpTransport that has been
037     * configured with the StompWireFormat and is used to convert STOMP commands to
038     * ActiveMQ commands. All of the conversion work is done by delegating to the
039     * ProtocolConverter.
040     * 
041     * @author <a href="http://hiramchirino.com">chirino</a>
042     */
043    public class StompTransportFilter extends TransportFilter {
044        private static final Log LOG = LogFactory.getLog(StompTransportFilter.class);
045        private final ProtocolConverter protocolConverter;
046        private final FrameTranslator frameTranslator;
047    
048        private boolean trace;
049    
050        public StompTransportFilter(Transport next, FrameTranslator translator, ApplicationContext applicationContext) {
051            super(next);
052            this.frameTranslator = translator;
053            this.protocolConverter = new ProtocolConverter(this, translator, applicationContext);
054        }
055    
056        public void oneway(Object o) throws IOException {
057            try {
058                final Command command = (Command)o;
059                protocolConverter.onActiveMQCommand(command);
060            } catch (JMSException e) {
061                throw IOExceptionSupport.create(e);
062            }
063        }
064    
065        public void onCommand(Object command) {
066            try {
067                if (trace) {
068                    LOG.trace("Received: \n" + command);
069                }
070               
071                protocolConverter.onStompCommand((StompFrame)command);
072            } catch (IOException e) {
073                onException(e);
074            } catch (JMSException e) {
075                onException(IOExceptionSupport.create(e));
076            }
077        }
078    
079        public void sendToActiveMQ(Command command) {
080            TransportListener l = transportListener;
081            if (l!=null) {
082                l.onCommand(command);
083            }
084        }
085    
086        public void sendToStomp(StompFrame command) throws IOException {
087            if (trace) {
088                LOG.trace("Sending: \n" + command);
089            }
090            Transport n = next;
091            if (n!=null) {
092                n.oneway(command);
093            }
094        }
095    
096        public FrameTranslator getFrameTranslator() {
097            return frameTranslator;
098        }
099    
100        public X509Certificate[] getPeerCertificates() {
101            if(next instanceof SslTransport) {      
102                    X509Certificate[] peerCerts = ((SslTransport)next).getPeerCertificates();
103                    if (trace && peerCerts != null) {
104                    LOG.debug("Peer Identity has been verified\n");
105                }
106                    return peerCerts;
107            }
108            return null;
109        }
110        
111        public boolean isTrace() {
112            return trace;
113        }
114    
115        public void setTrace(boolean trace) {
116            this.trace = trace;
117        }
118    }