Plugin samples

Methods

executePostPlugin

Description:

MethodReturn valuesDescription

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

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 "is" stream allow to uploading a document.

This method execute a REST call at POST.

Example:

package com.openkm;

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

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

public class Test {
    public static void main(String[] args) {
        String host = "http://localhost:8080/OpenKM";
        String username = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);        
        try {
            Map<String, String> parameters = new HashMap<>();
            parameters.put("param1", "value1");
            parameters.put("param2", "value2");
            String value = (String) ws.executePostPlugin("com.openkm.plugin.rest.TestRestPlugin", parameters, String.class, null);
            System.out.println(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

executePostPluginReturnFile

Description:

MethodReturn valuesDescription

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

InputStream

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 java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

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

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openkm.bean.Document;
import com.openkm.integration.cifs.device.PathUtils;
import com.openkm.module.DocumentModule;
import com.openkm.module.ModuleManager;

/**
 * Sample rest plugin
 */
@PluginImplementation
public class TestGetDocumentRestPlugin implements RestPlugin {
	private static Logger log = LoggerFactory.getLogger(TestGetDocumentRestPlugin.class);

	@Override
	public Object executePlugin(Map<String, String> parameters, InputStream is) throws Exception {
		log.debug("executePlugin({})", parameters);
		DocumentModule dm = ModuleManager.getDocumentModule();
		String docId = parameters.get("docId");
		boolean inline = parameters.containsKey("inline");
		Document doc = dm.getProperties(null, docId);
		String mimeType = doc.getMimeType();
		String fileName = PathUtils.getFileNameFromPath(doc.getPath());
		final InputStream isContent = dm.getContent(null, docId, false);
		StreamingOutput stream = new StreamingOutput() {
			@Override
			public void write(OutputStream os) throws IOException, WebApplicationException {
				IOUtils.copy(isContent, os);
				IOUtils.closeQuietly(isContent);
				IOUtils.closeQuietly(os);
			}
		};
		Response.ResponseBuilder response = Response.ok(stream);
		response.type(mimeType); // because the method produces MediaType.APPLICATION_OCTET_STREAM can be set any document mime type
// inline true when you want to embeded the content into if (inline) { response.header("Content-disposition", "inline; filename=\"" + fileName + "\""); } else { response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\""); }
return response; } }

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.OKMWebservices;
import com.openkm.sdk4j.OKMWebservicesFactory;

public class Test {
    public static void main(String[] args) {
        String host = "http://localhost:8080/OpenKM";
        String username = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { Map<String, String> parameters = new HashMap<>(); parameters.put("docId", "/okm:root/invoices/00000001_001_of_001.pdf"); InputStream is = ws.executePostPluginReturnFile("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(); } } }

executeGetPlugin

Description:

MethodReturn valuesDescription

executeGetPlugin(String className, Map<String, String> parameters, Class<?> 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:

package com.openkm;

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

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

public class Test {
    public static void main(String[] args) {
        String host = "http://localhost:8080/OpenKM";
        String username = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { // 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.executeGetPlugin("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

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 java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

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

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openkm.bean.Document;
import com.openkm.integration.cifs.device.PathUtils;
import com.openkm.module.DocumentModule;
import com.openkm.module.ModuleManager;

/**
 * Sample rest plugin
 */
@PluginImplementation
public class TestGetDocumentRestPlugin implements RestPlugin {
	private static Logger log = LoggerFactory.getLogger(TestGetDocumentRestPlugin.class);

	@Override
	public Object executePlugin(Map<String, String> parameters, InputStream is) throws Exception {
		log.debug("executePlugin({})", parameters);
		DocumentModule dm = ModuleManager.getDocumentModule();
		String docId = parameters.get("docId");
		boolean inline = parameters.containsKey("inline");
		Document doc = dm.getProperties(null, docId);
		String mimeType = doc.getMimeType();
		String fileName = PathUtils.getFileNameFromPath(doc.getPath());
		final InputStream isContent = dm.getContent(null, docId, false);
		StreamingOutput stream = new StreamingOutput() {
			@Override
			public void write(OutputStream os) throws IOException, WebApplicationException {
				IOUtils.copy(isContent, os);
				IOUtils.closeQuietly(isContent);
				IOUtils.closeQuietly(os);
			}
		};
Response.ResponseBuilder response = Response.ok(stream); response.type(mimeType); // because the method produces MediaType.APPLICATION_OCTET_STREAM can be set any document mime type
// inline true when you want to embeded the content into if (inline) { response.header("Content-disposition", "inline; filename=\"" + fileName + "\""); } else { response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\""); }
return response; } }

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:

package com.openkm;

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

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

public class Test {
    public static void main(String[] args) {
        String host = "http://localhost:8080/OpenKM";
        String username = "okmAdmin";
        String password = "admin";
        OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { // 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.executeGetPluginReturnFile("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(); } } }