public void upgrade() { // START SNIPPET: client FhirContext ctxDstu2 = FhirContext.forDstu2(); IGenericClient clientDstu2 = ctxDstu2.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu2"); // END SNIPPET: client }
@Override public IGenericClient newClient(FhirContext theFhirContext, HttpServletRequest theRequest, String theServerBaseUrl) { // Create a client IGenericClient client = theFhirContext.newRestfulGenericClient(theServerBaseUrl); // Register an interceptor which adds credentials client.registerInterceptor( new BasicAuthInterceptor(ContextListener.username, ContextListener.password)); return client; }
@SuppressWarnings("unused") public void getResourceTags() { // START SNIPPET: context // Create a DSTU2 context, which will use DSTU2 semantics FhirContext ctx = FhirContext.forDstu2(); // This parser supports DSTU2 IParser parser = ctx.newJsonParser(); // This client supports DSTU2 IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu2"); // END SNIPPET: context }
public static void deferModelScanning() { // START SNIPPET: deferModelScanning // Create a context and configure it for deferred child scanning FhirContext ctx = FhirContext.forDstu2(); ctx.setPerformanceOptions(PerformanceOptionsEnum.DEFERRED_MODEL_SCANNING); // Now create a client and use it String serverBase = "http://fhirtest.uhn.ca/baseDstu2"; IGenericClient client = ctx.newRestfulGenericClient(serverBase); // END SNIPPET: deferModelScanning }
@Override public IGenericClient newClient(FhirContext theFhirContext, HttpServletRequest theRequest, String theServerBaseUrl) { // Create a client IGenericClient client = theFhirContext.newRestfulGenericClient(theServerBaseUrl); // Register an interceptor which adds credentials client.registerInterceptor(new BasicAuthInterceptor("someusername", "somepassword")); return client; }
public static void main(String[] args) { //START SNIPPET: createClient // Create a client FhirContext ctx = FhirContext.forDstu2(); // Create an instance of the JAX RS client factory and // set it on the context JaxRsRestfulClientFactory clientFactory = new JaxRsRestfulClientFactory(ctx); ctx.setRestfulClientFactory(clientFactory); // This client uses JAX-RS! IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu2"); //END SNIPPET: createClient }
@SuppressWarnings("unused") public void enableValidation() { // START SNIPPET: clientValidation FhirContext ctx = FhirContext.forDstu3(); ctx.setParserErrorHandler(new StrictErrorHandler()); // This client will have strict parser validation enabled IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu3"); // END SNIPPET: clientValidation }
@SuppressWarnings("unused") public void createOkHttp() { // START SNIPPET: okhttp FhirContext ctx = FhirContext.forDstu3(); // Use OkHttp ctx.setRestfulClientFactory(new OkHttpRestfulClientFactory(ctx)); // Create the client IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); // END SNIPPET: okhttp }
public static void performance() { // START SNIPPET: dontValidate // Create a context FhirContext ctx = FhirContext.forDstu2(); // Disable server validation (don't pull the server's metadata first) ctx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); // Now create a client and use it String serverBase = "http://fhirtest.uhn.ca/baseDstu2"; IGenericClient client = ctx.newRestfulGenericClient(serverBase); // END SNIPPET: dontValidate }
protected IGenericClient newClientWithBaseUrl(CommandLine theCommandLine, String theBaseUrl, String theBasicAuthOptionName, String theBearerTokenOptionName) throws ParseException { myFhirCtx.getRestfulClientFactory().setSocketTimeout(10 * 60 * 1000); IGenericClient retVal = myFhirCtx.newRestfulGenericClient(theBaseUrl); String basicAuthHeaderValue = getAndParseOptionBasicAuthHeader(theCommandLine, theBasicAuthOptionName); if (isNotBlank(basicAuthHeaderValue)) { retVal.registerInterceptor(new SimpleRequestHeaderInterceptor(Constants.HEADER_AUTHORIZATION, basicAuthHeaderValue)); } if (isNotBlank(theBearerTokenOptionName)) { String bearerToken = getAndParseBearerTokenAuthHeader(theCommandLine, theBearerTokenOptionName); if (isNotBlank(bearerToken)) { retVal.registerInterceptor(new SimpleRequestHeaderInterceptor(Constants.HEADER_AUTHORIZATION, Constants.HEADER_AUTHORIZATION_VALPREFIX_BEARER + bearerToken)); } } return retVal; }
@Override public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T> theClass, String theUri) { String resName = myCtx.getResourceDefinition(theClass).getName(); ourLog.info("Attempting to fetch {} at URL: {}", resName, theUri); myCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); IGenericClient client = myCtx.newRestfulGenericClient("http://example.com"); T result; try { result = client.read(theClass, theUri); } catch (BaseServerResponseException e) { throw new CommandFailureException("FAILURE: Received HTTP " + e.getStatusCode() + ": " + e.getMessage()); } ourLog.info("Successfully loaded resource"); return result; }
public static void main(String[] args) throws Exception { FhirContext ctx = FhirContext.forDstu2(); String serverBase = "http://fhirtest.uhn.ca/baseDstu2"; IGenericClient client = ctx.newRestfulGenericClient(serverBase); // use RelatedPerson because there aren't that many on the server Bundle bundle = client.search().forResource(RelatedPerson.class).returnBundle(Bundle.class).execute(); BundleFetcher.fetchRestOfBundle(client, bundle); if (bundle.getTotal() != bundle.getEntry().size()) { System.out.println("Counts didn't match! Expected " + bundle.getTotal() + " but bundle only had " + bundle.getEntry().size() + " entries!"); } // IParser parser = ctx.newXmlParser().setPrettyPrint(true); // System.out.println(parser.encodeResourceToString(bundle)); } }
@Override public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T> theClass, String theUri) { String resName = myCtx.getResourceDefinition(theClass).getName(); ourLog.info("Attempting to fetch {} at URL: {}", resName, theUri); myCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); IGenericClient client = myCtx.newRestfulGenericClient("http://example.com"); T result; try { result = client.read(theClass, theUri); } catch (BaseServerResponseException e) { throw new CommandFailureException("FAILURE: Received HTTP " + e.getStatusCode() + ": " + e.getMessage()); } ourLog.info("Successfully loaded resource"); return result; }
@SuppressWarnings("unused") public void createTimeouts() { // START SNIPPET: timeouts FhirContext ctx = FhirContext.forDstu2(); // Set how long to try and establish the initial TCP connection (in ms) ctx.getRestfulClientFactory().setConnectTimeout(20 * 1000); // Set how long to block for individual read/write operations (in ms) ctx.getRestfulClientFactory().setSocketTimeout(20 * 1000); // Create the client IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); // END SNIPPET: timeouts }
@Override public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T> theClass, String theUri) { String resName = myCtx.getResourceDefinition(theClass).getName(); ourLog.info("Attempting to fetch {} at URL: {}", resName, theUri); myCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); IGenericClient client = myCtx.newRestfulGenericClient("http://example.com"); T result; try { result = client.read(theClass, theUri); } catch (BaseServerResponseException e) { throw new CommandFailureException("FAILURE: Received HTTP " + e.getStatusCode() + ": " + e.getMessage()); } ourLog.info("Successfully loaded resource"); return result; }
@SuppressWarnings("unused") public void createProxy() { // START SNIPPET: proxy FhirContext ctx = FhirContext.forDstu2(); // Set connections to access the network via the HTTP proxy at // example.com : 8888 ctx.getRestfulClientFactory().setProxy("example.com", 8888); // If the proxy requires authentication, use the following as well ctx.getRestfulClientFactory().setProxyCredentials("theUsername", "thePassword"); // Create the client IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); // END SNIPPET: proxy }
public static void simpleExample() { // START SNIPPET: simple // We're connecting to a DSTU1 compliant server in this example FhirContext ctx = FhirContext.forDstu2(); String serverBase = "http://fhirtest.uhn.ca/baseDstu2"; IGenericClient client = ctx.newRestfulGenericClient(serverBase); // Perform a search Bundle results = client .search() .forResource(Patient.class) .where(Patient.FAMILY.matches().value("duck")) .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) .execute(); System.out.println("Found " + results.getEntry().size() + " patients named 'duck'"); // END SNIPPET: simple }
private static void tmp() { // Create a FHIR Context FhirContext ctx = FhirContext.forDstu2(); // Create a client IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu2"); // Read a patient with the given ID Patient patient = client .read() .resource(Patient.class) .withId("952975") .execute(); // Print the patient's name String string = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); System.out.println(string); }
@SuppressWarnings("unused") public void createSecurityBearer() { // START SNIPPET: securityBearer // Create a context and get the client factory so it can be configured FhirContext ctx = FhirContext.forDstu2(); IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory(); // In reality the token would have come from an authorization server String token = "3w03fj.r3r3t"; BearerTokenAuthInterceptor authInterceptor = new BearerTokenAuthInterceptor(token); // Register the interceptor with your client (either style) IPatientClient annotationClient = ctx.newRestfulClient(IPatientClient.class, "http://localhost:9999/fhir"); annotationClient.registerInterceptor(authInterceptor); IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); annotationClient.registerInterceptor(authInterceptor); // END SNIPPET: securityBearer }
@SuppressWarnings("unused") public void processMessage() { // START SNIPPET: processMessage FhirContext ctx = FhirContext.forDstu3(); // Create the client IGenericClient client = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); Bundle bundle = new Bundle(); // ..populate the bundle.. Bundle response = client .operation() .processMessage() // New operation for sending messages .setMessageBundle(bundle) .asynchronous(Bundle.class) .execute(); // END SNIPPET: processMessage }