Plugin samples

Basics

Suggested code sample

First, you must create the webservice object:

OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

Then you should log in using the "login" method. You can access the "login" method from the webservice object "ws" as shown below:

ws.login(user, password);

Once you are logged in to the webservice, the session is kept in the webservice object. Then you can use the other API methods.

At this point you can use all the Plugin methods from the "plugin" class as shown below:

ws.plugin.executePluginPost("com.openkm.plugin.rest.TestRestPlugin", parameters, String.class, null);

Methods

executePluginPost

Description:

MethodReturn valuesDescription

executePluginPost(String className, Map<String, String> parameters, Class<?> clazz, InputStream is)

Object

Returns the Object value resulting from the execution of a class which implements RestPlugin.

  • The "className" value must be the canonical class name of a class which implements the RestPlugin interface.
  • The "parameters" map contains values that will be used by the class specified in className.
  • The "clazz" value should be the Class of the returned object. It's used by REST for unmarshalling the result of the query.
  • The "is" stream allows uploading a document.

This method executes a REST call via POST.

Example:

package com.openkm;

import java.util.HashMap;
import java.util.Map;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            Map<String, String> parameters = new HashMap<>();
            parameters.put("param1", "value1");
            parameters.put("param2", "value2");
            String value = (String) ws.plugin.executePluginPost("com.openkm.plugin.rest.TestRestPlugin", parameters, String.class, null);
            System.out.println(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

executePostPluginReturnFile

Description:

MethodReturn valuesDescription

executePluginPostReturnFile(String className, Map<String, String> parameters, InputStream is)

InputStream

Returns an InputStream resulting from the execution of a class which implements RestPlugin.

  • The "className" value must be the canonical class name of a class which implements the RestPlugin interface.
  • The "parameters" map contains values that will be used by the class specified in className.
  • The "is" stream allows uploading a document.

This method executes a REST call via POST.

The RestPlugin must return a Response object. Take a look at the following sample implementation of the RestPlugin:

package com.openkm.plugin.rest;

import net.xeoh.plugins.base.annotations.PluginImplementation;

import java.io.InputStream;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.openkm.bean.Document;
import com.openkm.module.db.DbDocumentModule;
import com.openkm.plugin.BasePlugin;
import com.openkm.util.PathUtils;

/**
 * Sample rest plugin
 *
 * @author jllort
 */
@PluginImplementation
public class TestGetDocumentRestPlugin extends BasePlugin implements RestPlugin {

    private static Logger log = LoggerFactory.getLogger(TestGetDocumentRestPlugin.class);

    @Autowired
    private DbDocumentModule dbDocumentModule;

    @Autowired
    private PathUtils pathUtils;

    @Override
    public Object executePlugin(Map<String, String> parameters, InputStream is) throws Exception {
        log.debug("executePlugin({})", parameters);
        String docId = parameters.get("docId");
        boolean inline = parameters.containsKey("inline");
        Document doc = dbDocumentModule.getProperties(null, docId);
        String mimeType = doc.getMimeType();
        String fileName = pathUtils.getName(doc.getPath());
        InputStream isContent = dbDocumentModule.getContent(null, docId, false);

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", mimeType);

        // inline true when you want to embedded the content into
        if (inline) {
            responseHeaders.add("Content-disposition", "inline; filename=\"" + fileName + "\"");
        } else {
            responseHeaders.add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        }

        responseHeaders.setContentLength(doc.getActualVersion().getSize());
        InputStreamResource inputStreamResource = new InputStreamResource(isContent);
        log.debug("TestGetDocumentRestPlugin: [BINARY]");
        return new ResponseEntity<>(inputStreamResource, responseHeaders, HttpStatus.OK);
    }
}

Example:

package com.openkm;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            Map<String, String> parameters = new HashMap<>();
            parameters.put("docId", "/okm:root/invoices/00000001_001_of_001.pdf");
            InputStream is = ws.plugin.executePluginPostReturnFile("com.openkm.plugin.rest.TestGetDocumentRestPlugin", parameters, null);
            OutputStream fos = new FileOutputStream("/home/user/Desktop/test.pdf");
            IOUtils.copy(is, fos);
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

executePluginGet

Description:

MethodReturn valuesDescription

executePluginGet(String className, Map<String, String> parameters, Class<?> clazz)

Object

Returns the Object value resulting from the execution of a class which implements RestPlugin.

  • The "className" value must be the canonical class name of a class which implements the RestPlugin interface.
  • The "parameters" map contains values that will be used by the class specified in className.
  • The "clazz" value should be the Class of the returned object. It's used by REST for unmarshalling the result of the query.

This method executes a REST call via GET.

You can also execute it directly from the browser; use the following URL as a sample:

http://localhost:8080/OpenKM/services/rest/plugin/executeGetPlugin?className=com.openkm.plugin.rest.TestGetDocumentRestPlugin&param={"key":"docId","value":"/okm:root/invoices/00000001_001_of_001.pdf"} 

Example:

package com.openkm;

import java.util.HashMap;
import java.util.Map;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            // Browser URL
            // http://localhost:8080/OpenKM/services/rest/plugin/executeGetPlugin?className=com.openkm.plugin.rest.TestGetDocumentRestPlugin&param={"key":"docId","value":"/okm:root/invoices/00000001_001_of_001.pdf"}
            Map<String, String> parameters = new HashMap<>();
            parameters.put("docId", "/okm:root/invoices/00000001_001_of_001.pdf");
            parameters.put("docId2", "/okm:root/invoices/00000001_001_of_001.pdf2");
            String value = (String) ws.plugin.executePluginGet("com.openkm.plugin.rest.TestRestPlugin", parameters, String.class);
            System.out.println(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

executePluginAtGetReturnFile

Description:

MethodReturn valuesDescription

executeGetPluginReturnFile(String className, Map<String, String> parameters)

InputStream

Returns the Object value resulting from the execution of a class which implements RestPlugin.

  • The "className" value must be the canonical class name of a class which implements the RestPlugin interface.
  • The "parameters" map contains values that will be used by the class specified in className.

This method executes a REST call via GET.

The RestPlugin must return a Response object. Take a look at the following sample implementation of the RestPlugin:

package com.openkm.plugin.rest;

import net.xeoh.plugins.base.annotations.PluginImplementation;

import java.io.InputStream;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.openkm.bean.Document;
import com.openkm.module.db.DbDocumentModule;
import com.openkm.plugin.BasePlugin;
import com.openkm.util.PathUtils;

/**
 * Sample rest plugin
 *
 * @author jllort
 */
@PluginImplementation
public class TestGetDocumentRestPlugin extends BasePlugin implements RestPlugin {

    private static Logger log = LoggerFactory.getLogger(TestGetDocumentRestPlugin.class);

    @Autowired
    private DbDocumentModule dbDocumentModule;

    @Autowired
    private PathUtils pathUtils;

    @Override
    public Object executePlugin(Map<String, String> parameters, InputStream is) throws Exception {
        log.debug("executePlugin({})", parameters);
        String docId = parameters.get("docId");
        boolean inline = parameters.containsKey("inline");
        Document doc = dbDocumentModule.getProperties(null, docId);
        String mimeType = doc.getMimeType();
        String fileName = pathUtils.getName(doc.getPath());
        InputStream isContent = dbDocumentModule.getContent(null, docId, false);

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", mimeType);

        // inline true when you want to embedded the content into
        if (inline) {
            responseHeaders.add("Content-disposition", "inline; filename=\"" + fileName + "\"");
        } else {
            responseHeaders.add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        }

        responseHeaders.setContentLength(doc.getActualVersion().getSize());
        InputStreamResource inputStreamResource = new InputStreamResource(isContent);
        log.debug("TestGetDocumentRestPlugin: [BINARY]");
        return new ResponseEntity<>(inputStreamResource, responseHeaders, HttpStatus.OK);
    }
}

You can also execute it directly from the browser; use the following URL as a sample:

http://localhost:8080/OpenKM/services/rest/plugin/executeGetPluginReturnFile?className=com.openkm.plugin.rest.TestGetDocumentRestPlugin&param={"key":"docId","value":"/okm:root/invoices/00000001_001_of_001.pdf"} 

Example:

package com.openkm;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;

import com.openkm.sdk4j.OKMWebservicesFactory;
import com.openkm.sdk4j.impl.OKMWebservices;

public class Test {

    public static void main(String[] args) {
        String host = "http://localhost:8080/openkm";
        String user = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.getInstance(host);

        try {
            ws.login(user, password);
            // Browser URL
            // http://localhost:8180/OpenKM/services/rest/plugin/executeGetPluginReturnFile?className=com.openkm.plugin.rest.TestGetDocumentRestPlugin&param={"key":"docId","value":"/okm:root/invoices/00000001_001_of_001.pdf"}
            // http://localhost:8180/OpenKM/services/rest/plugin/executeGetPluginReturnFile?className=com.openkm.plugin.rest.TestGetDocumentRestPlugin&param={"key":"docId","value":"/okm:root/invoices/00000001_001_of_001.pdf"}&param={"key":"inline","value":"true"}
            Map<String, String> parameters = new HashMap<>();
            parameters.put("docId", "/okm:root/invoices/00000001_001_of_001.pdf");
            InputStream is = ws.plugin.executePluginGetReturnFile("com.openkm.plugin.rest.TestGetDocumentRestPlugin", parameters);
            OutputStream fos = new FileOutputStream("/home/jllort/Escritorio/test.pdf");
            IOUtils.copy(is, fos);
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}