diff --git a/hapi-fhir-base/examples/src/main/java/example/ClientExamples.java b/hapi-fhir-base/examples/src/main/java/example/ClientExamples.java index 0c3c3be7689..cd22c69c41f 100644 --- a/hapi-fhir-base/examples/src/main/java/example/ClientExamples.java +++ b/hapi-fhir-base/examples/src/main/java/example/ClientExamples.java @@ -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 diff --git a/hapi-fhir-base/examples/src/main/java/example/ValidatorExamples.java b/hapi-fhir-base/examples/src/main/java/example/ValidatorExamples.java index d11feca8dda..c2460d85004 100644 --- a/hapi-fhir-base/examples/src/main/java/example/ValidatorExamples.java +++ b/hapi-fhir-base/examples/src/main/java/example/ValidatorExamples.java @@ -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,51 +10,50 @@ 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 { public void validateResource() { - //START SNIPPET: basicValidation + // START SNIPPET: basicValidation // As always, you need a context FhirContext ctx = new FhirContext(); - + // Create and populate a new patient object Patient p = new Patient(); p.addName().addFamily("Smith").addGiven("John").addGiven("Q"); p.addIdentifier("urn:foo:identifiers", "12345"); p.addTelecom().setSystem(ContactSystemEnum.PHONE).setValue("416 123-4567"); - + // Request a validator and apply it FhirValidator val = ctx.newValidator(); - try { + + ValidationResult result = val.validateWithResult(p); + if (result.isSuccessful()) { - val.validate(p); 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 { - validateFiles(); - + // END SNIPPET: basicValidation + } - private static void validateFiles() throws IOException, FileNotFoundException { - //START SNIPPET: validateFiles + public static void main(String[] args) throws Exception { + validateFiles(); + + } + + private static void validateFiles() throws Exception { + // START SNIPPET: validateFiles FhirContext ctx = new FhirContext(); // Create a validator and configure it @@ -67,19 +64,23 @@ public class ValidatorExamples { // Get a list of files in a given directory String[] fileList = new File("/home/some/dir").list(new WildcardFileFilter("*.txt")); for (String nextFile : fileList) { - + // For each file, load the contents into a string String nextFileContents = IOUtils.toString(new FileReader(nextFile)); - + // 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 + 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 } } diff --git a/hapi-fhir-base/src/changes/changes.xml b/hapi-fhir-base/src/changes/changes.xml index 9fdf2f28b7e..7ce8838e33a 100644 --- a/hapi-fhir-base/src/changes/changes.xml +++ b/hapi-fhir-base/src/changes/changes.xml @@ -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! + + Add a client interceptor which adds an HTTP cookie to each client request. Thanks to + Petro Mykhailysyn for the pull request! +