package org.jbpm.integration.cmis.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.commons.io.IOUtils;
import org.drools.core.common.DroolsObjectInputStream;
import org.jbpm.document.Document;
import org.jbpm.integration.cmis.UpdateMode;
import org.kie.api.marshalling.ObjectMarshallingStrategy;
public class OpenCMISPlaceholderResolverStrategy extends OpenCMISSupport implements ObjectMarshallingStrategy {
private String user;
private String password;
private String url;
private String repository;
private String contentUrl;
private UpdateMode mode = UpdateMode.OVERRIDE;
public OpenCMISPlaceholderResolverStrategy(String user, String password, String url, String repository) {
this.user = user;
this.password = password;
this.url = url;
this.repository = repository;
}
public OpenCMISPlaceholderResolverStrategy(String user, String password, String url, String repository, UpdateMode mode) {
this.user = user;
this.password = password;
this.url = url;
this.repository = repository;
this.mode = mode;
}
public OpenCMISPlaceholderResolverStrategy(String user, String password, String url, String repository, String contentUrl) {
this.user = user;
this.password = password;
this.url = url;
this.repository = repository;
this.contentUrl = contentUrl;
}
public OpenCMISPlaceholderResolverStrategy(String user, String password, String url, String repository, String contentUrl, UpdateMode mode) {
this.user = user;
this.password = password;
this.url = url;
this.repository = repository;
this.contentUrl = contentUrl;
this.mode = mode;
}
public boolean accept(Object object) {
if (object instanceof Document) {
return true;
}
return false;
}
public byte[] marshal(Context context, ObjectOutputStream os, Object object) throws IOException {
Document document = (Document) object;
Session session = getRepositorySession(user, password, url, repository);
try {
if (document.getContent() != null) {
String type = getType(document);
if (document.getIdentifier() == null || document.getIdentifier().isEmpty()) {
String location = getLocation(document);
Folder parent = findFolderForPath(session, location);
if (parent == null) {
parent = createFolder(session, null, location);
}
org.apache.chemistry.opencmis.client.api.Document doc = createDocument(session, parent, document.getName(), type, document.getContent());
document.setIdentifier(doc.getId());
document.addAttribute("updated", "true");
} else {
if (document.getContent() != null && "true".equals(document.getAttribute("updated"))) {
org.apache.chemistry.opencmis.client.api.Document doc = updateDocument(session, document.getIdentifier(), type, document.getContent(), mode);
document.setIdentifier(doc.getId());
document.addAttribute("updated", "false");
}
}
}
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( buff );
oos.writeUTF(document.getIdentifier());
oos.writeUTF(object.getClass().getCanonicalName());
oos.close();
return buff.toByteArray();
} finally {
session.clear();
}
}
public Object unmarshal(Context context, ObjectInputStream ois, byte[] object, ClassLoader classloader) throws IOException, ClassNotFoundException {
DroolsObjectInputStream is = new DroolsObjectInputStream( new ByteArrayInputStream( object ), classloader );
String objectId = is.readUTF();
String canonicalName = is.readUTF();
Session session = getRepositorySession(user, password, url, repository);
try {
org.apache.chemistry.opencmis.client.api.Document doc = (org.apache.chemistry.opencmis.client.api.Document) findObjectForId(session, objectId);
Document document = (Document) Class.forName(canonicalName).newInstance();
document.setAttributes(new HashMap<String, String>());
document.setIdentifier(objectId);
document.setName(doc.getName());
document.setLastModified(doc.getLastModificationDate().getTime());
document.setSize(doc.getContentStreamLength());
document.addAttribute("location", getFolderName(doc.getParents()) + getPathAsString(doc.getPaths()));
if (doc.getContentStream() != null && contentUrl == null) {
ContentStream stream = doc.getContentStream();
document.setContent(IOUtils.toByteArray(stream.getStream()));
document.addAttribute("updated", "false");
document.addAttribute("type", stream.getMimeType());
} else {
document.setLink(contentUrl + document.getIdentifier());
}
return document;
} catch(Exception e) {
throw new RuntimeException("Cannot read document from CMIS", e);
} finally {
is.close();
session.clear();
}
}
public Context createContext() {
return null;
}
// For backward compatibility with previous serialization mechanism
public void write(ObjectOutputStream os, Object object) throws IOException {
Document document = (Document) object;
Session session = getRepositorySession(user, password, url, repository);
try {
if (document.getContent() != null) {
String type = document.getAttribute("type");
if (document.getIdentifier() == null) {
String location = document.getAttribute("location");
Folder parent = findFolderForPath(session, location);
if (parent == null) {
parent = createFolder(session, null, location);
}
org.apache.chemistry.opencmis.client.api.Document doc = createDocument(session, parent, document.getName(), type, document.getContent());
document.setIdentifier(doc.getId());
document.addAttribute("updated", "false");
} else {
if (document.getContent() != null && "true".equals(document.getAttribute("updated"))) {
org.apache.chemistry.opencmis.client.api.Document doc = updateDocument(session, document.getIdentifier(), type, document.getContent(), mode);
document.setIdentifier(doc.getId());
document.addAttribute("updated", "false");
}
}
}
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( buff );
oos.writeUTF(document.getIdentifier());
oos.writeUTF(object.getClass().getCanonicalName());
oos.close();
} finally {
session.clear();
}
}
public Object read(ObjectInputStream os) throws IOException, ClassNotFoundException {
String objectId = os.readUTF();
String canonicalName = os.readUTF();
Session session = getRepositorySession(user, password, url, repository);
try {
org.apache.chemistry.opencmis.client.api.Document doc = (org.apache.chemistry.opencmis.client.api.Document) findObjectForId(session, objectId);
Document document = (Document) Class.forName(canonicalName).newInstance();
document.setIdentifier(objectId);
document.setName(doc.getName());
document.addAttribute("location", getFolderName(doc.getParents()) + getPathAsString(doc.getPaths()));
if (doc.getContentStream() != null) {
ContentStream stream = doc.getContentStream();
document.setContent(IOUtils.toByteArray(stream.getStream()));
document.addAttribute("updated", "false");
document.addAttribute("type", stream.getMimeType());
}
return document;
} catch(Exception e) {
throw new RuntimeException("Cannot read document from CMIS", e);
} finally {
session.clear();
}
}
}
package org.jbpm.integration.cmis.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.commons.io.IOUtils;
import org.drools.core.common.DroolsObjectInputStream;
import org.jbpm.document.Document;
import org.jbpm.integration.cmis.UpdateMode;
import org.kie.api.marshalling.ObjectMarshallingStrategy;
public class OpenCMISPlaceholderResolverStrategy extends OpenCMISSupport implements ObjectMarshallingStrategy {
private String user;
private String password;
private String url;
private String repository;
private String contentUrl;
private UpdateMode mode = UpdateMode.OVERRIDE;
public OpenCMISPlaceholderResolverStrategy(String user, String password, String url, String repository) {
this.user = user;
this.password = password;
this.url = url;
this.repository = repository;
}
public OpenCMISPlaceholderResolverStrategy(String user, String password, String url, String repository, UpdateMode mode) {
this.user = user;
this.password = password;
this.url = url;
this.repository = repository;
this.mode = mode;
}
public OpenCMISPlaceholderResolverStrategy(String user, String password, String url, String repository, String contentUrl) {
this.user = user;
this.password = password;
this.url = url;
this.repository = repository;
this.contentUrl = contentUrl;
}
public OpenCMISPlaceholderResolverStrategy(String user, String password, String url, String repository, String contentUrl, UpdateMode mode) {
this.user = user;
this.password = password;
this.url = url;
this.repository = repository;
this.contentUrl = contentUrl;
this.mode = mode;
}
public boolean accept(Object object) {
if (object instanceof Document) {
return true;
}
return false;
}
public byte[] marshal(Context context, ObjectOutputStream os, Object object) throws IOException {
Document document = (Document) object;
Session session = getRepositorySession(user, password, url, repository);
try {
if (document.getContent() != null) {
String type = getType(document);
if (document.getIdentifier() == null || document.getIdentifier().isEmpty()) {
String location = getLocation(document);
Folder parent = findFolderForPath(session, location);
if (parent == null) {
parent = createFolder(session, null, location);
}
org.apache.chemistry.opencmis.client.api.Document doc = createDocument(session, parent, document.getName(), type, document.getContent());
document.setIdentifier(doc.getId());
document.addAttribute("updated", "true");
} else {
if (document.getContent() != null && "true".equals(document.getAttribute("updated"))) {
org.apache.chemistry.opencmis.client.api.Document doc = updateDocument(session, document.getIdentifier(), type, document.getContent(), mode);
document.setIdentifier(doc.getId());
document.addAttribute("updated", "false");
}
}
}
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( buff );
oos.writeUTF(document.getIdentifier());
oos.writeUTF(object.getClass().getCanonicalName());
oos.close();
return buff.toByteArray();
} finally {
session.clear();
}
}
public Object unmarshal(Context context, ObjectInputStream ois, byte[] object, ClassLoader classloader) throws IOException, ClassNotFoundException {
DroolsObjectInputStream is = new DroolsObjectInputStream( new ByteArrayInputStream( object ), classloader );
String objectId = is.readUTF();
String canonicalName = is.readUTF();
Session session = getRepositorySession(user, password, url, repository);
try {
org.apache.chemistry.opencmis.client.api.Document doc = (org.apache.chemistry.opencmis.client.api.Document) findObjectForId(session, objectId);
Document document = (Document) Class.forName(canonicalName).newInstance();
document.setAttributes(new HashMap<String, String>());
document.setIdentifier(objectId);
document.setName(doc.getName());
document.setLastModified(doc.getLastModificationDate().getTime());
document.setSize(doc.getContentStreamLength());
document.addAttribute("location", getFolderName(doc.getParents()) + getPathAsString(doc.getPaths()));
if (doc.getContentStream() != null && contentUrl == null) {
ContentStream stream = doc.getContentStream();
document.setContent(IOUtils.toByteArray(stream.getStream()));
document.addAttribute("updated", "false");
document.addAttribute("type", stream.getMimeType());
} else {
document.setLink(contentUrl + document.getIdentifier());
}
return document;
} catch(Exception e) {
throw new RuntimeException("Cannot read document from CMIS", e);
} finally {
is.close();
session.clear();
}
}
public Context createContext() {
return null;
}
// For backward compatibility with previous serialization mechanism
public void write(ObjectOutputStream os, Object object) throws IOException {
Document document = (Document) object;
Session session = getRepositorySession(user, password, url, repository);
try {
if (document.getContent() != null) {
String type = document.getAttribute("type");
if (document.getIdentifier() == null) {
String location = document.getAttribute("location");
Folder parent = findFolderForPath(session, location);
if (parent == null) {
parent = createFolder(session, null, location);
}
org.apache.chemistry.opencmis.client.api.Document doc = createDocument(session, parent, document.getName(), type, document.getContent());
document.setIdentifier(doc.getId());
document.addAttribute("updated", "false");
} else {
if (document.getContent() != null && "true".equals(document.getAttribute("updated"))) {
org.apache.chemistry.opencmis.client.api.Document doc = updateDocument(session, document.getIdentifier(), type, document.getContent(), mode);
document.setIdentifier(doc.getId());
document.addAttribute("updated", "false");
}
}
}
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( buff );
oos.writeUTF(document.getIdentifier());
oos.writeUTF(object.getClass().getCanonicalName());
oos.close();
} finally {
session.clear();
}
}
public Object read(ObjectInputStream os) throws IOException, ClassNotFoundException {
String objectId = os.readUTF();
String canonicalName = os.readUTF();
Session session = getRepositorySession(user, password, url, repository);
try {
org.apache.chemistry.opencmis.client.api.Document doc = (org.apache.chemistry.opencmis.client.api.Document) findObjectForId(session, objectId);
Document document = (Document) Class.forName(canonicalName).newInstance();
document.setIdentifier(objectId);
document.setName(doc.getName());
document.addAttribute("location", getFolderName(doc.getParents()) + getPathAsString(doc.getPaths()));
if (doc.getContentStream() != null) {
ContentStream stream = doc.getContentStream();
document.setContent(IOUtils.toByteArray(stream.getStream()));
document.addAttribute("updated", "false");
document.addAttribute("type", stream.getMimeType());
}
return document;
} catch(Exception e) {
throw new RuntimeException("Cannot read document from CMIS", e);
} finally {
session.clear();
}
}
}