Skip to content

Commit d22a357

Browse files
committed
Implement handleException on the server interceptor framework, as well
as some site and documentation enhancements
1 parent 8f70403 commit d22a357

26 files changed

+528
-179
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package example;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter;
7+
8+
//START SNIPPET: interceptor
9+
public class RequestCounterInterceptor extends InterceptorAdapter
10+
{
11+
12+
private int myRequestCount;
13+
14+
public int getRequestCount() {
15+
return myRequestCount;
16+
}
17+
18+
/**
19+
* Override the incomingRequestPreProcessed method, which is called
20+
* for each incoming request before any processing is done
21+
*/
22+
@Override
23+
public boolean incomingRequestPreProcessed(HttpServletRequest theRequest, HttpServletResponse theResponse) {
24+
myRequestCount++;
25+
return true;
26+
}
27+
28+
}
29+
//END SNIPPET: interceptor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package example;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
9+
import ca.uhn.fhir.rest.method.RequestDetails;
10+
import ca.uhn.fhir.rest.server.Constants;
11+
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
12+
import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter;
13+
14+
//START SNIPPET: interceptor
15+
public class RequestExceptionInterceptor extends InterceptorAdapter
16+
{
17+
18+
@Override
19+
public boolean handleException(RequestDetails theRequestDetails, Throwable theException, HttpServletRequest theServletRequest,
20+
HttpServletResponse theServletResponse) throws ServletException, IOException {
21+
22+
// If the exception is a built-in type, it defines the correct status
23+
// code to return. Otherwise default to 500.
24+
if (theException instanceof BaseServerResponseException) {
25+
theServletResponse.setStatus(((BaseServerResponseException) theException).getStatusCode());
26+
} else {
27+
theServletResponse.setStatus(Constants.STATUS_HTTP_500_INTERNAL_ERROR);
28+
}
29+
30+
// Provide a response ourself
31+
theServletResponse.setContentType("text/plain");
32+
theServletResponse.getWriter().append("Failed to process!");
33+
theServletResponse.getWriter().close();
34+
35+
// Since we handled this response in the interceptor, we must return false
36+
// to stop processing immediately
37+
return false;
38+
}
39+
40+
41+
}
42+
//END SNIPPET: interceptor

examples/src/main/java/example/ServerInterceptors.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.io.IOException;
44
import java.util.List;
55

6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
69
import ca.uhn.fhir.context.FhirContext;
710
import ca.uhn.fhir.model.api.ExtensionDt;
811
import ca.uhn.fhir.model.dstu.composite.HumanNameDt;
@@ -11,12 +14,13 @@
1114
import ca.uhn.fhir.model.primitive.DateTimeDt;
1215
import ca.uhn.fhir.model.primitive.StringDt;
1316
import ca.uhn.fhir.parser.DataFormatException;
17+
import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter;
1418

1519
public class ServerInterceptors {
1620

1721
@SuppressWarnings("unused")
1822
public static void main(String[] args) throws DataFormatException, IOException {
19-
23+
2024

2125
// START SNIPPET: resourceExtension
2226
// Create an example patient

hapi-deployable-pom/pom.xml

+3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
<groupId>org.apache.maven.plugins</groupId>
2020
<artifactId>maven-project-info-reports-plugin</artifactId>
2121
<version>2.7</version>
22+
<inherited>false</inherited>
2223
<reportSets>
2324
<reportSet>
2425
<reports>
26+
<report>scm</report>
2527
</reports>
2628
</reportSet>
2729
</reportSets>
@@ -39,6 +41,7 @@
3941
<configuration>
4042
<links>
4143
<link>http://docs.oracle.com/javaee/7/api</link>
44+
<link>http://jamesagnew.github.io/hapi-fhir/apidocs</link>
4245
</links>
4346
</configuration>
4447
</reportSet>

hapi-fhir-base/pom.xml

+2-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515

1616
<name>HAPI FHIR - Core Library</name>
1717

18-
<distributionManagement>
19-
<site>
20-
<id>git.server</id>
21-
<url>scm:git:[email protected]:jamesagnew/hapi-fhir.git</url>
22-
</site>
23-
</distributionManagement>
24-
2518
<dependencies>
2619

2720
<!-- JSON -->
@@ -214,6 +207,8 @@
214207
<groupId>org.codehaus.mojo</groupId>
215208
<artifactId>findbugs-maven-plugin</artifactId>
216209
<version>3.0.0</version>
210+
<configuration>
211+
</configuration>
217212
</plugin>
218213
<plugin>
219214
<groupId>org.apache.maven.plugins</groupId>

hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ParameterUtil.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,11 @@ public static String escape(String theValue) {
207207
case ',':
208208
case '|':
209209
b.append('\\');
210-
// fall through
210+
break;
211211
default:
212-
b.append(next);
212+
break;
213213
}
214+
b.append(next);
214215
}
215216

216217
return b.toString();

hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/ISecurityManager.java

-32
This file was deleted.

0 commit comments

Comments
 (0)