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