001package ca.uhn.fhir.rest.server.exceptions; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2022 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 ca.uhn.fhir.i18n.Msg; 024import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; 025import org.hl7.fhir.instance.model.api.IIdType; 026 027import ca.uhn.fhir.model.api.IResource; 028import ca.uhn.fhir.model.base.composite.BaseIdentifierDt; 029import ca.uhn.fhir.model.primitive.IdDt; 030import ca.uhn.fhir.rest.api.Constants; 031import ca.uhn.fhir.util.CoverageIgnore; 032 033/** 034 * Represents an <b>HTTP 404 Resource Not Found</b> response, which means that the request is pointing to a resource that does not exist. 035 */ 036@CoverageIgnore 037public class ResourceNotFoundException extends BaseServerResponseException { 038 039 private static final long serialVersionUID = 1L; 040 041 public static final int STATUS_CODE = Constants.STATUS_HTTP_404_NOT_FOUND; 042 043 public ResourceNotFoundException(Class<? extends IResource> theClass, IdDt theId) { 044 super(STATUS_CODE, createErrorMessage(theClass, theId)); 045 } 046 047 public ResourceNotFoundException(Class<? extends IResource> theClass, IdDt theId, IBaseOperationOutcome theOperationOutcome) { 048 super(STATUS_CODE, createErrorMessage(theClass, theId), theOperationOutcome); 049 } 050 051 public ResourceNotFoundException(Class<? extends IResource> theClass, IIdType theId) { 052 super(STATUS_CODE, createErrorMessage(theClass, theId)); 053 } 054 055 public ResourceNotFoundException(Class<? extends IResource> theClass, IIdType theId, IBaseOperationOutcome theOperationOutcome) { 056 super(STATUS_CODE, createErrorMessage(theClass, theId), theOperationOutcome); 057 } 058 059 /** 060 * Constructor 061 * 062 * @param theMessage 063 * The message 064 * @param theOperationOutcome The OperationOutcome resource to return to the client 065 */ 066 public ResourceNotFoundException(String theMessage, IBaseOperationOutcome theOperationOutcome) { 067 super(STATUS_CODE, theMessage, theOperationOutcome); 068 } 069 070 /** 071 * @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 072 */ 073 @Deprecated 074 public ResourceNotFoundException(Class<? extends IResource> theClass, BaseIdentifierDt theId) { 075 super(STATUS_CODE, "Resource of type " + theClass.getSimpleName() + " with ID " + theId + " is not known"); 076 } 077 078 public ResourceNotFoundException(IdDt theId) { 079 super(STATUS_CODE, createErrorMessage(theId)); 080 } 081 082 public ResourceNotFoundException(IIdType theId) { 083 super(STATUS_CODE, createErrorMessage(theId)); 084 } 085 086 public ResourceNotFoundException(IIdType theId, IBaseOperationOutcome theOperationOutcome) { 087 super(STATUS_CODE, createErrorMessage(theId), theOperationOutcome); 088 } 089 090 public ResourceNotFoundException(String theMessage) { 091 super(STATUS_CODE, theMessage); 092 } 093 094 private static String createErrorMessage(Class<? extends IResource> theClass, IIdType theId) { 095 return Msg.code(970) + "Resource of type " + theClass.getSimpleName() + " with ID " + theId + " is not known"; 096 } 097 098 private static String createErrorMessage(IIdType theId) { 099 return Msg.code(971) + "Resource " + (theId != null ? theId.getValue() : "") + " is not known"; 100 } 101 102}