View Javadoc
1   package ca.uhn.fhir.rest.server.exceptions;
2   
3   import java.util.LinkedHashSet;
4   import java.util.Set;
5   
6   import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
7   
8   import ca.uhn.fhir.rest.api.Constants;
9   import ca.uhn.fhir.rest.api.RequestTypeEnum;
10  
11  /*
12   * #%L
13   * HAPI FHIR - Core Library
14   * %%
15   * Copyright (C) 2014 - 2018 University Health Network
16   * %%
17   * Licensed under the Apache License, Version 2.0 (the "License");
18   * you may not use this file except in compliance with the License.
19   * You may obtain a copy of the License at
20   * 
21   * http://www.apache.org/licenses/LICENSE-2.0
22   * 
23   * Unless required by applicable law or agreed to in writing, software
24   * distributed under the License is distributed on an "AS IS" BASIS,
25   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26   * See the License for the specific language governing permissions and
27   * limitations under the License.
28   * #L%
29   */
30  
31  /**
32   * Represents an <b>HTTP 405 Method Not Allowed</b> response.
33   * 
34   * <p>
35   * Note that a complete list of RESTful exceptions is available in the <a href="./package-summary.html">Package Summary</a>.
36   * </p>
37   * 
38   * @see UnprocessableEntityException Which should be used for business level validation failures
39   */
40  public class MethodNotAllowedException extends BaseServerResponseException {
41  	private static final long serialVersionUID = 1L;
42  	public static final int STATUS_CODE = Constants.STATUS_HTTP_405_METHOD_NOT_ALLOWED;
43  	private Set<RequestTypeEnum> myAllowedMethods;
44  
45  	/**
46  	 * Constructor
47  	 * 
48  	 * @param theMessage
49  	 *           The message
50  	 * @param theOperationOutcome
51  	 *           The OperationOutcome resource to return to the client
52  	 * @param theAllowedMethods
53  	 *           A list of allowed methods (see {@link #setAllowedMethods(RequestTypeEnum...)} )
54  	 */
55  	public MethodNotAllowedException(String theMessage, IBaseOperationOutcome theOperationOutcome, RequestTypeEnum... theAllowedMethods) {
56  		super(STATUS_CODE, theMessage, theOperationOutcome);
57  		setAllowedMethods(theAllowedMethods);
58  	}
59  
60  	/**
61  	 * Constructor
62  	 * 
63  	 * @param theMessage
64  	 *           The message
65  	 * @param theAllowedMethods
66  	 *           A list of allowed methods (see {@link #setAllowedMethods(RequestTypeEnum...)} )
67  	 */
68  	public MethodNotAllowedException(String theMessage, RequestTypeEnum... theAllowedMethods) {
69  		super(STATUS_CODE, theMessage);
70  		setAllowedMethods(theAllowedMethods);
71  	}
72  
73  	/**
74  	 * Constructor
75  	 * 
76  	 * @param theMessage
77  	 *           The message
78  	 * @param theOperationOutcome
79  	 *           The OperationOutcome resource to return to the client
80  	 */
81  	public MethodNotAllowedException(String theMessage, IBaseOperationOutcome theOperationOutcome) {
82  		super(STATUS_CODE, theMessage, theOperationOutcome);
83  	}
84  
85  	/**
86  	 * Constructor
87  	 * 
88  	 * @param theMessage
89  	 *           The message
90  	 */
91  	public MethodNotAllowedException(String theMessage) {
92  		super(STATUS_CODE, theMessage);
93  	}
94  
95  	/**
96  	 * Specifies the list of allowed HTTP methods (GET, POST, etc). This is provided in an <code>Allow</code> header, as required by the HTTP specification (RFC 2616).
97  	 */
98  	public Set<RequestTypeEnum> getAllowedMethods() {
99  		return myAllowedMethods;
100 	}
101 
102 	/**
103 	 * Specifies the list of allowed HTTP methods (GET, POST, etc). This is provided in an <code>Allow</code> header, as required by the HTTP specification (RFC 2616).
104 	 */
105 	public void setAllowedMethods(RequestTypeEnum... theAllowedMethods) {
106 		if (theAllowedMethods == null || theAllowedMethods.length == 0) {
107 			myAllowedMethods = null;
108 		} else {
109 			myAllowedMethods = new LinkedHashSet<RequestTypeEnum>();
110 			for (RequestTypeEnum next : theAllowedMethods) {
111 				myAllowedMethods.add(next);
112 			}
113 		}
114 		updateAllowHeader();
115 	}
116 
117 	/**
118 	 * Specifies the list of allowed HTTP methods (GET, POST, etc). This is provided in an <code>Allow</code> header, as required by the HTTP specification (RFC 2616).
119 	 */
120 	public void setAllowedMethods(Set<RequestTypeEnum> theAllowedMethods) {
121 		myAllowedMethods = theAllowedMethods;
122 		updateAllowHeader();
123 	}
124 
125 	private void updateAllowHeader() {
126 		getResponseHeaders().remove(Constants.HEADER_ALLOW);
127 
128 		StringBuilder b = new StringBuilder();
129 		for (RequestTypeEnum next : myAllowedMethods) {
130 			if (b.length() > 0) {
131 				b.append(',');
132 			}
133 			b.append(next.name());
134 		}
135 		addResponseHeader(Constants.HEADER_ALLOW, b.toString());
136 	}
137 
138 }