Plugin samples

Basics

Suggested code sample

First, you must create the webservice object:

OKMWebservices ws = OKMWebservicesFactory.newInstance(host);

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

ws.login(user, password);

Once you are logged with the webservices the session is keep in the webservice Object. Then you can use the other API method

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

ws.plugin.executePluginPostReturnFile("com.openkm.plugin.rest.TestGetDocumentRestPlugin", parameters, null);

Methods

executePluginPost

Description:

MethodReturn valuesDescription

executePluginPost(String className, Dictionary<String, String> parameters, Type clazz, FileStream fs)

Object

Return the Object value of execution of a class which implements RestPlugin.

  • The "className" value must be the canonical class name of a class which implements RestPlugin interface.
  • The "parameters" map are values what will be used by class set in className.
  • The "clazz" value should be the Class class of the returned object. It's used by REST for unmarshalling the result of the query.
  • The "fs" file stream allow to uploading a document.

This method execute a REST call at POST.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using com.openkm.sdk4csharp.bean;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8180/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);

            try
            {
                ws.login(user, password);
                FileStream fs = new FileStream(@"D:\Testing\pluging.docx", FileMode.Open, FileAccess.Read);
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters.Add("param1", "value1");
parameters.Add("param2", "value2");
String res = (String) ws.plugin.executePluginPost("com.openkm.plugin.rest.TestRestPlugin", parameters, typeof(string), fs);
fs.Close();
System.Console.WriteLine(res); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }

executePluginPostReturnFile

Description:

MethodReturn valuesDescription

executePluginPostReturnFile(String className, Dictionary<String, String> parameters, FileStream fs)

Stream

Return an InputStream of execution of a class which implements RestPlugin.

  • The "className" value must be the canonical class name of a class which implements RestPlugin interface.
  • The "parameters" map are values what will be used by class set in className.
  • The "is" stream allow to uploading a document.

This method execute a REST call at POST.

The RestPlugin must return a Response object. Take a look at the next 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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using com.openkm.sdk4csharp.bean;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8180/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);

            try
            {
                ws.login(user, password);
                FileStream fs = new FileStream(@"D:\Testing\pluging.docx", FileMode.Open, FileAccess.Read);
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters.Add("docId", "/okm:root/test.pdf");
Stream tmpFIle = ws.plugin.executePluginPostReturnFile("com.openkm.plugin.rest.TestGetDocumentRestPlugin", parameters, null);
FileStream destFile = new FileStream(@"D:\Testing\pluging.pdf", FileMode.OpenOrCreate);
tmpFIle.CopyTo(destFile);
destFile.Close();
tmpFIle.Close();
fs.Close(); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }

executePluginGet

Description:

MethodReturn valuesDescription

executePluginGet(String className, Dictionary<String, String> parameters, Type clazz)

Object

Return an InputStream of execution of a class which implements RestPlugin.

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

This method execute a REST call at GET.

You can also execute directly from the browser, take the next 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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using com.openkm.sdk4csharp.bean;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8180/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);

            try
            {
                ws.login(user, password);
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters.Add("docId", "/okm:root/invoices/00000001_001_of_001.pdf");
String res = (String) ws.plugin.executePluginGet("com.openkm.plugin.rest.TestRestPlugin", parameters, typeof(string));
System.Console.WriteLine(res); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }

executePluginGetReturnFile

Description:

MethodReturn valuesDescription

executePluginGetReturnFile(String className, Dictionary<String, String> parameters)

InputStream

Return the Object value of execution of a class which implements RestPlugin.

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

This method execute a REST call at GET.

The RestPlugin must return a Response object. Take a look at the next 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 directly from the browser, take the next 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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.openkm.sdk4csharp;
using com.openkm.sdk4csharp.bean;

namespace OKMRest
{
    public class Program
    {
        static void Main(string[] args)
        {
            String host = "http://localhost:8180/openkm";
            String username = "okmAdmin";
            String password = "admin";
            OKMWebservice ws = OKMWebservicesFactory.newInstance(host);

            try
            {
                ws.login(user, password);
                Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters.Add("docId", "/okm:root/test.pdf");
Stream tmpFIle = ws.plugin.executePluginGetReturnFile("com.openkm.plugin.rest.TestGetDocumentRestPlugin", parameters);
FileStream destFile = new FileStream(@"D:\Testing\pluging.pdf", FileMode.OpenOrCreate);
tmpFIle.CopyTo(destFile);
destFile.Close();
tmpFIle.Close(); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } } }