This project has retired. For details please refer to its Attic page.
Dispatcher xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.shared;
20  
21  import java.io.Serializable;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import javax.xml.stream.XMLStreamException;
28  
29  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
30  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
31  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
32  import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParseException;
33  import org.apache.chemistry.opencmis.commons.server.CallContext;
34  import org.apache.chemistry.opencmis.commons.server.CmisService;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * Dispatcher for the AtomPub and Browser binding servlet.
40   */
41  public class Dispatcher implements Serializable {
42  
43      private static final long serialVersionUID = 1L;
44  
45      public static final String BASE_URL_ATTRIBUTE = "org.apache.chemistry.opencmis.baseurl";
46  
47      public static final String METHOD_GET = "GET";
48      public static final String METHOD_HEAD = "HEAD";
49      public static final String METHOD_POST = "POST";
50      public static final String METHOD_PUT = "PUT";
51      public static final String METHOD_DELETE = "DELETE";
52  
53      private static final Logger LOG = LoggerFactory.getLogger(Dispatcher.class.getName());
54  
55      private final boolean caseSensitive;
56      private final Map<String, ServiceCall> serviceCallMap;
57  
58      public Dispatcher() {
59          this(true);
60      }
61  
62      public Dispatcher(boolean caseSensitive) {
63          this.caseSensitive = caseSensitive;
64          serviceCallMap = new HashMap<String, ServiceCall>();
65      }
66  
67      /**
68       * Connects a resource and HTTP method with an object that handles the call.
69       */
70      public void addResource(String resource, String httpMethod, ServiceCall serviceCall) {
71          serviceCallMap.put(getKey(resource, httpMethod), serviceCall);
72      }
73  
74      /**
75       * Handles the a call.
76       * 
77       * @return <code>true</code> if an object was found that can handle the
78       *         request, <code>false</code> otherwise.
79       */
80      public boolean dispatch(String resource, String httpMethod, CallContext context, CmisService service,
81              String repositoryId, HttpServletRequest request, HttpServletResponse response) {
82          ServiceCall serviceCall = serviceCallMap.get(getKey(resource, httpMethod));
83          if (serviceCall == null) {
84              return false;
85          }
86  
87          if (LOG.isDebugEnabled()) {
88              LOG.debug(repositoryId + " / " + resource + ", " + httpMethod + " -> " + serviceCall.getClass().getName());
89          }
90  
91          try {
92              serviceCall.serve(context, service, repositoryId, request, response);
93          } catch (CmisBaseException ce) {
94              throw ce;
95          } catch (XMLStreamException xse) {
96              throw new CmisInvalidArgumentException("Invalid XML!", xse);
97          } catch (JSONParseException jpe) {
98              throw new CmisInvalidArgumentException("Invalid JSON!", jpe);
99          } catch (Exception e) {
100             throw new CmisRuntimeException(e.getMessage(), e);
101         }
102 
103         return true;
104     }
105 
106     /**
107      * Generates a map key from a resource and an HTTP method.
108      */
109     private String getKey(String resource, String httpMethod) {
110         String s = resource + "/" + httpMethod;
111         return (caseSensitive ? s : s.toUpperCase());
112     }
113 }