Complete frontend extension sample
This sample shows how to quickly extend OpenKM features with JSP, HTML, CSS and JavaScript technologies using OpenKM Java API and how to use JavaScript functions of the exposed frontend Java methods.
Download the extension sample code complete_extension_sample.zip.
Description
Section titled “Description”The sample is shown in a new Workspace tab.
The main screen is shown as a toolbar with three buttons:
- The first button goes to the main screen of the sample.
- The second button shows Sample 1.
- The third button shows Sample 2.
Project structure:

Installation
Section titled “Installation”- Stop the application.
- Open the contents of the downloaded file into the $TOMCAT_HOME/webapps/openkm folder (or, preferably, into openkm.war)
- Delete $TOMCAT_HOME/work/catalina/localhost
- Start the application.
- Check the URL http://localhost:8080/openkm/sample.
Implementation details
Section titled “Implementation details”index.jsp
Section titled “index.jsp”Index.jsp controls the iframe height with JavaScript.
Another important point is that, for example, after uploading a document (Sample 2), the parameter urlToOpen is used to indicate which URL must be opened.
HttpSessionManager httpSessionManager = ContextWrapper.getContext().getBean(HttpSessionManager.class);OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);WebUtils webUtils = ContextWrapper.getContext().getBean(WebUtils.class);
httpSessionManager.add(request);okmAuth.login();String urlToOpen = URLDecoder.decode(webUtils.getString(request, "urlToOpen"), "UTF-8");if (urlToOpen.equals("")) { urlToOpen = "home.jsp";}menu.jsp
Section titled “menu.jsp”Contains the menu URL definitions.

home.jsp
Section titled “home.jsp”Sets the main home screen.

Sample 1
Section titled “Sample 1”Is composed of 4 files:
- action.jsp (the main controller where the logic is set).
- distributor.jsp (the main layer with two columns at 50%).
- full_list.jsp (shows the full list with no special JavaScript control).
- filtered_list.jsp (shows the filtered list with no special JavaScript control).
action.jsp
Section titled “action.jsp”The main controller.
<%@page import="java.util.Map"%><%@page import="java.util.HashMap"%><%@page import="java.util.List"%><%@page import="java.util.ArrayList"%><%@ page import="java.text.SimpleDateFormat"%><%@ page import="java.io.IOException"%><%@ page import="java.net.URLDecoder"%><%@ page import="org.slf4j.Logger"%><%@ page import="org.slf4j.LoggerFactory"%><%@ page import="com.openkm.bean.Document"%><%@ page import="com.openkm.util.WebUtils"%><%@ page import="com.openkm.bean.CommonUser"%><%@ page import="com.openkm.api.OKMAuth"%><%@ page import="com.openkm.api.OKMRepository"%><%@ page import="com.openkm.api.OKMDocument"%><%@ page import="com.openkm.util.ContextWrapper"%><%@ page extends="com.openkm.extension.servlet.BaseServlet"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%!private static Logger log = LoggerFactory.getLogger("com.openkm.sample");private static SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");private WebUtils webUtils = ContextWrapper.getContext().getBean(WebUtils.class);
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String action = webUtils.getString(request, "action"); try { list(request, response); } catch (Exception e) { log.error(e.getMessage(), e); sendErrorRedirect(request, response, e); }}
// Used by fileupload returnpublic void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String action = webUtils.getString(request, "action"); try { if (action.equals("filter")) { list(request, response); } } catch (Exception e) { log.error(e.getMessage(), e); sendErrorRedirect(request, response, e); }}
private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception { OKMAuth okmAuth = ContextWrapper.getContext().getBean(OKMAuth.class);
String nameFilter = webUtils.getString(request, "nameFilter"); String userFilter = webUtils.getString(request, "userFilter");
// Getting lists List<Map<String, String>> leftDocsMaps = leftList(""); // Pending documents for all users List<Map<String, String>> rightDocsMaps = rightList(userFilter, nameFilter); List<String> users = new ArrayList<String>(); // UserList for (CommonUser user : okmAuth.getUsers(null)) { users.add(user.getId()); }
ServletContext sc = getServletContext(); sc.setAttribute("nameFilter", nameFilter); sc.setAttribute("leftDocsMaps", leftDocsMaps); sc.setAttribute("rightDocsMaps", rightDocsMaps); sc.setAttribute("users", users); sc.setAttribute("nameFilter", nameFilter); sc.setAttribute("userFilter", userFilter); sc.getRequestDispatcher("/sample/sample1/distributor.jsp").forward(request, response);}
private List<Map<String, String>> leftList(String user) throws Exception { OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); OKMRepository okmRepository = ContextWrapper.getContext().getBean(OKMRepository.class); String uuid = okmRepository.getNodeUuid(null, "/okm:root");
List<Map<String, String>> documentsMap = new ArrayList<Map<String, String>>(); // To change List<Document> pendingDocuments = new ArrayList<Document>(); for (Document doc : okmDocument.getChildren(null, uuid)) { Map<String, String> docMap = new HashMap<String, String>(); docMap.put("author", doc.getAuthor()); docMap.put("name", doc.getPath().substring(doc.getPath().lastIndexOf("/") + 1)); docMap.put("uuid", doc.getUuid()); docMap.put("path", doc.getPath()); docMap.put("mimeType", doc.getMimeType()); docMap.put("lastModified", df.format(doc.getLastModified().getTime())); documentsMap.add(docMap); } return documentsMap;}
private List<Map<String, String>> rightList(String user, String nameFilter) throws Exception { OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); OKMRepository okmRepository = ContextWrapper.getContext().getBean(OKMRepository.class); String uuid = okmRepository.getNodeUuid(null, "/okm:root");
List<Map<String, String>> documentsMap = new ArrayList<Map<String, String>>(); for (Document doc : okmDocument.getChildren(null, uuid)) { boolean found = true; if (!user.equals("") && !user.equals(doc.getAuthor())) { found = false; } if (!nameFilter.equals("") && !doc.getPath().contains(nameFilter)) { found = false; }
if (found) { Map<String, String> docMap = new HashMap<String, String>(); docMap.put("author", doc.getAuthor()); docMap.put("name", doc.getPath().substring(doc.getPath().lastIndexOf("/") + 1)); docMap.put("uuid", doc.getUuid()); docMap.put("path", doc.getPath()); docMap.put("mimeType", doc.getMimeType()); docMap.put("lastModified", df.format(doc.getLastModified().getTime())); documentsMap.add(docMap); } } return documentsMap;}%>distributor.jsp
Section titled “distributor.jsp”The main layer.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/sample/css/style.css" /> <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/js/vanadium-min.js" ></script></head><body> <table width="100%"> <tbody> <tr> <td valign="top" width="50%"> <jsp:include page="/sample/sample1/full_list.jsp"></jsp:include> </td> <td valign="top" width="10"></td> <td valign="top" width="50%"> <jsp:include page="/sample/sample1/filtered_list.jsp"></jsp:include> </td> </tr> </tbody> </table></body></html>