org.jclouds.util.Strings2 Java Examples

The following examples show how to use org.jclouds.util.Strings2. 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: JenkinsErrorHandler.java    From jenkins-rest with Apache License 2.0 6 votes vote down vote up
private String parseMessage(final HttpCommand command, final HttpResponse response) {
    if (response.getPayload() != null) {
        try {
            return Strings2.toStringAndClose(response.getPayload().openStream());
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    } else {
        final String errorMessage = response.getFirstHeaderOrNull("X-Error");
        return new StringBuffer(command.getCurrentRequest().getRequestLine())
                .append(" -> ")
                .append(response.getStatusLine())
                .append(" -> ")
                .append(errorMessage != null ? errorMessage : "")
                .toString();
    }
}
 
Example #2
Source File: HookSettingsParser.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
@Override
public HookSettings apply(final HttpResponse input) {
    final int statusCode = input.getStatusCode();
    try {
        final String payload;
        switch (statusCode) {
            case 200: // means we have actual settings
                payload = Strings2.toStringAndClose(input.getPayload().openStream());
                break;
            case 204: // means we have no settings
                payload = null;
                break;
            default:
                throw new RuntimeException(input.getStatusLine());
        }
        final JsonElement settings = BitbucketUtils.nullToJsonElement(payload);
        return HookSettings.of(settings);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
 
Example #3
Source File: RawContentParser.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
@Override
public RawContent apply(final HttpResponse input) {
    try (final InputStream inputStream = input.getPayload().openStream()) {
        final String value = Strings2.toStringAndClose(inputStream);
        return RawContent.create(value, null);
    } catch (final Exception e) {
        throw Throwables.propagate(e);
    }
}
 
Example #4
Source File: BitbucketErrorHandler.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
private String parseMessage(final HttpCommand command, final HttpResponse response) {
    if (response.getPayload() != null) {
        try {
            return Strings2.toStringAndClose(response.getPayload().openStream());
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    } else {
        return new StringBuffer(command.getCurrentRequest().getRequestLine())
                .append(" -> ")
                .append(response.getStatusLine())
                .toString();
    }
}
 
Example #5
Source File: TestUtilities.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
/**
 * Execute `args` at the `workingDir`.
 *
 * @param args list of arguments to pass to Process.
 * @param workingDir directory to execute Process within.
 * @return possible output of Process.
 * @throws Exception if Process could not be successfully executed.
 */
public static String executionToString(final List<String> args, final Path workingDir) throws Exception {
    assertThat(args).isNotNull().isNotEmpty();
    assertThat(workingDir).isNotNull();
    assertThat(workingDir.toFile().isDirectory()).isTrue();

    final Process process = new ProcessBuilder(args)
            .redirectErrorStream(true)
            .directory(workingDir.toFile())
            .start();

    return Strings2.toStringAndClose(process.getInputStream());
}
 
Example #6
Source File: JcloudsStoreObjectAccessor.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public String get() {
    try {
        if (!blobStore.blobExists(containerName, blobName)) return null;
        Blob blob = blobStore.getBlob(containerName, blobName);
        if (blob==null) return null;
        return Strings2.toStringAndClose(blob.getPayload().openStream());
    } catch (IOException e) {
        Exceptions.propagateIfFatal(e);
        throw new IllegalStateException("Error reading blobstore "+containerName+" "+blobName+": "+e, e);
    }
}
 
Example #7
Source File: MainApp.java    From jclouds-examples with Apache License 2.0 5 votes vote down vote up
private static void providerExample(BlobStoreContext context) throws IOException {
   // Get the provider API
   GlacierClient client = context.unwrapApi(GlacierClient.class);

   // Create a vault
   final String vaultName =  "jclouds_providerExample_" + UUID.randomUUID().toString();
   client.createVault(vaultName);

   // Upload an archive
   Payload payload = new ByteSourcePayload(buildData(16));
   payload.getContentMetadata().setContentType(PLAIN_TEXT_UTF_8.toString());
   payload.getContentMetadata().setContentLength(16L);
   String archiveId = client.uploadArchive(vaultName, payload);

   // Create an archive retrieval job request
   JobRequest archiveRetrievalJobRequest = ArchiveRetrievalJobRequest.builder()
         .archiveId(archiveId)
         .description("retrieval job")
         .build();

   // Initiate job
   String jobId = client.initiateJob(vaultName, archiveRetrievalJobRequest);
   try {
      // Poll until the job is done
      new BasePollingStrategy(client).waitForSuccess(vaultName, jobId);

      // Get the job output
      Payload result = client.getJobOutput(vaultName, jobId);

      // Print the result
      System.out.println("The retrieved payload is: " + Strings2.toStringAndClose(result.openStream()));
   } catch (InterruptedException e) {
      Throwables.propagate(e);
   }
}
 
Example #8
Source File: CrumbParser.java    From jenkins-rest with Apache License 2.0 4 votes vote down vote up
private static String crumbValue(HttpResponse input) throws IOException {
    return Strings2.toStringAndClose(input.getPayload().openStream())
            .split(":")[1];
}