Difference between revisions of "REST API:StaticResourceUploadClient"
imported>Aeric |
imported>Aeric |
||
(3 intermediate revisions by the same user not shown) | |||
Line 30: | Line 30: | ||
{ | { | ||
public void execute(String | public void execute(String cookieString) | ||
{ | { | ||
String url = baseUrl + "/staticResource"; | String url = baseUrl + "/staticResource"; | ||
Line 37: | Line 37: | ||
String xml = "<platform>" | String xml = "<platform>" | ||
+ "<staticResource>" | + "<staticResource>" | ||
+ "<name> | + "<name>upload_test</name>" | ||
+ "<description>Test | + "<description>Test an API-driven upload</description>" | ||
+ "</staticResource>" | + "</staticResource>" | ||
+ "</platform>"; | + "</platform>"; | ||
Line 51: | Line 51: | ||
// XML part | // XML part | ||
StringPart sp = new StringPart(" | // For JSON, use __json_data__ and APPLICATION_JSON | ||
StringPart sp = new StringPart("__xml_data__", xml); | |||
sp.setContentType(MediaType.APPLICATION_XML); | sp.setContentType(MediaType.APPLICATION_XML); | ||
Line 57: | Line 58: | ||
final Part[] part = {fp, sp}; | final Part[] part = {fp, sp}; | ||
// | // Create the request | ||
HttpClient httpClient = new HttpClient(); | HttpClient httpClient = new HttpClient(); | ||
PostMethod httpMethod = new PostMethod(url); // <--[2] | PostMethod httpMethod = new PostMethod(url); // <--[2] | ||
// Use PutMethod for an update | // Use PutMethod for an update | ||
httpMethod. | // Set the response type (xml) and the session cookie. | ||
//(Had we logged in with HttpClient, the cookie would be handled | |||
// automatically. But since we let BaseClient do the log in and log out, | |||
// we need to tack on the cookie string here.) | |||
httpMethod.addRequestHeader("Cookie", cookieString); | |||
httpMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML); | httpMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML); | ||
httpMethod.setRequestEntity( | httpMethod.setRequestEntity( | ||
new MultipartRequestEntity(part, httpMethod.getParams()) | new MultipartRequestEntity(part, httpMethod.getParams()) | ||
Line 96: | Line 100: | ||
{ | { | ||
StaticResourceUploadClient client = new StaticResourceUploadClient(); | StaticResourceUploadClient client = new StaticResourceUploadClient(); | ||
String | String cookieString = client.login(); | ||
client.execute( | client.execute(cookieString); | ||
client.logout(); | client.logout(); | ||
} | } | ||
Line 105: | Line 109: | ||
:''Notes:'' | :''Notes:'' | ||
:* '''[1,2]''' - Change these lines to update an existing resource | :* '''[1,2]''' - Change these lines to update an existing resource | ||
:* '''[3]''' - Use the [{{DOCHOST}}/samples/demo/rest/BaseUtil | :* '''[3]''' - Use the [{{DOCHOST}}/samples/demo/rest/BaseUtil.java BaseUtil mediaType()] method to automatically set the content type appropriate for the file. | ||
<noinclude> | <noinclude> | ||
[[Category:REST API|static resource client]] | [[Category:REST API|static resource client]] | ||
</noinclude> | </noinclude> |
Latest revision as of 00:42, 25 May 2012
The StaticResourceUploadClient demo program extends the REST API:BaseClient class to upload a static resource. With a couple of modifications, it can be used to do updates or load arbitrary file types.
Tip: The REST_samples.zip file contains BaseClient, the utility class it uses, and extensions like this one that demonstrate REST operations.
- <syntaxhighlight lang="java" enclose="div">
package demo.rest;
import java.io.File; import javax.ws.rs.core.MediaType;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PutMethod; 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;
/**
* Load a static resource file. * Load a static resource file. This program extends demo.rest.BaseClient.java * which in turn makes use of demo.rest.BaseUtil.java.
*
* Note:
* This class uses the Apache commons HttpClient.
* The Apache wink RestClient is generally more straightforward,
* but multipart requests currently do not work when made using that client.
*/
public class StaticResourceUploadClient extends BaseClient
{
public void execute(String cookieString)
{
String url = baseUrl + "/staticResource";
//Use this for an update:
// String url = baseUrl + "/staticResource/{id}"; // <--[1]
String xml = "<platform>"
+ "<staticResource>"
+ "<name>upload_test</name>"
+ "<description>Test an API-driven upload</description>"
+ "</staticResource>"
+ "</platform>";
try
{
// File part
File file = new File("C:/testfiles/test.txt");
FilePart fp = new FilePart("file_part", file);
fp.setContentType(MediaType.TEXT_PLAIN); // <--[3]
//Use BaseUtil.mediaType() to set type based on file extension
// XML part
// For JSON, use __json_data__ and APPLICATION_JSON
StringPart sp = new StringPart("__xml_data__", xml);
sp.setContentType(MediaType.APPLICATION_XML);
// Multipart wrapper
final Part[] part = {fp, sp};
// Create the request
HttpClient httpClient = new HttpClient();
PostMethod httpMethod = new PostMethod(url); // <--[2]
// Use PutMethod for an update
// Set the response type (xml) and the session cookie.
//(Had we logged in with HttpClient, the cookie would be handled
// automatically. But since we let BaseClient do the log in and log out,
// we need to tack on the cookie string here.)
httpMethod.addRequestHeader("Cookie", cookieString);
httpMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML);
httpMethod.setRequestEntity(
new MultipartRequestEntity(part, httpMethod.getParams())
);
// Check status & echo response
int status = httpClient.executeMethod(httpMethod);
String responseXml = httpMethod.getResponseBodyAsString();
if ((status == 200) || (status == 201))
{
// 200=Successful GET, PUT, or DELETE. 201=Successful POST.
echoResponse(httpMethod.getResponseBodyAsStream());
}
else
{
echoStatus(status, responseXml);
}
}
catch (ClientWebException webException)
{
echoResponse(webException);
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args)
{
StaticResourceUploadClient client = new StaticResourceUploadClient();
String cookieString = client.login();
client.execute(cookieString);
client.logout();
}
}
</syntaxhighlight>
- Notes:
- [1,2] - Change these lines to update an existing resource
- [3] - Use the BaseUtil mediaType() method to automatically set the content type appropriate for the file.