This project has retired. For details please refer to its
Attic page.
Dispatcher xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
69
70 public void addResource(String resource, String httpMethod, ServiceCall serviceCall) {
71 serviceCallMap.put(getKey(resource, httpMethod), serviceCall);
72 }
73
74
75
76
77
78
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
108
109 private String getKey(String resource, String httpMethod) {
110 String s = resource + "/" + httpMethod;
111 return (caseSensitive ? s : s.toUpperCase());
112 }
113 }