Watermarking documents on edit offline click in Alfresco

cancel
Showing results for 
Search instead for 
Did you mean: 
rs2019
Member II

Watermarking documents on edit offline click in Alfresco

I am trying to watermark a MS word document on edit offline action in Alfresco.
So before document gets downloaded it is watermarked.

I have created java backed web script to achieve the same:
I am trying to override edit offline functionality of Alfresco.
Below is my custom edit offline JavaScript which will call the watermark java class
Unable to build connection between custom Edit offline JavaScript to java class watermark
content is not getting write on the file in temp.

--------------------------------------------------------------------------------------------------------
1. From share-config-custom.xml I am calling my custom edit offline script

<config evaluator="string-compare" condition="DocLibActions">
<actions>
<action id="document-edit-offline" type="javascript" label="actions.document.edit-offline">
<param name="function">onActionCustomEditOffline</param>
<permissions>
<permission allow="true">Write</permission>
</permissions>
<evaluator>evaluator.doclib.action.offlineEdit</evaluator>
<evaluator>evaluator.doclib.action.hasContent</evaluator>
<evaluator negate="true">evaluator.doclib.action.notEditable</evaluator>
</action>
</actions>
</config>
<config>
<forms>
<dependencies>
<js src="/acme-cms-poc-share-jar/js/action/customEditOfflineAction.js"/>
</dependencies>
</forms>
</config>

---------------------------------------------------------------------------

2. CustomEditOffline.js

(function(){
YAHOO.Bubbling.fire('registerAction', {
actionName: 'onActionCustomEditOffline',
fn: function(asset) {
alert("Custom Action called");
var displayName = asset.displayName,
nodeRef = new Alfresco.util.NodeRef(asset.nodeRef);
var str = nodeRef.nodeRef.toString().trim();
var data1 = str.substr(24, str.length-24);
var json =data1;
var req = new XMLHttpRequest();
var params = 'nodeRef='+str;
req.open("POST", Alfresco.constants.PROXY_URI + "watermark/post", true);
req.setRequestHeader('Content-Type', 'application/json');
req.responseType = "blob";
req.onload = function(e) {
if (this.status === 200) {
/*
var blob = this.response;
var filename = "test.doc";
var downloadLink = window.document.createElement('a');
var contentTypeHeader = req.getResponseHeader("Content-Type");
downloadLink.href = window.URL.createObjectURL(new Blob([blob], { type: contentTypeHeader }));
downloadLink.download = filename;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
*/
var resp = this.response;
console.log(resp);
}
else{
console.log("FAILED");
}
};
req.send(params);
}
});
})();

---------------------------------------------------------------------------------------

3. For post request watermark.post.desc.xml

<webscript>
<shortname>WaterMark</shortname>
<description>Watermark documents on checkout</description>
<url>/watermark/post</url>
<!--<format default="html"></format>-->
<authentication>user</authentication>
<family>POC</family>
</webscript>

