View Javadoc
1   package org.exoplatform.wcm.connector.collaboration;
2   
3   import java.io.InputStream;
4   import java.net.URLDecoder;
5   import java.net.URLEncoder;
6   
7   import javax.jcr.AccessDeniedException;
8   import javax.jcr.Node;
9   import javax.jcr.PathNotFoundException;
10  import javax.jcr.Session;
11  import javax.ws.rs.GET;
12  import javax.ws.rs.Path;
13  import javax.ws.rs.PathParam;
14  import javax.ws.rs.QueryParam;
15  import javax.ws.rs.core.MediaType;
16  import javax.ws.rs.core.Response;
17  
18  import org.exoplatform.common.http.HTTPStatus;
19  import org.exoplatform.ecm.utils.text.Text;
20  import org.exoplatform.services.jcr.core.ManageableRepository;
21  import org.exoplatform.services.jcr.ext.common.SessionProvider;
22  import org.exoplatform.services.rest.resource.ResourceContainer;
23  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
24  
25  /**
26   * Enables downloading the content of _nt\:file_.
27   *
28   * @LevelAPI Provisional
29   *
30   * @anchor DownloadConnector
31   */
32  @Path("/contents/")
33  public class DownloadConnector implements ResourceContainer{
34  
35    /**
36     * Returns to browser a stream got from _jcr\:content_/_jcr\:data_ for downloading the content of the node.
37     *
38     * @param workspace The workspace where stores the document node.
39     * @param path The path to the document node.
40     * @param version The version name.
41     * @return the instance of javax.ws.rs.core.Response.
42     * @throws Exception The exception
43     *
44     * @anchor DownloadConnector.download
45     */
46    @GET
47    @Path("/download/{workspace}/{path:.*}")
48    public Response download(@PathParam("workspace") String workspace,
49                             @PathParam("path") String path,
50                             @QueryParam("version") String version) throws Exception {
51      InputStream is = null;
52      String mimeType = MediaType.TEXT_XML;
53      Node node = null;
54      String fileName = null;
55      SessionProvider sessionProvider = WCMCoreUtils.getUserSessionProvider();
56      ManageableRepository manageableRepository = WCMCoreUtils.getRepository();
57      Session session = sessionProvider.getSession(workspace, manageableRepository);
58  
59      if (!path.startsWith("/")) {
60        path = "/" + path;
61      }
62  
63      try {
64        node = (Node) session.getItem(path);
65        fileName = node.getName();
66        if (node.hasProperty("exo:title")){
67          fileName = node.getProperty("exo:title").getString();
68        }
69        fileName = Text.unescapeIllegalJcrChars(fileName);
70        fileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
71        // In case version is specified, get file from version history
72        if (version != null) {
73          node = node.getVersionHistory().getVersion(version).getNode("jcr:frozenNode");
74        }
75  
76        Node jrcNode = node.getNode("jcr:content");
77        is = jrcNode.getProperty("jcr:data").getStream();
78      }catch (PathNotFoundException pne) {
79        return Response.status(HTTPStatus.NOT_FOUND).build();
80      } catch (AccessDeniedException ade) {
81        return Response.status(HTTPStatus.UNAUTHORIZED).build();
82      }
83      if (node.isNodeType("nt:file") || (node.isNodeType("nt:frozenNode")) && node.getProperty("jcr:frozenPrimaryType").getValue().getString().equals("nt:file")) {
84        mimeType = node.getNode("jcr:content").getProperty("jcr:mimeType").getString();
85      }
86      return Response.ok(is, mimeType)
87            .header("Content-Disposition","attachment; filename=\"" + fileName + "\"; filename*=UTF-8''" + fileName)
88              .build();
89    }
90  }