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.webdav;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import net.sf.webdav.IWebdavStore;
32  import net.sf.webdav.WebdavServlet;
33  
34  /**
35   * Custom servlet implementation that provides WebDAV access to a JCR repository. Nodes in the repository with a specified primary
36   * type (nt:file, by default) are treated as WebDAV resources (files) while nodes with any other primary type are treated as
37   * WebDAV folders.
38   */
39  public class ModeShapeWebdavServlet extends WebdavServlet {
40  
41      private static final long serialVersionUID = 1L;
42  
43      public static final String INIT_REQUEST_RESOLVER_CLASS_NAME = "org.modeshape.web.jcr.webdav.REQUEST_RESOLVER_CLASS_NAME";
44  
45      public static final String INIT_CONTENT_PRIMARY_TYPE_NAMES = "org.modeshape.web.jcr.webdav.CONTENT_PRIMARY_TYPE_NAMES";
46      public static final String INIT_RESOURCE_PRIMARY_TYPES_NAMES = "org.modeshape.web.jcr.webdav.RESOURCE_PRIMARY_TYPE_NAMES";
47      public static final String INIT_NEW_FOLDER_PRIMARY_TYPE_NAME = "org.modeshape.web.jcr.webdav.NEW_FOLDER_PRIMARY_TYPE_NAME";
48      public static final String INIT_NEW_RESOURCE_PRIMARY_TYPE_NAME = "org.modeshape.web.jcr.webdav.NEW_RESOURCE_PRIMARY_TYPE_NAME";
49      public static final String INIT_NEW_CONTENT_PRIMARY_TYPE_NAME = "org.modeshape.web.jcr.webdav.NEW_CONTENT_PRIMARY_TYPE_NAME";
50  
51      private RequestResolver requestResolver;
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      protected IWebdavStore constructStore( String clazzName,
58                                             File root ) {
59          return new ModeShapeWebdavStore(getInitParameter(INIT_CONTENT_PRIMARY_TYPE_NAMES),
60                                          getInitParameter(INIT_RESOURCE_PRIMARY_TYPES_NAMES),
61                                          getInitParameter(INIT_NEW_FOLDER_PRIMARY_TYPE_NAME),
62                                          getInitParameter(INIT_NEW_RESOURCE_PRIMARY_TYPE_NAME),
63                                          getInitParameter(INIT_NEW_CONTENT_PRIMARY_TYPE_NAME), requestResolver);
64      }
65  
66      /**
67       * Loads and initializes the {@link #requestResolver}
68       */
69      private void constructRequestResolver() {
70          // Initialize the request resolver
71          String requestResolverClassName = getInitParameter(INIT_REQUEST_RESOLVER_CLASS_NAME);
72          if (requestResolverClassName == null) {
73              this.requestResolver = new DefaultRequestResolver();
74          } else {
75              try {
76                  Class<? extends RequestResolver> clazz = Class.forName(requestResolverClassName).asSubclass(RequestResolver.class);
77                  this.requestResolver = clazz.newInstance();
78              } catch (Exception ex) {
79                  throw new IllegalStateException(ex);
80              }
81          }
82  
83          this.requestResolver.initialize(getServletContext());
84      }
85  
86      @Override
87      public void init() throws ServletException {
88          constructRequestResolver();
89  
90          super.init();
91      }
92  
93      /**
94       * {@inheritDoc}
95       * <p>
96       * This method also sets and clears a thread-local reference to the incoming {@link HttpServletRequest request}.
97       * </p>
98       */
99      @Override
100     protected void service( HttpServletRequest req,
101                             HttpServletResponse resp ) throws ServletException, IOException {
102         ModeShapeWebdavStore.setRequest(req);
103         try {
104             super.service(req, resp);
105         } finally {
106             ModeShapeWebdavStore.setRequest(null);
107         }
108     }
109 }