All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.bingads.internal.utilities.HttpClientHttpFileService Maven / Gradle / Ivy

Go to download

The Bing Ads Java SDK is a library improving developer experience when working with the Bing Ads services by providing high-level access to features such as Bulk API, OAuth Authorization and SOAP API.

There is a newer version: 13.0.22.1
Show newest version
package com.microsoft.bingads.internal.utilities;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.Future;

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.microsoft.bingads.AsyncCallback;
import com.microsoft.bingads.CouldNotDownloadResultFileException;
import com.microsoft.bingads.CouldNotUploadFileException;
import com.microsoft.bingads.internal.ResultFuture;
import com.microsoft.bingads.internal.functionalinterfaces.Consumer;

public class HttpClientHttpFileService implements HttpFileService {
	
    @Override
    public void downloadFile(String url, File tempZipFile, boolean overwrite, int timeoutInMilliseconds) throws IOException, URISyntaxException {
        if (!overwrite && tempZipFile.exists()) {
            throw new IOException(String.format("Could not download result file due to file %s already exists", tempZipFile));
        }

        CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom()
                .setConnectionRequestTimeout(timeoutInMilliseconds)
                .setConnectTimeout(timeoutInMilliseconds)
                .setSocketTimeout(timeoutInMilliseconds).build())
                .useSystemProperties()
                .build();

        try {
            HttpGet httpget = new HttpGet(new URI(url));
            HttpResponse response = client.execute(httpget);
            InputStream content = response.getEntity().getContent();

            FileOutputStream tempFileOutput = null;

            try {                
                tempFileOutput = new FileOutputStream(tempZipFile);
                
                FileUtils.copy(content, tempFileOutput);
            } finally {
                if (tempFileOutput != null) {
                    tempFileOutput.close();
                }
            }
        } catch (IOException ex) {
        	throw new CouldNotDownloadResultFileException(ex);
        } catch (IllegalStateException ex) {
        	throw new CouldNotDownloadResultFileException(ex);
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }

    @Override
    public void uploadFile(URI uri, File uploadFilePath, Consumer addHeaders, int timeoutInMilliseconds) {
        FileInputStream stream = null;

        try {
            stream = new FileInputStream(uploadFilePath);

            CloseableHttpClient client = HttpClients.custom()
                    .setDefaultRequestConfig(RequestConfig.custom()
                        .setConnectionRequestTimeout(timeoutInMilliseconds)
                        .setConnectTimeout(timeoutInMilliseconds)
                        .setSocketTimeout(timeoutInMilliseconds)
                        .build())
                    .useSystemProperties()
                    .build();
            

            try {            	
                HttpPost post = new HttpPost(uri);
                addHeaders.accept(post);
                MultipartEntityBuilder builder = MultipartEntityBuilder.create()
                        .addBinaryBody("upstream", stream, ContentType.create("application/zip"), uploadFilePath.getName());
                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                
                post.setEntity(builder.build());
                
                HttpResponse response = client.execute(post);
                
                if (response.getStatusLine().getStatusCode() != 200) {
                    InputStream in = response.getEntity().getContent();
                    BufferedReader streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                    StringBuilder exceptionMessage = new StringBuilder();

                    String inputStr;
                    while ((inputStr = streamReader.readLine()) != null) {
                    	exceptionMessage.append(inputStr); 
                    }
            	
                    throw new CouldNotUploadFileException("Unsuccessful Status Code: " + response.getStatusLine().getStatusCode() + "; Exception Message: " + exceptionMessage);
                }
            } catch (ClientProtocolException e) {
                throw new CouldNotUploadFileException(e);
            } catch (IOException e) {
                throw new CouldNotUploadFileException(e);
            } catch (IllegalStateException e) {
            	throw new CouldNotUploadFileException(e);
            } finally {
                if (client != null) {
                    try {
                        client.close();
                    } catch (IOException e) {
                        // Ignore
                        e.printStackTrace();
                    }
                }
            }
        } catch (FileNotFoundException e) {
            throw new CouldNotUploadFileException(e);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    throw new CouldNotUploadFileException(e);
                }
            }
        }
    }

    @Override
    public Future downloadFileAsync(String url, File tempZipFile, AsyncCallback callback, int timeoutInMilliseconds) {
        final ResultFuture resultFuture = new ResultFuture(callback);

        CloseableHttpClient client = HttpClients
                .custom()
                .setDefaultRequestConfig(RequestConfig.custom()
                    .setConnectionRequestTimeout(timeoutInMilliseconds)
                    .setConnectTimeout(timeoutInMilliseconds)
                    .setSocketTimeout(timeoutInMilliseconds)
                    .build())
                .useSystemProperties()
                .build();

        try {
        	
            HttpGet httpget = new HttpGet(new URI(url));
            HttpResponse response = client.execute(httpget);
            InputStream content = response.getEntity().getContent();

            FileOutputStream tempFileOutput = null;

            try {
                tempFileOutput = new FileOutputStream(tempZipFile);
                FileUtils.copy(content, tempFileOutput);

                resultFuture.setResult(tempZipFile);
            } finally {
                if (tempFileOutput != null) {
                    tempFileOutput.close();
                }
            }
        } catch (URISyntaxException ex) {
            resultFuture.setException(ex);
        } catch (IOException ex) {
            resultFuture.setException(new CouldNotDownloadResultFileException(ex));
        } catch (IllegalStateException ex) {
            resultFuture.setException(new CouldNotDownloadResultFileException(ex));
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    // Ignore
                    e.printStackTrace();
                }
            }
        }

        return resultFuture;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy