1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.net.HttpURLConnection;
|
---|
5 | import java.net.Authenticator.RequestorType;
|
---|
6 | import java.nio.ByteBuffer;
|
---|
7 | import java.nio.CharBuffer;
|
---|
8 | import java.nio.charset.CharacterCodingException;
|
---|
9 | import java.nio.charset.Charset;
|
---|
10 | import java.nio.charset.CharsetEncoder;
|
---|
11 | import java.util.logging.Logger;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.io.auth.CredentialsManagerException;
|
---|
14 | import org.openstreetmap.josm.io.auth.CredentialsManagerFactory;
|
---|
15 | import org.openstreetmap.josm.io.auth.CredentialsManagerResponse;
|
---|
16 | import 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 | */
|
---|
24 | public 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 | }
|
---|