Matt,
Since I last posted on this thread, we have tested HttpClient 3.0. I have pasted (tested) sample code that will work once you edit variables that are specific to your instance of DMM (marked clearly in the code with comments). Please give this a try. I have also attached the Java file to this post for your convenience (you will need to change the extension to .java).
-Hashir
Sample code below
--------------------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///ENTER IN YOUR INSTANCE SPECIFIC DATA BELOW
String dmmDomainName = "dmmServer.someCompanny.com";
String usernameOfUploader = "adminUsername";
String passwordOfUploader = "adminPassword";
String fileName = "someImage.jpg";//This example uploads an image
String assetDescription = "AssetDescription";
String fileType = "IMAGES";//This does not need to be changed if you are uploading an image
String categoryId = "6";//6 is usually the category for images
String filePath = "C:\\Documents and Settings\\user\\images\\" + fileName;
///YOU DO NOT NEED TO MODIFY ANY CODE BELOW THIS LINE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Create an instance of HttpClient.
HttpClient client = new HttpClient();
//authentication.
client.getParams().setAuthenticationPreemptive(true);
Credentials defaultCreds = new UsernamePasswordCredentials(usernameOfUploader, passwordOfUploader);
client.getState().setCredentials(new AuthScope(dmmDomainName, 8080, AuthScope.ANY_REALM), defaultCreds);
//Create an instance of one of the methods (PostMethod in this case). The URL to connect to is passed in to the the method constructor.
PostMethod filePost = new PostMethod("http://" + dmmDomainName + ":8080/xTAS-core/services/content/assets/");
//set xml data as a variable
String xmlData = "<n1:asset xmlns:n1=\"http://www.cisco.com/dms/xml/ns/dsm/contentManagement\">" +
" <title>AssetTitle</title>" +
" <fileName>" + fileName + "</fileName>" +
" <description>" + assetDescription + "</description>" +
" <fileType>" + fileType + "</fileType>" +
" <estimatedDuration>" +
" <Hours>0</Hours>" +
" <Minutes>0</Minutes>" +
" <Seconds>0</Seconds>" +
" </estimatedDuration>" +
" <categoryIdList>" +
" <id>" + categoryId + "</id>" +
" </categoryIdList>" +
" <assetOwner>" + usernameOfUploader + "</assetOwner>" +
" <storageType>file</storageType>" +
" <downloadurl>true</downloadurl>" +
" </n1:asset>";
//create a File part. This will be one part of a multipart form
File f = new File(filePath);
FilePart filePart = null;
try {
filePart = new FilePart("upload_file", f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//String part. This is another part of a multipart form
StringPart stringPart = new StringPart("data", xmlData);
//Place all parts into a Parts array
Part[] parts = {
filePart,
stringPart
};
//create the multipart form
filePost.setRequestEntity(
new MultipartRequestEntity(parts, filePost.getParams())
);
//execute post
try {
client.executeMethod(filePost);
} catch (HttpException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
//Read the response.
String responseBody = null;
try {
responseBody = filePost.getResponseBodyAsString();
} catch (IOException e) {
e.printStackTrace();
}
//Release the connection.
filePost.releaseConnection();
//Deal with the response.
System.out.println(responseBody);
filePost.releaseConnection();
}
}