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 org.codehaus.jettison.json.JSONObject; 27 import org.modeshape.common.util.CheckArg; 28 import org.modeshape.common.util.Logger; 29 import org.modeshape.web.jcr.rest.client.domain.Repository; 30 import org.modeshape.web.jcr.rest.client.domain.Server; 31 32 import java.net.URL; 33 import java.util.ArrayList; 34 import java.util.Collection; 35 import java.util.Iterator; 36 37 /** 38 * The <code>ServerNode</code> class is responsible for knowing how to create a URL for a server, create a URL to obtain a 39 * server's repositories, and parse a JSON response into {@link Repository repository} objects. 40 */ 41 public final class ServerNode extends JsonNode { 42 43 // =========================================================================================================================== 44 // Fields 45 // =========================================================================================================================== 46 47 /** 48 * The LOGGER. 49 */ 50 private static final Logger LOGGER = Logger.getLogger(ServerNode.class); 51 52 /** 53 * The server containing ModeShape repositories. 54 */ 55 private final Server server; 56 57 // =========================================================================================================================== 58 // Constructors 59 // =========================================================================================================================== 60 61 /** 62 * @param server the server containing the repositories (never <code>null</code>) 63 */ 64 public ServerNode( Server server ) { 65 super(server.getName()); 66 this.server = server; 67 } 68 69 // =========================================================================================================================== 70 // Methods 71 // =========================================================================================================================== 72 73 /** 74 * {@inheritDoc} 75 * <p> 76 * The URL will NOT end in '/'. 77 * 78 * @see org.modeshape.web.jcr.rest.client.json.JsonNode#getUrl() 79 */ 80 @Override 81 public URL getUrl() throws Exception { 82 StringBuilder url = new StringBuilder(this.server.getUrl()); 83 84 // strip off last '/' if necessary 85 if (url.lastIndexOf("/") == (url.length() - 1)) { 86 url.delete((url.length() - 1), (url.length() - 1)); 87 } 88 return new URL(url.toString()); 89 } 90 91 /** 92 * @return the URL to use when requesting the repositories (never <code>null</code>) 93 * @throws Exception if there is a problem obtaining the repositories 94 */ 95 public URL getFindRepositoriesUrl() throws Exception { 96 return new URL(getUrl().toString() + '/'); 97 } 98 99 /** 100 * @param jsonResponse the HTTP connection JSON response (never <code>null</code>) containing the repositories 101 * @return the repositories found in the JSON response (never <code>null</code>) 102 * @throws Exception if there is a problem obtaining the repositories 103 */ 104 @SuppressWarnings( "unchecked" ) 105 public Collection<Repository> getRepositories( String jsonResponse ) throws Exception { 106 CheckArg.isNotNull(jsonResponse, "jsonResponse"); 107 Collection<Repository> repositories = new ArrayList<Repository>(); 108 LOGGER.trace("getRepositories:jsonResponse={0}", jsonResponse); 109 JSONObject jsonObj = new JSONObject(jsonResponse); 110 111 // keys are the repository names 112 for (Iterator<String> itr = jsonObj.keys(); itr.hasNext();) { 113 String name = JsonUtils.decode(itr.next()); 114 Repository repository = new Repository(name, this.server); 115 repositories.add(repository); 116 LOGGER.trace("getRepositories: adding repository={0}", repository); 117 } 118 119 return repositories; 120 } 121 122 }