Java Code Examples for org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity#writeRequest()

The following examples show how to use org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity#writeRequest() . 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: UploadWebScriptTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
public PostRequest buildMultipartPostRequest(File file, String filename, String siteId, String containerId) throws IOException
{
    Part[] parts = 
        { 
            new FilePart("filedata", file.getName(), file, "text/plain", null), 
            new StringPart("filename", filename),
            new StringPart("description", "description"), 
            new StringPart("siteid", siteId), 
            new StringPart("containerid", containerId) 
        };

    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new HttpMethodParams());

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    multipartRequestEntity.writeRequest(os);

    PostRequest postReq = new PostRequest(UPLOAD_URL, os.toByteArray(), multipartRequestEntity.getContentType());
    return postReq;
}
 
Example 2
Source File: UploadServletTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private MockHttpServletRequest getMultipartRequest( ) throws Exception
{
    MockHttpServletRequest request = new MockHttpServletRequest( );
    byte [ ] fileContent = new byte [ ] {
            1, 2, 3
    };
    Part [ ] parts = new Part [ ] {
        new FilePart( "file1", new ByteArrayPartSource( "file1", fileContent ) )
    };
    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity( parts, new PostMethod( ).getParams( ) );
    // Serialize request body
    ByteArrayOutputStream requestContent = new ByteArrayOutputStream( );
    multipartRequestEntity.writeRequest( requestContent );
    // Set request body to HTTP servlet request
    request.setContent( requestContent.toByteArray( ) );
    // Set content type to HTTP servlet request (important, includes Mime boundary string)
    request.setContentType( multipartRequestEntity.getContentType( ) );
    request.setMethod( "POST" );
    return request;
}
 
Example 3
Source File: CustomModelImportTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
public PostRequest buildMultipartPostRequest(File file) throws IOException
{
    Part[] parts = { new FilePart("filedata", file.getName(), file, "application/zip", null) };

    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new HttpMethodParams());

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    multipartRequestEntity.writeRequest(os);

    PostRequest postReq = new PostRequest(UPLOAD_URL, os.toByteArray(), multipartRequestEntity.getContentType());
    return postReq;
}
 
Example 4
Source File: MultiPartBuilder.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public MultiPartRequest build() throws IOException
{
    List<Part> parts = new ArrayList<>();

    if (fileData != null)
    {
        FilePart fp = new FilePart("filedata", fileData.getFileName(), fileData.getFile(), fileData.getMimetype(), null);
        // Get rid of the default values added upon FilePart instantiation
        fp.setCharSet(fileData.getEncoding());
        fp.setContentType(fileData.getMimetype());
        parts.add(fp);
        addPartIfNotNull(parts, "name", fileData.getFileName());
    }
    addPartIfNotNull(parts, "relativepath", relativePath);
    addPartIfNotNull(parts, "updatenoderef", updateNodeRef);
    addPartIfNotNull(parts, "description", description);
    addPartIfNotNull(parts, "contenttype", contentTypeQNameStr);
    addPartIfNotNull(parts, "aspects", getCommaSeparated(aspects));
    addPartIfNotNull(parts, "majorversion", majorVersion);
    addPartIfNotNull(parts, "overwrite", overwrite);
    addPartIfNotNull(parts, "autorename", autoRename);
    addPartIfNotNull(parts, "nodetype", nodeType);
    addPartIfNotNull(parts, "renditions", getCommaSeparated(renditionIds));

    HttpMethodParams params = new HttpMethodParams();

    if (!properties.isEmpty())
    {
        for (String propertyName : properties.keySet())
        {

            Serializable expected = properties.get(propertyName);
            if (expected instanceof List)
            {

                List<String> multipleValues = (List<String>) expected;
                for (String value : multipleValues)
                {
                    {
                        parts.add(new StringPart(propertyName, value));
                    }
                }
            }
            else
            {
                parts.add(new StringPart(propertyName, (String) expected));
            }

        }
    }

    MultipartRequestEntity req = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), new HttpMethodParams());

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    req.writeRequest(os);

    return new MultiPartRequest(os.toByteArray(), req.getContentType(), req.getContentLength());
}
 
Example 5
Source File: StreamPipeTest.java    From iaf with Apache License 2.0 4 votes vote down vote up
private MockMultipartHttpServletRequest createMultipartHttpRequest(StreamPipe pipe, boolean addAntiVirusParts, boolean antiVirusLastPartFailed) throws Exception {
	MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
	request.setContentType("multipart/mixed;boundary=gc0p4Jq0M2Yt08jU534c0p");
	List<Part> parts = new ArrayList<Part>();
	String string = "<hello>test</hello>";
	StringPart stringPart = new StringPart("string1", string);
	parts.add(stringPart);
	URL url = ClassUtils.getResourceURL(this, "/Documents/doc001.pdf");
	File file = new File(url.toURI());
	FilePart filePart = new FilePart("file1", file.getName(), file);
	parts.add(filePart);
	if (addAntiVirusParts) {
		StringPart antiVirusPassedPart = new StringPart(
				pipe.getAntiVirusPartName(),
				pipe.getAntiVirusPassedMessage());
		parts.add(antiVirusPassedPart);
	}
	URL url2 = ClassUtils.getResourceURL(this, "/Documents/doc002.pdf");
	File file2 = new File(url2.toURI());
	FilePart filePart2 = new FilePart("file2", file2.getName(), file2);
	parts.add(filePart2);
	if (addAntiVirusParts) {
		String antiVirusLastPartMessage;
		if (antiVirusLastPartFailed) {
			antiVirusLastPartMessage = "Fail";
			if (antiVirusLastPartMessage.equalsIgnoreCase(
					pipe.getAntiVirusPassedMessage())) {
				throw new Exception("fail message ["
						+ antiVirusLastPartMessage
						+ "] must differ from pass message ["
						+ pipe.getAntiVirusPassedMessage() + "]");
			}
		} else {
			antiVirusLastPartMessage = pipe
					.getAntiVirusPassedMessage();
		}
		StringPart antiVirusPassedPart2 = new StringPart(
				pipe.getAntiVirusPartName(),
				antiVirusLastPartMessage);
		parts.add(antiVirusPassedPart2);
	}
	Part allParts[] = new Part[parts.size()];
	allParts = parts.toArray(allParts);
	MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(
			allParts, new PostMethod().getParams());
	ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
	multipartRequestEntity.writeRequest(requestContent);
	request.setContent(requestContent.toByteArray());
	request.setContentType(multipartRequestEntity.getContentType());
	return request;
}