com.amazonaws.Request.getContent()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(171)

本文整理了Java中com.amazonaws.Request.getContent方法的一些代码示例,展示了Request.getContent的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.getContent方法的具体详情如下:
包路径:com.amazonaws.Request
类名称:Request
方法名:getContent

Request.getContent介绍

[英]Returns the optional stream containing the payload data to include for this request. Not all requests will contain payload data.
[中]返回可选流,该流包含此请求要包含的有效负载数据。并非所有请求都包含有效负载数据。

代码示例

代码示例来源:origin: aws/aws-sdk-java

/**
 * @return The request content input stream or an empty input stream if there is no content.
 */
private InputStream getContent(Request<?> request) {
  return (request.getContent() == null) ? new ByteArrayInputStream(new byte[0]) :
      request.getContent();
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * Used to perform a last reset on the content input stream (if mark-supported); this is so
 * that, for backward compatibility reason, any "blind" retry (ie without calling reset) by
 * user of this library with the same input stream (such as ByteArrayInputStream) could
 * still succeed.
 *
 * @param t the failure
 * @return the failure as given
 */
private <T extends Throwable> T lastReset(final T t) {
  try {
    InputStream content = request.getContent();
    if (content != null) {
      if (content.markSupported()) {
        content.reset();
      }
    }
  } catch (Exception ex) {
    log.debug("FYI: failed to reset content inputstream before throwing up", ex);
  }
  return t;
}

代码示例来源:origin: aws/aws-sdk-java

@Override
  public void beforeRequest(Request<?> request) {
    request.addHandlerContext(S3HandlerContextKeys.IS_PAYLOAD_SIGNING_ENABLED, true);
    if (request.getContent() == null && request.getHttpMethod() == HttpMethodName.POST) {
      request.setContent(new ByteArrayInputStream(new byte[0]));
    }
  }
}

代码示例来源:origin: aws/aws-sdk-java

@Override
  public void operationComplete(ChannelFuture future) throws Exception {
    try {
      Channel channel = future.channel();
      HttpRequest request = RequestAdapter.adapt(marshalledRequest);
      // Send request
      channel.writeAndFlush(request);
      // Send chunked content
      channel.writeAndFlush(new HttpChunkedInput(new ChunkedStream(marshalledRequest.getContent())));
    } catch (Exception e) {
      responseHandler.onFailure(e);
    }
  }
});

代码示例来源:origin: aws/aws-sdk-java

/**
 * Reset the input stream of the request before a retry.
 *
 * @param request Request containing input stream to reset
 * @param retriedException
 * @throws ResetException If Input Stream can't be reset which means the request can't be
 *                        retried
 */
private void resetRequestInputStream(final Request<?> request, SdkBaseException retriedException)
    throws ResetException {
  InputStream requestInputStream = request.getContent();
  if (requestInputStream != null) {
    if (requestInputStream.markSupported()) {
      try {
        requestInputStream.reset();
      } catch (IOException ex) {
        ResetException resetException = new ResetException(
            "The request to the service failed with a retryable reason, but resetting the request input " +
            "stream has failed. See exception.getExtraInfo or debug-level logging for the original failure " +
            "that caused this retry.",
            ex);
        resetException.setExtraInfo(retriedException.getMessage());
        throw resetException;
      }
    }
  }
}

代码示例来源:origin: aws/aws-sdk-java

