View Javadoc

1   package org.modeshape.web.jcr.rest;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import javax.jcr.PropertyType;
6   import javax.jcr.RepositoryException;
7   import javax.jcr.Session;
8   import javax.jcr.Value;
9   import javax.servlet.http.HttpServletRequest;
10  import org.modeshape.common.text.UrlEncoder;
11  import org.modeshape.common.util.Base64;
12  import org.modeshape.web.jcr.RepositoryFactory;
13  
14  abstract class AbstractHandler {
15  
16      protected static final String BASE64_ENCODING_SUFFIX = "/base64/";
17  
18      protected static final UrlEncoder URL_ENCODER = new UrlEncoder();
19  
20      /** Name to be used when the repository name is empty string as {@code "//"} is not a valid path. */
21      public static final String EMPTY_REPOSITORY_NAME = "<default>";
22      /** Name to be used when the workspace name is empty string as {@code "//"} is not a valid path. */
23      public static final String EMPTY_WORKSPACE_NAME = "<default>";
24  
25  
26      /**
27       * Returns an active session for the given workspace name in the named repository.
28       * 
29       * @param request the servlet request; may not be null or unauthenticated
30       * @param rawRepositoryName the URL-encoded name of the repository in which the session is created
31       * @param rawWorkspaceName the URL-encoded name of the workspace to which the session should be connected
32       * @return an active session with the given workspace in the named repository
33       * @throws RepositoryException if any other error occurs
34       */
35      protected Session getSession( HttpServletRequest request,
36                                  String rawRepositoryName,
37                                  String rawWorkspaceName ) throws RepositoryException {
38          assert request != null;
39  
40          return RepositoryFactory.getSession(request, repositoryNameFor(rawRepositoryName), workspaceNameFor(rawWorkspaceName));
41      }
42  
43      private String workspaceNameFor( String rawWorkspaceName ) {
44          String workspaceName = URL_ENCODER.decode(rawWorkspaceName);
45  
46          if (EMPTY_WORKSPACE_NAME.equals(workspaceName)) {
47              workspaceName = "";
48          }
49  
50          return workspaceName;
51      }
52  
53      private String repositoryNameFor( String rawRepositoryName ) {
54          String repositoryName = URL_ENCODER.decode(rawRepositoryName);
55  
56          if (EMPTY_REPOSITORY_NAME.equals(repositoryName)) {
57              repositoryName = "";
58          }
59  
60          return repositoryName;
61      }
62  
63      /**
64       * Return the JSON-compatible string representation of the given property value. If the value is a {@link PropertyType#BINARY
65       * binary} value, then this method returns the Base-64 encoding of that value. Otherwise, it just returns the string
66       * representation of the value.
67       * 
68       * @param value the property value; may not be null
69       * @return the string representation of the value
70       * @throws RepositoryException if there is a problem accessing the value
71       */
72      protected String jsonEncodedStringFor( Value value ) throws RepositoryException {
73          // Encode the binary value in Base64 ...
74          InputStream stream = value.getBinary().getStream();
75          try {
76              return Base64.encode(stream);
77          } finally {
78              if (stream != null) {
79                  try {
80                      stream.close();
81                  } catch (IOException e) {
82                      // Error accessing the value, so throw this ...
83                      throw new RepositoryException(e);
84                  }
85              }
86          }
87      }
88      
89  }