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 org.codehaus.jettison.json.JSONObject; 28 import org.modeshape.common.util.CheckArg; 29 30 /** 31 * The <code>JsonNode</code> class defines the API for interacing with JSON objects. Every <code>JsonNode</code> knows how to 32 * create their URL and create their JCR content. 33 */ 34 public abstract class JsonNode extends JSONObject { 35 36 // =========================================================================================================================== 37 // Fields 38 // =========================================================================================================================== 39 40 /** 41 * The node identifier. 42 */ 43 private final String id; 44 45 // =========================================================================================================================== 46 // Constructors 47 // =========================================================================================================================== 48 49 /** 50 * @param id the node identifier (never <code>null</code>) 51 */ 52 protected JsonNode( String id ) { 53 CheckArg.isNotNull(id, "id"); 54 this.id = id; 55 } 56 57 // =========================================================================================================================== 58 // Methods 59 // =========================================================================================================================== 60 61 /** 62 * @return the content that gets published 63 * @throws Exception if there is a problem obtaining the node content 64 */ 65 public byte[] getContent() throws Exception { 66 return super.toString().getBytes(); 67 } 68 69 /** 70 * @return a unique identifier for this node 71 */ 72 public String getId() { 73 return this.id; 74 } 75 76 /** 77 * @return an HTTP URL representing this node 78 * @throws Exception if there is a problem constructing the URL 79 */ 80 public abstract URL getUrl() throws Exception; 81 82 /** 83 * {@inheritDoc} 84 * 85 * @see org.codehaus.jettison.json.JSONObject#toString() 86 */ 87 @Override 88 public String toString() { 89 StringBuilder txt = new StringBuilder(); 90 txt.append("ID: ").append(getId()); 91 txt.append(", URL: "); 92 93 try { 94 txt.append(getUrl()); 95 } catch (Exception e) { 96 txt.append("exception obtaining URL"); 97 } 98 99 txt.append(", content: ").append(super.toString()); 100 return txt.toString(); 101 } 102 103 }