| 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 | import org.openstreetmap.josm.Main;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * A ChangesetClosedException is thrown if the server replies with a HTTP
|
|---|
| 18 | * return code 409 (Conflict) with the error header {@link #ERROR_HEADER_PATTERN}.
|
|---|
| 19 | *
|
|---|
| 20 | * Depending on the context the exception is thrown in we have to react differently.
|
|---|
| 21 | * <ul>
|
|---|
| 22 | * <li>if it is thrown when we try to update a changeset, the changeset was most
|
|---|
| 23 | * likely closed before, either explicitly by the user or because of a timeout</li>
|
|---|
| 24 | * <li>if it is thrown when we try to upload data to the changeset, the changeset
|
|---|
| 25 | * was most likely closed because we reached the servers capability limit for the size
|
|---|
| 26 | * of a changeset.</li>
|
|---|
| 27 | * </ul>
|
|---|
| 28 | */
|
|---|
| 29 | public class ChangesetClosedException extends OsmTransferException {
|
|---|
| 30 | /** the error header pattern for in case of HTTP response 409 indicating
|
|---|
| 31 | * that a changeset was closed
|
|---|
| 32 | */
|
|---|
| 33 | public static final String ERROR_HEADER_PATTERN = "The changeset (\\d+) was closed at (.*)";
|
|---|
| 34 |
|
|---|
| 35 | public static enum Source {
|
|---|
| 36 | /**
|
|---|
| 37 | * The exception was thrown when a changeset was updated. This most likely means
|
|---|
| 38 | * that the changeset was closed before.
|
|---|
| 39 | */
|
|---|
| 40 | UPDATE_CHANGESET,
|
|---|
| 41 | /**
|
|---|
| 42 | * The exception was thrown when data was uploaded to the changeset. This most
|
|---|
| 43 | * likely means that the servers capability limits for a changeset have been
|
|---|
| 44 | * exceeded.
|
|---|
| 45 | */
|
|---|
| 46 | UPLOAD_DATA,
|
|---|
| 47 | /**
|
|---|
| 48 | * Unspecified source
|
|---|
| 49 | */
|
|---|
| 50 | UNSPECIFIED
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Replies true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
|
|---|
| 55 | *
|
|---|
| 56 | * @param errorHeader the error header
|
|---|
| 57 | * @return true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
|
|---|
| 58 | */
|
|---|
| 59 | public static boolean errorHeaderMatchesPattern(String errorHeader) {
|
|---|
| 60 | if (errorHeader == null)
|
|---|
| 61 | return false;
|
|---|
| 62 | Pattern p = Pattern.compile(ERROR_HEADER_PATTERN);
|
|---|
| 63 | Matcher m = p.matcher(errorHeader);
|
|---|
| 64 | return m.matches();
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /** the changeset id */
|
|---|
| 68 | private long changesetId;
|
|---|
| 69 | /** the date on which the changeset was closed */
|
|---|
| 70 | private Date closedOn;
|
|---|
| 71 | /** the source */
|
|---|
| 72 | private Source source;
|
|---|
| 73 |
|
|---|
| 74 | protected final void parseErrorHeader(String errorHeader) {
|
|---|
| 75 | Pattern p = Pattern.compile(ERROR_HEADER_PATTERN);
|
|---|
| 76 | Matcher m = p.matcher(errorHeader);
|
|---|
| 77 | if (m.matches()) {
|
|---|
| 78 | changesetId = Long.parseLong(m.group(1));
|
|---|
| 79 | // Example: "2010-09-07 14:39:41 UTC". Always parsed with US locale regardless
|
|---|
| 80 | // of the current locale in JOSM
|
|---|
| 81 | DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US);
|
|---|
| 82 | try {
|
|---|
| 83 | closedOn = formatter.parse(m.group(2));
|
|---|
| 84 | } catch(ParseException ex) {
|
|---|
| 85 | Main.error(tr("Failed to parse date ''{0}'' replied by server.", m.group(2)));
|
|---|
| 86 | Main.error(ex);
|
|---|
| 87 | }
|
|---|
| 88 | } else {
|
|---|
| 89 | Main.error(tr("Unexpected format of error header for conflict in changeset update. Got ''{0}''", errorHeader));
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Creates the exception with the given <code>errorHeader</code>
|
|---|
| 95 | *
|
|---|
| 96 | * @param errorHeader the error header
|
|---|
| 97 | */
|
|---|
| 98 | public ChangesetClosedException(String errorHeader) {
|
|---|
| 99 | super(errorHeader);
|
|---|
| 100 | parseErrorHeader(errorHeader);
|
|---|
| 101 | this.source = Source.UNSPECIFIED;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Creates the exception with the given error header and the given
|
|---|
| 106 | * source.
|
|---|
| 107 | *
|
|---|
| 108 | * @param errorHeader the error header
|
|---|
| 109 | * @param source the source for the exception
|
|---|
| 110 | */
|
|---|
| 111 | public ChangesetClosedException(String errorHeader, Source source) {
|
|---|
| 112 | super(errorHeader);
|
|---|
| 113 | parseErrorHeader(errorHeader);
|
|---|
| 114 | this.source = source == null ? Source.UNSPECIFIED : source;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | * Creates the exception
|
|---|
| 119 | *
|
|---|
| 120 | * @param changesetId the id if the closed changeset
|
|---|
| 121 | * @param closedOn the date the changeset was closed on
|
|---|
| 122 | * @param source the source for the exception
|
|---|
| 123 | */
|
|---|
| 124 | public ChangesetClosedException(long changesetId, Date closedOn, Source source) {
|
|---|
| 125 | super("");
|
|---|
| 126 | this.source = source == null ? Source.UNSPECIFIED : source;
|
|---|
| 127 | this.changesetId = changesetId;
|
|---|
| 128 | this.closedOn = closedOn;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Replies the id of the changeset which was closed
|
|---|
| 133 | *
|
|---|
| 134 | * @return the id of the changeset which was closed
|
|---|
| 135 | */
|
|---|
| 136 | public long getChangesetId() {
|
|---|
| 137 | return changesetId;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /**
|
|---|
| 141 | * Replies the date the changeset was closed
|
|---|
| 142 | *
|
|---|
| 143 | * @return the date the changeset was closed. May be null if the date isn't known.
|
|---|
| 144 | */
|
|---|
| 145 | public Date getClosedOn() {
|
|---|
| 146 | return closedOn;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Replies the source where the exception was thrown
|
|---|
| 151 | *
|
|---|
| 152 | * @return the source
|
|---|
| 153 | */
|
|---|
| 154 | public Source getSource() {
|
|---|
| 155 | return source;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | public void setSource(Source source) {
|
|---|
| 159 | this.source = source == null ? Source.UNSPECIFIED : source;
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|