Changeset 5386 in josm
- Timestamp:
- 2012-07-31T21:48:49+02:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r5266 r5386 25 25 import java.util.HashMap; 26 26 27 import javax.xml.parsers.ParserConfigurationException;28 27 import javax.xml.parsers.SAXParserFactory; 29 28 … … 43 42 44 43 /** 45 * Class that encapsulates the communications with the OSM API.44 * Class that encapsulates the communications with the <a href="http://wiki.openstreetmap.org/wiki/API_v0.6">OSM API</a>.<br/><br/> 46 45 * 47 * All interaction with the server-side OSM API should go through this class. 46 * All interaction with the server-side OSM API should go through this class.<br/><br/> 48 47 * 49 48 * It is conceivable to extract this into an interface later and create various … … 52 51 */ 53 52 public class OsmApi extends OsmConnection { 54 /** max number of retries to send a request in case of HTTP 500 errors or timeouts */ 53 54 /** 55 * Maximum number of retries to send a request in case of HTTP 500 errors or timeouts 56 */ 55 57 static public final int DEFAULT_MAX_NUM_RETRIES = 5; 56 58 57 /** the collection of instantiated OSM APIs */ 59 /** 60 * Maximum number of concurrent download threads, imposed by 61 * <a href="http://wiki.openstreetmap.org/wiki/API_usage_policy#Technical_Usage_Requirements"> 62 * OSM API usage policy.</a> 63 */ 64 static public final int MAX_DOWNLOAD_THREADS = 2; 65 66 // The collection of instantiated OSM APIs 58 67 private static HashMap<String, OsmApi> instances = new HashMap<String, OsmApi>(); 59 68 60 69 /** 61 * replies the {@link OsmApi} for a given server URL70 * Replies the {@link OsmApi} for a given server URL 62 71 * 63 72 * @param serverUrl the server URL … … 74 83 return api; 75 84 } 76 /** 77 * replies the {@link OsmApi} for the URL given by the preference <code>osm-server.url</code> 85 86 /** 87 * Replies the {@link OsmApi} for the URL given by the preference <code>osm-server.url</code> 78 88 * 79 89 * @return the OsmApi 80 * @ exceptionIllegalStateException thrown, if the preference <code>osm-server.url</code> is not set90 * @throws IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set 81 91 * 82 92 */ … … 129 139 * 130 140 * @param serverUrl the server URL. Must not be null 131 * @ exceptionIllegalArgumentException thrown, if serverUrl is null141 * @throws IllegalArgumentException thrown, if serverUrl is null 132 142 */ 133 143 protected OsmApi(String serverUrl) { … … 137 147 138 148 /** 139 * Re turns the OSM protocol version we use to talk to the server.149 * Replies the OSM protocol version we use to talk to the server. 140 150 * @return protocol version, or null if not yet negotiated. 141 151 */ … … 144 154 } 145 155 156 /** 157 * Replies the host name of the server URL. 158 * @return the host name of the server URL, or null if the server URL is malformed. 159 */ 146 160 public String getHost() { 147 161 String host = null; … … 163 177 this.fastFail = fastFail; 164 178 } 179 165 180 @Override 166 181 protected byte[] updateData() throws OsmTransferException { 167 String s = sendRequest("GET", "capabilities", null, monitor, false, fastFail); 168 return s.getBytes(); 169 } 170 } 171 172 public void initialize(ProgressMonitor monitor) throws OsmApiInitializationException, OsmTransferCanceledException { 182 return sendRequest("GET", "capabilities", null, monitor, false, fastFail).getBytes(); 183 } 184 } 185 186 /** 187 * Initializes this component by negotiating a protocol version with the server. 188 * 189 * @param monitor the progress monitor 190 * @throws OsmTransferCanceledException If the initialisation has been cancelled by user. 191 * @throws OsmApiInitializationException If any other exception occurs. Use getCause() to get the original exception. 192 */ 193 public void initialize(ProgressMonitor monitor) throws OsmTransferCanceledException, OsmApiInitializationException { 173 194 initialize(monitor, false); 174 195 } 175 /** 176 * Initializes this component by negotiating a protocol version with the server. 177 * 178 * @param monitor 196 197 /** 198 * Initializes this component by negotiating a protocol version with the server, with the ability to control the timeout. 199 * 200 * @param monitor the progress monitor 179 201 * @param fastFail true to request quick initialisation with a small timeout (more likely to throw exception) 180 * @exception OsmApiInitializationException thrown, if an exception occurs 181 */ 182 public void initialize(ProgressMonitor monitor, boolean fastFail) throws OsmApiInitializationException, OsmTransferCanceledException { 202 * @throws OsmTransferCanceledException If the initialisation has been cancelled by user. 203 * @throws OsmApiInitializationException If any other exception occurs. Use getCause() to get the original exception. 204 */ 205 public void initialize(ProgressMonitor monitor, boolean fastFail) throws OsmTransferCanceledException, OsmApiInitializationException { 183 206 if (initialized) 184 207 return; … … 215 238 * to load the layers in the first place becuase they would have 216 239 * been disabled! */ 217 if (Main. main.isDisplayingMapView()) {240 if (Main.isDisplayingMapView()) { 218 241 for (Layer l : Main.map.mapView.getLayersOfType(ImageryLayer.class)) { 219 242 if (((ImageryLayer) l).getInfo().isBlacklisted()) { … … 224 247 } 225 248 226 } catch(IOException e) { 227 initialized = false; 228 throw new OsmApiInitializationException(e); 229 } catch(SAXException e) { 230 initialized = false; 231 throw new OsmApiInitializationException(e); 232 } catch(ParserConfigurationException e) { 233 initialized = false; 234 throw new OsmApiInitializationException(e); 235 } catch(OsmTransferCanceledException e){ 249 } catch (OsmTransferCanceledException e) { 236 250 throw e; 237 } catch (OsmTransferException e) {251 } catch (Exception e) { 238 252 initialized = false; 239 253 throw new OsmApiInitializationException(e); … … 299 313 * 300 314 * @param osm the primitive 315 * @param monitor the progress monitor 301 316 * @throws OsmTransferException if something goes wrong 302 317 */ … … 339 354 * Deletes an OSM primitive on the server. 340 355 * @param osm the primitive 356 * @param monitor the progress monitor 341 357 * @throws OsmTransferException if something goes wrong 342 358 */ … … 535 551 } 536 552 537 private String sendRequest(String requestMethod, String urlSuffix,String requestBody, ProgressMonitor monitor, boolean doAuth) throws OsmTransferException {538 return sendRequest(requestMethod, urlSuffix, requestBody, monitor, doAuth, false);539 }540 541 553 /** 542 554 * Generic method for sending requests to the OSM API. … … 555 567 * 556 568 * @return the body of the HTTP response, if and only if the response code was "200 OK". 557 * @ exceptionOsmTransferException if the HTTP return code was not 200 (and retries have569 * @throws OsmTransferException if the HTTP return code was not 200 (and retries have 558 570 * been exhausted), or rewrapping a Java exception. 559 571 */ … … 684 696 685 697 /** 686 * returns the API capabilities; null, if the API is not initialized yet687 * 688 * @return the API capabilities 698 * Replies the API capabilities 699 * 700 * @return the API capabilities, or null, if the API is not initialized yet 689 701 */ 690 702 public Capabilities getCapabilities() { … … 704 716 throw new OsmTransferException(tr("ID of current changeset > 0 required. Current ID is {0}.", changeset.getId())); 705 717 } 718 706 719 /** 707 720 * Replies the changeset data uploads are currently directed to -
trunk/src/org/openstreetmap/josm/io/OsmApiException.java
r3511 r5386 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 /** 6 * Exception thrown when a communication error occurs when accessing the <a href="http://wiki.openstreetmap.org/wiki/API_v0.6">OSM API</a>. 7 * @see OsmApi 8 */ 5 9 public class OsmApiException extends OsmTransferException { 6 10 … … 10 14 private String accessedUrl; 11 15 12 public OsmApiException() { 13 super(); 14 } 15 16 /** 17 * Constructs an {@code OsmApiException} with the specified response code, error header and error body 18 * @param responseCode The HTTP response code replied by the OSM server. See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values 19 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header 20 * @param errorBody The error body, as transmitted in the HTTP response body 21 */ 16 22 public OsmApiException(int responseCode, String errorHeader, String errorBody) { 17 23 this.responseCode = responseCode; … … 20 26 } 21 27 28 /** 29 * Constructs an {@code OsmApiException} with the specified detail message. 30 * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}. 31 * 32 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method) 33 */ 34 public OsmApiException(String message) { 35 super(message); 36 } 37 38 /** 39 * Constructs an {@code OsmApiException} with the specified cause and a detail message of 40 * <tt>(cause==null ? null : cause.toString())</tt> 41 * (which typically contains the class and detail message of <tt>cause</tt>). 42 * 43 * @param cause the cause (which is saved for later retrieval by the {@link #getCause} method). 44 * A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown. 45 */ 46 public OsmApiException(Throwable cause) { 47 super(cause); 48 } 49 50 /** 51 * Constructs an {@code OsmApiException} with the specified detail message and cause. 52 * 53 * <p> Note that the detail message associated with {@code cause} is <i>not</i> automatically incorporated 54 * into this exception's detail message. 55 * 56 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method) 57 * @param cause The cause (which is saved for later retrieval by the {@link #getCause} method). 58 * A null value is permitted, and indicates that the cause is nonexistent or unknown. 59 * 60 */ 22 61 public OsmApiException(String message, Throwable cause) { 23 62 super(message, cause); 24 63 } 25 64 26 public OsmApiException(String message) { 27 super(message); 28 } 29 30 public OsmApiException(Throwable cause) { 31 super(cause); 32 } 33 65 /** 66 * Replies the HTTP response code. 67 * @return The HTTP response code replied by the OSM server. Refer to <a href="http://wiki.openstreetmap.org/wiki/API_v0.6">OSM API</a> to see the list of response codes returned by the API for each call. 68 */ 34 69 public int getResponseCode() { 35 70 return responseCode; 36 71 } 37 72 73 /** 74 * Sets the HTTP response code. 75 * @param responseCode The HTTP response code replied by the OSM server. See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values 76 */ 38 77 public void setResponseCode(int responseCode) { 39 78 this.responseCode = responseCode; 40 79 } 41 80 81 /** 82 * Replies the error header. 83 * @return the error header, as transmitted in the {@code Error} field of the HTTP response header 84 */ 42 85 public String getErrorHeader() { 43 86 return errorHeader; 44 87 } 45 88 89 /** 90 * Sets the error header. 91 * @param errorHeader the error header, as transmitted in the {@code Error} field of the HTTP response header 92 */ 46 93 public void setErrorHeader(String errorHeader) { 47 94 this.errorHeader = errorHeader; 48 95 } 49 96 97 /** 98 * Replies the error body. 99 * @return The error body, as transmitted in the HTTP response body 100 */ 50 101 public String getErrorBody() { 51 102 return errorBody; 52 103 } 53 104 105 /** 106 * Sets the error body. 107 * @param errorBody The error body, as transmitted in the HTTP response body 108 */ 54 109 public void setErrorBody(String errorBody) { 55 110 this.errorBody = errorBody; … … 73 128 } 74 129 catch (Exception e) { 130 // Ignored 75 131 } 76 132 try … … 84 140 } 85 141 catch (Exception e) { 142 // Ignored 86 143 } 87 144 return sb.toString(); … … 108 165 } 109 166 167 /** 168 * Sets the complete URL accessed when this error occured. This is distinct from the one set with {@link #setUrl}, which is generally only the base URL of the server. 169 * @param url the complete URL accessed when this error occured. 170 */ 110 171 public void setAccessedUrl(String url) { 111 172 this.accessedUrl = url; 112 173 } 113 174 175 /** 176 * Replies the complete URL accessed when this error occured. This is distinct from the one returned by {@link #getUrl}, which is generally only the base URL of the server. 177 * @return the complete URL accessed when this error occured. 178 */ 114 179 public String getAccessedUrl() { 115 180 return accessedUrl; -
trunk/src/org/openstreetmap/josm/io/OsmApiInitializationException.java
r3083 r5386 2 2 package org.openstreetmap.josm.io; 3 3 4 /** 5 * Exception thrown when a communication error occured with the OSM server during API initialization. 6 * @see OsmApi#initialize 7 */ 4 8 public class OsmApiInitializationException extends OsmTransferException { 5 9 6 public OsmApiInitializationException() { 7 super(); 8 } 9 10 public OsmApiInitializationException(String message, Throwable cause) { 11 super(message, cause); 12 } 13 10 /** 11 * Constructs an {@code OsmApiInitializationException} with the specified detail message. 12 * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}. 13 * 14 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method) 15 */ 14 16 public OsmApiInitializationException(String message) { 15 17 super(message); 16 18 } 17 19 20 /** 21 * Constructs an {@code OsmApiInitializationException} with the specified cause and a detail message of 22 * <tt>(cause==null ? null : cause.toString())</tt> 23 * (which typically contains the class and detail message of <tt>cause</tt>). 24 * 25 * @param cause the cause (which is saved for later retrieval by the {@link #getCause} method). 26 * A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown. 27 */ 18 28 public OsmApiInitializationException(Throwable cause) { 19 29 super(cause); 20 30 } 31 32 /** 33 * Constructs an {@code OsmApiInitializationException} with the specified detail message and cause. 34 * 35 * <p> Note that the detail message associated with {@code cause} is <i>not</i> automatically incorporated 36 * into this exception's detail message. 37 * 38 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method) 39 * @param cause The cause (which is saved for later retrieval by the {@link #getCause} method). 40 * A null value is permitted, and indicates that the cause is nonexistent or unknown. 41 * 42 */ 43 public OsmApiInitializationException(String message, Throwable cause) { 44 super(message, cause); 45 } 21 46 } -
trunk/src/org/openstreetmap/josm/io/OsmTransferCanceledException.java
r4310 r5386 2 2 package org.openstreetmap.josm.io; 3 3 4 /** 5 * Exception thrown when a communication with the OSM server has been cancelled by the user. 6 */ 4 7 public class OsmTransferCanceledException extends OsmTransferException { 5 8 -
trunk/src/org/openstreetmap/josm/io/OsmTransferException.java
r3255 r5386 2 2 package org.openstreetmap.josm.io; 3 3 4 /** 5 * Exception thrown when a communication error with the OSM server occurs. 6 */ 4 7 public class OsmTransferException extends Exception { 5 8 6 9 private String url = OsmApi.getOsmApi().getBaseUrl(); 7 10 11 /** 12 * Constructs an {@code OsmTransferException} with {@code null} as its error detail message. 13 * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}. 14 */ 8 15 public OsmTransferException() { 9 16 } 10 17 18 /** 19 * Constructs an {@code OsmTransferException} with the specified detail message. 20 * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}. 21 * 22 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method) 23 */ 11 24 public OsmTransferException(String message) { 12 25 super(message); 13 26 } 14 27 28 /** 29 * Constructs an {@code OsmTransferException} with the specified cause and a detail message of 30 * <tt>(cause==null ? null : cause.toString())</tt> 31 * (which typically contains the class and detail message of <tt>cause</tt>). 32 * 33 * @param cause the cause (which is saved for later retrieval by the {@link #getCause} method). 34 * A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown. 35 */ 15 36 public OsmTransferException(Throwable cause) { 16 37 super(cause); 17 38 } 18 39 40 /** 41 * Constructs an {@code OsmTransferException} with the specified detail message and cause. 42 * 43 * <p> Note that the detail message associated with {@code cause} is <i>not</i> automatically incorporated 44 * into this exception's detail message. 45 * 46 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method) 47 * @param cause The cause (which is saved for later retrieval by the {@link #getCause} method). 48 * A null value is permitted, and indicates that the cause is nonexistent or unknown. 49 * 50 */ 19 51 public OsmTransferException(String message, Throwable cause) { 20 52 super(message, cause); 21 53 } 22 54 55 /** 56 * Sets the URL related to this error. 57 * @param url the URL related to this error (which is saved for later retrieval by the {@link #getUrl} method). 58 */ 23 59 public void setUrl(String url) { 24 60 this.url = url; … … 26 62 27 63 /** 28 * 29 * @return A pi base url or urlset usingsetUrlmethod64 * Gets the URL related to this error. 65 * @return API base URL or URL set using the {@link #setUrl} method 30 66 */ 31 67 public String getUrl() { 32 68 return url; 33 69 } 34 35 70 }
Note:
See TracChangeset
for help on using the changeset viewer.