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