Skip to content

Commit

Permalink
Implement handleException on the server interceptor framework, as well
Browse files Browse the repository at this point in the history
as some site and documentation enhancements
  • Loading branch information
jamesagnew committed Nov 7, 2014
1 parent 8f70403 commit d22a357
Show file tree
Hide file tree
Showing 26 changed files with 528 additions and 179 deletions.
29 changes: 29 additions & 0 deletions examples/src/main/java/example/RequestCounterInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter;

//START SNIPPET: interceptor
public class RequestCounterInterceptor extends InterceptorAdapter
{

private int myRequestCount;

public int getRequestCount() {
return myRequestCount;
}

/**
* Override the incomingRequestPreProcessed method, which is called
* for each incoming request before any processing is done
*/
@Override
public boolean incomingRequestPreProcessed(HttpServletRequest theRequest, HttpServletResponse theResponse) {
myRequestCount++;
return true;
}

}
//END SNIPPET: interceptor
42 changes: 42 additions & 0 deletions examples/src/main/java/example/RequestExceptionInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package example;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ca.uhn.fhir.rest.method.RequestDetails;
import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter;

//START SNIPPET: interceptor
public class RequestExceptionInterceptor extends InterceptorAdapter
{

@Override
public boolean handleException(RequestDetails theRequestDetails, Throwable theException, HttpServletRequest theServletRequest,
HttpServletResponse theServletResponse) throws ServletException, IOException {

// If the exception is a built-in type, it defines the correct status
// code to return. Otherwise default to 500.
if (theException instanceof BaseServerResponseException) {
theServletResponse.setStatus(((BaseServerResponseException) theException).getStatusCode());
} else {
theServletResponse.setStatus(Constants.STATUS_HTTP_500_INTERNAL_ERROR);
}

// Provide a response ourself
theServletResponse.setContentType("text/plain");
theServletResponse.getWriter().append("Failed to process!");
theServletResponse.getWriter().close();

// Since we handled this response in the interceptor, we must return false
// to stop processing immediately
return false;
}


}
//END SNIPPET: interceptor
6 changes: 5 additions & 1 deletion examples/src/main/java/example/ServerInterceptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.dstu.composite.HumanNameDt;
Expand All @@ -11,12 +14,13 @@
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter;

public class ServerInterceptors {

@SuppressWarnings("unused")
public static void main(String[] args) throws DataFormatException, IOException {


// START SNIPPET: resourceExtension
// Create an example patient
Expand Down
3 changes: 3 additions & 0 deletions hapi-deployable-pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.7</version>
<inherited>false</inherited>
<reportSets>
<reportSet>
<reports>
<report>scm</report>
</reports>
</reportSet>
</reportSets>
Expand All @@ -39,6 +41,7 @@
<configuration>
<links>
<link>http://docs.oracle.com/javaee/7/api</link>
<link>http://jamesagnew.github.io/hapi-fhir/apidocs</link>
</links>
</configuration>
</reportSet>
Expand Down
9 changes: 2 additions & 7 deletions hapi-fhir-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@

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

<distributionManagement>
<site>
<id>git.server</id>
<url>scm:git:git@github.com:jamesagnew/hapi-fhir.git</url>
</site>
</distributionManagement>

<dependencies>

<!-- JSON -->
Expand Down Expand Up @@ -214,6 +207,8 @@
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,11 @@ public static String escape(String theValue) {
case ',':
case '|':
b.append('\\');
// fall through
break;
default:
b.append(next);
break;
}
b.append(next);
}

return b.toString();
Expand Down

This file was deleted.

Loading

0 comments on commit d22a357

Please sign in to comment.