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.http;
018    
019    import java.net.InetSocketAddress;
020    import java.net.URI;
021    import java.util.Map;
022    
023    import org.apache.activemq.command.BrokerInfo;
024    import org.apache.activemq.transport.SocketConnectorFactory;
025    import org.apache.activemq.transport.WebTransportServerSupport;
026    import org.apache.activemq.transport.util.TextWireFormat;
027    import org.apache.activemq.transport.xstream.XStreamWireFormat;
028    import org.apache.activemq.util.ServiceStopper;
029    import org.eclipse.jetty.server.Connector;
030    import org.eclipse.jetty.server.Server;
031    import org.eclipse.jetty.server.handler.GzipHandler;
032    import org.eclipse.jetty.servlet.ServletContextHandler;
033    import org.eclipse.jetty.servlet.ServletHolder;
034    
035    public class HttpTransportServer extends WebTransportServerSupport {
036    
037        private TextWireFormat wireFormat;
038        private HttpTransportFactory transportFactory;
039    
040        public HttpTransportServer(URI uri, HttpTransportFactory factory) {
041            super(uri);
042            this.bindAddress = uri;
043            this.transportFactory = factory;
044            socketConnectorFactory = new SocketConnectorFactory();
045        }
046    
047        public void setBrokerInfo(BrokerInfo brokerInfo) {
048        }
049    
050        // Properties
051        // -------------------------------------------------------------------------
052        public TextWireFormat getWireFormat() {
053            if (wireFormat == null) {
054                wireFormat = createWireFormat();
055            }
056            return wireFormat;
057        }
058    
059        public void setWireFormat(TextWireFormat wireFormat) {
060            this.wireFormat = wireFormat;
061        }
062    
063        // Implementation methods
064        // -------------------------------------------------------------------------
065        protected TextWireFormat createWireFormat() {
066            return new XStreamWireFormat();
067        }
068    
069        protected void setConnector(Connector connector) {
070            this.connector = connector;
071        }
072    
073        protected void doStart() throws Exception {
074            server = new Server();
075            if (connector == null) {
076                connector = socketConnectorFactory.createConnector();
077            }
078    
079            URI bind = getBindLocation();
080            bind();
081    
082            ServletContextHandler contextHandler =
083                new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY);
084    
085            ServletHolder holder = new ServletHolder();
086            holder.setServlet(new HttpTunnelServlet());
087            contextHandler.addServlet(holder, "/");
088    
089            contextHandler.setAttribute("acceptListener", getAcceptListener());
090            contextHandler.setAttribute("wireFormat", getWireFormat());
091            contextHandler.setAttribute("transportFactory", transportFactory);
092            contextHandler.setAttribute("transportOptions", transportOptions);
093    
094            GzipHandler gzipHandler = new GzipHandler();
095            contextHandler.setHandler(gzipHandler);
096    
097            server.start();
098        }
099    
100        protected void doStop(ServiceStopper stopper) throws Exception {
101            Server temp = server;
102            server = null;
103            if (temp != null) {
104                temp.stop();
105            }
106        }
107    
108        public InetSocketAddress getSocketAddress() {
109            return null;
110        }
111    
112        @Override
113        public void setTransportOption(Map<String, Object> transportOptions) {
114            socketConnectorFactory.setTransportOptions(transportOptions);
115            super.setTransportOption(transportOptions);
116        }
117    
118        @Override
119        public boolean isSslServer() {
120            return false;
121        }
122    }