View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.wcm.connector.fckeditor;
18  
19  import javax.jcr.Node;
20  
21  import org.exoplatform.container.ExoContainerContext;
22  import org.exoplatform.ecm.connector.fckeditor.FCKFileHandler;
23  import org.exoplatform.services.jcr.access.AccessControlEntry;
24  import org.exoplatform.services.jcr.access.AccessControlList;
25  import org.exoplatform.services.jcr.access.PermissionType;
26  import org.exoplatform.services.jcr.core.ExtendedNode;
27  import org.exoplatform.services.jcr.core.ManageableRepository;
28  import org.exoplatform.services.security.IdentityConstants;
29  import org.exoplatform.services.wcm.core.NodetypeConstant;
30  import org.exoplatform.services.wcm.core.WCMConfigurationService;
31  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
32  
33  /*
34   * Created by The eXo Platform SAS Author : Anh Do Ngoc anh.do@exoplatform.com
35   * Sep 26, 2008
36   */
37  /**
38   * The Class DocumentLinkHandler.
39   */
40  public class DocumentLinkHandler extends FCKFileHandler {
41  
42    /** The base uri. */
43    private String baseURI;
44  
45    /** The current portal. */
46    private String currentPortal;
47  
48    /**
49     * Instantiates a new document link handler.
50     */
51    public DocumentLinkHandler() {
52      super(ExoContainerContext.getCurrentContainer());
53    }
54  
55    /**
56     * Sets the base uri.
57     *
58     * @param baseURI the new base uri
59     */
60    public void setBaseURI(String baseURI) {
61      this.baseURI = baseURI;
62    }
63  
64    /* (non-Javadoc)
65     * @see org.exoplatform.ecm.connector.fckeditor.FCKFileHandler#getFileURL(javax.jcr.Node)
66     */
67    public String getFileURL(final Node node) throws Exception {
68      String accessMode = "private";
69      AccessControlList acl = ((ExtendedNode) node).getACL();
70      for (AccessControlEntry entry : acl.getPermissionEntries()) {
71        if (entry.getIdentity().equalsIgnoreCase(IdentityConstants.ANY)
72            && entry.getPermission().equalsIgnoreCase(PermissionType.READ)) {
73          accessMode = "public";
74          break;
75        }
76      }
77      String repository = ((ManageableRepository) node.getSession().getRepository()).getConfiguration()
78                                                                                    .getName();
79      String workspace = node.getSession().getWorkspace().getName();
80      String nodePath = node.getPath();
81      StringBuilder builder = new StringBuilder();
82      if (node.isNodeType(NodetypeConstant.NT_FILE)) {
83        if ("public".equals(accessMode)) {
84          return builder.append(baseURI)
85                        .append("/jcr/")
86                        .append(repository)
87                        .append("/")
88                        .append(workspace)
89                        .append(nodePath)
90                        .toString();
91        }
92        return builder.append(baseURI)
93                      .append("/private/jcr/")
94                      .append(repository)
95                      .append("/")
96                      .append(workspace)
97                      .append(nodePath)
98                      .toString();
99      }
100     WCMConfigurationService configurationService = WCMCoreUtils.getService(WCMConfigurationService.class);
101     String parameterizedPageViewerURI = configurationService.
102         getRuntimeContextParam(WCMConfigurationService.PARAMETERIZED_PAGE_URI);
103     return baseURI.replace("/rest", "") + "/" + accessMode + "/" + currentPortal
104         + parameterizedPageViewerURI + "/" + repository + "/" + workspace + nodePath;
105   }
106 
107   /**
108    * Sets the current portal.
109    *
110    * @param currentPortal the new current portal
111    */
112   public void setCurrentPortal(String currentPortal) {
113     this.currentPortal = currentPortal;
114   }
115 
116   /**
117    * Gets the current portal.
118    *
119    * @return the current portal
120    */
121   public String getCurrentPortal() {
122     return this.currentPortal;
123   }
124 }