public static <R extends JdcloudRequest> ByteArrayContent formatRequestBody(String contentType, R request, String method) throws JAXBException, IOException { ByteArrayContent bodyContent = null; Object body = content(request, method); if (body == null) { return bodyContent; } if (ContentType.TEXT_XML.toString().equals(contentType)) { bodyContent = new ByteArrayContent(contentType, beanToXmlByte(body)); } else { String content = GSON.toJson(body); bodyContent = ByteArrayContent.fromString(contentType, content); } InputStream inputStream = bodyContent.getInputStream(); ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return bodyContent; }
/** * Post-processes the request content to conform to the requirements of Google Cloud Storage. * * @param content the content produced by the {@link BatchJobUploadBodyProvider}. * @param isFirstRequest if this is the first request for the batch job. * @param isLastRequest if this is the last request for the batch job. */ private ByteArrayContent postProcessContent( ByteArrayContent content, boolean isFirstRequest, boolean isLastRequest) throws IOException { if (isFirstRequest && isLastRequest) { return content; } String serializedRequest = Streams.readAll(content.getInputStream(), UTF_8); serializedRequest = trimStartEndElements(serializedRequest, isFirstRequest, isLastRequest); // The request is part of a set of incremental uploads, so pad to the required content // length. This is not necessary if all operations for the job are being uploaded in a // single request. int numBytes = serializedRequest.getBytes(UTF_8).length; int remainder = numBytes % REQUIRED_CONTENT_LENGTH_INCREMENT; if (remainder > 0) { int pad = REQUIRED_CONTENT_LENGTH_INCREMENT - remainder; serializedRequest = Strings.padEnd(serializedRequest, numBytes + pad, ' '); } return new ByteArrayContent(content.getType(), serializedRequest.getBytes(UTF_8)); }
/** * Post-processes the request content to conform to the requirements of Google Cloud Storage. * * @param content the content produced by the {@link BatchJobUploadBodyProvider}. * @param isFirstRequest if this is the first request for the batch job. * @param isLastRequest if this is the last request for the batch job. */ private ByteArrayContent postProcessContent( ByteArrayContent content, boolean isFirstRequest, boolean isLastRequest) throws IOException { if (isFirstRequest && isLastRequest) { return content; } String serializedRequest = Streams.readAll(content.getInputStream(), UTF_8); serializedRequest = trimStartEndElements(serializedRequest, isFirstRequest, isLastRequest); // The request is part of a set of incremental uploads, so pad to the required content // length. This is not necessary if all operations for the job are being uploaded in a // single request. int numBytes = serializedRequest.getBytes(UTF_8).length; int remainder = numBytes % REQUIRED_CONTENT_LENGTH_INCREMENT; if (remainder > 0) { int pad = REQUIRED_CONTENT_LENGTH_INCREMENT - remainder; serializedRequest = Strings.padEnd(serializedRequest, numBytes + pad, ' '); } return new ByteArrayContent(content.getType(), serializedRequest.getBytes(UTF_8)); }
.header(ParameterConstant.X_JDCLOUD_NONCE, nonceId) .header(ParameterConstant.CONTENT_TYPE, request.getHeaders().getContentType()) .content(request.getContent() == null ? null : ((ByteArrayContent) request.getContent()).getInputStream());
new GenericUrl(effectiveStatus.getResumableUploadUri()), content); requestXml = Streams.readAll(content.getInputStream(), UTF_8); content.getInputStream().reset();
new GenericUrl(effectiveStatus.getResumableUploadUri()), content); requestXml = Streams.readAll(content.getInputStream(), UTF_8); content.getInputStream().reset();
@Test public void testValidOperations() throws BatchJobException, IOException, SAXException { RequestT request = createMutateRequest(); addBudgetOperation(request, -1L, "Test budget", 50000000L, "STANDARD"); addCampaignOperation( request, -2L, "Test campaign #1", "PAUSED", "SEARCH", -1L, "MANUAL_CPC", false); addCampaignOperation( request, -3L, "Test campaign #2", "PAUSED", "SEARCH", -1L, "MANUAL_CPC", false); addCampaignNegativeKeywordOperation(request, -2L, "venus", "BROAD"); addCampaignNegativeKeywordOperation(request, -3L, "venus", "BROAD"); ByteArrayContent httpContent = request.createBatchJobUploadBodyProvider().getHttpContent(request, true, true); String actualRequestXml = Streams.readAll(httpContent.getInputStream(), Charset.forName(UTF_8)); actualRequestXml = SoapRequestXmlProvider.normalizeXmlForComparison(actualRequestXml, getApiVersion()); String expectedRequestXml = SoapRequestXmlProvider.getTestBatchUploadRequest(getApiVersion()); // Perform an XML diff using the custom difference listener that properly handles namespaces // and attributes. Diff diff = new Diff(expectedRequestXml, actualRequestXml); DifferenceListener diffListener = new CustomDifferenceListener(); diff.overrideDifferenceListener(diffListener); XMLAssert.assertXMLEqual("Serialized upload request does not match expected XML", diff, true); } }