Package org.wso2.carbon.server.transports.http

Source Code of org.wso2.carbon.server.transports.http.HttpTransport

/*
* Copyright 2004,2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.server.transports.http;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonException;
import org.wso2.carbon.server.TomcatUtil;
import org.wso2.carbon.server.transports.Transport;
import org.wso2.carbon.server.transports.TransportParameter;
import org.wso2.carbon.tomcat.BetterTomcat;
import org.wso2.carbon.utils.CarbonUtils;

import java.util.Map;

/**
* Corresponds to the HTTP transports when Carbon is running standlone. Responsible for managing
* the corresponding Tomcat connector.
*/
public class HttpTransport implements Transport {
    private static final Log log = LogFactory.getLog(HttpTransport.class);
    private boolean isStarted;
    private Connector connector;
    private int proxyPort = -1;
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void init(Map<String, TransportParameter> parameters) throws CarbonException {
        try {
            TransportParameter portParam = parameters.get("port");
            TransportParameter proxyPortParam = parameters.get("proxyPort");
            TransportParameter protocolParam = parameters.get("protocol");
            TransportParameter clientAuthParam = parameters.get("clientAuth");
            TransportParameter addressParam = parameters.get("address");
            String address = addressParam != null ? addressParam.getValue() : null;
            calculateProxyPort(proxyPortParam);
            BetterTomcat.Protocol protocol;
            try {
                protocol = getProtocol(protocolParam);
            } catch (Exception e) {
                String msg = "Protocol " + protocolParam.getName() + ":" +
                             protocolParam.getValue() + " is invalid. " +
                             "Please set the proper protocol in the mgt-transports.xml file.";
                log.error(msg);
                throw new CarbonException(msg, e);
            }
            int port = getPort(portParam);
            connector = TomcatUtil.getConnector(protocol, address, port);
            if(clientAuthParam != null){
                TomcatUtil.getTomcat().setClientAuth(connector, clientAuthParam.getValue());
            }
            log.info("Created Connector [" + protocol + "://" + (address != null ? address : "*") +
                         ":" + port + "]");
            for (Map.Entry<String, TransportParameter> entry : parameters.entrySet()) {
                String paramName = entry.getKey();
                if (!paramName.equals("port") && !paramName.equals("proxyPort") &&
                    !paramName.equals("protocol") && !paramName.equals("clientAuth")) {
                    connector.setAttribute(paramName, entry.getValue().getValue());
                }
            }
            connector.setEnableLookups(true);
        } catch (Exception e) {
            String msg = "Exception occurred while starting the Tomcat HTTP connector";
            log.error(msg, e);
            throw new CarbonException(msg, e);
        }
    }

    private void calculateProxyPort(TransportParameter proxyPortParam) {
        if (proxyPortParam != null) {
            try {
                proxyPort = Integer.parseInt(proxyPortParam.getValue());
            } catch (NumberFormatException e) {
                proxyPort = -1;
            }
        }
    }

    private int getPort(TransportParameter portParam) {
        int port = name.equals("http") ? 9763 : 9443;
        String portSysProp = System.getProperty(name + "Port");
        if (portSysProp != null) {  // System property is given the highest precedence
            try {
                return Integer.parseInt(portSysProp);
            } catch (NumberFormatException ignored) {
            }
        }
        if (portParam != null) {
            String portParamValue = portParam.getValue();
            if (portParamValue.startsWith("${")) { // should port be taken from carbon.xml?
                port = CarbonUtils.getPortFromServerConfig(portParamValue);
            } else {
                port = Integer.parseInt(portParamValue);
            }
        }
        return port;
    }

    private BetterTomcat.Protocol getProtocol(TransportParameter protocolParam) {
        BetterTomcat.Protocol protocol;
        if (protocolParam != null) {
            protocol = BetterTomcat.Protocol.valueOf(protocolParam.getValue());
        } else {
            protocol = BetterTomcat.Protocol.HTTP_11;
        }
        return protocol;
    }

    public void start() throws Exception {
        if (isStarted) {
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("Starting HTTP Transport...");
        }
        if (connector != null) {
            connector.start();
        } else {
            throw new RuntimeException("Cannot start transport before initializing it. " +
                                       "Please  call #init() first.");
        }
        isStarted = true;
    }

    public void stop() throws Exception {
        if (!isStarted) {
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("Stopping HTTP transport...");
        }
        try {
            connector.stop();
//            TomcatServerCache.removeConnector("http");
            isStarted = false;
        } catch (LifecycleException e) {
            String msg = "Cannot stop Tomcat HTTP connector";
            log.error(msg, e);
            throw new Exception(msg, e);
        }
    }

    public int getPort() {
        return connector.getPort();
    }

    public int getProxyPort() {
        return proxyPort;
    }
}
TOP

Related Classes of org.wso2.carbon.server.transports.http.HttpTransport

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.