+

Search Tips   |   Advanced Search


REST service examples

See examples of different service requests that work with composite applications resources.


Get favorites

try {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(
        new AuthScope(HOSTNAME, PORT,AuthScope.ANY_REALM),
        new UsernamePasswordCredentials(USERNAME, PASSWORD));
    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    client.setParams(params);
    //application favorites entry point - context 'wps' may be different on your system
    HttpMethod httpMethod = new GetMethod("http://" + HOSTNAME + ":" + PORT + "/wps/mycontenthandler/ai/application-favorites");
    httpMethod.setDoAuthentication(true);
    client.executeMethod(httpMethod);
                
    System.out.println("Status code: " + httpMethod.getStatusCode());
    Header contentTypeHeader = httpMethod.getResponseHeader("content-type");
    System.out.println("Mimetype: " + contentTypeHeader.getValue());
    System.out.println("Response: " + httpMethod.getResponseBodyAsString());
    httpMethod.releaseConnection();        } catch (org.apache.commons.httpclient.HttpException he){
    he.printStackTrace();} catch (IOException ioe){
    ioe.printStackTrace();} 


Get template by filtering template

try {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(
        new AuthScope(HOSTNAME, PORT,AuthScope.ANY_REALM),
        new UsernamePasswordCredentials(USERNAME, PASSWORD));
    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    client.setParams(params);
    //template entry point - context 'wps' may be different on your system
    //added URL encoded filtering parameter: 'filer=title=Portal Blank Template'
    String url = "http://" + HOSTNAME + ":" + PORT + "/wps/mycontenthandler/ai/templates?filter=title%3DPortal%20Blank%20Template";
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.setDoAuthentication(true);
    client.executeMethod(httpMethod);
                
    System.out.println("Status code: " + httpMethod.getStatusCode());
    Header contentTypeHeader = httpMethod.getResponseHeader("content-type");
    System.out.println("Mimetype: " + contentTypeHeader.getValue());
    System.out.println("Response: " + httpMethod.getResponseBodyAsString());
    httpMethod.releaseConnection();            } catch (org.apache.commons.httpclient.HttpException he){
    he.printStackTrace();} catch (IOException ioe){
    ioe.printStackTrace();} 


Export template as ZIP file

try {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(
        new AuthScope(HOSTNAME, PORT,AuthScope.ANY_REALM),
        new UsernamePasswordCredentials(USERNAME, PASSWORD));
    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    client.setParams(params);
    //export URL retrieved from application's feed - do not rely on the URL syntax in this example, yours may be different, retrieve it yourself
    String url = "http://" + HOSTNAME + ":" + PORT + "/wps/mycontenthandler/!ut/p/ai/applications/1F_AESU3F540GQL00IOCR30NG08T7?mime-type=application%2fzip";
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.setDoAuthentication(true);
    client.executeMethod(httpMethod);
                
    System.out.println("Status code: " + httpMethod.getStatusCode());
    Header contentTypeHeader = httpMethod.getResponseHeader("content-type");
    System.out.println("Mimetype: " + contentTypeHeader.getValue());
                
    File file = new File("exportedTemplate.zip");
    FileOutputStream out = new FileOutputStream(file, false);
    InputStream in = httpMethod.getResponseBodyAsStream();
    int character;
    while ((character=in.read()) != -1) {
        out.write((char)character);
    }
    in.close();
    out.close();
    httpMethod.releaseConnection();        } catch (org.apache.commons.httpclient.HttpException he){
    he.printStackTrace();} catch (IOException ioe){
    ioe.printStackTrace();}


Create template by uploading ZIP file

try {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(
        new AuthScope(HOSTNAME, PORT,AuthScope.ANY_REALM),
        new UsernamePasswordCredentials(USERNAME, PASSWORD));
    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    client.setParams(params);
    //template entry point - context 'wps' may be different on your system
    PostMethod postMethod = new PostMethod("http://" + HOSTNAME + ":" + PORT + "/wps/mycontenthandler/ai/templates");
    postMethod.setDoAuthentication(true);
    Header mtHeader = new Header();
    mtHeader.setName("content-type");
    mtHeader.setValue("application/zip");
    postMethod.addRequestHeader(mtHeader);
                
    //inside this ZIP there is the template like you get when exporting one
    String path = "/doc/createTemplate-RequestBody.zip";
    File file = new File(java.net.URI.create(CodeExamples.class.getResource(path).toString()));
    long length = file.length();
    byte[] bytes = new byte[(int)length];
    int offset = 0;
    int numRead = 0;
    InputStream is = DocumentationUtil.class.getResourceAsStream(path);
    while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }
    is.close();
                
    RequestEntity re = new ByteArrayRequestEntity(bytes, "application/zip");
    postMethod.setRequestEntity(re);
    postMethod.setDoAuthentication(true);
    client.executeMethod(postMethod);
                
    System.out.println("Status code: " + postMethod.getStatusCode());
    Header contentTypeHeader = postMethod.getResponseHeader("content-type");
    System.out.println("Mimetype: " + contentTypeHeader.getValue());
    System.out.println("Response: " + postMethod.getResponseBodyAsString());
    postMethod.releaseConnection();              } catch (org.apache.commons.httpclient.HttpException he){
    he.printStackTrace();} catch (IOException ioe){
    ioe.printStackTrace();}


Create template by uploading ZIP in multipart request

try {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(
        new AuthScope(HOSTNAME, PORT,AuthScope.ANY_REALM),
        new UsernamePasswordCredentials(USERNAME, PASSWORD));
    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    client.setParams(params);
    //template entry point - context 'wps' may be different on your system
    PostMethod postMethod = new PostMethod("http://" + HOSTNAME + ":" + PORT + "/wps/mycontenthandler/ai/templates");
    postMethod.setDoAuthentication(true);
    //do not set mimetype header as this will be done automatically and a separation string is inserted into it
                
    //inside this ZIP there is the template like you get when exporting one
    String path = "/doc/createTemplate-RequestBody.zip";
    File file = new File(java.net.URI.create(CodeExamples.class.getResource(path).toString()));
    long length = file.length();
    byte[] bytes = new byte[(int)length];
    int offset = 0;
    int numRead = 0;
    InputStream is = CodeExamples.class.getResourceAsStream(path);
    while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }
    is.close();

    Part[] parts = new Part[1];
    FilePart part = new FilePart("inputFile", new ByteArrayPartSource("inputFile", bytes));
    parts[0] = part;
    RequestEntity re = new MultipartRequestEntity(parts, postMethod.getParams());

    postMethod.setRequestEntity(re);
    postMethod.setDoAuthentication(true);
    client.executeMethod(postMethod);

    System.out.println("Status code: " + postMethod.getStatusCode());
    Header contentTypeHeader = postMethod.getResponseHeader("content-type");
    System.out.println("Mimetype: " + contentTypeHeader.getValue());
    System.out.println("Response: " + postMethod.getResponseBodyAsString());
    postMethod.releaseConnection();              } catch (org.apache.commons.httpclient.HttpException he){
    he.printStackTrace();} catch (IOException ioe){
    ioe.printStackTrace();}


Parent topic:

Composite Applications REST services