1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.net.HttpURLConnection;
|
---|
5 | import java.util.regex.Matcher;
|
---|
6 | import java.util.regex.Pattern;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Represents an exception thrown by the OSM API if JOSM tries to update or delete a primitive
|
---|
12 | * which is already deleted on the server.
|
---|
13 | * @since 2198
|
---|
14 | */
|
---|
15 | public class OsmApiPrimitiveGoneException extends OsmApiException {
|
---|
16 | /**
|
---|
17 | * The regexp pattern for the error header replied by the OSM API
|
---|
18 | */
|
---|
19 | public static final String ERROR_HEADER_PATTERN = "The (\\S+) with the id (\\d+) has already been deleted";
|
---|
20 | /** the type of the primitive which is gone on the server */
|
---|
21 | private final OsmPrimitiveType type;
|
---|
22 | /** the id of the primitive */
|
---|
23 | private final long id;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Constructs a new {@code OsmApiPrimitiveGoneException}.
|
---|
27 | * @param errorHeader error header
|
---|
28 | * @param errorBody error body
|
---|
29 | */
|
---|
30 | public OsmApiPrimitiveGoneException(String errorHeader, String errorBody) {
|
---|
31 | super(HttpURLConnection.HTTP_GONE, errorHeader, errorBody);
|
---|
32 | if (errorHeader != null) {
|
---|
33 | Matcher m = Pattern.compile(ERROR_HEADER_PATTERN).matcher(errorHeader);
|
---|
34 | if (m.matches()) {
|
---|
35 | type = OsmPrimitiveType.from(m.group(1));
|
---|
36 | id = Long.parseLong(m.group(2));
|
---|
37 | } else {
|
---|
38 | type = null;
|
---|
39 | id = 0;
|
---|
40 | }
|
---|
41 | } else {
|
---|
42 | type = null;
|
---|
43 | id = 0;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Replies true if we know what primitive this exception was thrown for
|
---|
49 | *
|
---|
50 | * @return true if we know what primitive this exception was thrown for
|
---|
51 | */
|
---|
52 | public boolean isKnownPrimitive() {
|
---|
53 | return id > 0 && type != null;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Replies the type of the primitive this exception was thrown for. null,
|
---|
58 | * if the type is not known.
|
---|
59 | *
|
---|
60 | * @return the type of the primitive this exception was thrown for
|
---|
61 | */
|
---|
62 | public OsmPrimitiveType getPrimitiveType() {
|
---|
63 | return type;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Replies the id of the primitive this exception was thrown for. 0, if
|
---|
68 | * the id is not known.
|
---|
69 | *
|
---|
70 | * @return the id of the primitive this exception was thrown for
|
---|
71 | */
|
---|
72 | public long getPrimitiveId() {
|
---|
73 | return id;
|
---|
74 | }
|
---|
75 | }
|
---|