View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.web.jcr.rest.client.json;
25  
26  import java.net.URL;
27  import net.jcip.annotations.Immutable;
28  import org.codehaus.jettison.json.JSONObject;
29  import org.modeshape.web.jcr.rest.client.IJcrConstants;
30  import org.modeshape.web.jcr.rest.client.domain.Workspace;
31  
32  /**
33   * The <code>FolderNode</code> class is responsible for knowing how to create a URL for a folder, create a JSON representation of
34   * a folder, and create the appropriate JCR nodes for a folder.
35   */
36  @Immutable
37  public final class FolderNode extends JsonNode {
38  
39      // ===========================================================================================================================
40      // Fields
41      // ===========================================================================================================================
42  
43      /**
44       * The workspace where the file is being published.
45       */
46      private final Workspace workspace;
47  
48      // ===========================================================================================================================
49      // Constructors
50      // ===========================================================================================================================
51  
52      /**
53       * @param workspace the workspace being used (never <code>null</code>)
54       * @param fullPath the full path of the folder within the workspace (never <code>null</code>)
55       * @throws Exception if there is a problem creating the folder node
56       */
57      public FolderNode( Workspace workspace,
58                         String fullPath ) throws Exception {
59          super(fullPath);
60      	assert workspace != null;
61      	assert fullPath != null;
62  
63          this.workspace = workspace;
64  
65          // add properties
66          JSONObject properties = new JSONObject();
67          properties.put(IJcrConstants.PRIMARY_TYPE_PROPERTY, IJcrConstants.FOLDER_NODE_TYPE);
68          put(IJsonConstants.PROPERTIES_KEY, properties);
69      }
70  
71      // ===========================================================================================================================
72      // Methods
73      // ===========================================================================================================================
74  
75      /**
76       * @return the full path of folder within the workspace
77       */
78      public String getPath() {
79          return getId();
80      }
81  
82      /**
83       * {@inheritDoc}
84       * <p>
85       * The URL will NOT end in '/'.
86       * 
87       * @see org.modeshape.web.jcr.rest.client.json.JsonNode#getUrl()
88       */
89      @Override
90      public URL getUrl() throws Exception {
91          WorkspaceNode workspaceNode = new WorkspaceNode(this.workspace);
92          StringBuilder url = new StringBuilder(workspaceNode.getUrl().toString());
93  
94          // make sure path starts with a '/'
95          String path = getPath();
96  
97          if (!path.startsWith("/")) {
98              path = '/' + path;
99          }
100 
101         // make sure path does NOT end with a '/'
102         if (path.endsWith("/")) {
103             path = path.substring(0, path.length() - 1);
104         }
105 
106         // path needs to be encoded
107         url.append(JsonUtils.encode(path));
108 
109         return new URL(url.toString());
110     }
111 
112 }