/** * @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(); }
/** * 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; }
@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])); } } }
@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); } } });
/** * 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; } } } }
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) {
@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; }
/** * 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); }
@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; }
final InputStream originalContent = request.getContent(); if (originalContent != null && originalContent.markSupported() && !(originalContent instanceof BufferedInputStream)) {
/** * @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(); }
final InputStream origContent = request.getContent(); final InputStream toBeClosed = beforeRequest(); // for progress tracking
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); } }
@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); } } }
/** * 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; } }
/** * @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; }
@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; }
@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()); }
@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()); }
/** * 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); } }