org.apache.http.entity.mime.FormBodyPartBuilder Java Examples

The following examples show how to use org.apache.http.entity.mime.FormBodyPartBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WineryConnector.java    From container with Apache License 2.0 5 votes vote down vote up
private String uploadCSARToWinery(final File file, final boolean overwrite) throws URISyntaxException, IOException {
    final MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    final ContentBody fileBody = new FileBody(file);
    final ContentBody overwriteBody = new StringBody(String.valueOf(overwrite), ContentType.TEXT_PLAIN);
    final FormBodyPart filePart = FormBodyPartBuilder.create("file", fileBody).build();
    final FormBodyPart overwritePart = FormBodyPartBuilder.create("overwrite", overwriteBody).build();
    builder.addPart(filePart);
    builder.addPart(overwritePart);

    final HttpEntity entity = builder.build();

    final HttpPost wineryPost = new HttpPost();

    wineryPost.setURI(new URI(this.wineryPath));
    wineryPost.setEntity(entity);

    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        final CloseableHttpResponse wineryResp = httpClient.execute(wineryPost);
        String location = getHeaderValue(wineryResp, HttpHeaders.LOCATION);
        wineryResp.close();

        if (Objects.nonNull(location) && location.endsWith("/")) {
            location = location.substring(0, location.length() - 1);
        }
        return location;
    } catch (final IOException e) {
        LOG.error("Exception while uploading CSAR to the Container Repository: ", e);
        return "";
    }
}
 
Example #2
Source File: HttpSender.java    From iaf with Apache License 2.0 5 votes vote down vote up
protected FormBodyPart createMultipartBodypart(String name, String message, String contentType) {
	ContentType cType = ContentType.create("text/plain", getCharSet());
	if(StringUtils.isNotEmpty(contentType))
		cType = ContentType.create(contentType, getCharSet());

	FormBodyPartBuilder bodyPart = FormBodyPartBuilder.create()
		.setName(name)
		.setBody(new StringBody(message, cType));

	if (StringUtils.isNotEmpty(getMtomContentTransferEncoding()))
		bodyPart.setField(MIME.CONTENT_TRANSFER_ENC, getMtomContentTransferEncoding());

	return bodyPart.build();
}
 
Example #3
Source File: HttpSender.java    From iaf with Apache License 2.0 5 votes vote down vote up
protected FormBodyPart createMultipartBodypart(String name, InputStream is, String fileName, String contentType) {
	if (log.isDebugEnabled()) log.debug(getLogPrefix()+"appending filepart ["+name+"] with value ["+is+"] fileName ["+fileName+"] and contentType ["+contentType+"]");
	FormBodyPartBuilder bodyPart = FormBodyPartBuilder.create()
		.setName(name)
		.setBody(new InputStreamBody(is, ContentType.create(contentType, getCharSet()), fileName));
	return bodyPart.build();
}
 
Example #4
Source File: WineryConnector.java    From container with Apache License 2.0 4 votes vote down vote up
public QName createServiceTemplateFromXaaSPackage(final File file, final QName artifactType,
                                                  final Set<QName> nodeTypes, final QName infrastructureNodeType,
                                                  final Map<String, String> tags) throws URISyntaxException,
    IOException {
    final MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    // file
    final ContentBody fileBody = new FileBody(file);
    final FormBodyPart filePart = FormBodyPartBuilder.create("file", fileBody).build();
    builder.addPart(filePart);

    // artefactType
    final ContentBody artefactTypeBody = new StringBody(artifactType.toString(), ContentType.TEXT_PLAIN);
    final FormBodyPart artefactTypePart = FormBodyPartBuilder.create("artefactType", artefactTypeBody).build();
    builder.addPart(artefactTypePart);

    // nodeTypes
    if (!nodeTypes.isEmpty()) {
        String nodeTypesAsString = "";
        for (final QName nodeType : nodeTypes) {
            nodeTypesAsString += nodeType.toString() + ",";
        }

        final ContentBody nodeTypesBody =
            new StringBody(nodeTypesAsString.substring(0, nodeTypesAsString.length() - 1), ContentType.TEXT_PLAIN);
        final FormBodyPart nodeTypesPart = FormBodyPartBuilder.create("nodeTypes", nodeTypesBody).build();
        builder.addPart(nodeTypesPart);
    }

    // infrastructureNodeType
    if (infrastructureNodeType != null) {
        final ContentBody infrastructureNodeTypeBody =
            new StringBody(infrastructureNodeType.toString(), ContentType.TEXT_PLAIN);
        final FormBodyPart infrastructureNodeTypePart =
            FormBodyPartBuilder.create("infrastructureNodeType", infrastructureNodeTypeBody).build();
        builder.addPart(infrastructureNodeTypePart);
    }

    // tags
    if (!tags.isEmpty()) {
        String tagsString = "";
        for (final String key : tags.keySet()) {
            if (tags.get(key) == null) {
                tagsString += key + ",";
            } else {
                tagsString += key + ":" + tags.get(key) + ",";
            }
        }

        final ContentBody tagsBody =
            new StringBody(tagsString.substring(0, tagsString.length() - 1), ContentType.TEXT_PLAIN);
        final FormBodyPart tagsPart = FormBodyPartBuilder.create("tags", tagsBody).build();
        builder.addPart(tagsPart);
    }

    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        // POST to XaaSPackager
        final HttpPost xaasPOST = new HttpPost();
        xaasPOST.setURI(new URI(this.wineryPath + "servicetemplates/"));
        xaasPOST.setEntity(builder.build());
        final CloseableHttpResponse xaasResp = httpClient.execute(xaasPOST);
        xaasResp.close();

        // create QName of the created serviceTemplate resource
        String location = getHeaderValue(xaasResp, HttpHeaders.LOCATION);

        if (location.endsWith("/")) {
            location = location.substring(0, location.length() - 1);
        }

        final String localPart = getLastPathFragment(location);
        final String namespaceDblEnc = getLastPathFragment(location.substring(0, location.lastIndexOf("/")));
        final String namespace = URLDecoder.decode(URLDecoder.decode(namespaceDblEnc));

        return new QName(namespace, localPart);
    } catch (final IOException e) {
        LOG.error("Exception while calling Xaas packager: ", e);
        return null;
    }
}
 
Example #5
Source File: MultipartEntityBuilder.java    From iaf with Apache License 2.0 4 votes vote down vote up
public MultipartEntityBuilder addPart(String name, ContentBody contentBody) {
	Args.notNull(name, "Name");
	Args.notNull(contentBody, "Content body");
	return addPart(FormBodyPartBuilder.create(name, contentBody).build());
}