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

Last change on this file was 18211, checked in by Don-vip, 3 years ago

global use of !Utils.isEmpty/isBlank

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