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