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.common.util.CheckArg; 30 import org.modeshape.web.jcr.rest.client.IJcrConstants; 31 import org.modeshape.web.jcr.rest.client.domain.Workspace; 32 33 /** 34 * The <code>FolderNode</code> class is responsible for knowing how to create a URL for a folder, create a JSON representation of 35 * a folder, and create the appropriate JCR nodes for a folder. 36 */ 37 @Immutable 38 public final class FolderNode extends JsonNode { 39 40 // =========================================================================================================================== 41 // Fields 42 // =========================================================================================================================== 43 44 /** 45 * The workspace where the file is being published. 46 */ 47 private final Workspace workspace; 48 49 // =========================================================================================================================== 50 // Constructors 51 // =========================================================================================================================== 52 53 /** 54 * @param workspace the workspace being used (never <code>null</code>) 55 * @param fullPath the full path of the folder within the workspace (never <code>null</code>) 56 * @throws Exception if there is a problem creating the folder node 57 */ 58 public FolderNode( Workspace workspace, 59 String fullPath ) throws Exception { 60 super(fullPath); 61 62 CheckArg.isNotNull(workspace, "workspace"); 63 CheckArg.isNotNull(fullPath, "fullPath"); 64 65 this.workspace = workspace; 66 67 // add properties 68 JSONObject properties = new JSONObject(); 69 properties.put(IJcrConstants.PRIMARY_TYPE_PROPERTY, IJcrConstants.FOLDER_NODE_TYPE); 70 put(IJsonConstants.PROPERTIES_KEY, properties); 71 } 72 73 // =========================================================================================================================== 74 // Methods 75 // =========================================================================================================================== 76 77 /** 78 * @return the full path of folder within the workspace 79 */ 80 public String getPath() { 81 return getId(); 82 } 83 84 /** 85 * {@inheritDoc} 86 * <p> 87 * The URL will NOT end in '/'. 88 * 89 * @see org.modeshape.web.jcr.rest.client.json.JsonNode#getUrl() 90 */ 91 @Override 92 public URL getUrl() throws Exception { 93 WorkspaceNode workspaceNode = new WorkspaceNode(this.workspace); 94 StringBuilder url = new StringBuilder(workspaceNode.getUrl().toString()); 95 96 // make sure path starts with a '/' 97 String path = getPath(); 98 99 if (!path.startsWith("/")) { 100 path = '/' + path; 101 } 102 103 // make sure path does NOT end with a '/' 104 if (path.endsWith("/")) { 105 path = path.substring(0, path.length() - 1); 106 } 107 108 // path needs to be encoded 109 url.append(JsonUtils.encode(path)); 110 111 return new URL(url.toString()); 112 } 113 114 }