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 * @since 2198 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 final OsmPrimitiveType type; 022 /** the id of the primitive */ 023 private final long id; 024 025 /** 026 * Constructs a new {@code OsmApiPrimitiveGoneException}. 027 * @param errorHeader error header 028 * @param errorBody error body 029 */ 030 public OsmApiPrimitiveGoneException(String errorHeader, String errorBody) { 031 super(HttpURLConnection.HTTP_GONE, errorHeader, errorBody); 032 if (errorHeader != null) { 033 Matcher m = Pattern.compile(ERROR_HEADER_PATTERN).matcher(errorHeader); 034 if (m.matches()) { 035 type = OsmPrimitiveType.from(m.group(1)); 036 id = Long.parseLong(m.group(2)); 037 } else { 038 type = null; 039 id = 0; 040 } 041 } else { 042 type = null; 043 id = 0; 044 } 045 } 046 047 /** 048 * Replies true if we know what primitive this exception was thrown for 049 * 050 * @return true if we know what primitive this exception was thrown for 051 */ 052 public boolean isKnownPrimitive() { 053 return id > 0 && type != null; 054 } 055 056 /** 057 * Replies the type of the primitive this exception was thrown for. null, 058 * if the type is not known. 059 * 060 * @return the type of the primitive this exception was thrown for 061 */ 062 public OsmPrimitiveType getPrimitiveType() { 063 return type; 064 } 065 066 /** 067 * Replies the id of the primitive this exception was thrown for. 0, if 068 * the id is not known. 069 * 070 * @return the id of the primitive this exception was thrown for 071 */ 072 public long getPrimitiveId() { 073 return id; 074 } 075}