This project has retired. For details please refer to its
Attic page.
AbstractCmisHttpServlet 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.util.Map;
22
23 import javax.servlet.ServletConfig;
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
31 import org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException;
32 import org.apache.chemistry.opencmis.commons.impl.ClassLoaderUtil;
33 import org.apache.chemistry.opencmis.commons.impl.Constants;
34 import org.apache.chemistry.opencmis.commons.server.CallContext;
35 import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
36 import org.apache.chemistry.opencmis.server.impl.CallContextImpl;
37 import org.apache.chemistry.opencmis.server.impl.CmisRepositoryContextListener;
38 import org.apache.chemistry.opencmis.server.impl.browser.BrowserCallContextImpl;
39
40 public abstract class AbstractCmisHttpServlet extends HttpServlet {
41
42 public static final String PARAM_CALL_CONTEXT_HANDLER = "callContextHandler";
43 public static final String PARAM_CMIS_VERSION = "cmisVersion";
44
45 private static final long serialVersionUID = 1L;
46
47 private CmisServiceFactory factory;
48 private String binding;
49 private CmisVersion cmisVersion;
50 private CallContextHandler callContextHandler;
51 private CsrfManager csrfManager;
52
53 @Override
54 public void init(ServletConfig config) throws ServletException {
55 super.init(config);
56
57
58 callContextHandler = loadCallContextHandler(config);
59
60
61 factory = CmisRepositoryContextListener.getServiceFactory(config.getServletContext());
62
63 if (factory == null) {
64 throw new ServletException("Service factory not available! Configuration problem?");
65 }
66
67
68 csrfManager = new CsrfManager(config);
69 }
70
71
72
73
74
75 public static CallContextHandler loadCallContextHandler(ServletConfig config) throws ServletException {
76 String callContextHandlerClass = config.getInitParameter(PARAM_CALL_CONTEXT_HANDLER);
77 if (callContextHandlerClass != null) {
78 try {
79 return (CallContextHandler) ClassLoaderUtil.loadClass(callContextHandlerClass).newInstance();
80 } catch (Exception e) {
81 throw new ServletException("Could not load call context handler: " + e, e);
82 }
83 }
84
85 return null;
86 }
87
88
89
90
91 protected void setBinding(String binding) {
92 this.binding = binding;
93 }
94
95
96
97
98 protected CmisVersion getCmisVersion() {
99 return cmisVersion;
100 }
101
102 protected void setCmisVersion(CmisVersion cmisVersion) {
103 this.cmisVersion = cmisVersion;
104 }
105
106
107
108
109 protected CmisServiceFactory getServiceFactory() {
110 return factory;
111 }
112
113
114
115
116 protected CallContextHandler getCallContextHandler() {
117 return callContextHandler;
118 }
119
120
121
122
123
124 protected void checkCsrfToken(HttpServletRequest req, HttpServletResponse resp, boolean isRepositoryInfoRequest,
125 boolean isContentRequest) {
126 csrfManager.check(req, resp, isRepositoryInfoRequest, isContentRequest);
127 }
128
129
130
131
132 protected CallContext createContext(ServletContext servletContext, HttpServletRequest request,
133 HttpServletResponse response, TempStoreOutputStreamFactory streamFactory) {
134 String[] pathFragments = HttpUtils.splitPath(request);
135
136 String repositoryId = null;
137 if (pathFragments.length > 0) {
138 repositoryId = pathFragments[0];
139 }
140
141 if (repositoryId == null && CallContext.BINDING_ATOMPUB.equals(binding)) {
142
143
144 repositoryId = HttpUtils.getStringParameter(request, Constants.PARAM_REPOSITORY_ID);
145 }
146
147 CallContextImpl context = null;
148
149 if (CallContext.BINDING_BROWSER.equals(binding)) {
150 context = new BrowserCallContextImpl(binding, cmisVersion, repositoryId, servletContext, request, response,
151 factory, streamFactory);
152 } else {
153 context = new CallContextImpl(binding, cmisVersion, repositoryId, servletContext, request, response,
154 factory, streamFactory);
155 }
156
157
158 context.setRange(request.getHeader("Range"));
159
160
161 context.setAcceptLanguage(request.getHeader("Accept-Language"));
162
163
164 if (callContextHandler != null) {
165 Map<String, String> callContextMap = callContextHandler.getCallContextMap(request);
166 if (callContextMap != null) {
167 for (Map.Entry<String, String> e : callContextMap.entrySet()) {
168 context.put(e.getKey(), e.getValue());
169 }
170 }
171 }
172
173 return context;
174 }
175
176 protected static String createLogMessage(Throwable t, HttpServletRequest request) {
177 StringBuilder sb = new StringBuilder(256);
178
179 sb.append(t.getMessage());
180
181 sb.append(" (");
182 sb.append(request.getMethod());
183 sb.append(' ');
184 sb.append(request.getRequestURL().toString());
185 if (request.getQueryString() != null) {
186 sb.append('?');
187 sb.append(request.getQueryString());
188 }
189 sb.append(')');
190
191 return sb.toString();
192 }
193 }