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