001/**
002 *
003 * Copyright 2009 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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 */
017
018package org.jivesoftware.smack.bosh;
019
020import java.net.URI;
021import java.net.URISyntaxException;
022
023import org.jivesoftware.smack.ConnectionConfiguration;
024import org.jivesoftware.smack.proxy.ProxyInfo;
025import org.jivesoftware.smack.util.dns.HostAddress;
026
027/**
028 * Configuration to use while establishing the connection to the XMPP server via
029 * HTTP binding.
030 * 
031 * @see XMPPBOSHConnection
032 * @author Guenther Niess
033 */
034public class BOSHConfiguration extends ConnectionConfiguration {
035
036    private boolean ssl;
037    private String file;
038
039    public BOSHConfiguration(String xmppDomain) {
040        super(xmppDomain, 7070);
041        ssl = false;
042        file = "/http-bind/";
043    }
044
045    public BOSHConfiguration(String xmppDomain, int port) {
046        super(xmppDomain, port);
047        ssl = false;
048        file = "/http-bind/";
049    }
050
051    /**
052     * Create a HTTP Binding configuration.
053     * 
054     * @param https true if you want to use SSL
055     *             (e.g. false for http://domain.lt:7070/http-bind).
056     * @param host the hostname or IP address of the connection manager
057     *             (e.g. domain.lt for http://domain.lt:7070/http-bind).
058     * @param port the port of the connection manager
059     *             (e.g. 7070 for http://domain.lt:7070/http-bind).
060     * @param filePath the file which is described by the URL
061     *             (e.g. /http-bind for http://domain.lt:7070/http-bind).
062     * @param xmppDomain the XMPP service name
063     *             (e.g. domain.lt for the user alice@domain.lt)
064     */
065    public BOSHConfiguration(boolean https, String host, int port, String filePath, String xmppDomain) {
066        super(host, port, xmppDomain);
067        ssl = https;
068        file = (filePath != null ? filePath : "/");
069    }
070
071    /**
072     * Create a HTTP Binding configuration.
073     * 
074     * @param https true if you want to use SSL
075     *             (e.g. false for http://domain.lt:7070/http-bind).
076     * @param host the hostname or IP address of the connection manager
077     *             (e.g. domain.lt for http://domain.lt:7070/http-bind).
078     * @param port the port of the connection manager
079     *             (e.g. 7070 for http://domain.lt:7070/http-bind).
080     * @param filePath the file which is described by the URL
081     *             (e.g. /http-bind for http://domain.lt:7070/http-bind).
082     * @param proxy the configuration of a proxy server.
083     * @param xmppDomain the XMPP service name
084     *             (e.g. domain.lt for the user alice@domain.lt)
085     */
086    public BOSHConfiguration(boolean https, String host, int port, String filePath, ProxyInfo proxy, String xmppDomain) {
087        super(host, port, xmppDomain, proxy);
088        ssl = https;
089        file = (filePath != null ? filePath : "/");
090    }
091
092    public boolean isProxyEnabled() {
093        return (proxy != null && proxy.getProxyType() != ProxyInfo.ProxyType.NONE);
094    }
095
096    public ProxyInfo getProxyInfo() {
097        return proxy;
098    }
099
100    public String getProxyAddress() {
101        return (proxy != null ? proxy.getProxyAddress() : null);
102    }
103
104    public int getProxyPort() {
105        return (proxy != null ? proxy.getProxyPort() : 8080);
106    }
107
108    public boolean isUsingSSL() {
109        return ssl;
110    }
111
112    public URI getURI() throws URISyntaxException {
113        if (file.charAt(0) != '/') {
114            file = '/' + file;
115        }
116        String host;
117        int port;
118        if (hostAddresses != null) {
119            HostAddress hostAddress = hostAddresses.get(0);
120            host = hostAddress.getFQDN();
121            port = hostAddress.getPort();
122        } else {
123            host = getServiceName();
124            port = 80;
125        }
126        return new URI((ssl ? "https://" : "http://") + host + ":" + port + file);
127    }
128}