]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.client.rap/src/main/java/org/argeo/slc/client/rap/OpenJcrFileService.java
a4dfaf42c696bd89822fdc61ebaec6ab8344f099
[gpl/argeo-slc.git] / org.argeo.slc.client.rap / src / main / java / org / argeo / slc / client / rap / OpenJcrFileService.java
1 package org.argeo.slc.client.rap;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 import javax.jcr.Node;
7 import javax.jcr.Property;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
10 import javax.jcr.nodetype.NodeType;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.apache.commons.io.IOUtils;
15 import org.argeo.jcr.JcrUtils;
16 import org.argeo.slc.SlcException;
17 import org.eclipse.rwt.RWT;
18 import org.eclipse.rwt.service.IServiceHandler;
19
20 /**
21 * Basic Default service handler that retrieves a file from a NT_FILE JCR node
22 * and launch the download.
23 */
24 public class OpenJcrFileService implements IServiceHandler {
25
26 /* DEPENDENCY INJECTION */
27 final private Node fileNode;
28
29 public OpenJcrFileService(Node fileNode) {
30 this.fileNode = fileNode;
31 }
32
33 public void service() throws IOException, ServletException {
34 // Get the file content
35 byte[] download = getFileAsByteArray();
36
37 // Send the file in the response
38 HttpServletResponse response = RWT.getResponse();
39 response.setContentType("application/octet-stream");
40 response.setContentLength(download.length);
41 String contentDisposition = null;
42 try {
43 contentDisposition = "attachment; filename=\""
44 + JcrUtils.lastPathElement(fileNode.getPath()) + "\"";
45 } catch (RepositoryException e) {
46 throw new SlcException("Error while getting file Path " + fileNode,
47 e);
48 }
49 response.setHeader("Content-Disposition", contentDisposition);
50
51 try {
52 response.getOutputStream().write(download);
53 } catch (IOException ioe) {
54 throw new SlcException("Error while writing the file " + fileNode
55 + " to the servlet response", ioe);
56 }
57 }
58
59 protected byte[] getFileAsByteArray() {
60
61 Session businessSession = null;
62 try {
63 boolean isValid = true;
64 Node child = null;
65 if (!fileNode.isNodeType(NodeType.NT_FILE))
66 isValid = false;
67 else {
68 child = fileNode.getNode(Property.JCR_CONTENT);
69 if (!(child.isNodeType(NodeType.NT_RESOURCE) || child
70 .hasProperty(Property.JCR_DATA)))
71 isValid = false;
72 }
73
74 if (!isValid)
75 return null;
76
77 byte[] ba = null;
78 InputStream fis = null;
79 try {
80 fis = (InputStream) child.getProperty(Property.JCR_DATA)
81 .getBinary().getStream();
82 ba = IOUtils.toByteArray(fis);
83 } catch (Exception e) {
84 throw new SlcException("Stream error while opening file "
85 + fileNode, e);
86 } finally {
87 IOUtils.closeQuietly(fis);
88 }
89 return ba;
90
91 } catch (RepositoryException e) {
92 throw new SlcException("Unexpected error while "
93 + "opening file node " + fileNode, e);
94 } finally {
95 JcrUtils.logoutQuietly(businessSession);
96 }
97 }
98 }