View Javadoc

1   package org.modeshape.web.jcr.rest;
2   
3   import javax.jcr.RepositoryException;
4   import javax.jcr.Session;
5   import javax.servlet.http.HttpServletRequest;
6   import org.modeshape.common.text.UrlEncoder;
7   import org.modeshape.web.jcr.RepositoryFactory;
8   
9   abstract class AbstractHandler {
10  
11      protected static final UrlEncoder URL_ENCODER = new UrlEncoder();
12  
13      /** Name to be used when the repository name is empty string as {@code "//"} is not a valid path. */
14      public static final String EMPTY_REPOSITORY_NAME = "<default>";
15      /** Name to be used when the workspace name is empty string as {@code "//"} is not a valid path. */
16      public static final String EMPTY_WORKSPACE_NAME = "<default>";
17  
18  
19      /**
20       * Returns an active session for the given workspace name in the named repository.
21       * 
22       * @param request the servlet request; may not be null or unauthenticated
23       * @param rawRepositoryName the URL-encoded name of the repository in which the session is created
24       * @param rawWorkspaceName the URL-encoded name of the workspace to which the session should be connected
25       * @return an active session with the given workspace in the named repository
26       * @throws RepositoryException if any other error occurs
27       */
28      protected Session getSession( HttpServletRequest request,
29                                  String rawRepositoryName,
30                                  String rawWorkspaceName ) throws RepositoryException {
31          assert request != null;
32  
33          return RepositoryFactory.getSession(request, repositoryNameFor(rawRepositoryName), workspaceNameFor(rawWorkspaceName));
34      }
35  
36      private String workspaceNameFor( String rawWorkspaceName ) {
37          String workspaceName = URL_ENCODER.decode(rawWorkspaceName);
38  
39          if (EMPTY_WORKSPACE_NAME.equals(workspaceName)) {
40              workspaceName = "";
41          }
42  
43          return workspaceName;
44      }
45  
46      private String repositoryNameFor( String rawRepositoryName ) {
47          String repositoryName = URL_ENCODER.decode(rawRepositoryName);
48  
49          if (EMPTY_REPOSITORY_NAME.equals(repositoryName)) {
50              repositoryName = "";
51          }
52  
53          return repositoryName;
54      }
55  
56      
57  }