source: josm/trunk/src/org/openstreetmap/josm/io/OsmApiException.java@ 13170

Last change on this file since 13170 was 12992, checked in by Don-vip, 7 years ago

fix #15435 - do not cache incorrect login credentials when using basic auth

  • Property svn:eol-style set to native
File size: 8.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import org.openstreetmap.josm.tools.Logging;
6
7/**
8 * Exception thrown when a communication error occurs when accessing the <a href="http://wiki.openstreetmap.org/wiki/API_v0.6">OSM API</a>.
9 * @see OsmApi
10 */
11public class OsmApiException extends OsmTransferException {
12
13 private int responseCode;
14 private String errorHeader;
15 private String errorBody;
16 private String accessedUrl;
17 private String login;
18
19 /**
20 * Constructs an {@code OsmApiException} with the specified response code, error header and error body
21 * @param responseCode The HTTP response code replied by the OSM server.
22 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
23 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
24 * @param errorBody The error body, as transmitted in the HTTP response body
25 * @param accessedUrl The complete URL accessed when this error occured
26 * @param login the login used to connect to OSM API (can be null)
27 * @since 12992
28 */
29 public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl, String login) {
30 this.responseCode = responseCode;
31 this.errorHeader = errorHeader;
32 this.errorBody = errorBody;
33 this.accessedUrl = accessedUrl;
34 this.login = login;
35 }
36
37 /**
38 * Constructs an {@code OsmApiException} with the specified response code, error header and error body
39 * @param responseCode The HTTP response code replied by the OSM server.
40 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
41 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
42 * @param errorBody The error body, as transmitted in the HTTP response body
43 * @param accessedUrl The complete URL accessed when this error occured
44 * @since 5584
45 */
46 public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl) {
47 this(responseCode, errorHeader, errorBody, accessedUrl, null);
48 }
49
50 /**
51 * Constructs an {@code OsmApiException} with the specified response code, error header and error body
52 * @param responseCode The HTTP response code replied by the OSM server.
53 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
54 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
55 * @param errorBody The error body, as transmitted in the HTTP response body
56 */
57 public OsmApiException(int responseCode, String errorHeader, String errorBody) {
58 this(responseCode, errorHeader, errorBody, null);
59 }
60
61 /**
62 * Constructs an {@code OsmApiException} with the specified detail message.
63 * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}.
64 *
65 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method)
66 */
67 public OsmApiException(String message) {
68 super(message);
69 }
70
71 /**
72 * Constructs an {@code OsmApiException} with the specified cause and a detail message of
73 * <tt>(cause==null ? null : cause.toString())</tt>
74 * (which typically contains the class and detail message of <tt>cause</tt>).
75 *
76 * @param cause the cause (which is saved for later retrieval by the {@link #getCause} method).
77 * A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown.
78 */
79 public OsmApiException(Throwable cause) {
80 super(cause);
81 }
82
83 /**
84 * Constructs an {@code OsmApiException} with the specified detail message and cause.
85 *
86 * <p> Note that the detail message associated with {@code cause} is <i>not</i> automatically incorporated
87 * into this exception's detail message.
88 *
89 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method)
90 * @param cause The cause (which is saved for later retrieval by the {@link #getCause} method).
91 * A null value is permitted, and indicates that the cause is nonexistent or unknown.
92 *
93 */
94 public OsmApiException(String message, Throwable cause) {
95 super(message, cause);
96 }
97
98 /**
99 * Replies the HTTP response code.
100 * @return The HTTP response code replied by the OSM server. Refer to
101 * <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.
102 */
103 public int getResponseCode() {
104 return responseCode;
105 }
106
107 /**
108 * Sets the HTTP response code.
109 * @param responseCode The HTTP response code replied by the OSM server.
110 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
111 */
112 public void setResponseCode(int responseCode) {
113 this.responseCode = responseCode;
114 }
115
116 /**
117 * Replies the error header.
118 * @return the error header, as transmitted in the {@code Error} field of the HTTP response header
119 */
120 public String getErrorHeader() {
121 return errorHeader;
122 }
123
124 /**
125 * Sets the error header.
126 * @param errorHeader the error header, as transmitted in the {@code Error} field of the HTTP response header
127 */
128 public void setErrorHeader(String errorHeader) {
129 this.errorHeader = errorHeader;
130 }
131
132 /**
133 * Replies the error body.
134 * @return The error body, as transmitted in the HTTP response body
135 */
136 public String getErrorBody() {
137 return errorBody;
138 }
139
140 /**
141 * Sets the error body.
142 * @param errorBody The error body, as transmitted in the HTTP response body
143 */
144 public void setErrorBody(String errorBody) {
145 this.errorBody = errorBody;
146 }
147
148 @Override
149 public String getMessage() {
150 StringBuilder sb = new StringBuilder();
151 sb.append("ResponseCode=")
152 .append(responseCode);
153 String eh = "";
154 try {
155 if (errorHeader != null)
156 eh = tr(errorHeader.trim());
157 if (!eh.isEmpty()) {
158 sb.append(", Error Header=<")
159 .append(eh)
160 .append('>');
161 }
162 } catch (IllegalArgumentException e) {
163 // Ignored
164 Logging.trace(e);
165 }
166 try {
167 String eb = errorBody != null ? tr(errorBody.trim()) : "";
168 if (!eb.isEmpty() && !eb.equals(eh)) {
169 sb.append(", Error Body=<")
170 .append(eb)
171 .append('>');
172 }
173 } catch (IllegalArgumentException e) {
174 // Ignored
175 Logging.trace(e);
176 }
177 return sb.toString();
178 }
179
180 /**
181 * Replies a message suitable to be displayed in a message dialog
182 *
183 * @return a message which is suitable to be displayed in a message dialog
184 */
185 public String getDisplayMessage() {
186 StringBuilder sb = new StringBuilder();
187 if (errorHeader != null) {
188 sb.append(tr(errorHeader));
189 sb.append(tr("(Code={0})", responseCode));
190 } else if (errorBody != null && !errorBody.trim().isEmpty()) {
191 errorBody = errorBody.trim();
192 sb.append(tr(errorBody));
193 sb.append(tr("(Code={0})", responseCode));
194 } else {
195 sb.append(tr("The server replied an error with code {0}.", responseCode));
196 }
197 return sb.toString();
198 }
199
200 /**
201 * Sets the complete URL accessed when this error occured.
202 * This is distinct from the one set with {@link #setUrl}, which is generally only the base URL of the server.
203 * @param url the complete URL accessed when this error occured.
204 */
205 public void setAccessedUrl(String url) {
206 this.accessedUrl = url;
207 }
208
209 /**
210 * Replies the complete URL accessed when this error occured.
211 * This is distinct from the one returned by {@link #getUrl}, which is generally only the base URL of the server.
212 * @return the complete URL accessed when this error occured.
213 */
214 public String getAccessedUrl() {
215 return accessedUrl;
216 }
217
218 /**
219 * Sets the login used to connect to OSM API.
220 * @param login the login used to connect to OSM API
221 * @since 12992
222 */
223 public void setLogin(String login) {
224 this.login = login;
225 }
226
227 /**
228 * Replies the login used to connect to OSM API.
229 * @return the login used to connect to OSM API, or {@code null}
230 * @since 12992
231 */
232 public String getLogin() {
233 return login;
234 }
235}
Note: See TracBrowser for help on using the repository browser.