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 != null) { 051 if (builder.file.charAt(0) != '/') { 052 file = '/' + builder.file; 053 } else { 054 file = builder.file; 055 } 056 } else { 057 file = null; 058 } 059 httpHeaders = builder.httpHeaders; 060 } 061 062 public boolean isProxyEnabled() { 063 return proxy != null; 064 } 065 066 @Override 067 public ProxyInfo getProxyInfo() { 068 return proxy; 069 } 070 071 public String getProxyAddress() { 072 return proxy != null ? proxy.getProxyAddress() : null; 073 } 074 075 public int getProxyPort() { 076 return proxy != null ? proxy.getProxyPort() : 8080; 077 } 078 079 public boolean isUsingHTTPS() { 080 return https; 081 } 082 083 public URI getURI() throws URISyntaxException { 084 String uri = (https ? "https://" : "http://") + getHostString() + ":" + this.port + (file != null ? file : ""); 085 return new URI(uri); 086 } 087 088 public Map<String, String> getHttpHeaders() { 089 return httpHeaders; 090 } 091 092 public static Builder builder() { 093 return new Builder(); 094 } 095 096 public static final class Builder extends ConnectionConfiguration.Builder<Builder, BOSHConfiguration> { 097 private boolean https; 098 private String file; 099 private Map<String, String> httpHeaders = new HashMap<>(); 100 101 private Builder() { 102 } 103 104 public Builder setUseHttps(boolean useHttps) { 105 https = useHttps; 106 return this; 107 } 108 109 public Builder useHttps() { 110 return setUseHttps(true); 111 } 112 113 public Builder setFile(String file) { 114 this.file = file; 115 return this; 116 } 117 118 public Builder addHttpHeader(String name, String value) { 119 httpHeaders.put(name, value); 120 return this; 121 } 122 123 @Override 124 public BOSHConfiguration build() { 125 return new BOSHConfiguration(this); 126 } 127 128 @Override 129 protected Builder getThis() { 130 return this; 131 } 132 } 133}