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