001package ca.uhn.fhir.rest.server.exceptions; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2021 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; 024import org.hl7.fhir.instance.model.api.IIdType; 025 026import ca.uhn.fhir.model.api.IResource; 027import ca.uhn.fhir.model.base.composite.BaseIdentifierDt; 028import ca.uhn.fhir.model.primitive.IdDt; 029import ca.uhn.fhir.rest.api.Constants; 030import ca.uhn.fhir.util.CoverageIgnore; 031 032/** 033 * Represents an <b>HTTP 404 Resource Not Found</b> response, which means that the request is pointing to a resource that does not exist. 034 */ 035@CoverageIgnore 036public class ResourceNotFoundException extends BaseServerResponseException { 037 038 private static final long serialVersionUID = 1L; 039 040 public static final int STATUS_CODE = Constants.STATUS_HTTP_404_NOT_FOUND; 041 042 public ResourceNotFoundException(Class<? extends IResource> theClass, IdDt theId) { 043 super(STATUS_CODE, createErrorMessage(theClass, theId)); 044 } 045 046 public ResourceNotFoundException(Class<? extends IResource> theClass, IdDt theId, IBaseOperationOutcome theOperationOutcome) { 047 super(STATUS_CODE, createErrorMessage(theClass, theId), theOperationOutcome); 048 } 049 050 public ResourceNotFoundException(Class<? extends IResource> theClass, IIdType theId) { 051 super(STATUS_CODE, createErrorMessage(theClass, theId)); 052 } 053 054 public ResourceNotFoundException(Class<? extends IResource> theClass, IIdType theId, IBaseOperationOutcome theOperationOutcome) { 055 super(STATUS_CODE, createErrorMessage(theClass, theId), theOperationOutcome); 056 } 057 058 /** 059 * Constructor 060 * 061 * @param theMessage 062 * The message 063 * @param theOperationOutcome The OperationOutcome resource to return to the client 064 */ 065 public ResourceNotFoundException(String theMessage, IBaseOperationOutcome theOperationOutcome) { 066 super(STATUS_CODE, theMessage, theOperationOutcome); 067 } 068 069 /** 070 * @deprecated This doesn't make sense, since an identifier is not a resource ID and shouldn't generate a 404 if it isn't found - Should be removed 071 */ 072 @Deprecated 073 public ResourceNotFoundException(Class<? extends IResource> theClass, BaseIdentifierDt theId) { 074 super(STATUS_CODE, "Resource of type " + theClass.getSimpleName() + " with ID " + theId + " is not known"); 075 } 076 077 public ResourceNotFoundException(IdDt theId) { 078 super(STATUS_CODE, createErrorMessage(theId)); 079 } 080 081 public ResourceNotFoundException(IIdType theId) { 082 super(STATUS_CODE, createErrorMessage(theId)); 083 } 084 085 public ResourceNotFoundException(IIdType theId, IBaseOperationOutcome theOperationOutcome) { 086 super(STATUS_CODE, createErrorMessage(theId), theOperationOutcome); 087 } 088 089 public ResourceNotFoundException(String theMessage) { 090 super(STATUS_CODE, theMessage); 091 } 092 093 private static String createErrorMessage(Class<? extends IResource> theClass, IIdType theId) { 094 return "Resource of type " + theClass.getSimpleName() + " with ID " + theId + " is not known"; 095 } 096 097 private static String createErrorMessage(IIdType theId) { 098 return "Resource " + (theId != null ? theId.getValue() : "") + " is not known"; 099 } 100 101}