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 java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Iterator;
30  import org.codehaus.jettison.json.JSONObject;
31  import org.modeshape.common.util.CheckArg;
32  import org.modeshape.web.jcr.rest.client.domain.Repository;
33  import org.modeshape.web.jcr.rest.client.domain.Workspace;
34  
35  /**
36   * The <code>RepositoryNode</code> class is responsible for knowing how to create a URL for a repository and to parse a JSON
37   * response into {@link Workspace workspace} objects.
38   */
39  public final class RepositoryNode extends JsonNode {
40  
41      // ===========================================================================================================================
42      // Fields
43      // ===========================================================================================================================
44  
45      /**
46       * The ModeShape repository.
47       */
48      private final Repository repository;
49  
50      // ===========================================================================================================================
51      // Constructors
52      // ===========================================================================================================================
53  
54      /**
55       * @param repository the ModeShape repository (never <code>null</code>)
56       */
57      public RepositoryNode( Repository repository ) {
58          super(repository.getName());
59          this.repository = repository;
60      }
61  
62      // ===========================================================================================================================
63      // Methods
64      // ===========================================================================================================================
65  
66      /**
67       * {@inheritDoc}
68       * <p>
69       * This URL can be used to obtain the workspaces contained in this repository. The URL will NOT end in '/'.
70       * 
71       * @see org.modeshape.web.jcr.rest.client.json.JsonNode#getUrl()
72       */
73      @Override
74      public URL getUrl() throws Exception {
75          ServerNode serverNode = new ServerNode(this.repository.getServer());
76          StringBuilder url = new StringBuilder(serverNode.getUrl().toString());
77  
78          // add repository path
79          url.append('/').append(JsonUtils.encode(repository.getName()));
80          return new URL(url.toString());
81      }
82  
83      /**
84       * @param jsonResponse the HTTP connection JSON response (never <code>null</code>) containing the workspaces
85       * @return the workspaces for this repository (never <code>null</code>)
86       * @throws Exception if there is a problem obtaining the workspaces
87       */
88      @SuppressWarnings( "unchecked" )
89      public Collection<Workspace> getWorkspaces( String jsonResponse ) throws Exception {
90          CheckArg.isNotNull(jsonResponse, "jsonResponse");
91          Collection<Workspace> workspaces = new ArrayList<Workspace>();
92          JSONObject jsonObj = new JSONObject(jsonResponse);
93  
94          // keys are the repository names
95          for (Iterator<String> itr = jsonObj.keys(); itr.hasNext();) {
96              String name = JsonUtils.decode(itr.next());
97              Workspace workspace = new Workspace(name, this.repository);
98              workspaces.add(workspace);
99          }
100 
101         return workspaces;
102     }
103 
104 }