Add an example for #35 - Cookie Interceptor
This commit is contained in:
parent
e31970f095
commit
4f8eec4075
|
@ -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.api.IBasicClient;
|
||||||
import ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor;
|
import ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor;
|
||||||
import ca.uhn.fhir.rest.client.interceptor.BearerTokenAuthInterceptor;
|
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.client.interceptor.LoggingInterceptor;
|
||||||
import ca.uhn.fhir.rest.server.EncodingEnum;
|
import ca.uhn.fhir.rest.server.EncodingEnum;
|
||||||
|
|
||||||
|
@ -46,6 +47,27 @@ public class ClientExamples {
|
||||||
// END SNIPPET: security
|
// 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")
|
@SuppressWarnings("unused")
|
||||||
public void createSecurityBearer() {
|
public void createSecurityBearer() {
|
||||||
// START SNIPPET: securityBearer
|
// START SNIPPET: securityBearer
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
package example;
|
package example;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.io.filefilter.WildcardFileFilter;
|
import org.apache.commons.io.filefilter.WildcardFileFilter;
|
||||||
|
@ -12,51 +10,50 @@ import ca.uhn.fhir.context.FhirContext;
|
||||||
import ca.uhn.fhir.model.api.IResource;
|
import ca.uhn.fhir.model.api.IResource;
|
||||||
import ca.uhn.fhir.model.dstu.resource.Patient;
|
import ca.uhn.fhir.model.dstu.resource.Patient;
|
||||||
import ca.uhn.fhir.model.dstu.valueset.ContactSystemEnum;
|
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.FhirValidator;
|
||||||
import ca.uhn.fhir.validation.ValidationFailureException;
|
import ca.uhn.fhir.validation.ValidationResult;
|
||||||
|
|
||||||
public class ValidatorExamples {
|
public class ValidatorExamples {
|
||||||
|
|
||||||
public void validateResource() {
|
public void validateResource() {
|
||||||
//START SNIPPET: basicValidation
|
// START SNIPPET: basicValidation
|
||||||
// As always, you need a context
|
// As always, you need a context
|
||||||
FhirContext ctx = new FhirContext();
|
FhirContext ctx = new FhirContext();
|
||||||
|
|
||||||
// Create and populate a new patient object
|
// Create and populate a new patient object
|
||||||
Patient p = new Patient();
|
Patient p = new Patient();
|
||||||
p.addName().addFamily("Smith").addGiven("John").addGiven("Q");
|
p.addName().addFamily("Smith").addGiven("John").addGiven("Q");
|
||||||
p.addIdentifier("urn:foo:identifiers", "12345");
|
p.addIdentifier("urn:foo:identifiers", "12345");
|
||||||
p.addTelecom().setSystem(ContactSystemEnum.PHONE).setValue("416 123-4567");
|
p.addTelecom().setSystem(ContactSystemEnum.PHONE).setValue("416 123-4567");
|
||||||
|
|
||||||
// Request a validator and apply it
|
// Request a validator and apply it
|
||||||
FhirValidator val = ctx.newValidator();
|
FhirValidator val = ctx.newValidator();
|
||||||
try {
|
|
||||||
|
ValidationResult result = val.validateWithResult(p);
|
||||||
|
if (result.isSuccessful()) {
|
||||||
|
|
||||||
val.validate(p);
|
|
||||||
System.out.println("Validation passed");
|
System.out.println("Validation passed");
|
||||||
|
|
||||||
} catch (ValidationFailureException e) {
|
} else {
|
||||||
// We failed validation!
|
// We failed validation!
|
||||||
|
|
||||||
System.out.println("Validation failed");
|
System.out.println("Validation failed");
|
||||||
|
|
||||||
// The ValidationFailureException which gets thrown by the validator
|
// The result contains an OperationOutcome outlining the failures
|
||||||
// will contain an OperationOutcome resource describing the failure
|
String results = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(result.getOperationOutcome());
|
||||||
String results = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome());
|
|
||||||
System.out.println(results);
|
System.out.println(results);
|
||||||
}
|
}
|
||||||
//END SNIPPET: basicValidation
|
// END SNIPPET: basicValidation
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) throws DataFormatException, IOException {
|
|
||||||
validateFiles();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void validateFiles() throws IOException, FileNotFoundException {
|
public static void main(String[] args) throws Exception {
|
||||||
//START SNIPPET: validateFiles
|
validateFiles();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateFiles() throws Exception {
|
||||||
|
// START SNIPPET: validateFiles
|
||||||
FhirContext ctx = new FhirContext();
|
FhirContext ctx = new FhirContext();
|
||||||
|
|
||||||
// Create a validator and configure it
|
// Create a validator and configure it
|
||||||
|
@ -67,19 +64,23 @@ public class ValidatorExamples {
|
||||||
// Get a list of files in a given directory
|
// Get a list of files in a given directory
|
||||||
String[] fileList = new File("/home/some/dir").list(new WildcardFileFilter("*.txt"));
|
String[] fileList = new File("/home/some/dir").list(new WildcardFileFilter("*.txt"));
|
||||||
for (String nextFile : fileList) {
|
for (String nextFile : fileList) {
|
||||||
|
|
||||||
// For each file, load the contents into a string
|
// For each file, load the contents into a string
|
||||||
String nextFileContents = IOUtils.toString(new FileReader(nextFile));
|
String nextFileContents = IOUtils.toString(new FileReader(nextFile));
|
||||||
|
|
||||||
// Parse that string (this example assumes JSON encoding)
|
// Parse that string (this example assumes JSON encoding)
|
||||||
IResource resource = ctx.newJsonParser().parseResource(nextFileContents);
|
IResource resource = ctx.newJsonParser().parseResource(nextFileContents);
|
||||||
|
|
||||||
|
// 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!");
|
||||||
|
}
|
||||||
|
|
||||||
// Apply the validation. This will throw an exception on the first validation failure
|
|
||||||
validator.validate(resource);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we make it here with no exception, all the files validated!
|
// END SNIPPET: validateFiles
|
||||||
//END SNIPPET: validateFiles
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,6 +120,10 @@
|
||||||
Change validation API so that it uses a return type instead of exceptions to communicate
|
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!
|
validation failures. Thanks to Joe Athman for the pull request!
|
||||||
</action>
|
</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>
|
||||||
<release version="0.6" date="2014-Sep-08" description="This release brings a number of new features and bug fixes!">
|
<release version="0.6" date="2014-Sep-08" description="This release brings a number of new features and bug fixes!">
|
||||||
<!--
|
<!--
|
||||||
|
|
|
@ -438,8 +438,22 @@
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Note that individual requests and responses
|
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>
|
</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>
|
</section>
|
||||||
|
|
||||||
|
|
|
@ -71,15 +71,15 @@
|
||||||
|
|
||||||
</subsection>
|
</subsection>
|
||||||
|
|
||||||
<subsection name="Configuring an HTTP Proxy">
|
<subsection name="Cookies: Add an HTTP Cookie Header to Client Requests">
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
An HTTP proxy may be configured directly on the
|
The <code>CookieInterceptor</code> can be used to
|
||||||
restful client factory, as shown below.
|
add an HTTP Cookie header to each request created by the client.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<macro name="snippet">
|
<macro name="snippet">
|
||||||
<param name="id" value="logging" />
|
<param name="id" value="cookie" />
|
||||||
<param name="file" value="examples/src/main/java/example/ClientExamples.java" />
|
<param name="file" value="examples/src/main/java/example/ClientExamples.java" />
|
||||||
</macro>
|
</macro>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue