1 package org
.argeo
.slc
.client
.rap
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
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
;
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
;
22 * Basic Default service handler that retrieves a file from a NT_FILE JCR node
23 * and launch the download.
25 public class OpenJcrFileService
implements IServiceHandler
{
27 /* DEPENDENCY INJECTION */
28 final private Node fileNode
;
30 public OpenJcrFileService(Node fileNode
) {
31 this.fileNode
= fileNode
;
34 public void service(HttpServletRequest request
, HttpServletResponse response
) throws IOException
, ServletException
{
35 // Get the file content
36 byte[] download
= getFileAsByteArray();
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;
44 contentDisposition
= "attachment; filename=\""
45 + JcrUtils
.lastPathElement(fileNode
.getPath()) + "\"";
46 } catch (RepositoryException e
) {
47 throw new SlcException("Error while getting file Path " + fileNode
,
50 response
.setHeader("Content-Disposition", contentDisposition
);
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
);
60 protected byte[] getFileAsByteArray() {
62 Session businessSession
= null;
64 boolean isValid
= true;
66 if (!fileNode
.isNodeType(NodeType
.NT_FILE
))
69 child
= fileNode
.getNode(Property
.JCR_CONTENT
);
70 if (!(child
.isNodeType(NodeType
.NT_RESOURCE
) || child
71 .hasProperty(Property
.JCR_DATA
)))
79 InputStream fis
= null;
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 "
88 IOUtils
.closeQuietly(fis
);
92 } catch (RepositoryException e
) {
93 throw new SlcException("Unexpected error while "
94 + "opening file node " + fileNode
, e
);
96 JcrUtils
.logoutQuietly(businessSession
);