1 23 24 package org.apache.webdav.lib; 25 26 import java.io.IOException ; 27 import org.apache.commons.httpclient.Credentials; 28 import org.apache.commons.httpclient.HostConfiguration; 29 import org.apache.commons.httpclient.HttpClient; 30 import org.apache.commons.httpclient.HttpState; 31 import org.apache.commons.httpclient.UsernamePasswordCredentials; 32 import org.apache.commons.httpclient.HttpURL; 33 34 52 public abstract class WebdavSession { 53 54 55 57 58 61 public WebdavSession() { 62 super(); 63 } 64 65 66 68 69 72 protected HttpClient client; 73 74 77 protected Credentials hostCredentials = null; 78 79 82 protected String proxyHost = null; 83 84 87 protected int proxyPort = -1; 88 89 92 protected Credentials proxyCredentials = null; 93 94 95 98 protected int debug = 0; 99 100 101 103 104 107 public void setDebug(int debug) { 108 this.debug = debug; 109 } 110 111 112 114 115 124 public HttpClient getSessionInstance(HttpURL httpURL) 125 throws IOException { 126 127 return getSessionInstance(httpURL, false); 128 } 129 130 131 141 public HttpClient getSessionInstance(HttpURL httpURL, boolean reset) 142 throws IOException { 143 144 if (reset || client == null) { 145 client = new HttpClient(); 146 client.setState(new WebdavState()); 148 HostConfiguration hostConfig = client.getHostConfiguration(); 149 hostConfig.setHost(httpURL); 150 if (proxyHost != null && proxyPort > 0) 151 hostConfig.setProxy(proxyHost, proxyPort); 152 153 if (hostCredentials == null) { 154 String userName = httpURL.getUser(); 155 if (userName != null && userName.length() > 0) { 156 hostCredentials = 157 new UsernamePasswordCredentials(userName, 158 httpURL.getPassword()); 159 } 160 } 161 162 if (hostCredentials != null) { 163 HttpState clientState = client.getState(); 164 clientState.setCredentials(null, httpURL.getHost(), 165 hostCredentials); 166 clientState.setAuthenticationPreemptive(true); 167 } 168 169 if (proxyCredentials != null) { 170 client.getState().setProxyCredentials(null, proxyHost, 171 proxyCredentials); 172 } 173 } 174 175 return client; 176 } 177 178 183 public void setCredentials(Credentials credentials) { 184 hostCredentials = credentials; 185 } 186 187 189 public void setProxy(String host, int port) 190 { 191 this.proxyHost = host; 192 this.proxyPort = port; 193 } 194 195 200 public void setProxyCredentials(Credentials credentials) { 201 proxyCredentials = credentials; 202 } 203 204 209 public void closeSession() 210 throws IOException { 211 if (client != null) { 212 client.getHttpConnectionManager().getConnection( 213 client.getHostConfiguration()).close(); 214 client = null; 215 } 216 } 217 218 219 226 public synchronized void closeSession(HttpClient client) 227 throws IOException { 228 closeSession(); 229 } 230 231 232 237 250 251 252 257 262 } 263 | Popular Tags |