AgileApps Support Wiki Pre Release

REST API:staticResource Resource

From AgileApps Support Wiki

Manage Static Resources stored on the platform using the REST API.

Access Permissions

Lock-tiny.gif

Users that have the Use Development Features permission can use these resources. However, a logged in user can access the APIs listed in the sections Get a List of all Static Resources and Get a Static Resource, regardless of the Use Development Features permission. 

Get a List of all Static Resources

Method
GET
URI
https://{yourDomain}/networking/rest/staticResource
Response
<syntaxhighlight lang="xml" enclose="div">

<platform>

   <staticResource>
       <id>567rty789uio</id>
       <name>JSPsample</name>
       <resource_file_name>jsp.txt</resource_file_name>
       <size>613</size>
       <mime_type>text/plain</mime_type>
       <date_created>2010-11-12T13:14:15Z</date_created>
       <created_id type="USER" 
         uri="https://{yourDomain}/networking/rest/user/123qwe456rty" 
         displayValue="John Public">123qwe456rty</created_id>
       <date_modified>2010-11-12T13:14:15Z</date_modified>
       <modified_id type="USER" 
         uri="https://{yourDomain}/networking/rest/user/123qwe456rty" 
         displayValue="John Public">123qwe456rty</modified_id>
       </staticResource>
       ...
   <message>
       0
       <description>Success</description>
   </message>
   <recordCount>24</recordCount>

</platform> </syntaxhighlight>

See also: REST API:Error Codes

Get a Static Resource

Method
GET
URI
https://{yourDomain}/networking/rest/staticResource/{resourceId}?{query_parameters}
Query Parameters
  • getfile
  • If true, download the physical file
  • If false, return the XML descriptor only (default)
For more information, see: Specifying Query Parameters in REST APIs
Response
<syntaxhighlight lang="xml" enclose="div">

<message>

   <staticResource>
       <id>567rty789uio</id>
       <name>JSPsample</name>
       <resource_file_name>jsp.txt</resource_file_name>
       <size>613</size>
       <mime_type>text/plain</mime_type>
       <date_created>2010-11-12T13:14:15Z</date_created>
       <created_id type="USER" 
         uri="https://{yourDomain}/networking/rest/user/123qwe456rty" 
         displayValue="John Public">123qwe456rty</created_id>
       <date_modified>2010-11-12T13:14:15Z</date_modified>
       <modified_id type="USER" 
         uri="https://{yourDomain}/networking/rest/user/123qwe456rty" 
         displayValue="John Public">123qwe456rty</modified_id>
       </staticResource>
   0
   <description>Success</description>
   </message>

</platform> </syntaxhighlight>

See also: REST API:Error Codes

Add a Static Resource

Method
POST
URI
https://{yourDomain}/networking/rest/staticResource
Request
A multipart request with file part for the resource and an XML/JSON part with processing information.
Here's a sample request that loads a properties file.
Considerations
  • For a JSON payload, the name would be __json_data__, and the content-type would be application/json.
  • For the full program, see the Sample Http Client below.))
<syntaxhighlight lang="xml" enclose="div">

Content-Type: multipart/form-data; boundary=.............................103832778631715

--.............................103832778631715 Content-Disposition: form-data; name="__xml_data__"; Content-Type=application/xml;

<platform>

   <record>
       <name>JSPsample</name>
       <description>Simple JSP page.</description>
   </record>

</platform>

.............................103832778631715 Content-Disposition: form-data; name="file_part"; filename="message.properties" Content-type: text/plain

Hello World!

.............................103832778631715-- </syntaxhighlight>

Learn more: REST API:Multipart Request
Response
<syntaxhighlight lang="xml" enclose="div">

<platform>

   <message>
       0
       <description>Success</description>
       <id>...</id>   // ID of the added resource
   </message>

</platform> </syntaxhighlight>

See also: REST API:Error Codes

Update a Static Resource

Method
PUT
URI
https://{yourDomain}/networking/rest/staticResource/{resourceId}
Request
The format of the request is identical to that shown in Add a Static Resource.
Response
<syntaxhighlight lang="xml" enclose="div">

<platform>

   <message>
       0
       <description>Success</description>
       <id>...</id>   // ID of the updated resource
   </message>

</platform> </syntaxhighlight>

See also: REST API:Error Codes

Delete a Static Resource

Method
DELETE
URI
https://{yourDomain}/networking/rest/staticResource/{recordId}
Response
<syntaxhighlight lang="xml" enclose="div">

<platform>

   <message>
       0
       <description>Success</description>
   </message>

</platform> </syntaxhighlight>

See also: REST API:Error Codes

Fields

Name Type Attribute Required During Add Description Additional Information
id String Read Only Record Id
name String Read Only Checkmark.gif Static Resource name
description String Editable on Add/Update Description of content
document_id String Read Only
resource_file_name String Read Only
size Integer Read Only
mime_type String Read Only
date_created Data Read Only
created_id Lookup Read Only
date_modified Date Read Only
modified_id Lookup Read Only

Sample Http Client to Upload a Static Resource

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.

Thumbsup.gif

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.