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 Map<String, Object> wireFormatOptions = IntrospectionSupport.extractProperties(options, "wireFormat."); 052 result.setJettyOptions(jettyOptions); 053 result.setTransportOption(transportOptions); 054 result.setHttpOptions(httpOptions); 055 result.setWireFormatOptions(wireFormatOptions); 056 return result; 057 } catch (URISyntaxException e) { 058 throw IOExceptionSupport.create(e); 059 } 060 } 061 062 protected TextWireFormat asTextWireFormat(WireFormat wireFormat) { 063 if (wireFormat instanceof TextWireFormat) { 064 return (TextWireFormat)wireFormat; 065 } 066 LOG.trace("Not created with a TextWireFormat: {}", wireFormat); 067 return new XStreamWireFormat(); 068 } 069 070 @Override 071 protected String getDefaultWireFormatType() { 072 return "xstream"; 073 } 074 075 @Override 076 protected Transport createTransport(URI location, WireFormat wf) throws IOException { 077 TextWireFormat textWireFormat = asTextWireFormat(wf); 078 // need to remove options from uri 079 URI uri; 080 try { 081 uri = URISupport.removeQuery(location); 082 } catch (URISyntaxException e) { 083 MalformedURLException cause = new MalformedURLException("Error removing query on " + location); 084 cause.initCause(e); 085 throw cause; 086 } 087 return new HttpClientTransport(textWireFormat, uri); 088 } 089 090 @Override 091 @SuppressWarnings("rawtypes") 092 public Transport serverConfigure(Transport transport, WireFormat format, HashMap options) throws Exception { 093 return compositeConfigure(transport, format, options); 094 } 095 096 @Override 097 @SuppressWarnings("rawtypes") 098 public Transport compositeConfigure(Transport transport, WireFormat format, Map options) { 099 transport = super.compositeConfigure(transport, format, options); 100 HttpClientTransport httpTransport = transport.narrow(HttpClientTransport.class); 101 if (httpTransport != null && httpTransport.isTrace()) { 102 try { 103 transport = TransportLoggerFactory.getInstance().createTransportLogger(transport); 104 } catch (Throwable e) { 105 LOG.error("Could not create TransportLogger object for: " + TransportLoggerFactory.defaultLogWriterName + ", reason: " + e, e); 106 } 107 } 108 boolean useInactivityMonitor = "true".equals(getOption(options, "useInactivityMonitor", "true")); 109 if (useInactivityMonitor) { 110 transport = new HttpInactivityMonitor(transport); 111 IntrospectionSupport.setProperties(transport, options); 112 } 113 114 return transport; 115 } 116}