Example usage for org.apache.http.entity ContentType create

List of usage examples for org.apache.http.entity ContentType create

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType create.

Prototype

public static ContentType create(String str, String str2) throws UnsupportedCharsetException 

Source Link

Usage

From source file:org.fuin.esc.eshttp.ESHttpEventStore.java

private static HttpPost createPost(final URI uri, final String content, final ESEnvelopeType envelopeType) {
    final HttpPost post = new HttpPost(uri);
    post.setHeader("Content-Type",
            envelopeType.getWriteContentType() + "; charset=" + envelopeType.getMetaCharset());
    final ContentType contentType = ContentType.create(envelopeType.getMetaType(),
            envelopeType.getMetaCharset());
    post.setEntity(new StringEntity(content, contentType));
    return post;//from   w  w w.  j av  a2s.c o m
}

From source file:org.jets3t.service.CloudFrontService.java

/**
 * Create a streaming or non-streaming distribution.
 *
 * @param config Configuration document// w  w  w. j  ava 2 s. com
 * @return Information about the newly-created distribution.
 * @throws CloudFrontServiceException exception
 */
protected Distribution createDistributionImpl(DistributionConfig config) throws CloudFrontServiceException {
    if (log.isDebugEnabled()) {
        log.debug("Creating " + (config.isStreamingDistributionConfig() ? "streaming" : "")
                + " distribution for origins: " + Arrays.asList(config.getOrigins()));
    }

    HttpPost httpMethod = new HttpPost(ENDPOINT + VERSION
            + (config.isStreamingDistributionConfig() ? "/streaming-distribution" : "/distribution"));

    try {
        String distributionConfigXml = buildDistributionConfigXmlDocument(config);

        httpMethod.setEntity(new StringEntity(distributionConfigXml,
                ContentType.create("text/xml", Constants.DEFAULT_ENCODING)));

        HttpResponse response = performRestRequest(httpMethod, 201);

        DistributionHandler handler = (new CloudFrontXmlResponsesSaxParser(this.jets3tProperties))
                .parseDistributionResponse(response.getEntity().getContent());

        return handler.getDistribution();
    } catch (CloudFrontServiceException e) {
        throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new CloudFrontServiceException(e);
    }
}

From source file:org.ldp4j.server.frontend.ServerFrontendITest.java

@Test
@Category({ ExceptionPath.class })
@OperateOnDeployment(DEPLOYMENT)/*from ww  w  .  j a va 2s .c  o  m*/
public void testPostPostconditionFailure(@ArquillianResource final URL url) throws Exception {
    LOGGER.info("Started {}", testName.getMethodName());
    HELPER.base(url);
    HELPER.setLegacy(false);

    HttpPost post = HELPER.newRequest(TestingApplication.ROOT_BAD_RESOURCE_PATH, HttpPost.class);
    post.setEntity(new StringEntity(TEST_SUITE_BODY, ContentType.create("text/turtle", "UTF-8")));
    Metadata response = HELPER.httpRequest(post);
    assertThat(response.status, equalTo(HttpStatus.SC_INTERNAL_SERVER_ERROR));
    assertThat(response.body, notNullValue());
    assertThat(response.contentType, startsWith("text/plain"));
    assertThat(response.language, equalTo(Locale.ENGLISH));

}

From source file:com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient.java

private String postRequest(String path, String content) throws IOException {
    HttpPost request = new HttpPost(this.baseURL + path);
    request.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8")));
    return postRequest(request);
}

From source file:com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient.java

private String putRequest(String path, String content) throws IOException, InterruptedException {
    HttpPut request = new HttpPut(path);
    request.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8")));
    return doRequest(request);
}

From source file:com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient.java

private String postRequest(String path, String content) throws IOException, InterruptedException {
    HttpPost httppost = new HttpPost(path);
    httppost.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8")));
    return doRequest(httppost);
}

From source file:com.vmware.vchs.publicapi.samples.VMCreateSample.java

/**
 * This method will initialize and deploy a vApp using the instantiationHref, vappTemplateHref,
 * vmNetworkName and vdc provided./*from  w w w .j av  a  2  s  .co  m*/
 * 
 * @param instantiateHref
 *            the href to instantiatevApp action
 * @param vappTempalteHref
 *            the href to the vApp Template to be used to create vApp
 * @param vmNetworkName
 *            the network name for VM
 * @param VdcType
 *            the vdc
 * @return VappType if the initialize vapp succeeds, null otherwise
 */