-----------------------------------------------------------------------------------------------
4. Context file to initialize the bean

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<!-- Spring bean -->
<bean id="webscript.alfresco.com.watermark.post" class="com.watermark.watermark"
parent="webscript">
<property name="repository" ref="repositoryHelper" />
<property name="serviceRegistry" ref="ServiceRegistry" />
<property name="nodeService" ref="NodeService" />
<property name="transactionService" ref="TransactionService" />
<property name="contentService" ref="ContentService" />
</bean>
</beans>
---------------------------------------------------------------------------------------
5. 5. Java class
//input should come node ref
package com.watermark;
import com.acme.cmspoc.platformsample.HelloWorldWebScript;
import com.ibm.wsdl.extensions.mime.MIMEConstants;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.model.Repository;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.cmr.repository.*;
import org.alfresco.service.cmr.security.AccessPermission;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.extensions.webscripts.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class watermark extends DeclarativeWebScript {
private Repository repository;private static Log logger = LogFactory.getLog(watermark.class);
private NodeService nodeService;
public void setRepository(Repository repository) {
this.repository = repository;
}
/** The content service. */
private ContentService contentService;
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
NodeRef myNoderef = null;
logger.info("before try catch to get...");
try {
// system.out.println("attempting to get..." + req.getContent().getContent());
logger.info("attempting to get..." + req.getContent().getContent());
//to get the documents in the local drive
// injecting content service bean in custom-action-services-context.xml
myNoderef = new NodeRef("workspace","SpacesStore",req.getContent().getContent());
logger.error(myNoderef);
logger.error(req);
// logger.error("NoderRef : "+myNoderef.getId());
ContentReader contentReader = contentService.getReader(myNoderef, ContentModel.PROP_CONTENT);
InputStream is = contentReader.getContentInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(is));
ContentWriter writer = contentService.getWriter(myNoderef, ContentModel.PROP_CONTENT, true);
//writer.setLocale(CONTENT_LOCALE);
File file = new File("D:\\watermarkcontent\\temp.doc");
writer.setMimetype("text/plain");
writer.putContent(file);
logger.debug("file written");

} catch (IOException e) {
e.printStackTrace();
}
// construct model for response template to render
Map<String, Object> model = new HashMap<String, Object>();
model.put("folder", "test");
model.put("nodeRef", "");
model.put("message", "success!");
logger.debug("returning data to ui.");
return model;
}
private NodeRef getDocumentNode(String guid){
NodeRef ref = new NodeRef(guid);
return new NodeRef("workspace","SpacesStore",guid);
}
/**
* Create NodeRef instance from a WebScriptRequest parameter.
*
* @param req the req
* @param paramName the param name
* @return the parameter as node ref
*/
private NodeRef getParameterAsNodeRef(final WebScriptRequest req, final String paramName) {
final String nodeRefStr = StringUtils.trimToNull(req.getParameter(paramName));
if (StringUtils.isBlank(nodeRefStr)) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing " + paramName + " parameter");
}
if (!NodeRef.isNodeRef(nodeRefStr)) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Incorrect format for " + paramName + " paramater");
}
final NodeRef nodeRef = new NodeRef(nodeRefStr);
if (!nodeService.exists(nodeRef)) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, paramName + " not found");
}
return nodeRef;
}
/**
* Sets the content service.
*
* @param contentService the content service
*/
public void setContentService(final ContentService contentService) {
this.contentService = contentService;
}
public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
public void setTransactionService(String transactionService) {
}
public void setServiceRegistry(String serviceRegistry) {
}
}
-----------------------------------------------------------------------------------
Error: 6. Unable to build connection between custom Edit offline javascript to java class watermark
content is not getting write on the file in temp.
Error: Exception from executeScript: 08130002 The content node was not specified so the content cannot be streamed to the client: classpath*:alfresco/templates/webscripts/org/alfresco/repository/thumbnail/thumbnail.get.js
org.springframework.extensions.webscripts.WebScriptException: 08130002 The content node was not specified so the content cannot be streamed to the client: classpath*:alfresco/templates/webscripts/org/alfresco/repository/thumbnail/thumbnail.get.js
at org.alfresco.repo.web.scripts.content.StreamContent.execute(StreamContent.java:176)
at org.alfresco.repo.web.scripts.RepositoryContainer$3.execute(RepositoryContainer.java:512)
at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:464)
at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:587)
at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:656)
at org.alfresco.repo.web.scripts.RepositoryContainer.executeScriptInternal(RepositoryContainer.java:428)
at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:308)
at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:399)
at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:210)
at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:132)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:68)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

1 Reply
jpotts
Advanced II

Re: Watermarking documents on edit offline click in Alfresco

What do you intend by this line:

myNoderef = new NodeRef("workspace","SpacesStore",req.getContent().getContent());

It looks like you want to create an instance of a NodeRef, but the constructor expects an ID, not content.