public static void main(String[] args)
throws Exception {
HttpUrlConnectionFactory urlConnectionFactory =
new HttpUrlConnectionFactory(API_KEY, SECRET, null);
HttpCallCommandFactory commandFactory =
new HttpCallCommandFactory(urlConnectionFactory);
// create design data using xml
HttpCallCommand createDesignCommand =
commandFactory.createPlainHttpCallCommand(UPLOAD_URL, HttpMethod.POST, null);
createDesignCommand.setInput(getXMLData(UPLOAD_XML));
createDesignCommand.setApiKeyProtected(true);
createDesignCommand.execute();
if (createDesignCommand.getStatus() >= 400) {
throw new Exception("Could not create design xml!");
}
log.info("XML location is: " + createDesignCommand.getUrl());
// get created design xml
HttpCallCommand getDesignCommand =
commandFactory.createPlainHttpCallCommand(createDesignCommand.getLocation(), HttpMethod.GET, null);
getDesignCommand.execute();
if (createDesignCommand.getStatus() >= 400) {
throw new Exception("Could not retrieve design xml from " + createDesignCommand.getLocation() + "!");
}
String message = (String) getDesignCommand.getOutput();
// determine upload location
String searchString = "resource xlink:href=\"";
int index = message.indexOf(searchString);
String uploadUrl = message.substring(index + searchString.length(),
message.indexOf("\"", index + searchString.length() + 1));
log.info("Upload location is: " + uploadUrl);
// upload image
HttpCallCommand uploadDesignCommand =
commandFactory.createFileHttpCallCommand(uploadUrl, HttpMethod.PUT, null, new File(UPLOAD_IMAGE), null);
uploadDesignCommand.setApiKeyProtected(true);
uploadDesignCommand.execute();
if (uploadDesignCommand.getStatus() >= 400) {
log.error(uploadDesignCommand.getErrorMessage());
throw new Exception("Status above 400 expected but status was " + uploadDesignCommand.getStatus() + "!");
}
// create product data using xml
HttpCallCommand createProductCommand =
commandFactory.createPlainHttpCallCommand(CREATION_URL, HttpMethod.POST, null);
String productData = getXMLData(PRODUCT_XML);
// use id from fetched design xml here -> my solution is only a hack
productData = productData.replace("THE_DESIGN_ID", "u" + uploadUrl.substring(uploadUrl.lastIndexOf('/') + 1));
createProductCommand.setInput(productData);
createProductCommand.setApiKeyProtected(true);