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;
025
026/**
027 * Configuration to use while establishing the connection to the XMPP server via
028 * HTTP binding.
029 *
030 * @see XMPPBOSHConnection
031 * @author Guenther Niess
032 */
033public final class BOSHConfiguration extends ConnectionConfiguration {
034
035    private final boolean https;
036    private final String file;
037
038    private BOSHConfiguration(Builder builder) {
039        super(builder);
040        if (proxy != null) {
041            if (proxy.getProxyType() != ProxyInfo.ProxyType.HTTP) {
042                throw new IllegalArgumentException(
043                                "Only HTTP proxies are support with BOSH connections");
044            }
045        }
046        https = builder.https;
047        if (builder.file.charAt(0) != '/') {
048            file = '/' + builder.file;
049        } else {
050            file = builder.file;
051        }
052    }
053
054    public boolean isProxyEnabled() {
055        return proxy != null;
056    }
057
058    @Override
059    public ProxyInfo getProxyInfo() {
060        return proxy;
061    }
062
063    public String getProxyAddress() {
064        return (proxy != null ? proxy.getProxyAddress() : null);
065    }
066
067    public int getProxyPort() {
068        return (proxy != null ? proxy.getProxyPort() : 8080);
069    }
070
071    public boolean isUsingHTTPS() {
072        return https;
073    }
074
075    public URI getURI() throws URISyntaxException {
076        return new URI((https ? "https://" : "http://") + this.host + ":" + this.port + file);
077    }
078
079    public static Builder builder() {
080        return new Builder();
081    }
082
083    public static final class Builder extends ConnectionConfiguration.Builder<Builder, BOSHConfiguration> {
084        private boolean https;
085        private String file;
086
087        private Builder() {
088        }
089
090        public Builder setUseHttps(boolean useHttps) {
091            https = useHttps;
092            return this;
093        }
094
095        public Builder useHttps() {
096            return setUseHttps(true);
097        }
098
099        public Builder setFile(String file) {
100            this.file = file;
101            return this;
102        }
103
104        @Override
105        public BOSHConfiguration build() {
106            return new BOSHConfiguration(this);
107        }
108
109        @Override
110        protected Builder getThis() {
111            return this;
112        }
113    }
114}