source: josm/trunk/src/org/openstreetmap/josm/io/ChangesetClosedException.java@ 13503

Last change on this file since 13503 was 13207, checked in by Don-vip, 6 years ago

enable PMD rule PreserveStackTrace + add missing jars to run new PMD rule designer

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.text.ParseException;
7import java.util.Date;
8import java.util.regex.Matcher;
9import java.util.regex.Pattern;
10
11import org.openstreetmap.josm.tools.Logging;
12import org.openstreetmap.josm.tools.date.DateUtils;
13
14/**
15 * A ChangesetClosedException is thrown if the server replies with a HTTP
16 * return code 409 (Conflict) with the error header {@link #ERROR_HEADER_PATTERN}.
17 *
18 * Depending on the context the exception is thrown in we have to react differently.
19 * <ul>
20 * <li>if it is thrown when we try to update a changeset, the changeset was most
21 * likely closed before, either explicitly by the user or because of a timeout</li>
22 * <li>if it is thrown when we try to upload data to the changeset, the changeset
23 * was most likely closed because we reached the servers capability limit for the size
24 * of a changeset.</li>
25 * </ul>
26 */
27public class ChangesetClosedException extends OsmTransferException {
28 /** the error header pattern for in case of HTTP response 409 indicating
29 * that a changeset was closed
30 */
31 public static final String ERROR_HEADER_PATTERN = "The changeset (\\d+) was closed at (.*)";
32
33 public enum Source {
34 /**
35 * The exception was thrown when a changeset was updated. This most likely means
36 * that the changeset was closed before.
37 */
38 UPDATE_CHANGESET,
39 /**
40 * The exception was thrown when data was uploaded to the changeset. This most
41 * likely means that the servers capability limits for a changeset have been
42 * exceeded.
43 */
44 UPLOAD_DATA,
45 /**
46 * Unspecified source
47 */
48 UNSPECIFIED
49 }
50
51 /**
52 * Replies true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
53 *
54 * @param errorHeader the error header
55 * @return true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
56 */
57 public static boolean errorHeaderMatchesPattern(String errorHeader) {
58 if (errorHeader == null)
59 return false;
60 Pattern p = Pattern.compile(ERROR_HEADER_PATTERN);
61 Matcher m = p.matcher(errorHeader);
62 return m.matches();
63 }
64
65 /** the changeset id */
66 private long changesetId;
67 /** the date on which the changeset was closed */
68 private Date closedOn;
69 /** the source */
70 private Source source;
71
72 protected final void parseErrorHeader(String errorHeader) {
73 Pattern p = Pattern.compile(ERROR_HEADER_PATTERN);
74 Matcher m = p.matcher(errorHeader);
75 if (m.matches()) {
76 changesetId = Long.parseLong(m.group(1));
77 try {
78 closedOn = DateUtils.newOsmApiDateTimeFormat().parse(m.group(2));
79 } catch (ParseException ex) {
80 Logging.error(tr("Failed to parse date ''{0}'' replied by server.", m.group(2)));
81 Logging.error(ex);
82 }
83 } else {
84 Logging.error(tr("Unexpected format of error header for conflict in changeset update. Got ''{0}''", errorHeader));
85 }
86 }
87
88 /**
89 * Creates the exception with the given <code>errorHeader</code>
90 *
91 * @param errorHeader the error header
92 */
93 public ChangesetClosedException(String errorHeader) {
94 super(errorHeader);
95 parseErrorHeader(errorHeader);
96 this.source = Source.UNSPECIFIED;
97 }
98
99 /**
100 * Creates the exception with the given error header and source.
101 *
102 * @param errorHeader the error header
103 * @param source the source for the exception
104 */
105 public ChangesetClosedException(String errorHeader, Source source) {
106 this(errorHeader, source, null);
107 }
108
109 /**
110 * Creates the exception with the given error header, source and cause.
111 *
112 * @param errorHeader the error header
113 * @param source the source for the exception
114 * @param cause The cause (which is saved for later retrieval by the {@link #getCause} method).
115 * A null value is permitted, and indicates that the cause is nonexistent or unknown.
116 * @since 13207
117 */
118 public ChangesetClosedException(String errorHeader, Source source, Throwable cause) {
119 super(errorHeader, cause);
120 parseErrorHeader(errorHeader);
121 this.source = source == null ? Source.UNSPECIFIED : source;
122 }
123
124 /**
125 * Creates the exception
126 *
127 * @param changesetId the id if the closed changeset
128 * @param closedOn the date the changeset was closed on
129 * @param source the source for the exception
130 */
131 public ChangesetClosedException(long changesetId, Date closedOn, Source source) {
132 super("");
133 this.source = source == null ? Source.UNSPECIFIED : source;
134 this.changesetId = changesetId;
135 this.closedOn = DateUtils.cloneDate(closedOn);
136 }
137
138 /**
139 * Replies the id of the changeset which was closed
140 *
141 * @return the id of the changeset which was closed
142 */
143 public long getChangesetId() {
144 return changesetId;
145 }
146
147 /**
148 * Replies the date the changeset was closed
149 *
150 * @return the date the changeset was closed. May be null if the date isn't known.
151 */
152 public Date getClosedOn() {
153 return DateUtils.cloneDate(closedOn);
154 }
155
156 /**
157 * Replies the source where the exception was thrown
158 *
159 * @return the source
160 */
161 public Source getSource() {
162 return source;
163 }
164
165 public void setSource(Source source) {
166 this.source = source == null ? Source.UNSPECIFIED : source;
167 }
168}
Note: See TracBrowser for help on using the repository browser.