/** * Get the restfull client factory */ public IRestfulClientFactory getRestfulClientFactory() { return myContext.getRestfulClientFactory(); }
/** * Instantiates a new generic client. A generic client is able to perform any of the FHIR RESTful operations against * a compliant server, but does not have methods defining the specific functionality required (as is the case with * {@link #newRestfulClient(Class, String) non-generic clients}). * * <p> * Performance Note: <b>This method is cheap</b> to call, and may be called once for every operation invocation * without incurring any performance penalty * </p> * * @param theServerBase The URL of the base for the restful FHIR server to connect to */ public IGenericClient newRestfulGenericClient(String theServerBase) { return getRestfulClientFactory().newGenericClient(theServerBase); }
/** * Instantiates a new client instance. This method requires an interface which is defined specifically for your use * cases to contain methods for each of the RESTful operations you wish to implement (e.g. "read ImagingStudy", * "search Patient by identifier", etc.). This interface must extend {@link IRestfulClient} (or commonly its * sub-interface {@link IBasicClient}). See the <a * href="http://jamesagnew.github.io/hapi-fhir/doc_rest_client.html">RESTful Client</a> documentation for more * information on how to define this interface. * * <p> * Performance Note: <b>This method is cheap</b> to call, and may be called once for every operation invocation * without incurring any performance penalty * </p> * * @param theClientType The client type, which is an interface type to be instantiated * @param theServerBase The URL of the base for the restful FHIR server to connect to * @return A newly created client * @throws ConfigurationException If the interface type is not an interface */ public <T extends IRestfulClient> T newRestfulClient(Class<T> theClientType, String theServerBase) { return getRestfulClientFactory().newClient(theClientType, theServerBase); }
@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 }
@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 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 }
public static void main(String[] args) { /* * This is out ot date - Just keeping * it in case it's helpful... */ final String authUser = "username"; final String authPassword = "password"; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("10.10.10.10", 8080), new UsernamePasswordCredentials(authUser, authPassword)); HttpHost myProxy = new HttpHost("10.10.10.10", 8080); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder .setProxy(myProxy) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) .setDefaultCredentialsProvider(credsProvider) .disableCookieManagement(); CloseableHttpClient httpClient = clientBuilder.build(); FhirContext ctx = FhirContext.forDstu2(); String serverBase = "http://spark.furore.com/fhir/"; ctx.getRestfulClientFactory().setHttpClient(httpClient); IGenericClient client = ctx.newRestfulGenericClient(serverBase); IdDt id = new IdDt("Patient", "123"); client.read(Patient.class, id); }
@SuppressWarnings("unused") public void gzip() { // START SNIPPET: gzip // Create a context and get the client factory so it can be configured FhirContext ctx = FhirContext.forDstu2(); IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory(); // Register the interceptor with your client (either style) IPatientClient annotationClient = ctx.newRestfulClient(IPatientClient.class, "http://localhost:9999/fhir"); annotationClient.registerInterceptor(new GZipContentInterceptor()); // END SNIPPET: gzip }
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; }
IHttpClient client = myFhirContext.getRestfulClientFactory().getHttpClient(url, params, "", RequestTypeEnum.POST, headers); IHttpRequest request = client.createParamRequest(myFhirContext, params, null); try {
@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; }
@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; }
@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; }
myFhirContext.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); IGenericClient client = null; if (isNotBlank(endpointUrl)) {
@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 createCookie() { // START SNIPPET: cookie // Create a context and get the client factory so it can be configured FhirContext ctx = FhirContext.forDstu2(); 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 createSecurity() { // START SNIPPET: security // Create a context and get the client factory so it can be configured FhirContext ctx = FhirContext.forDstu2(); IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory(); // Create an HTTP basic auth interceptor String username = "foobar"; String password = "boobear"; IClientInterceptor authInterceptor = new BasicAuthInterceptor(username, password); // If you're usinf an annotation client, use this style to // register it IPatientClient annotationClient = ctx.newRestfulClient(IPatientClient.class, "http://localhost:9999/fhir"); annotationClient.registerInterceptor(authInterceptor); // If you're using a generic client, use this instead IGenericClient genericClient = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); genericClient.registerInterceptor(authInterceptor); // END SNIPPET: security }
/** * Get the restfull client factory */ public IRestfulClientFactory getRestfulClientFactory() { return myContext.getRestfulClientFactory(); }
IRestfulClientFactory clientFactory = ctx.getRestfulClientFactory();
/** * Instantiates a new generic client. A generic client is able to perform any of the FHIR RESTful operations against * a compliant server, but does not have methods defining the specific functionality required (as is the case with * {@link #newRestfulClient(Class, String) non-generic clients}). * * <p> * Performance Note: <b>This method is cheap</b> to call, and may be called once for every operation invocation * without incurring any performance penalty * </p> * * @param theServerBase The URL of the base for the restful FHIR server to connect to */ public IGenericClient newRestfulGenericClient(String theServerBase) { return getRestfulClientFactory().newGenericClient(theServerBase); }