import org.apache.commons.io.IOUtils;
import org.argeo.jcr.JcrUtils;
import org.argeo.slc.SlcException;
-import org.argeo.slc.repo.RepoService;
import org.eclipse.rwt.RWT;
import org.eclipse.rwt.service.IServiceHandler;
-import org.eclipse.rwt.service.IServiceManager;
/**
- * Basic Default service handler that retrieves and launch download of a file
- * stored in a JCR Repository
+ * Basic Default service handler that retrieves a file from a NT_FILE JCR node
+ * and launch the download.
*/
public class OpenJcrFileService implements IServiceHandler {
- public final static String ID = SlcRapPlugin.PLUGIN_ID + ".openJcrFileService";
-
- // use local node repo and repository factory to retrieve and log to
- // relevant repository
- public final static String PARAM_REPO_NODE_PATH = "param.repoNodePath";
- // use URI and repository factory to retrieve and ANONYMOUSLY log in
- // relevant repository
- public final static String PARAM_REPO_URI = "param.repoUri";
- public final static String PARAM_WORKSPACE_NAME = "param.workspaceName";
- public final static String PARAM_FILE_PATH = "param.filePath";
-
- public final static String SCHEME_HOST_SEPARATOR = "://";
-
/* DEPENDENCY INJECTION */
- private RepoService repoService;
-
- public OpenJcrFileService() {
- }
+ final private Node fileNode;
- public void init() {
- IServiceManager manager = RWT.getServiceManager();
- manager.registerServiceHandler(ID, this);
- }
-
- public void destroy() {
- IServiceManager manager = RWT.getServiceManager();
- manager.unregisterServiceHandler(ID);
+ public OpenJcrFileService(Node fileNode) {
+ this.fileNode = fileNode;
}
public void service() throws IOException, ServletException {
- String repoNodePath = RWT.getRequest().getParameter(PARAM_REPO_NODE_PATH);
- String repoUri = RWT.getRequest().getParameter(PARAM_REPO_URI);
- String wkspName = RWT.getRequest().getParameter(PARAM_WORKSPACE_NAME);
- String filePath = RWT.getRequest().getParameter(PARAM_FILE_PATH);
-
// Get the file content
- byte[] download = getFileAsByteArray(repoNodePath, repoUri, wkspName,
- filePath);
+ byte[] download = getFileAsByteArray();
// Send the file in the response
HttpServletResponse response = RWT.getResponse();
response.setContentType("application/octet-stream");
response.setContentLength(download.length);
- String contentDisposition = "attachment; filename=\""
- + JcrUtils.lastPathElement(filePath) + "\"";
+ String contentDisposition = null;
+ try {
+ contentDisposition = "attachment; filename=\""
+ + JcrUtils.lastPathElement(fileNode.getPath()) + "\"";
+ } catch (RepositoryException e) {
+ throw new SlcException("Error while getting file Path " + fileNode,
+ e);
+ }
response.setHeader("Content-Disposition", contentDisposition);
try {
response.getOutputStream().write(download);
} catch (IOException ioe) {
- throw new SlcException("Error while writing the file " + filePath
+ throw new SlcException("Error while writing the file " + fileNode
+ " to the servlet response", ioe);
}
}
- protected byte[] getFileAsByteArray(String repoNodePath, String repoUri,
- String wkspName, String filePath) {
+ protected byte[] getFileAsByteArray() {
+
Session businessSession = null;
try {
- businessSession = repoService.getRemoteSession(repoNodePath,
- repoUri, wkspName);
- Node result = businessSession.getNode(filePath);
-
boolean isValid = true;
Node child = null;
- if (!result.isNodeType(NodeType.NT_FILE))
+ if (!fileNode.isNodeType(NodeType.NT_FILE))
isValid = false;
else {
- child = result.getNode(Property.JCR_CONTENT);
+ child = fileNode.getNode(Property.JCR_CONTENT);
if (!(child.isNodeType(NodeType.NT_RESOURCE) || child
.hasProperty(Property.JCR_DATA)))
isValid = false;
if (!isValid)
return null;
-
+
byte[] ba = null;
InputStream fis = null;
try {
.getBinary().getStream();
ba = IOUtils.toByteArray(fis);
} catch (Exception e) {
- throw new SlcException(
- "Stream error while opening file " + filePath
- + " from repo " + repoUri == null ? repoNodePath
- : repoUri, e);
+ throw new SlcException("Stream error while opening file "
+ + fileNode, e);
} finally {
IOUtils.closeQuietly(fis);
}
} catch (RepositoryException e) {
throw new SlcException("Unexpected error while "
- + "getting repoNode info for repoNode at path "
- + repoNodePath, e);
+ + "opening file node " + fileNode, e);
} finally {
JcrUtils.logoutQuietly(businessSession);
}
}
-
- /* DEPENDENCY INJECTION */
- public void setRepoService(RepoService repoService) {
- this.repoService = repoService;
- }
-
}
\ No newline at end of file