Skip to content

Chc conditions #1612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package snippets.healthcare.dicom;

// [START healthcare_dicomweb_search_studies]
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.healthcare.v1beta1.CloudHealthcare;
import com.google.api.services.healthcare.v1beta1.CloudHealthcareScopes;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collections;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;

public class DicomWebSearchStudies {
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final NetHttpTransport HTTP_TRANSPORT = new NetHttpTransport();

public static void dicomWebSearchStudies(String dicomStoreName)
throws IOException, URISyntaxException {
// String dicomStoreName =
// String.format(
// DICOM_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-dicom-id");

// Initialize the client, which will be used to interact with the service.
CloudHealthcare client = createClient();

HttpClient httpClient = HttpClients.createDefault();
String uri =
String.format("%sv1beta1/%s/dicomWeb/studies", client.getRootUrl(), dicomStoreName);
URIBuilder uriBuilder =
new URIBuilder(uri)
.setParameter("access_token", getAccessToken())
// Refine your search by appending DICOM tags to the
// request in the form of query parameters. This sample
// searches for studies containing a patient's name.
.setParameter("PatientName", "Sally Zhang");

HttpUriRequest request =
RequestBuilder.get()
.setUri(uriBuilder.build())
.addHeader("Content-Type", "application/json")
.addHeader("Accept-Charset", "utf-8")
.build();

// Execute the request and process the results.
// Execute the request and process the results.
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.err.print(
String.format(
"Exception searching for studies using tags: %s\n",
response.getStatusLine().toString()));
responseEntity.writeTo(System.err);
throw new RuntimeException();
}
System.out.println("Studies found: \n" + response.toString());
responseEntity.writeTo(System.out);
}

private static CloudHealthcare createClient() throws IOException {
// Use Application Default Credentials (ADC) to authenticate the requests
// For more information see https://cloud.google.com/docs/authentication/production
GoogleCredential credential =
GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY)
.createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM));

// Create a HttpRequestInitializer, which will provide a baseline configuration to all requests.
HttpRequestInitializer requestInitializer =
request -> {
credential.initialize(request);
request.setConnectTimeout(60000); // 1 minute connect timeout
request.setReadTimeout(60000); // 1 minute read timeout
};

// Build the client for interacting with the service.
return new CloudHealthcare.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer)
.setApplicationName("your-application-name")
.build();
}

private static String getAccessToken() throws IOException {
GoogleCredential credential =
GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY)
.createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM));
credential.refreshToken();
return credential.getAccessToken();
}
}
// [END healthcare_dicomweb_search_studies]
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.util.UUID;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
Expand All @@ -43,6 +42,7 @@
import snippets.healthcare.dicom.DicomWebRetrieveRendered;
import snippets.healthcare.dicom.DicomWebRetrieveStudy;
import snippets.healthcare.dicom.DicomWebSearchForInstances;
import snippets.healthcare.dicom.DicomWebSearchStudies;
import snippets.healthcare.dicom.DicomWebStoreInstance;

@RunWith(JUnit4.class)
Expand All @@ -57,8 +57,8 @@ public class DicomWebTests {
private static String studyId = "2.25.330012077234033941963257891139480825153";
private static String seriesId = "2.25.143186483950719304925806365081717734297";
private static String instanceId = "2.25.195151962645072062560826889007364152748";
private static String dicomWebInstancePath = String.format(
"studies/%s/series/%s/instances/%s", studyId, seriesId, instanceId);
private static String dicomWebInstancePath =
String.format("studies/%s/series/%s/instances/%s", studyId, seriesId, instanceId);
private static String dicomWebRenderedPath = dicomWebInstancePath + "/rendered";

private static String instanceOutput = "instance.dcm";
Expand All @@ -85,11 +85,8 @@ public static void checkRequirements() {
@BeforeClass
public static void setUp() throws IOException {
String datasetId = "dataset-" + UUID.randomUUID().toString().replaceAll("-", "_");
datasetName = String.format(
"projects/%s/locations/%s/datasets/%s",
PROJECT_ID,
REGION_ID,
datasetId);
datasetName =
String.format("projects/%s/locations/%s/datasets/%s", PROJECT_ID, REGION_ID, datasetId);
DatasetCreate.datasetCreate(PROJECT_ID, REGION_ID, datasetId);

String dicomStoreId = "dicom-" + UUID.randomUUID().toString().replaceAll("-", "_");
Expand All @@ -109,8 +106,7 @@ public void beforeTest() throws IOException, URISyntaxException {
System.setOut(new PrintStream(bout));

// Store before each test so it is always available.
DicomWebStoreInstance.dicomWebStoreInstance(
dicomStoreName, "src/test/resources/jpeg_text.dcm");
DicomWebStoreInstance.dicomWebStoreInstance(dicomStoreName, "src/test/resources/jpeg_text.dcm");

bout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bout));
Expand All @@ -124,8 +120,7 @@ public void tearDown() {

@Test
public void test_DicomWebStoreInstance() throws Exception {
DicomWebStoreInstance.dicomWebStoreInstance(
dicomStoreName, "src/test/resources/jpeg_text.dcm");
DicomWebStoreInstance.dicomWebStoreInstance(dicomStoreName, "src/test/resources/jpeg_text.dcm");

String output = bout.toString();
assertThat(output, containsString("DICOM instance stored:"));
Expand All @@ -138,6 +133,13 @@ public void test_DicomWebSearchInstances() throws Exception {
assertThat(output, containsString("Dicom store instances found:"));
}

@Test
public void test_DicomWebSearchStudies() throws Exception {
DicomWebSearchStudies.dicomWebSearchStudies(dicomStoreName);
String output = bout.toString();
assertThat(output, containsString("Studies found:"));
}

@Test
public void test_DicomWebRetrieveStudy() throws Exception {
DicomWebRetrieveStudy.dicomWebRetrieveStudy(dicomStoreName, studyId);
Expand Down Expand Up @@ -179,4 +181,3 @@ public void test_DicomWebDeleteStudy() throws IOException {
assertThat(output, containsString("DICOM study deleted."));
}
}