| [2512] | 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.io;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| [17840] | 6 | import java.time.Instant;
|
|---|
| [2512] | 7 | import java.util.regex.Matcher;
|
|---|
| 8 | import java.util.regex.Pattern;
|
|---|
| 9 |
|
|---|
| [12620] | 10 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| [17840] | 11 | import org.openstreetmap.josm.tools.UncheckedParseException;
|
|---|
| [7299] | 12 | import org.openstreetmap.josm.tools.date.DateUtils;
|
|---|
| [6248] | 13 |
|
|---|
| [2569] | 14 | /**
|
|---|
| 15 | * A ChangesetClosedException is thrown if the server replies with a HTTP
|
|---|
| [5266] | 16 | * return code 409 (Conflict) with the error header {@link #ERROR_HEADER_PATTERN}.
|
|---|
| [2569] | 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 | */
|
|---|
| [2512] | 27 | public class ChangesetClosedException extends OsmTransferException {
|
|---|
| [2569] | 28 | /** the error header pattern for in case of HTTP response 409 indicating
|
|---|
| 29 | * that a changeset was closed
|
|---|
| 30 | */
|
|---|
| [6889] | 31 | public static final String ERROR_HEADER_PATTERN = "The changeset (\\d+) was closed at (.*)";
|
|---|
| [2512] | 32 |
|
|---|
| [18283] | 33 | /**
|
|---|
| 34 | * Identifies when the changeset exception occurred.
|
|---|
| 35 | */
|
|---|
| [8836] | 36 | public enum Source {
|
|---|
| [2569] | 37 | /**
|
|---|
| 38 | * The exception was thrown when a changeset was updated. This most likely means
|
|---|
| 39 | * that the changeset was closed before.
|
|---|
| 40 | */
|
|---|
| 41 | UPDATE_CHANGESET,
|
|---|
| 42 | /**
|
|---|
| 43 | * The exception was thrown when data was uploaded to the changeset. This most
|
|---|
| 44 | * likely means that the servers capability limits for a changeset have been
|
|---|
| 45 | * exceeded.
|
|---|
| 46 | */
|
|---|
| 47 | UPLOAD_DATA,
|
|---|
| 48 | /**
|
|---|
| [18283] | 49 | * The exception was thrown when we tried to close a changeset. Probably the changeset
|
|---|
| 50 | * already timed out on the server.
|
|---|
| 51 | * @since 18283
|
|---|
| 52 | */
|
|---|
| 53 | CLOSE_CHANGESET,
|
|---|
| 54 | /**
|
|---|
| [2569] | 55 | * Unspecified source
|
|---|
| 56 | */
|
|---|
| 57 | UNSPECIFIED
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| [5266] | 61 | * Replies true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
|
|---|
| [2711] | 62 | *
|
|---|
| [2569] | 63 | * @param errorHeader the error header
|
|---|
| [5266] | 64 | * @return true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
|
|---|
| [2569] | 65 | */
|
|---|
| [6889] | 66 | public static boolean errorHeaderMatchesPattern(String errorHeader) {
|
|---|
| [2569] | 67 | if (errorHeader == null)
|
|---|
| 68 | return false;
|
|---|
| 69 | Pattern p = Pattern.compile(ERROR_HEADER_PATTERN);
|
|---|
| 70 | Matcher m = p.matcher(errorHeader);
|
|---|
| 71 | return m.matches();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| [2512] | 74 | /** the changeset id */
|
|---|
| 75 | private long changesetId;
|
|---|
| 76 | /** the date on which the changeset was closed */
|
|---|
| [17840] | 77 | private Instant closedOn;
|
|---|
| [2569] | 78 | /** the source */
|
|---|
| 79 | private Source source;
|
|---|
| [2512] | 80 |
|
|---|
| [6890] | 81 | protected final void parseErrorHeader(String errorHeader) {
|
|---|
| [2569] | 82 | Pattern p = Pattern.compile(ERROR_HEADER_PATTERN);
|
|---|
| [2512] | 83 | Matcher m = p.matcher(errorHeader);
|
|---|
| 84 | if (m.matches()) {
|
|---|
| 85 | changesetId = Long.parseLong(m.group(1));
|
|---|
| 86 | try {
|
|---|
| [17840] | 87 | closedOn = DateUtils.parseInstant(m.group(2));
|
|---|
| 88 | } catch (UncheckedParseException ex) {
|
|---|
| [12620] | 89 | Logging.error(tr("Failed to parse date ''{0}'' replied by server.", m.group(2)));
|
|---|
| 90 | Logging.error(ex);
|
|---|
| [2512] | 91 | }
|
|---|
| 92 | } else {
|
|---|
| [12620] | 93 | Logging.error(tr("Unexpected format of error header for conflict in changeset update. Got ''{0}''", errorHeader));
|
|---|
| [2512] | 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| [2569] | 97 | /**
|
|---|
| 98 | * Creates the exception with the given <code>errorHeader</code>
|
|---|
| [2711] | 99 | *
|
|---|
| [2569] | 100 | * @param errorHeader the error header
|
|---|
| 101 | */
|
|---|
| [2512] | 102 | public ChangesetClosedException(String errorHeader) {
|
|---|
| 103 | super(errorHeader);
|
|---|
| 104 | parseErrorHeader(errorHeader);
|
|---|
| [2569] | 105 | this.source = Source.UNSPECIFIED;
|
|---|
| [2512] | 106 | }
|
|---|
| 107 |
|
|---|
| [2569] | 108 | /**
|
|---|
| [13207] | 109 | * Creates the exception with the given error header and source.
|
|---|
| [2711] | 110 | *
|
|---|
| [2569] | 111 | * @param errorHeader the error header
|
|---|
| 112 | * @param source the source for the exception
|
|---|
| 113 | */
|
|---|
| 114 | public ChangesetClosedException(String errorHeader, Source source) {
|
|---|
| [13207] | 115 | this(errorHeader, source, null);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | * Creates the exception with the given error header, source and cause.
|
|---|
| 120 | *
|
|---|
| 121 | * @param errorHeader the error header
|
|---|
| 122 | * @param source the source for the exception
|
|---|
| 123 | * @param cause The cause (which is saved for later retrieval by the {@link #getCause} method).
|
|---|
| 124 | * A null value is permitted, and indicates that the cause is nonexistent or unknown.
|
|---|
| 125 | * @since 13207
|
|---|
| 126 | */
|
|---|
| 127 | public ChangesetClosedException(String errorHeader, Source source, Throwable cause) {
|
|---|
| 128 | super(errorHeader, cause);
|
|---|
| [2569] | 129 | parseErrorHeader(errorHeader);
|
|---|
| 130 | this.source = source == null ? Source.UNSPECIFIED : source;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| [2599] | 134 | * Creates the exception
|
|---|
| [2711] | 135 | *
|
|---|
| [2599] | 136 | * @param changesetId the id if the closed changeset
|
|---|
| 137 | * @param closedOn the date the changeset was closed on
|
|---|
| 138 | * @param source the source for the exception
|
|---|
| 139 | */
|
|---|
| [17840] | 140 | public ChangesetClosedException(long changesetId, Instant closedOn, Source source) {
|
|---|
| [2599] | 141 | super("");
|
|---|
| 142 | this.source = source == null ? Source.UNSPECIFIED : source;
|
|---|
| 143 | this.changesetId = changesetId;
|
|---|
| [17840] | 144 | this.closedOn = closedOn;
|
|---|
| [2599] | 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| [2569] | 148 | * Replies the id of the changeset which was closed
|
|---|
| [2711] | 149 | *
|
|---|
| [2569] | 150 | * @return the id of the changeset which was closed
|
|---|
| 151 | */
|
|---|
| [2512] | 152 | public long getChangesetId() {
|
|---|
| 153 | return changesetId;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| [2569] | 156 | /**
|
|---|
| 157 | * Replies the date the changeset was closed
|
|---|
| [2711] | 158 | *
|
|---|
| [2569] | 159 | * @return the date the changeset was closed. May be null if the date isn't known.
|
|---|
| 160 | */
|
|---|
| [17840] | 161 | public Instant getClosedOn() {
|
|---|
| 162 | return closedOn;
|
|---|
| [2512] | 163 | }
|
|---|
| [2569] | 164 |
|
|---|
| 165 | /**
|
|---|
| 166 | * Replies the source where the exception was thrown
|
|---|
| [2711] | 167 | *
|
|---|
| [2569] | 168 | * @return the source
|
|---|
| 169 | */
|
|---|
| 170 | public Source getSource() {
|
|---|
| 171 | return source;
|
|---|
| 172 | }
|
|---|
| [2599] | 173 |
|
|---|
| [18283] | 174 | /**
|
|---|
| 175 | * Sets the source where the exception was thrown
|
|---|
| 176 | *
|
|---|
| 177 | * @param source the source where the exception was thrown
|
|---|
| 178 | */
|
|---|
| [2599] | 179 | public void setSource(Source source) {
|
|---|
| 180 | this.source = source == null ? Source.UNSPECIFIED : source;
|
|---|
| 181 | }
|
|---|
| [2512] | 182 | }
|
|---|