mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Exported some Repo functionality via RMI. First (still broken) Repo
based CLT. Some cleanup and modification to other CLTs. WIP. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4500 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
281
source/java/org/alfresco/repo/remote/RepoRemoteService.java
Normal file
281
source/java/org/alfresco/repo/remote/RepoRemoteService.java
Normal file
@@ -0,0 +1,281 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.remote;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.service.cmr.model.FileFolderService;
|
||||
import org.alfresco.service.cmr.model.FileInfo;
|
||||
import org.alfresco.service.cmr.model.FileNotFoundException;
|
||||
import org.alfresco.service.cmr.remote.RepoRemote;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.ContentWriter;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* Server side implementation of RepoRemote.
|
||||
* @author britt
|
||||
*/
|
||||
public class RepoRemoteService implements RepoRemote
|
||||
{
|
||||
private static Logger fgLogger = Logger.getLogger(RepoRemoteService.class);
|
||||
|
||||
/**
|
||||
* The NodeService instance.
|
||||
*/
|
||||
private NodeService fNodeService;
|
||||
|
||||
/**
|
||||
* The ContentService instance.
|
||||
*/
|
||||
private ContentService fContentService;
|
||||
|
||||
/**
|
||||
* The FileFolderService instance.
|
||||
*/
|
||||
private FileFolderService fFileFolderService;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public RepoRemoteService()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the NodeService instance.
|
||||
*/
|
||||
public void setNodeService(NodeService service)
|
||||
{
|
||||
fNodeService = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the ContentService instance.
|
||||
*/
|
||||
public void setContentService(ContentService service)
|
||||
{
|
||||
fContentService = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the FileFolderService instance.
|
||||
*/
|
||||
public void setFileFolderService(FileFolderService service)
|
||||
{
|
||||
fFileFolderService = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Path splitting utility.
|
||||
* @param path The path.
|
||||
* @return A List of components.
|
||||
*/
|
||||
private List<String> splitPath(String path)
|
||||
{
|
||||
String [] pathComponents = path.split("/+");
|
||||
List<String> pathList = new ArrayList<String>(pathComponents.length);
|
||||
for (String comp : pathComponents)
|
||||
{
|
||||
pathList.add(comp);
|
||||
}
|
||||
return pathList;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#createDirectory(org.alfresco.service.cmr.repository.NodeRef, java.lang.String)
|
||||
*/
|
||||
public NodeRef createDirectory(NodeRef base, String path)
|
||||
{
|
||||
Pair<NodeRef, String> parentChild = getParentChildRelative(base, path);
|
||||
FileInfo created = fFileFolderService.create(parentChild.getFirst(),
|
||||
parentChild.getSecond(),
|
||||
ContentModel.TYPE_FOLDER);
|
||||
return created.getNodeRef();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#createFile(org.alfresco.service.cmr.repository.NodeRef, java.lang.String)
|
||||
*/
|
||||
public OutputStream createFile(NodeRef base, String path)
|
||||
{
|
||||
Pair<NodeRef, String> parentChild = getParentChildRelative(base, path);
|
||||
FileInfo info = fFileFolderService.create(parentChild.getFirst(),
|
||||
parentChild.getSecond(),
|
||||
ContentModel.TYPE_CONTENT);
|
||||
// TODO is update supposed to be true.
|
||||
ContentWriter writer = fContentService.getWriter(info.getNodeRef(), ContentModel.PROP_CONTENT, true);
|
||||
return writer.getContentOutputStream();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#getListing(org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public Map<String, NodeRef> getListing(NodeRef dir)
|
||||
{
|
||||
List<FileInfo> list = fFileFolderService.listFiles(dir);
|
||||
Map<String, NodeRef> result = new TreeMap<String, NodeRef>();
|
||||
for (FileInfo info : list)
|
||||
{
|
||||
result.put(info.getName(), info.getNodeRef());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#getRoot()
|
||||
*/
|
||||
public NodeRef getRoot()
|
||||
{
|
||||
NodeRef storeRoot = fNodeService.getRootNode(new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"));
|
||||
List<ChildAssociationRef> listing = fNodeService.getChildAssocs(storeRoot);
|
||||
for (ChildAssociationRef child : listing)
|
||||
{
|
||||
fgLogger.error(child.getQName().getLocalName());
|
||||
if (child.getQName().getLocalName().equals("company_home"))
|
||||
{
|
||||
return child.getChildRef();
|
||||
}
|
||||
}
|
||||
throw new AlfrescoRuntimeException("Root Not Found!");
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#lookup(org.alfresco.service.cmr.repository.NodeRef, java.lang.String)
|
||||
*/
|
||||
public NodeRef lookup(NodeRef base, String path)
|
||||
{
|
||||
List<String> pathList = splitPath(path);
|
||||
if (pathList.size() == 1)
|
||||
{
|
||||
return fNodeService.getChildByName(base, ContentModel.ASSOC_CONTAINS, pathList.get(0));
|
||||
}
|
||||
try
|
||||
{
|
||||
FileInfo info = fFileFolderService.resolveNamePath(base, pathList);
|
||||
return info.getNodeRef();
|
||||
}
|
||||
catch (FileNotFoundException e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#readFile(org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public InputStream readFile(NodeRef fileRef)
|
||||
{
|
||||
return fContentService.getReader(fileRef, ContentModel.PROP_CONTENT).getContentInputStream();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#readFile(org.alfresco.service.cmr.repository.NodeRef, java.lang.String)
|
||||
*/
|
||||
public InputStream readFile(NodeRef base, String path)
|
||||
{
|
||||
NodeRef fileRef = lookup(base, path);
|
||||
if (fileRef == null)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Not Found: " + path);
|
||||
}
|
||||
return fContentService.getReader(fileRef, ContentModel.PROP_CONTENT).getContentInputStream();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#removeNode(org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public void removeNode(NodeRef toRemove)
|
||||
{
|
||||
fNodeService.deleteNode(toRemove);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#removeNode(org.alfresco.service.cmr.repository.NodeRef, java.lang.String)
|
||||
*/
|
||||
public void removeNode(NodeRef base, String path)
|
||||
{
|
||||
NodeRef toRemove = lookup(base, path);
|
||||
if (toRemove == null)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Not Found: " + path);
|
||||
}
|
||||
fNodeService.deleteNode(toRemove);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#rename(org.alfresco.service.cmr.repository.NodeRef, java.lang.String, java.lang.String)
|
||||
*/
|
||||
public void rename(NodeRef base, String src, String dst)
|
||||
{
|
||||
NodeRef srcRef = lookup(base, src);
|
||||
if (srcRef == null)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Not Found: " + src);
|
||||
}
|
||||
Pair<NodeRef, String> parentChild = getParentChildRelative(base, dst);
|
||||
try
|
||||
{
|
||||
fFileFolderService.move(srcRef, parentChild.getFirst(), parentChild.getSecond());
|
||||
}
|
||||
catch (FileNotFoundException e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Parent Not Found: " + dst, e);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.remote.RepoRemote#writeFile(org.alfresco.service.cmr.repository.NodeRef, java.lang.String)
|
||||
*/
|
||||
public OutputStream writeFile(NodeRef base, String path)
|
||||
{
|
||||
NodeRef target = lookup(base, path);
|
||||
return fContentService.getWriter(target, ContentModel.PROP_CONTENT, true).getContentOutputStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility for getting the parent NodeRef of a relative path.
|
||||
* @param base The base node ref.
|
||||
* @param path The relative path.
|
||||
* @return A Pair with the parent node ref and the name of the child.
|
||||
*/
|
||||
private Pair<NodeRef, String> getParentChildRelative(NodeRef base, String path)
|
||||
{
|
||||
List<String> pathList = splitPath(path);
|
||||
NodeRef parent;
|
||||
String name = null;
|
||||
if (pathList.size() == 1)
|
||||
{
|
||||
parent = base;
|
||||
name = pathList.get(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
name = pathList.get(pathList.size() - 1);
|
||||
pathList.remove(pathList.size() - 1);
|
||||
FileInfo info = fFileFolderService.resolveNamePath(base, pathList);
|
||||
parent = info.getNodeRef();
|
||||
}
|
||||
catch (FileNotFoundException e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Not Found: " + pathList, e);
|
||||
}
|
||||
}
|
||||
return new Pair<NodeRef, String>(parent, name);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user