001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import java.net.HttpURLConnection;
005import java.util.regex.Matcher;
006import java.util.regex.Pattern;
007
008import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
009
010/**
011 * Represents an exception thrown by the OSM API if JOSM tries to update or delete a primitive
012 * which is already deleted on the server.
013 *
014 */
015public class OsmApiPrimitiveGoneException extends OsmApiException {
016    /**
017     * The regexp pattern for the error header replied by the OSM API
018     */
019    public static final String ERROR_HEADER_PATTERN = "The (\\S+) with the id (\\d+) has already been deleted";
020    /** the type of the primitive which is gone on the server */
021    private OsmPrimitiveType type;
022    /** the id of the primitive */
023    private long id;
024
025    public OsmApiPrimitiveGoneException(String errorHeader, String errorBody) {
026        super(HttpURLConnection.HTTP_GONE, errorHeader, errorBody);
027        if (errorHeader == null) return;
028        Pattern p = Pattern.compile(ERROR_HEADER_PATTERN);
029        Matcher m = p.matcher(errorHeader);
030        if (m.matches()) {
031            type = OsmPrimitiveType.from(m.group(1));
032            id = Long.parseLong(m.group(2));
033        }
034    }
035
036    /**
037     * Replies true if we know what primitive this exception was thrown for
038     *
039     * @return true if we know what primitive this exception was thrown for
040     */
041    public boolean isKnownPrimitive() {
042        return id > 0 && type != null;
043    }
044
045    /**
046     * Replies the type of the primitive this exception was thrown for. null,
047     * if the type is not known.
048     *
049     * @return the type of the primitive this exception was thrown for
050     */
051    public OsmPrimitiveType getPrimitiveType() {
052        return type;
053    }
054
055    /**
056     * Replies the id of the primitive this exception was thrown for. 0, if
057     * the id is not known.
058     *
059     * @return the id of the primitive this exception was thrown for
060     */
061    public long getPrimitiveId() {
062        return id;
063    }
064}