[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 |
|
---|
| 6 | import java.text.ParseException;
|
---|
| 7 | import java.util.Date;
|
---|
| 8 | import java.util.regex.Matcher;
|
---|
| 9 | import java.util.regex.Pattern;
|
---|
| 10 |
|
---|
[6248] | 11 | import org.openstreetmap.josm.Main;
|
---|
[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 |
|
---|
[2599] | 33 | public static enum Source {
|
---|
[2569] | 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 | /**
|
---|
[5266] | 52 | * Replies true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
|
---|
[2711] | 53 | *
|
---|
[2569] | 54 | * @param errorHeader the error header
|
---|
[5266] | 55 | * @return true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
|
---|
[2569] | 56 | */
|
---|
[6889] | 57 | public static boolean errorHeaderMatchesPattern(String errorHeader) {
|
---|
[2569] | 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 |
|
---|
[2512] | 65 | /** the changeset id */
|
---|
| 66 | private long changesetId;
|
---|
| 67 | /** the date on which the changeset was closed */
|
---|
| 68 | private Date closedOn;
|
---|
[2569] | 69 | /** the source */
|
---|
| 70 | private Source source;
|
---|
[2512] | 71 |
|
---|
[6890] | 72 | protected final void parseErrorHeader(String errorHeader) {
|
---|
[2569] | 73 | Pattern p = Pattern.compile(ERROR_HEADER_PATTERN);
|
---|
[2512] | 74 | Matcher m = p.matcher(errorHeader);
|
---|
| 75 | if (m.matches()) {
|
---|
| 76 | changesetId = Long.parseLong(m.group(1));
|
---|
| 77 | try {
|
---|
[7299] | 78 | closedOn = DateUtils.newOsmApiDateTimeFormat().parse(m.group(2));
|
---|
[2512] | 79 | } catch(ParseException ex) {
|
---|
[6248] | 80 | Main.error(tr("Failed to parse date ''{0}'' replied by server.", m.group(2)));
|
---|
[6643] | 81 | Main.error(ex);
|
---|
[2512] | 82 | }
|
---|
| 83 | } else {
|
---|
[6248] | 84 | Main.error(tr("Unexpected format of error header for conflict in changeset update. Got ''{0}''", errorHeader));
|
---|
[2512] | 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[2569] | 88 | /**
|
---|
| 89 | * Creates the exception with the given <code>errorHeader</code>
|
---|
[2711] | 90 | *
|
---|
[2569] | 91 | * @param errorHeader the error header
|
---|
| 92 | */
|
---|
[2512] | 93 | public ChangesetClosedException(String errorHeader) {
|
---|
| 94 | super(errorHeader);
|
---|
| 95 | parseErrorHeader(errorHeader);
|
---|
[2569] | 96 | this.source = Source.UNSPECIFIED;
|
---|
[2512] | 97 | }
|
---|
| 98 |
|
---|
[2569] | 99 | /**
|
---|
| 100 | * Creates the exception with the given error header and the given
|
---|
| 101 | * source.
|
---|
[2711] | 102 | *
|
---|
[2569] | 103 | * @param errorHeader the error header
|
---|
| 104 | * @param source the source for the exception
|
---|
| 105 | */
|
---|
| 106 | public ChangesetClosedException(String errorHeader, Source source) {
|
---|
| 107 | super(errorHeader);
|
---|
| 108 | parseErrorHeader(errorHeader);
|
---|
| 109 | this.source = source == null ? Source.UNSPECIFIED : source;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
[2599] | 113 | * Creates the exception
|
---|
[2711] | 114 | *
|
---|
[2599] | 115 | * @param changesetId the id if the closed changeset
|
---|
| 116 | * @param closedOn the date the changeset was closed on
|
---|
| 117 | * @param source the source for the exception
|
---|
| 118 | */
|
---|
| 119 | public ChangesetClosedException(long changesetId, Date closedOn, Source source) {
|
---|
| 120 | super("");
|
---|
| 121 | this.source = source == null ? Source.UNSPECIFIED : source;
|
---|
| 122 | this.changesetId = changesetId;
|
---|
| 123 | this.closedOn = closedOn;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
[2569] | 127 | * Replies the id of the changeset which was closed
|
---|
[2711] | 128 | *
|
---|
[2569] | 129 | * @return the id of the changeset which was closed
|
---|
| 130 | */
|
---|
[2512] | 131 | public long getChangesetId() {
|
---|
| 132 | return changesetId;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[2569] | 135 | /**
|
---|
| 136 | * Replies the date the changeset was closed
|
---|
[2711] | 137 | *
|
---|
[2569] | 138 | * @return the date the changeset was closed. May be null if the date isn't known.
|
---|
| 139 | */
|
---|
[2512] | 140 | public Date getClosedOn() {
|
---|
| 141 | return closedOn;
|
---|
| 142 | }
|
---|
[2569] | 143 |
|
---|
| 144 | /**
|
---|
| 145 | * Replies the source where the exception was thrown
|
---|
[2711] | 146 | *
|
---|
[2569] | 147 | * @return the source
|
---|
| 148 | */
|
---|
| 149 | public Source getSource() {
|
---|
| 150 | return source;
|
---|
| 151 | }
|
---|
[2599] | 152 |
|
---|
| 153 | public void setSource(Source source) {
|
---|
| 154 | this.source = source == null ? Source.UNSPECIFIED : source;
|
---|
| 155 | }
|
---|
[2512] | 156 | }
|
---|