private VAppType createVApp(final String instantiateHref, final String vappTempalteHref, VdcType vdc) {
    System.out.print("Attempting to create vApp...");
    ReferenceType vappReference = new ReferenceType();
    vappReference.setHref(vappTempalteHref);

    // Create an InstantiateVAppTemplateParamsType object and initialize it
    InstantiateVAppTemplateParamsType instvApp = new InstantiateVAppTemplateParamsType();

    // Set the name of vApp using the options.vappname (command line option --targetvappname)
    instvApp.setName(options.vappName);

    // do not deploy this vApp.. we still need to update network info which requires the
    // vApp to be undeployed and not powered on.
    instvApp.setDeploy(Boolean.FALSE);
    instvApp.setPowerOn(Boolean.FALSE);

    // vApp reference to be used
    instvApp.setSource(vappReference);
    instvApp.setDescription("VM creation using VMCreateSample");
    instvApp.setAllEULAsAccepted(Boolean.TRUE);

    InstantiationParamsType instParams = new InstantiationParamsType();

    instvApp.setInstantiationParams(instParams);

    JAXBContext jaxbContexts = null;
    try {
        jaxbContexts = JAXBContext.newInstance(InstantiateVAppTemplateParamsType.class);
    } catch (JAXBException ex) {
        throw new RuntimeException("Problem creating JAXB Context: ", ex);
    }

    // Create HttpPost request to perform InstantiatevApp action
    HttpPost instantiateVAppPost = vcd.post(instantiateHref, options);
    com.vmware.vcloud.api.rest.schema.ObjectFactory obj = new com.vmware.vcloud.api.rest.schema.ObjectFactory();
    JAXBElement<InstantiateVAppTemplateParamsType> instvAppTemplate = obj
            .createInstantiateVAppTemplateParams(instvApp);
    OutputStream os = null;

    try {
        javax.xml.bind.Marshaller marshaller = jaxbContexts.createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        os = new ByteArrayOutputStream();
        // Marshal the object via JAXB to XML
        marshaller.marshal(instvAppTemplate, os);
    } catch (JAXBException e) {
        throw new RuntimeException("Problem marshalling instantiation vApp template", e);
    }

    // Set the Content-Type header for the VM vApp template parameters
    ContentType contentType = ContentType
            .create("application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml", "ISO-8859-1");
    StringEntity vapp = new StringEntity(os.toString(), contentType);
    instantiateVAppPost.setEntity(vapp);

    // Invoke the HttoPost to initiate the VM creation process
    HttpResponse response = HttpUtils.httpInvoke(instantiateVAppPost);

    VAppType vApp = null;

    // Make sure response status is 201 Created
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
        vApp = HttpUtils.unmarshal(response.getEntity(), VAppType.class);
    }

    if (null == vApp) {
        throw new RuntimeException("Could not instatiate or deploy the vApp.");
    }

    return vApp;
}

From source file:com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient.java

private String putRequest(String path, String content) throws IOException {
    HttpPut request = new HttpPut(this.baseURL + path);
    request.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8")));
    return doRequest(request);
}

From source file:com.vmware.vchs.publicapi.samples.VMCreateSample.java

/**
 * This method will make a POST call to the vApp deploy URL to deploy the vApp and it's
 * children Vms.//from  ww  w  . ja v  a 2  s . c  om
 * 
 * @param vApp the vApp instance to deploy
 * @return a TaskType that can be used to monitor the status of the task
 */
private TaskType deploy(VAppType vApp) {
    String deployHref = null;

    // Search the list of links for the vApp rel="deploy" to get the correct Href
    for (LinkType link : vApp.getLink()) {
        if (link.getRel().equalsIgnoreCase("deploy")) {
            deployHref = link.getHref();
            break;
        }
    }

    // Only proceed if we found a valid deploy Href
    if (null != deployHref) {
        DeployVAppParamsType deployParams = new DeployVAppParamsType();
        deployParams.setPowerOn(Boolean.TRUE);

        // Create HttpPost request to perform InstantiatevApp action
        HttpPost instantiateVAppPost = vcd.post(deployHref, options);

        ObjectFactory objFactory = new ObjectFactory();
        JAXBElement<DeployVAppParamsType> deployParamsType = objFactory.createDeployVAppParams(deployParams);

        OutputStream os = null;

        JAXBContext jaxbContexts = null;
        try {
            jaxbContexts = JAXBContext.newInstance(DeployVAppParamsType.class);
        } catch (JAXBException ex) {
            throw new RuntimeException("Problem creating JAXB Context: ", ex);
        }

        try {
            javax.xml.bind.Marshaller marshaller = jaxbContexts.createMarshaller();
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            os = new ByteArrayOutputStream();
            // Marshal the object via JAXB to XML
            marshaller.marshal(deployParamsType, os);
        } catch (JAXBException e) {
            throw new RuntimeException("Problem marshalling instantiation vApp template", e);
        }

        HttpPost deployPost = vcd.post(deployHref, options);
        ;
        // Set the Content-Type header for the VM vApp template parameters
        ContentType contentType = ContentType.create("application/vnd.vmware.vcloud.deployVAppParams+xml",
                "ISO-8859-1");
        StringEntity deployEntity = new StringEntity(os.toString(), contentType);
        deployPost.setEntity(deployEntity);

        // Invoke the HttoPost to initiate the VM creation process
        HttpResponse response = HttpUtils.httpInvoke(deployPost);

        // Make sure response status is 201 Created
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_ACCEPTED) {
            TaskType taskType = HttpUtils.unmarshal(response.getEntity(), TaskType.class);
            return taskType;
        }
    }

    throw new RuntimeException("Could not deploy " + vApp.getName());
}