if (request.getContent() == null && encodedParams != null) {
  entityEnclosingRequest.setEntity(ApacheUtils.newStringEntity(encodedParams));
} else {
if (request.getContent() != null) {
  HttpEntity entity = new RepeatableInputStreamRequestEntity(request);
  if (request.getHeaders().get(HttpHeaders.CONTENT_LENGTH) == null) {

代码示例来源:origin: aws/aws-sdk-java

@Override
public Request<OrigRequest> finishMarshalling() {
  // Content may already be set if the payload is binary data.
  if (request.getContent() == null) {
    // End the implicit request object if needed.
    if (!hasExplicitPayloadMember) {
      jsonGenerator.writeEndObject();
    }
    byte[] content = jsonGenerator.getBytes();
    request.setContent(new ByteArrayInputStream(content));
    if (content.length > 0) {
      request.addHeader("Content-Length", Integer.toString(content.length));
    }
  }
  if (!request.getHeaders().containsKey("Content-Type")) {
    request.addHeader("Content-Type", contentType);
  }
  return request;
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * Publishes the "request content length" event, and returns an input stream, which will be
 * made mark-and-resettable if possible, for progress tracking purposes.
 *
 * @return an input stream, which will be made mark-and-resettable if possible, for progress
 * tracking purposes; or null if the request doesn't have an input stream
 */
private InputStream beforeRequest() {
  ProgressListener listener = requestConfig.getProgressListener();
  reportContentLength(listener);
  if (request.getContent() == null) {
    return null;
  }
  final InputStream content = monitorStreamProgress(listener,
                           buffer(
                               makeResettable(
                                   request.getContent())));
  if (AmazonHttpClient.unreliableTestConfig == null) {
    return content;
  }
  return wrapWithUnreliableStream(content);
}

代码示例来源:origin: aws/aws-sdk-java

@Override
public HttpRequestBase create(final Request<?> request,
               final HttpClientSettings settings)
    throws
    FakeIOException {
  URI endpoint = request.getEndpoint();
  /*
   * HttpClient cannot handle url in pattern of "http://host//path", so we
   * have to escape the double-slash between endpoint and resource-path
   * into "/%2F"
   */
  String uri = SdkHttpUtils.appendUri(endpoint.toString(), request
      .getResourcePath(), true);
  String encodedParams = SdkHttpUtils.encodeParameters(request);
  /*
   * For all non-POST requests, and any POST requests that already have a
   * payload, we put the encoded params directly in the URI, otherwise,
   * we'll put them in the POST request's payload.
   */
  boolean requestHasNoPayload = request.getContent() != null;
  boolean requestIsPost = request.getHttpMethod() == HttpMethodName.POST;
  boolean putParamsInUri = !requestIsPost || requestHasNoPayload;
  if (encodedParams != null && putParamsInUri) {
    uri += "?" + encodedParams;
  }
  final HttpRequestBase base = createApacheRequest(request, uri, encodedParams);
  addHeadersToRequest(base, request);
  addRequestConfig(base, request, settings);
  return base;
}

代码示例来源:origin: aws/aws-sdk-java

final InputStream originalContent = request.getContent();
if (originalContent != null && originalContent.markSupported()
  && !(originalContent instanceof BufferedInputStream)) {

代码示例来源:origin: com.amazonaws/aws-java-sdk-core

/**
 * @return The request content input stream or an empty input stream if there is no content.
 */
private InputStream getContent(Request<?> request) {
  return (request.getContent() == null) ? new ByteArrayInputStream(new byte[0]) :
      request.getContent();
}

代码示例来源:origin: aws/aws-sdk-java

final InputStream origContent = request.getContent();
final InputStream toBeClosed = beforeRequest(); // for progress tracking

代码示例来源:origin: aws-amplify/aws-sdk-android

protected InputStream getBinaryRequestPayloadStreamWithoutQueryParams(Request<?> request) {
  try {
    final InputStream content = request.getContent();
    if (content == null) {
      return new ByteArrayInputStream(new byte[0]);
    }
    if (content instanceof StringInputStream) {
      return content;
    }
    if (!content.markSupported()) {
      throw new AmazonClientException("Unable to read request payload to sign request.");
    }
    return request.getContent();
  } catch (final Exception e) {
    throw new AmazonClientException("Unable to read request payload to sign request: "
        + e.getMessage(), e);
  }
}

代码示例来源:origin: aws-amplify/aws-sdk-android

@Override
  public void beforeRequest(com.amazonaws.Request<?> request) {
    // TODO: factor in clockskew
    startTime = System.currentTimeMillis();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream is = request.getContent();
    int currentByte = 0;
    try {
      while ((currentByte = is.read()) != -1) {
        baos.write(currentByte);
      }

      contentLength = baos.size();
      request.setContent(new ByteArrayInputStream(baos.toByteArray()));
    } catch (IOException e) {
      Log.e(TAG, "Cannot read content of request");
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: aws-amplify/aws-sdk-android

/**
   * Read the content of the request to get the length of the stream. This
   * method will wrap the stream by RepeatableInputStream if it is not
   * mark-supported.
   */
  static long getContentLength(Request<?> request) throws IOException {
    InputStream content = request.getContent();
    if (!content.markSupported()) {
      throw new AmazonClientException("Failed to get content length");
    }

    long contentLength = 0;
    byte[] tmp = new byte[DEFAULT_BYTE_LENGTH];
    int read;
    content.mark(-1);
    while ((read = content.read(tmp)) != -1) {
      contentLength += read;
    }
    content.reset();

    return contentLength;
  }
}

代码示例来源:origin: aws-amplify/aws-sdk-android

/**
 * @param request the request.
 * @return true if request is post and request has no payload.
 */
public static boolean usePayloadForQueryParameters(Request<?> request) {
  final boolean requestIsPOST = HttpMethodName.POST.equals(request.getHttpMethod());
  final boolean requestHasNoPayload = (request.getContent() == null);
  return requestIsPOST && requestHasNoPayload;
}

代码示例来源:origin: com.amazonaws/aws-java-sdk-core

@Override
public Request<OrigRequest> finishMarshalling() {
  // Content may already be set if the payload is binary data.
  if (request.getContent() == null) {
    // End the implicit request object if needed.
    if (!hasExplicitPayloadMember) {
      jsonGenerator.writeEndObject();
    }
    byte[] content = jsonGenerator.getBytes();
    request.setContent(new ByteArrayInputStream(content));
    if (content.length > 0) {
      request.addHeader("Content-Length", Integer.toString(content.length));
    }
  }
  if (!request.getHeaders().containsKey("Content-Type")) {
    request.addHeader("Content-Type", contentType);
  }
  return request;
}

代码示例来源:origin: aws-amplify/aws-sdk-android

@Test
public void test() throws Exception {
  PutEventsRequest putEventsRequest = new PutEventsRequest();
  List<Event> events = new ArrayList<Event>();
  events.add(createEvent());
  events.add(createEvent());
  putEventsRequest.setEvents(events);
  PutEventsRequestMarshaller marshaller = new PutEventsRequestMarshaller();
  Request<PutEventsRequest> request = marshaller.marshall(putEventsRequest);
  assertEquals("content encoding", "gzip", request.getHeaders().get("Content-Encoding"));
  byte[] content = IOUtils.toByteArray(request.getContent());
  System.out.println(content.length);
  assertEquals("content length", request.getHeaders().get("Content-Length"),
      String.valueOf(content.length));
  GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(content));
  String str = IOUtils.toString(gis);
  assertTrue("data is compressed", content.length < str.length());
}

代码示例来源:origin: aws-amplify/aws-sdk-android

@Test
public void test() throws Exception {
  PutEventsRequest putEventsRequest = new PutEventsRequest();
  List<Event> events = new ArrayList<Event>();
  events.add(createEvent());
  events.add(createEvent());
  putEventsRequest.setEvents(events);
  PutEventsRequestMarshaller marshaller = new PutEventsRequestMarshaller();
  Request<PutEventsRequest> request = marshaller.marshall(putEventsRequest);
  assertEquals("content encoding", "gzip", request.getHeaders().get("Content-Encoding"));
  byte[] content = IOUtils.toByteArray(request.getContent());
  System.out.println(content.length);
  assertEquals("content length", request.getHeaders().get("Content-Length"),
      String.valueOf(content.length));
  GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(content));
  String str = IOUtils.toString(gis);
  assertTrue("data is compressed", content.length < str.length());
}

代码示例来源:origin: aws-amplify/aws-sdk-android

/**
 * If necessary, creates a chunk-encoding wrapper on the request payload.
 */
@Override
protected void processRequestPayload(Request<?> request,
    HeaderSigningResult headerSigningResult) {
  if (useChunkEncoding(request)) {
    InputStream payloadStream = request.getContent();
    String dateTime = headerSigningResult.getDateTime();
    String keyPath = headerSigningResult.getScope();
    byte[] kSigning = headerSigningResult.getKSigning();
    String signature = BinaryUtils.toHex(headerSigningResult
        .getSignature());
    AwsChunkedEncodingInputStream chunkEncodededStream = new AwsChunkedEncodingInputStream(
        payloadStream, kSigning, dateTime, keyPath, signature, this);
    request.setContent(chunkEncodededStream);
  }
}

相关文章