Search in sources :

Example 36 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project syncope by apache.

the class HttpUtils method postWithDigestAuth.

public String postWithDigestAuth(final String url, final String file) {
    String responseBodyAsString = "";
    try (CloseableHttpResponse response = httpClient.execute(targetHost, httpPost(url, MultipartEntityBuilder.create().addPart("bin", new FileBody(new File(file))).build()), setAuth(targetHost, new DigestScheme()))) {
        responseBodyAsString = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
        handler.logOutput("Http status: " + response.getStatusLine().getStatusCode(), true);
        InstallLog.getInstance().info("Http status: " + response.getStatusLine().getStatusCode());
    } catch (IOException e) {
        final String messageError = "Error calling " + url + ": " + e.getMessage();
        handler.emitError(messageError, messageError);
        InstallLog.getInstance().error(messageError);
    }
    return responseBodyAsString;
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) FileBody(org.apache.http.entity.mime.content.FileBody) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) File(java.io.File)

Example 37 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project product-iots by wso2.

the class HTTPInvoker method uploadFile.

public static HTTPResponse uploadFile(String url, String fileName, String fileContentType) {
    HttpPost post = null;
    HttpResponse response = null;
    HTTPResponse httpResponse = new HTTPResponse();
    CloseableHttpClient httpclient = null;
    try {
        httpclient = (CloseableHttpClient) createHttpClient();
        post = new HttpPost(url);
        File file = new File(fileName);
        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(file, fileContentType);
        mpEntity.addPart("file", cbFile);
        post.setEntity(mpEntity);
        post.setHeader(Constants.Header.AUTH, OAUTH_BEARER + oAuthToken);
        // post.setHeader(Constants.Header.CONTENT_TYPE, "multipart/form-data");
        post.setHeader("Accept", Constants.ContentType.APPLICATION_JSON);
        response = httpclient.execute(post);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    BufferedReader rd = null;
    try {
        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    StringBuffer result = new StringBuffer();
    String line = "";
    try {
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    httpResponse.setResponseCode(response.getStatusLine().getStatusCode());
    httpResponse.setResponse(result.toString());
    try {
        httpclient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return httpResponse;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) FileBody(org.apache.http.entity.mime.content.FileBody) HTTPResponse(org.wso2.mdm.qsg.dto.HTTPResponse) HttpResponse(org.apache.http.HttpResponse) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ContentBody(org.apache.http.entity.mime.content.ContentBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity)

Example 38 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project questdb by bluestreak01.

the class HttpServerTest method upload.

private static void upload(File file) throws IOException {
    HttpPost post = new HttpPost("http://localhost:9000/upload");
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        MultipartEntityBuilder b = MultipartEntityBuilder.create();
        b.addPart("data", new FileBody(file));
        post.setEntity(b.build());
        client.execute(post);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody)

Example 39 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project mobile-android by photo.

the class ApiBase method createFileOnlyMultipartEntity.

private HttpEntity createFileOnlyMultipartEntity(ApiRequest request) throws UnsupportedEncodingException {
    MultipartEntity entity = new MultipartEntity();
    for (Parameter<?> parameter : request.getParametersMime()) {
        if (parameter.getValue() instanceof File) {
            File file = (File) parameter.getValue();
            entity.addPart(parameter.getName(), new FileBody(file));
        }
    }
    return entity;
}
Also used : FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) File(java.io.File)

Example 40 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project stanbol by apache.

the class ReasonersOfflineTest method setupMultipart.

@Before
public void setupMultipart() {
    FileBody bin = new FileBody(file);
    multiPart = new MultipartEntity();
    multiPart.addPart(fileParam, bin);
}
Also used : FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) Before(org.junit.Before)

Aggregations

FileBody (org.apache.http.entity.mime.content.FileBody)61 HttpPost (org.apache.http.client.methods.HttpPost)46 File (java.io.File)41 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)37 HttpResponse (org.apache.http.HttpResponse)31 StringBody (org.apache.http.entity.mime.content.StringBody)30 HttpEntity (org.apache.http.HttpEntity)18 IOException (java.io.IOException)17 Test (org.junit.Test)17 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)15 TestHttpClient (io.undertow.testutils.TestHttpClient)11 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)8 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)7 HttpClient (org.apache.http.client.HttpClient)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 JsonObject (com.google.gson.JsonObject)5 JSONObject (org.json.JSONObject)5 BlockingHandler (io.undertow.server.handlers.BlockingHandler)4 BufferedReader (java.io.BufferedReader)4 FileInputStream (java.io.FileInputStream)4