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 return new URI((https ? "https://" : "http://") + this.host + ":" + this.port + file); 081 } 082 083 public Map<String, String> getHttpHeaders() { 084 return httpHeaders; 085 } 086 087 public static Builder builder() { 088 return new Builder(); 089 } 090 091 public static final class Builder extends ConnectionConfiguration.Builder<Builder, BOSHConfiguration> { 092 private boolean https; 093 private String file; 094 private Map<String, String> httpHeaders = new HashMap<>(); 095 096 private Builder() { 097 } 098 099 public Builder setUseHttps(boolean useHttps) { 100 https = useHttps; 101 return this; 102 } 103 104 public Builder useHttps() { 105 return setUseHttps(true); 106 } 107 108 public Builder setFile(String file) { 109 this.file = file; 110 return this; 111 } 112 113 public Builder addHttpHeader(String name, String value) { 114 httpHeaders.put(name, value); 115 return this; 116 } 117 118 @Override 119 public BOSHConfiguration build() { 120 return new BOSHConfiguration(this); 121 } 122 123 @Override 124 protected Builder getThis() { 125 return this; 126 } 127 } 128}