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.io.IOException; 020import java.net.MalformedURLException; 021import java.net.URI; 022import java.net.URISyntaxException; 023import java.util.HashMap; 024import java.util.Map; 025 026import org.apache.activemq.transport.Transport; 027import org.apache.activemq.transport.TransportFactory; 028import org.apache.activemq.transport.TransportLoggerFactory; 029import org.apache.activemq.transport.TransportServer; 030import org.apache.activemq.transport.util.TextWireFormat; 031import org.apache.activemq.transport.xstream.XStreamWireFormat; 032import org.apache.activemq.util.IOExceptionSupport; 033import org.apache.activemq.util.IntrospectionSupport; 034import org.apache.activemq.util.URISupport; 035import org.apache.activemq.wireformat.WireFormat; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039public class HttpTransportFactory extends TransportFactory { 040 041 private static final Logger LOG = LoggerFactory.getLogger(HttpTransportFactory.class); 042 043 @Override 044 public TransportServer doBind(URI location) throws IOException { 045 try { 046 Map<String, String> options = new HashMap<String, String>(URISupport.parseParameters(location)); 047 HttpTransportServer result = new HttpTransportServer(location, this); 048 Map<String, Object> jettyOptions = IntrospectionSupport.extractProperties(options, "jetty."); 049 Map<String, Object> httpOptions = IntrospectionSupport.extractProperties(options, "http."); 050 Map<String, Object> transportOptions = IntrospectionSupport.extractProperties(options, "transport."); 051 result.setJettyOptions(jettyOptions); 052 result.setTransportOption(transportOptions); 053 result.setHttpOptions(httpOptions); 054 return result; 055 } catch (URISyntaxException e) { 056 throw IOExceptionSupport.create(e); 057 } 058 } 059 060 protected TextWireFormat asTextWireFormat(WireFormat wireFormat) { 061 if (wireFormat instanceof TextWireFormat) { 062 return (TextWireFormat)wireFormat; 063 } 064 LOG.trace("Not created with a TextWireFormat: {}", wireFormat); 065 return new XStreamWireFormat(); 066 } 067 068 @Override 069 protected String getDefaultWireFormatType() { 070 return "xstream"; 071 } 072 073 @Override 074 protected Transport createTransport(URI location, WireFormat wf) throws IOException { 075 TextWireFormat textWireFormat = asTextWireFormat(wf); 076 // need to remove options from uri 077 URI uri; 078 try { 079 uri = URISupport.removeQuery(location); 080 } catch (URISyntaxException e) { 081 MalformedURLException cause = new MalformedURLException("Error removing query on " + location); 082 cause.initCause(e); 083 throw cause; 084 } 085 return new HttpClientTransport(textWireFormat, uri); 086 } 087 088 @Override 089 @SuppressWarnings("rawtypes") 090 public Transport serverConfigure(Transport transport, WireFormat format, HashMap options) throws Exception { 091 return compositeConfigure(transport, format, options); 092 } 093 094 @Override 095 @SuppressWarnings("rawtypes") 096 public Transport compositeConfigure(Transport transport, WireFormat format, Map options) { 097 transport = super.compositeConfigure(transport, format, options); 098 HttpClientTransport httpTransport = transport.narrow(HttpClientTransport.class); 099 if (httpTransport != null && httpTransport.isTrace()) { 100 try { 101 transport = TransportLoggerFactory.getInstance().createTransportLogger(transport); 102 } catch (Throwable e) { 103 LOG.error("Could not create TransportLogger object for: " + TransportLoggerFactory.defaultLogWriterName + ", reason: " + e, e); 104 } 105 } 106 boolean useInactivityMonitor = "true".equals(getOption(options, "useInactivityMonitor", "true")); 107 if (useInactivityMonitor) { 108 transport = new HttpInactivityMonitor(transport); 109 IntrospectionSupport.setProperties(transport, options); 110 } 111 112 return transport; 113 } 114}