View Javadoc
1   package ca.uhn.fhir.rest.server.exceptions;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
24  import org.hl7.fhir.instance.model.api.IIdType;
25  
26  import ca.uhn.fhir.model.api.IResource;
27  import ca.uhn.fhir.model.base.composite.BaseIdentifierDt;
28  import ca.uhn.fhir.model.primitive.IdDt;
29  import ca.uhn.fhir.rest.api.Constants;
30  import ca.uhn.fhir.util.CoverageIgnore;
31  
32  /**
33   * Represents an <b>HTTP 404 Resource Not Found</b> response, which means that the request is pointing to a resource that does not exist.
34   */
35  @CoverageIgnore
36  public class ResourceNotFoundException extends BaseServerResponseException {
37  
38  	private static final long serialVersionUID = 1L;
39  
40  	public static final int STATUS_CODE = Constants.STATUS_HTTP_404_NOT_FOUND;
41  
42  	public ResourceNotFoundException(Class<? extends IResource> theClass, IdDt theId) {
43  		super(STATUS_CODE, createErrorMessage(theClass, theId));
44  	}
45  
46  	public ResourceNotFoundException(Class<? extends IResource> theClass, IdDt theId, IBaseOperationOutcome theOperationOutcome) {
47  		super(STATUS_CODE, createErrorMessage(theClass, theId), theOperationOutcome);
48  	}
49  
50  	public ResourceNotFoundException(Class<? extends IResource> theClass, IIdType theId) {
51  		super(STATUS_CODE, createErrorMessage(theClass, theId));
52  	}
53  
54  	public ResourceNotFoundException(Class<? extends IResource> theClass, IIdType theId, IBaseOperationOutcome theOperationOutcome) {
55  		super(STATUS_CODE, createErrorMessage(theClass, theId), theOperationOutcome);
56  	}
57  
58  	/**
59  	 * Constructor
60  	 * 
61  	 * @param theMessage
62  	 *            The message
63  	 *  @param theOperationOutcome The OperationOutcome resource to return to the client
64  	 */
65  	public ResourceNotFoundException(String theMessage, IBaseOperationOutcome theOperationOutcome) {
66  		super(STATUS_CODE, theMessage, theOperationOutcome);
67  	}
68  
69  	/**
70  	 * @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
71  	 */
72  	@Deprecated
73  	public ResourceNotFoundException(Class<? extends IResource> theClass, BaseIdentifierDt theId) {
74  		super(STATUS_CODE, "Resource of type " + theClass.getSimpleName() + " with ID " + theId + " is not known");
75  	}
76  
77  	public ResourceNotFoundException(IdDt theId) {
78  		super(STATUS_CODE, createErrorMessage(theId));
79  	}
80  
81  	public ResourceNotFoundException(IIdType theId) {
82  		super(STATUS_CODE, createErrorMessage(theId));
83  	}
84  
85  	public ResourceNotFoundException(IIdType theId, IBaseOperationOutcome theOperationOutcome) {
86  		super(STATUS_CODE, createErrorMessage(theId), theOperationOutcome);
87  	}
88  
89  	public ResourceNotFoundException(String theMessage) {
90  		super(STATUS_CODE, theMessage);
91  	}
92  
93    private static String createErrorMessage(Class<? extends IResource> theClass, IIdType theId) {
94  		return "Resource of type " + theClass.getSimpleName() + " with ID " + theId + " is not known";
95  	}
96  
97  	private static String createErrorMessage(IIdType theId) {
98  		return "Resource " + (theId != null ? theId.getValue() : "") + " is not known";
99  	}
100 
101 }