Search in sources :

Example 26 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project kie-wb-common by kiegroup.

the class WildflyClient method deploy.

/*
     * Deploys a new WAR file to the Wildfly Instance
     * @param File to be deployed, it must be a deployable file
     * @return the 200 on successful deployment
     * @throw a WildflyClientException with the status code on failure
     * @throw a WildflyClientException with the throwable in case of an internal exception
     */
public int deploy(File file) throws WildflyClientException {
    // the digest auth backend
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(host, managementPort), new UsernamePasswordCredentials(user, password));
    CloseableHttpClient httpclient = custom().setDefaultCredentialsProvider(credsProvider).build();
    HttpPost post = new HttpPost("http://" + host + ":" + managementPort + "/management-upload");
    post.addHeader("X-Management-Client-Name", "HAL");
    // the file to be uploaded
    FileBody fileBody = new FileBody(file);
    // the DMR operation
    ModelNode operation = new ModelNode();
    operation.get("address").add("deployment", file.getName());
    operation.get("operation").set("add");
    operation.get("runtime-name").set(file.getName());
    operation.get("enabled").set(true);
    // point to the multipart index used
    operation.get("content").add().get("input-stream-index").set(0);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    try {
        operation.writeBase64(bout);
    } catch (IOException ex) {
        getLogger(WildflyClient.class.getName()).log(SEVERE, null, ex);
    }
    // the multipart
    MultipartEntityBuilder builder = create();
    builder.setMode(BROWSER_COMPATIBLE);
    builder.addPart("uploadFormElement", fileBody);
    builder.addPart("operation", new ByteArrayBody(bout.toByteArray(), create("application/dmr-encoded"), "blob"));
    HttpEntity entity = builder.build();
    post.setEntity(entity);
    try {
        HttpResponse response = httpclient.execute(post);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            throw new WildflyClientException("Error Deploying App Status Code: " + statusCode);
        }
        return statusCode;
    } catch (IOException ex) {
        LOG.error("Error Deploying App : " + ex.getMessage(), ex);
        throw new WildflyClientException("Error Deploying App : " + ex.getMessage(), ex);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) WildflyClientException(org.guvnor.ala.wildfly.access.exceptions.WildflyClientException) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) AuthScope(org.apache.http.auth.AuthScope) ModelNode(org.jboss.dmr.ModelNode)

Example 27 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project scheduling by ow2-proactive.

the class RestSchedulerJobTaskTest method testSubmit.

@Test
public void testSubmit() throws Exception {
    String schedulerUrl = getResourceUrl("submit");
    HttpPost httpPost = new HttpPost(schedulerUrl);
    setSessionHeader(httpPost);
    File jobFile = RestFuncTHelper.getDefaultJobXmlfile();
    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().addPart("file", new FileBody(jobFile, ContentType.APPLICATION_XML));
    httpPost.setEntity(multipartEntityBuilder.build());
    HttpResponse response = executeUriRequest(httpPost);
    assertHttpStatusOK(response);
    JSONObject jsonObj = toJsonObject(response);
    assertNotNull(jsonObj.get("id").toString());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody) JSONObject(org.json.simple.JSONObject) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) Test(org.junit.Test)

Example 28 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project scheduling by ow2-proactive.

the class RestSchedulerJobTaskTest method testUrlMatrixParamsShouldReplaceJobVariables.

@Test
public void testUrlMatrixParamsShouldReplaceJobVariables() throws Exception {
    File jobFile = new File(RestSchedulerJobTaskTest.class.getResource("config/job_matrix_params.xml").toURI());
    String schedulerUrl = getResourceUrl("submit;var=matrix_param_val");
    HttpPost httpPost = new HttpPost(schedulerUrl);
    setSessionHeader(httpPost);
    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().addPart("file", new FileBody(jobFile, ContentType.APPLICATION_XML));
    httpPost.setEntity(multipartEntityBuilder.build());
    HttpResponse response = executeUriRequest(httpPost);
    assertHttpStatusOK(response);
    JSONObject jsonObj = toJsonObject(response);
    final String jobId = jsonObj.get("id").toString();
    assertNotNull(jobId);
    waitJobState(jobId, JobStatus.FINISHED, TimeUnit.MINUTES.toMillis(1));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody) JSONObject(org.json.simple.JSONObject) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) Test(org.junit.Test)

Example 29 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project RESTdoclet by IG-Group.

the class FileUploader method upload.

/**
    * Method to upload (post) a jar to the RESTDoclet web app at the given url,
    * and store it on the server in the given location.
    * 
    * @param url the RESTDoclet web app url to which to post
    * @param deployDir the RESTDoclet web app directory where the jar will be
    *           extracted
    * @param file the jar to be uploaded
    */
public static void upload(String url, String deployDir, File file) {
    LOG.debug("Uploading " + file.getName() + " to " + url);
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        FileBody bin = new FileBody(file);
        reqEntity.addPart("attachment_field", bin);
        httppost.setEntity(reqEntity);
        httppost.setHeader(RESTDOCLET_DEPLOY, deployDir);
        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode() != 200) {
            LOG.error("Failed to upload " + file.getName() + " to " + url + " : " + response.getStatusLine());
        }
    } catch (Exception e) {
        LOG.error("Failed to upload " + file.getName() + " to " + url, e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 30 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project streamsx.topology by IBMStreams.

the class RestUtils method postJob.

/**
	 * Submit an application bundle to execute as a job.
	 */
public static JsonObject postJob(CloseableHttpClient httpClient, JsonObject service, File bundle, JsonObject jobConfigOverlay) throws ClientProtocolException, IOException {
    final String serviceName = jstring(service, "name");
    final JsonObject credentials = service.getAsJsonObject("credentials");
    String url = getJobSubmitURL(credentials, bundle);
    HttpPost postJobWithConfig = new HttpPost(url);
    postJobWithConfig.addHeader("accept", ContentType.APPLICATION_JSON.getMimeType());
    postJobWithConfig.addHeader(AUTH.WWW_AUTH_RESP, getAPIKey(credentials));
    FileBody bundleBody = new FileBody(bundle, ContentType.APPLICATION_OCTET_STREAM);
    StringBody configBody = new StringBody(jobConfigOverlay.toString(), ContentType.APPLICATION_JSON);
    HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("sab", bundleBody).addPart(DeployKeys.JOB_CONFIG_OVERLAYS, configBody).build();
    postJobWithConfig.setEntity(reqEntity);
    JsonObject jsonResponse = getGsonResponse(httpClient, postJobWithConfig);
    RemoteContext.REMOTE_LOGGER.info("Streaming Analytics service (" + serviceName + "): submit job response:" + jsonResponse.toString());
    return jsonResponse;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) StringBody(org.apache.http.entity.mime.content.StringBody) JsonObject(com.google.gson.JsonObject)

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