org.jclouds.http.HttpResponse Java Examples

The following examples show how to use org.jclouds.http.HttpResponse. 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: GenerateTempURL.java    From jclouds-examples with Apache License 2.0 6 votes vote down vote up
private void generateDeleteTempURL() throws IOException {
   System.out.format("Generate DELETE Temp URL%n");

   HttpRequest request = blobStoreContext.getSigner(REGION).signRemoveBlob(CONTAINER, FILENAME);

   System.out.format("  %s %s%n", request.getMethod(), request.getEndpoint());

   // DELETE the file using jclouds
   HttpResponse response = blobStoreContext.utils().http().invoke(request);
   int statusCode = response.getStatusCode();

   if (statusCode >= 200 && statusCode < 299) {
      System.out.format("  DELETE Success (%s)%n", statusCode);
   }
   else {
      throw new HttpResponseException(null, response);
   }
}
 
Example #2
Source File: GenerateTempURL.java    From jclouds-examples with Apache License 2.0 6 votes vote down vote up
private void generatePutTempURL() throws IOException {
   System.out.format("Generate PUT Temp URL%n");

   // Create the Payload
   String data = "This object will be public for 10 minutes.";
   ByteSource source = ByteSource.wrap(data.getBytes());
   Payload payload = Payloads.newByteSourcePayload(source);

   // Create the Blob
   Blob blob = blobStore.blobBuilder(FILENAME).payload(payload).contentType("text/plain").build();
   HttpRequest request = blobStoreContext.getSigner(REGION).signPutBlob(CONTAINER, blob, TEN_MINUTES);

   System.out.format("  %s %s%n", request.getMethod(), request.getEndpoint());

   // PUT the file using jclouds
   HttpResponse response = blobStoreContext.utils().http().invoke(request);
   int statusCode = response.getStatusCode();

   if (statusCode >= 200 && statusCode < 299) {
      System.out.format("  PUT Success (%s)%n", statusCode);
   }
   else {
      throw new HttpResponseException(null, response);
   }
}
 
Example #3
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 #4
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 #5
Source File: OutputToProgressiveText.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
public ProgressiveText apply(HttpResponse response) {

      String text = getTextOutput(response);
      int size = getTextSize(response);
      boolean hasMoreData = getMoreData(response);
      return ProgressiveText.create(text, size, hasMoreData);
   }
 
Example #6
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 #7
Source File: DeleteRepositoryParser.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
@Override
public RequestStatus apply(final HttpResponse input) {
    final int statusCode = input.getStatusCode();
    if (statusCode >= 200 && statusCode < 400) {
        if (statusCode == 204) {
            throw new ResourceNotFoundException("The repository does not exist in this project.");
        } else {
            return RequestStatus.create(true, null);
        }
    } else {
        throw new RuntimeException(input.getStatusLine());
    }
}
 
Example #8
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 #9
Source File: RequestStatusParser.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
@Override
public RequestStatus apply(final HttpResponse input) {
    final int statusCode = input.getStatusCode();
    if (statusCode >= 200 && statusCode < 400) {
        return RequestStatus.create(true, null);
    } else {
        throw new RuntimeException(input.getStatusLine());
    }
}
 
Example #10
Source File: CrumbParser.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
@Override
public Crumb apply(final HttpResponse input) {
    final int statusCode = input.getStatusCode();
    if (statusCode >= 200 && statusCode < 400) {
        try {
            return Crumb.create(crumbValue(input), sessionIdCookie(input));
        } catch (final IOException e) {
            throw new RuntimeException(input.getStatusLine(), e);
        }
    } else {
        throw new RuntimeException(input.getStatusLine());
    }
}
 
Example #11
Source File: SystemInfoFromJenkinsHeaders.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
@Override
public SystemInfo apply(HttpResponse response) {
    final int statusCode = response.getStatusCode();
    if (statusCode >= 200 && statusCode < 400) {
        return SystemInfo.create(response.getFirstHeaderOrNull("X-Hudson"), response.getFirstHeaderOrNull("X-Jenkins"),
            response.getFirstHeaderOrNull("X-Jenkins-Session"),
            response.getFirstHeaderOrNull("X-Instance-Identity"), response.getFirstHeaderOrNull("X-SSH-Endpoint"),
            response.getFirstHeaderOrNull("Server"), null);
    } else {
        throw new RuntimeException(response.getStatusLine());
    }
}
 
Example #12
Source File: LocationToQueueId.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
public IntegerResponse apply(HttpResponse response) {

      String url = response.getFirstHeaderOrNull("Location");
      if (url != null) {
         Matcher matcher = pattern.matcher(url);
         if (matcher.find() && matcher.groupCount() == 1) {
            return IntegerResponse.create(Integer.valueOf(matcher.group(1)), null);
         }
      }
      final Error error = Error.create(null,
         "No queue item Location header could be found despite getting a valid HTTP response.", 
         NumberFormatException.class.getCanonicalName());
      return IntegerResponse.create(null, Lists.newArrayList(error));
   }
 
Example #13
Source File: RequestStatusParser.java    From jenkins-rest with Apache License 2.0 5 votes vote down vote up
@Override
public RequestStatus apply(final HttpResponse input) {
    final int statusCode = input.getStatusCode();
    if (statusCode >= 200 && statusCode < 400) {
        return RequestStatus.create(true, null);
    } else {
        throw new RuntimeException(input.getStatusLine());
    }
}
 
Example #14
Source File: CrumbParser.java    From jenkins-rest with Apache License 2.0 4 votes vote down vote up
private static String sessionIdCookie(HttpResponse input) {
    return input.getHeaders().get(HttpHeaders.SET_COOKIE).stream()
        .filter(c -> c.startsWith(JENKINS_COOKIES_JSESSIONID))
        .findFirst()
        .orElse("");
}
 
Example #15
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];
}
 
Example #16
Source File: BuildNumberToInteger.java    From jenkins-rest with Apache License 2.0 4 votes vote down vote up
public Integer apply(HttpResponse response) {
   return Integer.valueOf(getTextOutput(response));
}
 
Example #17
Source File: OutputToProgressiveText.java    From jenkins-rest with Apache License 2.0 4 votes vote down vote up
public boolean getMoreData(HttpResponse response) {
   String moreData = response.getFirstHeaderOrNull("X-More-Data");
   return moreData != null ? Boolean.valueOf(moreData) : false;
}
 
Example #18
Source File: OutputToProgressiveText.java    From jenkins-rest with Apache License 2.0 4 votes vote down vote up
public int getTextSize(HttpResponse response) {
   String textSize = response.getFirstHeaderOrNull("X-Text-Size");
   return textSize != null ? Integer.valueOf(textSize) : -1;
}