Add an example for #35 - Cookie Interceptor

This commit is contained in:
James Agnew 2014-10-17 16:39:47 -04:00
parent e31970f095
commit 4f8eec4075
6 changed files with 81 additions and 36 deletions

View File

@ -6,6 +6,7 @@ import ca.uhn.fhir.rest.client.IRestfulClientFactory;
import ca.uhn.fhir.rest.client.api.IBasicClient;
import ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor;
import ca.uhn.fhir.rest.client.interceptor.BearerTokenAuthInterceptor;
import ca.uhn.fhir.rest.client.interceptor.CookieInterceptor;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.server.EncodingEnum;
@ -46,6 +47,27 @@ public class ClientExamples {
// END SNIPPET: security
}
@SuppressWarnings("unused")
public void createCookie() {
// START SNIPPET: cookie
// Create a context and get the client factory so it can be configured
FhirContext ctx = new FhirContext();
IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();
// Create a cookie interceptor. This cookie will have the name "mycookie" and
// the value "Chips Ahoy"
CookieInterceptor interceptor = new CookieInterceptor("mycookie=Chips Ahoy");
// Register the interceptor with your client (either style)
IPatientClient annotationClient = ctx.newRestfulClient(IPatientClient.class, "http://localhost:9999/fhir");
annotationClient.registerInterceptor(interceptor);
IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir");
annotationClient.registerInterceptor(interceptor);
// END SNIPPET: cookie
}
@SuppressWarnings("unused")
public void createSecurityBearer() {
// START SNIPPET: securityBearer

View File

@ -1,9 +1,7 @@
package example;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
@ -12,9 +10,8 @@ import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.dstu.valueset.ContactSystemEnum;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.validation.FhirValidator;
import ca.uhn.fhir.validation.ValidationFailureException;
import ca.uhn.fhir.validation.ValidationResult;
public class ValidatorExamples {
@ -31,31 +28,31 @@ public class ValidatorExamples {
// Request a validator and apply it
FhirValidator val = ctx.newValidator();
try {
val.validate(p);
ValidationResult result = val.validateWithResult(p);
if (result.isSuccessful()) {
System.out.println("Validation passed");
} catch (ValidationFailureException e) {
} else {
// We failed validation!
System.out.println("Validation failed");
// The ValidationFailureException which gets thrown by the validator
// will contain an OperationOutcome resource describing the failure
String results = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome());
// The result contains an OperationOutcome outlining the failures
String results = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(result.getOperationOutcome());
System.out.println(results);
}
// END SNIPPET: basicValidation
}
public static void main(String[] args) throws DataFormatException, IOException {
public static void main(String[] args) throws Exception {
validateFiles();
}
private static void validateFiles() throws IOException, FileNotFoundException {
private static void validateFiles() throws Exception {
// START SNIPPET: validateFiles
FhirContext ctx = new FhirContext();
@ -74,11 +71,15 @@ public class ValidatorExamples {
// Parse that string (this example assumes JSON encoding)
IResource resource = ctx.newJsonParser().parseResource(nextFileContents);
// Apply the validation. This will throw an exception on the first validation failure
validator.validate(resource);
// Apply the validation. This will throw an exception on the first
// validation failure
ValidationResult result = validator.validateWithResult(resource);
if (result.isSuccessful() == false) {
throw new Exception("We failed!");
}
}
// If we make it here with no exception, all the files validated!
// END SNIPPET: validateFiles
}

View File

@ -120,6 +120,10 @@
Change validation API so that it uses a return type instead of exceptions to communicate
validation failures. Thanks to Joe Athman for the pull request!
</action>
<action type="add" issue="35" dev="petromykhailysyn">
Add a client interceptor which adds an HTTP cookie to each client request. Thanks to
Petro Mykhailysyn for the pull request!
</action>
</release>
<release version="0.6" date="2014-Sep-08" description="This release brings a number of new features and bug fixes!">
<!--

View File

@ -438,9 +438,23 @@
<p>
Note that individual requests and responses
can be tweaked using <a href="./doc_rest_client_interceptor.html">interceptors</a>.
can be tweaked using <a href="./doc_rest_client_interceptor.html">Client Interceptors</a>.
</p>
<subsection name="Configuring an HTTP Proxy">
<p>
The following example shows how to configure the use of an HTTP
proxy in the client.
</p>
<macro name="snippet">
<param name="id" value="proxy" />
<param name="file" value="examples/src/main/java/example/ClientExamples.java" />
</macro>
</subsection>
</section>
</body>

View File

@ -71,15 +71,15 @@
</subsection>
<subsection name="Configuring an HTTP Proxy">
<subsection name="Cookies: Add an HTTP Cookie Header to Client Requests">
<p>
An HTTP proxy may be configured directly on the
restful client factory, as shown below.
The <code>CookieInterceptor</code> can be used to
add an HTTP Cookie header to each request created by the client.
</p>
<macro name="snippet">
<param name="id" value="logging" />
<param name="id" value="cookie" />
<param name="file" value="examples/src/main/java/example/ClientExamples.java" />
</macro>

View File

@ -97,6 +97,10 @@
<id>jathman</id>
<name>Joe Athman</name>
</developer>
<developer>
<id>petromykhailysyn</id>
<name>Petro Mykhailyshyn</name>
</developer>
</developers>
<licenses>