/** * Writes the contents of the response to the specified File. * * @param outputFile the output file to write to * @throws FileNotFoundException if unable to write to {@code outputFile} * @throws IOException if unable to read the response contents */ public void saveToFile(String outputFile) throws FileNotFoundException, IOException { Streams.copy(getInputStream(), new BufferedOutputStream(new FileOutputStream(outputFile))); }
/** * Writes the contents of the response to the specified File. * * @param outputFile the output file to write to * @throws FileNotFoundException if unable to write to {@code outputFile} * @throws IOException if unable to read the response contents */ public void saveToFile(String outputFile) throws FileNotFoundException, IOException { Streams.copy(getInputStream(), new BufferedOutputStream(new FileOutputStream(outputFile))); }
/** * Returns the contents of the response as a String. Inflates the response if it is in one of the * gzip formats. * * @throws IOException if unable to read the response contents */ public String getAsString() throws IOException { InputStream inputStream = isGzipped() ? new GZIPInputStream(getInputStream()) : getInputStream(); return Streams.readAll(inputStream, rawResponse.getCharset()); }
/** * Returns the contents of the response as a String. Inflates the response if it is in one of the * gzip formats. * * @throws IOException if unable to read the response contents */ public String getAsString() throws IOException { InputStream inputStream = isGzipped() ? new GZIPInputStream(getInputStream()) : getInputStream(); return Streams.readAll(inputStream, rawResponse.getCharset()); }
/** * Downloads report from API (with retry logic) and returns a {@link File} object. */ @Override public File call() throws ReportProcessingException { ReportDownloadResponse reportDownloadResponse = getReportDownloadResponse(); InputStream inputStream = reportDownloadResponse.getInputStream(); return handleReportStreamResult(inputStream); }
/** * Downloads the file from the API into an InputStream. * * @return the InputStream from the online report, null if httpStatus is not {@code HTTP_OK}. */ private InputStream getReportInputStream() { ReportDownloaderInterface reportDownloader = AdWordsServicesUtil.getUtility(session, ReportDownloaderInterface.class); InputStream result = null; try { ReportDownloadResponse reportDownloadResponse = reportDownloader.downloadReport(reportDefinition); result = reportDownloadResponse.getInputStream(); } catch (ReportException | ReportDownloadResponseException e) { logger.error( "Failed to download report stream for {} with account {}.", reportDefinition.getReportType(), session.getClientCustomerId(), e); } return result; }
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#getInputStream()}. */ @Test public void testGetInputStream() throws IOException { String expectedContents = "a,b,c\nd,e,f\n"; InputStream inputStream = new ByteArrayInputStream(expectedContents.getBytes(UTF_8)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, inputStream, UTF_8, "CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); String actualContents = Streams.readAll(response.getInputStream(), UTF_8); assertEquals("input stream contents are incorrect", expectedContents, actualContents); }
@Test public void testHandleSuccessfulResponse() throws Exception { String responseBody = "Successful,report,response"; RawReportDownloadResponse rawResponse = new RawReportDownloadResponse( 200, ByteSource.wrap(responseBody.getBytes(REPORT_CHARSET)).openStream(), REPORT_CHARSET, "CSV"); ReportDownloadResponse reportResponse = helper.handleResponse(rawResponse, exceptionBuilder); String actualResponseBody = new String(ByteStreams.toByteArray(reportResponse.getInputStream()), REPORT_CHARSET); assertEquals("Response body not expected value", responseBody, actualResponseBody); }
@Test public void testSuccess() throws Exception { ByteArrayInputStream stream = new ByteArrayInputStream("Report data".getBytes(AdHocReportDownloadHelper.REPORT_CHARSET)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(200, stream, AdHocReportDownloadHelper.REPORT_CHARSET, DownloadFormat.CSV.name()); ReportDownloadResponse response = downloadReport(DownloadFormat.CSV, rawResponse, null); assertEquals(200, response.getHttpStatus()); assertEquals(stream, response.getInputStream()); assertEquals("SUCCESS", response.getHttpResponseMessage()); }
@Test public void testSuccess() throws Exception { ByteArrayInputStream stream = new ByteArrayInputStream("Report data".getBytes(AdHocReportDownloadHelper.REPORT_CHARSET)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(200, stream, AdHocReportDownloadHelper.REPORT_CHARSET, DownloadFormat.CSV.name()); ReportDownloadResponse response = downloadReport(DownloadFormat.CSV, rawResponse, null); assertEquals(200, response.getHttpStatus()); assertEquals(stream, response.getInputStream()); assertEquals("SUCCESS", response.getHttpResponseMessage()); }
@Test public void testSuccess() throws Exception { ByteArrayInputStream stream = new ByteArrayInputStream("Report data".getBytes(AdHocReportDownloadHelper.REPORT_CHARSET)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(200, stream, AdHocReportDownloadHelper.REPORT_CHARSET, DownloadFormat.CSV.name()); ReportDownloadResponse response = downloadReport(DownloadFormat.CSV, rawResponse, null); assertEquals(200, response.getHttpStatus()); assertEquals(stream, response.getInputStream()); assertEquals("SUCCESS", response.getHttpResponseMessage()); }
"Response contents incorrect", "test", Streams.readAll(response.getInputStream(), REPORT_CHARSET));
"Response contents incorrect", "test", Streams.readAll(response.getInputStream(), REPORT_CHARSET));