View Javadoc

1   package org.modeshape.web.jcr.rest;
2   
3   import java.io.IOException;
4   import java.util.HashMap;
5   import java.util.Map;
6   import javax.jcr.RepositoryException;
7   import javax.jcr.Session;
8   import javax.servlet.http.HttpServletRequest;
9   import net.jcip.annotations.Immutable;
10  import org.modeshape.web.jcr.rest.model.WorkspaceEntry;
11  
12  /**
13   * Resource handler that implements REST methods for repositories and workspaces.
14   */
15  @Immutable
16  class RepositoryHandler extends AbstractHandler {
17  
18      /**
19       * Returns the list of workspaces available to this user within the named repository.
20       * 
21       * @param rawRepositoryName the name of the repository; may not be null
22       * @param request the servlet request; may not be null
23       * @return the list of workspaces available to this user within the named repository.
24       * @throws IOException if the given repository name does not map to any repositories and there is an error writing the error
25       *         code to the response.
26       * @throws RepositoryException if there is any other error accessing the list of available workspaces for the repository
27       */
28      public Map<String, WorkspaceEntry> getWorkspaces( HttpServletRequest request,
29                                                        String rawRepositoryName ) throws RepositoryException, IOException {
30  
31          assert request != null;
32          assert rawRepositoryName != null;
33  
34          Map<String, WorkspaceEntry> workspaces = new HashMap<String, WorkspaceEntry>();
35  
36          Session session = getSession(request, rawRepositoryName, null);
37          rawRepositoryName = URL_ENCODER.encode(rawRepositoryName);
38  
39          for (String name : session.getWorkspace().getAccessibleWorkspaceNames()) {
40              if (name.trim().length() == 0) {
41                  name = EMPTY_WORKSPACE_NAME;
42              }
43              name = URL_ENCODER.encode(name);
44              workspaces.put(name, new WorkspaceEntry(request.getContextPath(), rawRepositoryName, name));
45          }
46  
47          return workspaces;
48      }
49  
50  }