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