source: josm/trunk/src/org/openstreetmap/josm/io/OsmConnection.java@ 2683

Last change on this file since 2683 was 2641, checked in by Gubaer, 14 years ago

new: supports system defined proxies if JOSM is started with -Djava.net.useSystemProxies=true
fixed #1641: JOSM doesn't allow for setting HTTP proxy user/password distrinct from OSM server user/password
fixed #2865: SOCKS Proxy Support
fixed #4182: Proxy Authentication

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.io;
3
4import java.net.HttpURLConnection;
5import java.net.Authenticator.RequestorType;
6import java.nio.ByteBuffer;
7import java.nio.CharBuffer;
8import java.nio.charset.CharacterCodingException;
9import java.nio.charset.Charset;
10import java.nio.charset.CharsetEncoder;
11import java.util.logging.Logger;
12
13import org.openstreetmap.josm.io.auth.CredentialsManagerException;
14import org.openstreetmap.josm.io.auth.CredentialsManagerFactory;
15import org.openstreetmap.josm.io.auth.CredentialsManagerResponse;
16import org.openstreetmap.josm.tools.Base64;
17
18/**
19 * Base class that handles common things like authentication for the reader and writer
20 * to the osm server.
21 *
22 * @author imi
23 */
24public class OsmConnection {
25 private static final Logger logger = Logger.getLogger(OsmConnection.class.getName());
26
27 protected boolean cancel = false;
28 protected HttpURLConnection activeConnection;
29
30 /**
31 * Initialize the http defaults and the authenticator.
32 */
33 static {
34 try {
35 HttpURLConnection.setFollowRedirects(true);
36 } catch (SecurityException e) {
37 e.printStackTrace();
38 }
39 }
40
41 public void cancel() {
42 cancel = true;
43 synchronized (this) {
44 if (activeConnection != null) {
45 activeConnection.setConnectTimeout(100);
46 activeConnection.setReadTimeout(100);
47 }
48 }
49 try {
50 Thread.sleep(100);
51 } catch (InterruptedException ex) {
52 }
53
54 synchronized (this) {
55 if (activeConnection != null) {
56 activeConnection.disconnect();
57 }
58 }
59 }
60
61 protected void addAuth(HttpURLConnection con) throws OsmTransferException {
62 CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
63 CredentialsManagerResponse response;
64 String token;
65 try {
66 synchronized (CredentialsManagerFactory.getCredentialManager()) {
67 response = CredentialsManagerFactory.getCredentialManager().getCredentials(RequestorType.SERVER, false /* don't know yet whether the credentials will succeed */);
68 }
69 } catch (CredentialsManagerException e) {
70 throw new OsmTransferException(e);
71 }
72 if (response == null) {
73 token = ":";
74 } else if (response.isCanceled()) {
75 cancel = true;
76 return;
77 } else {
78 String username= response.getUsername() == null ? "" : response.getUsername();
79 String password = response.getPassword() == null ? "" : String.valueOf(response.getPassword());
80 token = username + ":" + password;
81 try {
82 ByteBuffer bytes = encoder.encode(CharBuffer.wrap(token));
83 con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes));
84 } catch(CharacterCodingException e) {
85 throw new OsmTransferException(e);
86 }
87 }
88 }
89
90 /**
91 * Replies true if this connection is canceled
92 *
93 * @return true if this connection is canceled
94 * @return
95 */
96 public boolean isCanceled() {
97 return cancel;
98 }
99}
Note: See TracBrowser for help on using the repository browser.