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