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
.HttpServletRequest
;
13 import javax
.servlet
.http
.HttpServletResponse
;
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.service.IServiceHandler;
21 * Basic Default service handler that retrieves a file from a NT_FILE JCR node
22 * and launch the download.
24 public class OpenJcrFileService
{//implements IServiceHandler {
26 /* DEPENDENCY INJECTION */
27 final private Node fileNode
;
29 public OpenJcrFileService(Node fileNode
) {
30 this.fileNode
= fileNode
;
33 public void service(HttpServletRequest request
, HttpServletResponse response
) throws IOException
, ServletException
{
34 // Get the file content
35 byte[] download
= getFileAsByteArray();
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;
43 contentDisposition
= "attachment; filename=\""
44 + JcrUtils
.lastPathElement(fileNode
.getPath()) + "\"";
45 } catch (RepositoryException e
) {
46 throw new SlcException("Error while getting file Path " + fileNode
,
49 response
.setHeader("Content-Disposition", contentDisposition
);
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
);
59 protected byte[] getFileAsByteArray() {
61 Session businessSession
= null;
63 boolean isValid
= true;
65 if (!fileNode
.isNodeType(NodeType
.NT_FILE
))
68 child
= fileNode
.getNode(Property
.JCR_CONTENT
);
69 if (!(child
.isNodeType(NodeType
.NT_RESOURCE
) || child
70 .hasProperty(Property
.JCR_DATA
)))
78 InputStream fis
= null;
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 "
87 IOUtils
.closeQuietly(fis
);
91 } catch (RepositoryException e
) {
92 throw new SlcException("Unexpected error while "
93 + "opening file node " + fileNode
, e
);
95 JcrUtils
.logoutQuietly(businessSession
);