Java Code Examples for com.google.api.client.http.HttpResponse#download()

The following examples show how to use com.google.api.client.http.HttpResponse#download() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DriveSyncService.java    From narrate-android with Apache License 2.0 4 votes vote down vote up
@Override
public Entry download(String uuid) {
    LogUtil.log(getClass().getSimpleName(), "download()");

    File file = getEntry(uuid);

    if (file != null && file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
        try {
            HttpResponse resp =
                    service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
                            .execute();

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            resp.download(os);
            LogUtil.log(getClass().getSimpleName(), "Download: " + os.toString("UTF-8"));

            // process input stream
            Entry entry = EntryHelper.fromJson(os.toString("UTF-8"));

            // close stream to prevent a memory leak
            os.close();

            if (entry != null) {
                LogUtil.log(getClass().getSimpleName(), "entry != null");
                SyncInfo info = new SyncInfo();
                info.setTitle(entry.uuid);
                info.setSyncService(SyncService.GoogleDrive.ordinal());
                info.setModifiedDate(Calendar.getInstance(Locale.getDefault()).getTimeInMillis());
                info.setSyncStatus(SyncStatus.OK);
                info.setRevision(String.valueOf(file.getVersion()));
                SyncInfoDao.saveData(info);
            } else
                LogUtil.log(getClass().getSimpleName(), "entry == null");

            return entry;

        } catch (Exception e) {
            LogUtil.log(getClass().getSimpleName(), "Exception in download() - " + e.getMessage());
            if (!BuildConfig.DEBUG) Crashlytics.logException(e);
        }
    } else {
        LogUtil.log(getClass().getSimpleName(), "file == null: " + (file == null));
        if (!BuildConfig.DEBUG) Crashlytics.log("file == null in DriveSyncService#download(Entry): " + (file == null));
    }

    return null;
}
 
Example 2
Source File: DicomWebRetrieveInstance.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void dicomWebRetrieveInstance(String dicomStoreName, String dicomWebPath)
    throws IOException {
  // String dicomStoreName =
  //    String.format(
  //        DICOM_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-dicom-id");
  // String dicomWebPath = String.format(DICOMWEB_PATH, "your-study-id", "your-series-id",
  // "your-instance-id");

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

  // Create request and configure any parameters.
  Instances.RetrieveInstance request =
      client
          .projects()
          .locations()
          .datasets()
          .dicomStores()
          .studies()
          .series()
          .instances()
          .retrieveInstance(dicomStoreName, dicomWebPath);

  // Execute the request and process the results.
  HttpResponse response = request.executeUnparsed();

  String outputPath = "instance.dcm";
  OutputStream outputStream = new FileOutputStream(new File(outputPath));
  try {
    response.download(outputStream);
    System.out.println("DICOM instance written to file " + outputPath);
  } finally {
    outputStream.close();
  }

  if (!response.isSuccessStatusCode()) {
    System.err.print(
        String.format("Exception retrieving DICOM instance: %s\n", response.getStatusMessage()));
    throw new RuntimeException();
  }
}
 
Example 3
Source File: DicomWebRetrieveStudy.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void dicomWebRetrieveStudy(String dicomStoreName, String studyId)
    throws IOException {
  // String dicomStoreName =
  //    String.format(
  //        DICOM_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-dicom-id");
  // String studyId = "your-study-id";

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

  // Create request and configure any parameters.
  Studies.RetrieveStudy request =
      client
          .projects()
          .locations()
          .datasets()
          .dicomStores()
          .studies()
          .retrieveStudy(dicomStoreName, "studies/" + studyId);

  // Execute the request and process the results.
  HttpResponse response = request.executeUnparsed();

  // When specifying the output file, use an extension like ".multipart".
  // Then, parse the downloaded multipart file to get each individual
  // DICOM file.
  String outputPath = "study.multipart";
  OutputStream outputStream = new FileOutputStream(new File(outputPath));
  try {
    response.download(outputStream);
    System.out.println("DICOM study written to file " + outputPath);
  } finally {
    outputStream.close();
  }

  if (!response.isSuccessStatusCode()) {
    System.err.print(
        String.format("Exception retrieving DICOM study: %s\n", response.getStatusMessage()));
    throw new RuntimeException();
  }
}
 
Example 4
Source File: DicomWebRetrieveRendered.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void dicomWebRetrieveRendered(String dicomStoreName, String dicomWebPath)
    throws IOException {
  // String dicomStoreName =
  //    String.format(
  //        DICOM_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-dicom-id");
  // String dicomWebPath = String.format(DICOMWEB_PATH, "your-study-id", "your-series-id",
  // "your-instance-id");

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

  // Create request and configure any parameters.
  Instances.RetrieveRendered request =
      client
          .projects()
          .locations()
          .datasets()
          .dicomStores()
          .studies()
          .series()
          .instances()
          .retrieveRendered(dicomStoreName, dicomWebPath);

  // Execute the request and process the results.
  HttpResponse response = request.executeUnparsed();

  String outputPath = "image.png";
  OutputStream outputStream = new FileOutputStream(new File(outputPath));
  try {
    response.download(outputStream);
    System.out.println("DICOM rendered PNG image written to file " + outputPath);
  } finally {
    outputStream.close();
  }

  if (!response.isSuccessStatusCode()) {
    System.err.print(
        String.format(
            "Exception retrieving DICOM rendered image: %s\n", response.getStatusMessage()));
    throw new RuntimeException();
  }
}