| 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.DateFormat;
|
|---|
| 7 | import java.text.ParseException;
|
|---|
| 8 | import java.text.SimpleDateFormat;
|
|---|
| 9 | import java.util.Date;
|
|---|
| 10 | import java.util.Locale;
|
|---|
| 11 | import java.util.regex.Matcher;
|
|---|
| 12 | import java.util.regex.Pattern;
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * A ChangesetClosedException is thrown if the server replies with a HTTP
|
|---|
| 16 | * return code 409 (Conflict) with the error header {@see #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 | */
|
|---|
| 27 | public 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 | final static public String ERROR_HEADER_PATTERN = "The changeset (\\d+) was closed at (.*)";
|
|---|
| 32 |
|
|---|
| 33 | static 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 {@see #ERROR_HEADER_PATTERN}
|
|---|
| 53 | *
|
|---|
| 54 | * @param errorHeader the error header
|
|---|
| 55 | * @return true if <code>errorHeader</code> matches with {@see #ERROR_HEADER_PATTERN}
|
|---|
| 56 | */
|
|---|
| 57 | static public 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 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 | // Example: Tue Oct 15 10:00:00 UTC 2009. Always parsed with english locale regardless
|
|---|
| 78 | // of the current locale in JOSM
|
|---|
| 79 | DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.ENGLISH);
|
|---|
| 80 | try {
|
|---|
| 81 | closedOn = formatter.parse(m.group(2));
|
|---|
| 82 | } catch(ParseException ex) {
|
|---|
| 83 | System.err.println(tr("Failed to parse date ''{0}'' replied by server.", m.group(2)));
|
|---|
| 84 | ex.printStackTrace();
|
|---|
| 85 | }
|
|---|
| 86 | } else {
|
|---|
| 87 | System.err.println(tr("Unexpected format of error header for conflict in changeset update. Got ''{0}''", errorHeader));
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Creates the exception with the given <code>errorHeader</code>
|
|---|
| 93 | *
|
|---|
| 94 | * @param errorHeader the error header
|
|---|
| 95 | */
|
|---|
| 96 | public ChangesetClosedException(String errorHeader) {
|
|---|
| 97 | super(errorHeader);
|
|---|
| 98 | parseErrorHeader(errorHeader);
|
|---|
| 99 | this.source = Source.UNSPECIFIED;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Creates the exception with the given error header and the given
|
|---|
| 104 | * source.
|
|---|
| 105 | *
|
|---|
| 106 | * @param errorHeader the error header
|
|---|
| 107 | * @param source the source for the exception
|
|---|
| 108 | */
|
|---|
| 109 | public ChangesetClosedException(String errorHeader, Source source) {
|
|---|
| 110 | super(errorHeader);
|
|---|
| 111 | parseErrorHeader(errorHeader);
|
|---|
| 112 | this.source = source == null ? Source.UNSPECIFIED : source;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Replies the id of the changeset which was closed
|
|---|
| 117 | *
|
|---|
| 118 | * @return the id of the changeset which was closed
|
|---|
| 119 | */
|
|---|
| 120 | public long getChangesetId() {
|
|---|
| 121 | return changesetId;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * Replies the date the changeset was closed
|
|---|
| 126 | *
|
|---|
| 127 | * @return the date the changeset was closed. May be null if the date isn't known.
|
|---|
| 128 | */
|
|---|
| 129 | public Date getClosedOn() {
|
|---|
| 130 | return closedOn;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Replies the source where the exception was thrown
|
|---|
| 135 | *
|
|---|
| 136 | * @return the source
|
|---|
| 137 | */
|
|---|
| 138 | public Source getSource() {
|
|---|
| 139 | return source;
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|