[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.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 |
|
---|
[6248] | 14 | import org.openstreetmap.josm.Main;
|
---|
| 15 |
|
---|
[2569] | 16 | /**
|
---|
| 17 | * A ChangesetClosedException is thrown if the server replies with a HTTP
|
---|
[5266] | 18 | * return code 409 (Conflict) with the error header {@link #ERROR_HEADER_PATTERN}.
|
---|
[2569] | 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 | */
|
---|
[2512] | 29 | public class ChangesetClosedException extends OsmTransferException {
|
---|
[2569] | 30 | /** the error header pattern for in case of HTTP response 409 indicating
|
---|
| 31 | * that a changeset was closed
|
---|
| 32 | */
|
---|
| 33 | final static public String ERROR_HEADER_PATTERN = "The changeset (\\d+) was closed at (.*)";
|
---|
[2512] | 34 |
|
---|
[2599] | 35 | public static enum Source {
|
---|
[2569] | 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 | /**
|
---|
[5266] | 54 | * Replies true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
|
---|
[2711] | 55 | *
|
---|
[2569] | 56 | * @param errorHeader the error header
|
---|
[5266] | 57 | * @return true if <code>errorHeader</code> matches with {@link #ERROR_HEADER_PATTERN}
|
---|
[2569] | 58 | */
|
---|
| 59 | static public 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 |
|
---|
[2512] | 67 | /** the changeset id */
|
---|
| 68 | private long changesetId;
|
---|
| 69 | /** the date on which the changeset was closed */
|
---|
| 70 | private Date closedOn;
|
---|
[2569] | 71 | /** the source */
|
---|
| 72 | private Source source;
|
---|
[2512] | 73 |
|
---|
| 74 | protected void parseErrorHeader(String errorHeader) {
|
---|
[2569] | 75 | Pattern p = Pattern.compile(ERROR_HEADER_PATTERN);
|
---|
[2512] | 76 | Matcher m = p.matcher(errorHeader);
|
---|
| 77 | if (m.matches()) {
|
---|
| 78 | changesetId = Long.parseLong(m.group(1));
|
---|
[3562] | 79 | // Example: "2010-09-07 14:39:41 UTC". Always parsed with US locale regardless
|
---|
[2512] | 80 | // of the current locale in JOSM
|
---|
[3562] | 81 | DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US);
|
---|
[2512] | 82 | try {
|
---|
| 83 | closedOn = formatter.parse(m.group(2));
|
---|
| 84 | } catch(ParseException ex) {
|
---|
[6248] | 85 | Main.error(tr("Failed to parse date ''{0}'' replied by server.", m.group(2)));
|
---|
[6643] | 86 | Main.error(ex);
|
---|
[2512] | 87 | }
|
---|
| 88 | } else {
|
---|
[6248] | 89 | Main.error(tr("Unexpected format of error header for conflict in changeset update. Got ''{0}''", errorHeader));
|
---|
[2512] | 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[2569] | 93 | /**
|
---|
| 94 | * Creates the exception with the given <code>errorHeader</code>
|
---|
[2711] | 95 | *
|
---|
[2569] | 96 | * @param errorHeader the error header
|
---|
| 97 | */
|
---|
[2512] | 98 | public ChangesetClosedException(String errorHeader) {
|
---|
| 99 | super(errorHeader);
|
---|
| 100 | parseErrorHeader(errorHeader);
|
---|
[2569] | 101 | this.source = Source.UNSPECIFIED;
|
---|
[2512] | 102 | }
|
---|
| 103 |
|
---|
[2569] | 104 | /**
|
---|
| 105 | * Creates the exception with the given error header and the given
|
---|
| 106 | * source.
|
---|
[2711] | 107 | *
|
---|
[2569] | 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 | /**
|
---|
[2599] | 118 | * Creates the exception
|
---|
[2711] | 119 | *
|
---|
[2599] | 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 | /**
|
---|
[2569] | 132 | * Replies the id of the changeset which was closed
|
---|
[2711] | 133 | *
|
---|
[2569] | 134 | * @return the id of the changeset which was closed
|
---|
| 135 | */
|
---|
[2512] | 136 | public long getChangesetId() {
|
---|
| 137 | return changesetId;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[2569] | 140 | /**
|
---|
| 141 | * Replies the date the changeset was closed
|
---|
[2711] | 142 | *
|
---|
[2569] | 143 | * @return the date the changeset was closed. May be null if the date isn't known.
|
---|
| 144 | */
|
---|
[2512] | 145 | public Date getClosedOn() {
|
---|
| 146 | return closedOn;
|
---|
| 147 | }
|
---|
[2569] | 148 |
|
---|
| 149 | /**
|
---|
| 150 | * Replies the source where the exception was thrown
|
---|
[2711] | 151 | *
|
---|
[2569] | 152 | * @return the source
|
---|
| 153 | */
|
---|
| 154 | public Source getSource() {
|
---|
| 155 | return source;
|
---|
| 156 | }
|
---|
[2599] | 157 |
|
---|
| 158 | public void setSource(Source source) {
|
---|
| 159 | this.source = source == null ? Source.UNSPECIFIED : source;
|
---|
| 160 | }
|
---|
[2512] | 161 | }
|
---|