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    import org.slf4j.Logger;
035    import org.slf4j.LoggerFactory;
036    
037    public class HttpTransportServer extends WebTransportServerSupport {
038    
039        private static final Logger LOG = LoggerFactory.getLogger(HttpTransportServer.class);
040    
041        private TextWireFormat wireFormat;
042        private final HttpTransportFactory transportFactory;
043    
044        public HttpTransportServer(URI uri, HttpTransportFactory factory) {
045            super(uri);
046            this.bindAddress = uri;
047            this.transportFactory = factory;
048            socketConnectorFactory = new SocketConnectorFactory();
049        }
050    
051        @Override
052        public void setBrokerInfo(BrokerInfo brokerInfo) {
053        }
054    
055        // Properties
056        // -------------------------------------------------------------------------
057        public TextWireFormat getWireFormat() {
058            if (wireFormat == null) {
059                wireFormat = createWireFormat();
060            }
061            return wireFormat;
062        }
063    
064        public void setWireFormat(TextWireFormat wireFormat) {
065            this.wireFormat = wireFormat;
066        }
067    
068        // Implementation methods
069        // -------------------------------------------------------------------------
070        protected TextWireFormat createWireFormat() {
071            return new XStreamWireFormat();
072        }
073    
074        protected void setConnector(Connector connector) {
075            this.connector = connector;
076        }
077    
078        @Override
079        protected void doStart() throws Exception {
080            server = new Server();
081            if (connector == null) {
082                connector = socketConnectorFactory.createConnector();
083            }
084    
085            URI boundTo = bind();
086    
087            ServletContextHandler contextHandler =
088                new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY);
089    
090            ServletHolder holder = new ServletHolder();
091            holder.setServlet(new HttpTunnelServlet());
092            contextHandler.addServlet(holder, "/");
093    
094            contextHandler.setAttribute("acceptListener", getAcceptListener());
095            contextHandler.setAttribute("wireFormat", getWireFormat());
096            contextHandler.setAttribute("transportFactory", transportFactory);
097            contextHandler.setAttribute("transportOptions", transportOptions);
098    
099            GzipHandler gzipHandler = new GzipHandler();
100            contextHandler.setHandler(gzipHandler);
101    
102            server.start();
103    
104            // Update the Connect To URI with our actual location in case the configured port
105            // was set to zero so that we report the actual port we are listening on.
106    
107            int port = boundTo.getPort();
108            if (connector.getLocalPort() != -1) {
109                port = connector.getLocalPort();
110            }
111    
112            setConnectURI(new URI(boundTo.getScheme(),
113                                  boundTo.getUserInfo(),
114                                  boundTo.getHost(),
115                                  port,
116                                  boundTo.getPath(),
117                                  boundTo.getQuery(),
118                                  boundTo.getFragment()));
119        }
120    
121        @Override
122        protected void doStop(ServiceStopper stopper) throws Exception {
123            Server temp = server;
124            server = null;
125            if (temp != null) {
126                temp.stop();
127            }
128        }
129    
130        @Override
131        public InetSocketAddress getSocketAddress() {
132            return null;
133        }
134    
135        @Override
136        public void setTransportOption(Map<String, Object> transportOptions) {
137            socketConnectorFactory.setTransportOptions(transportOptions);
138            super.setTransportOption(transportOptions);
139        }
140    
141        @Override
142        public boolean isSslServer() {
143            return false;
144        }
145    }