1 package ca.uhn.fhir.rest.server.exceptions;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import ca.uhn.fhir.model.dstu.resource.OperationOutcome;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public abstract class BaseServerResponseException extends RuntimeException {
35
36 private static final Map<Integer, Class<? extends BaseServerResponseException>> ourStatusCodeToExceptionType = new HashMap<Integer, Class<? extends BaseServerResponseException>>();
37 private static final long serialVersionUID = 1L;
38
39 static {
40 registerExceptionType(AuthenticationException.STATUS_CODE, AuthenticationException.class);
41 registerExceptionType(InternalErrorException.STATUS_CODE, InternalErrorException.class);
42 registerExceptionType(InvalidRequestException.STATUS_CODE, InvalidRequestException.class);
43 registerExceptionType(MethodNotAllowedException.STATUS_CODE, MethodNotAllowedException.class);
44 registerExceptionType(ResourceNotFoundException.STATUS_CODE, ResourceNotFoundException.class);
45 registerExceptionType(ResourceVersionNotSpecifiedException.STATUS_CODE, ResourceVersionNotSpecifiedException.class);
46 registerExceptionType(ResourceVersionConflictException.STATUS_CODE, ResourceVersionConflictException.class);
47 registerExceptionType(UnprocessableEntityException.STATUS_CODE, UnprocessableEntityException.class);
48 registerExceptionType(ResourceGoneException.STATUS_CODE, ResourceGoneException.class);
49 registerExceptionType(NotImplementedOperationException.STATUS_CODE, NotImplementedOperationException.class);
50 }
51
52 private OperationOutcome myOperationOutcome;
53 private String myResponseBody;
54 private String myResponseMimeType;
55 private int myStatusCode;
56
57
58
59
60
61
62
63
64
65 public BaseServerResponseException(int theStatusCode, String theMessage) {
66 super(theMessage);
67 myStatusCode = theStatusCode;
68 myOperationOutcome = null;
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82 public BaseServerResponseException(int theStatusCode, String theMessage, OperationOutcome theOperationOutcome) {
83 super(theMessage);
84 myStatusCode = theStatusCode;
85 myOperationOutcome = theOperationOutcome;
86 }
87
88
89
90
91
92
93
94
95
96
97
98 public BaseServerResponseException(int theStatusCode, String theMessage, Throwable theCause) {
99 super(theMessage, theCause);
100 myStatusCode = theStatusCode;
101 myOperationOutcome = null;
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 public BaseServerResponseException(int theStatusCode, String theMessage, Throwable theCause, OperationOutcome theOperationOutcome) {
118 super(theMessage, theCause);
119 myStatusCode = theStatusCode;
120 myOperationOutcome = theOperationOutcome;
121 }
122
123
124
125
126
127
128
129
130
131 public BaseServerResponseException(int theStatusCode, Throwable theCause) {
132 super(theCause.toString(), theCause);
133 myStatusCode = theStatusCode;
134 myOperationOutcome = null;
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148 public BaseServerResponseException(int theStatusCode, Throwable theCause, OperationOutcome theOperationOutcome) {
149 super(theCause.toString(), theCause);
150 myStatusCode = theStatusCode;
151 myOperationOutcome = theOperationOutcome;
152 }
153
154
155
156
157 public OperationOutcome getOperationOutcome() {
158 return myOperationOutcome;
159 }
160
161
162
163
164
165
166
167
168 public String getResponseBody() {
169 return myResponseBody;
170 }
171
172
173
174
175
176
177
178
179 public String getResponseMimeType() {
180 return myResponseMimeType;
181 }
182
183
184
185
186 public int getStatusCode() {
187 return myStatusCode;
188 }
189
190
191
192
193
194
195
196
197 public void setOperationOutcome(OperationOutcome theOperationOutcome) {
198 myOperationOutcome = theOperationOutcome;
199 }
200
201
202
203
204 public void setResponseBody(String theResponseBody) {
205 myResponseBody = theResponseBody;
206 }
207
208
209
210
211 public void setResponseMimeType(String theResponseMimeType) {
212 myResponseMimeType = theResponseMimeType;
213 }
214
215
216
217
218 static boolean isExceptionTypeRegistered(Class<?> theType) {
219 return ourStatusCodeToExceptionType.values().contains(theType);
220 }
221
222 public static BaseServerResponseException newInstance(int theStatusCode, String theMessage) {
223 if (ourStatusCodeToExceptionType.containsKey(theStatusCode)) {
224 try {
225 return ourStatusCodeToExceptionType.get(theStatusCode).getConstructor(new Class[] { String.class }).newInstance(theMessage);
226 } catch (InstantiationException e) {
227 throw new InternalErrorException(e);
228 } catch (IllegalAccessException e) {
229 throw new InternalErrorException(e);
230 } catch (IllegalArgumentException e) {
231 throw new InternalErrorException(e);
232 } catch (InvocationTargetException e) {
233 throw new InternalErrorException(e);
234 } catch (NoSuchMethodException e) {
235 throw new InternalErrorException(e);
236 } catch (SecurityException e) {
237 throw new InternalErrorException(e);
238 }
239 } else {
240 return new UnclassifiedServerFailureException(theStatusCode, theMessage);
241 }
242 }
243
244 static void registerExceptionType(int theStatusCode, Class<? extends BaseServerResponseException> theType) {
245 if (ourStatusCodeToExceptionType.containsKey(theStatusCode)) {
246 throw new Error("Can not register " + theType + " to status code " + theStatusCode + " because " + ourStatusCodeToExceptionType.get(theStatusCode) + " already registers that code");
247 }
248 ourStatusCodeToExceptionType.put(theStatusCode, theType);
249 }
250
251 }