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