This project has retired. For details please refer to its
Attic page.
AbstractService 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.impl.webservices;
20
21 import java.math.BigInteger;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.xml.ws.WebServiceContext;
29 import javax.xml.ws.handler.MessageContext;
30
31 import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
32 import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
33 import org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException;
34 import org.apache.chemistry.opencmis.commons.exceptions.CmisFilterNotValidException;
35 import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
36 import org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException;
37 import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
38 import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
39 import org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException;
40 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
41 import org.apache.chemistry.opencmis.commons.exceptions.CmisStorageException;
42 import org.apache.chemistry.opencmis.commons.exceptions.CmisStreamNotSupportedException;
43 import org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException;
44 import org.apache.chemistry.opencmis.commons.exceptions.CmisVersioningException;
45 import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisException;
46 import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisFaultType;
47 import org.apache.chemistry.opencmis.commons.impl.jaxb.EnumServiceException;
48 import org.apache.chemistry.opencmis.commons.server.CallContext;
49 import org.apache.chemistry.opencmis.commons.server.CmisService;
50 import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
51 import org.apache.chemistry.opencmis.server.impl.CallContextImpl;
52 import org.apache.chemistry.opencmis.server.impl.CmisRepositoryContextListener;
53 import org.apache.chemistry.opencmis.server.impl.ServerVersion;
54 import org.apache.chemistry.opencmis.server.shared.ExceptionHelper;
55 import org.w3c.dom.Node;
56
57
58
59
60 public abstract class AbstractService {
61
62 public static final String CALL_CONTEXT_MAP = "org.apache.chemistry.opencmis.callcontext";
63
64
65
66
67 protected CmisServiceFactory getServiceFactory(WebServiceContext wsContext) {
68 ServletContext servletContext = (ServletContext) wsContext.getMessageContext().get(
69 MessageContext.SERVLET_CONTEXT);
70
71
72 CmisServiceFactory factory = (CmisServiceFactory) servletContext
73 .getAttribute(CmisRepositoryContextListener.SERVICES_FACTORY);
74
75 if (factory == null) {
76 throw new CmisRuntimeException("Service factory not available! Configuration problem?");
77 }
78
79 HttpServletResponse httpResp = (HttpServletResponse) wsContext.getMessageContext().get(
80 MessageContext.SERVLET_RESPONSE);
81 httpResp.setHeader("Server", ServerVersion.OPENCMIS_SERVER);
82
83 return factory;
84 }
85
86
87
88
89 @SuppressWarnings("unchecked")
90 protected CallContext createContext(WebServiceContext wsContext, String repositoryId) {
91 CallContextImpl context = new CallContextImpl(CallContext.BINDING_WEBSERVICES, repositoryId, false);
92
93 MessageContext mc = wsContext.getMessageContext();
94 Map<String, String> callContextMap = (Map<String, String>) mc.get(CALL_CONTEXT_MAP);
95 if (callContextMap != null) {
96 for (Map.Entry<String, String> e : callContextMap.entrySet()) {
97 context.put(e.getKey(), e.getValue());
98 }
99 }
100
101 ServletContext servletContext = (ServletContext) wsContext.getMessageContext().get(
102 MessageContext.SERVLET_CONTEXT);
103 context.put(CallContext.SERVLET_CONTEXT, servletContext);
104
105 HttpServletRequest request = (HttpServletRequest) wsContext.getMessageContext().get(
106 MessageContext.SERVLET_REQUEST);
107 context.put(CallContext.HTTP_SERVLET_REQUEST, request);
108
109 HttpServletResponse response = (HttpServletResponse) wsContext.getMessageContext().get(
110 MessageContext.SERVLET_RESPONSE);
111 context.put(CallContext.HTTP_SERVLET_RESPONSE, response);
112
113 Map<String, List<String>> headers = (Map<String, List<String>>) wsContext.getMessageContext().get(
114 MessageContext.HTTP_REQUEST_HEADERS);
115 if (headers != null) {
116 for (Map.Entry<String, List<String>> header : headers.entrySet()) {
117 if (header.getKey().equalsIgnoreCase("Accept-Language") && (header.getValue() != null)) {
118 String acceptLanguage = header.getValue().get(0);
119 if (acceptLanguage != null) {
120 String[] locale = acceptLanguage.split("-");
121 context.put(CallContext.LOCALE_ISO639_LANGUAGE, locale[0].trim());
122 if (locale.length > 1) {
123 int x = locale[1].indexOf(',');
124 if (x == -1) {
125 context.put(CallContext.LOCALE_ISO3166_COUNTRY, locale[1].trim());
126 } else {
127 context.put(CallContext.LOCALE_ISO3166_COUNTRY, locale[1].substring(0, x).trim());
128 }
129 }
130 }
131 break;
132 }
133 }
134 }
135
136 return context;
137 }
138
139
140
141
142 protected CmisService getService(WebServiceContext wsContext, String repositoryId) {
143 CmisServiceFactory factory = getServiceFactory(wsContext);
144 CallContext context = createContext(wsContext, repositoryId);
145 return factory.getService(context);
146 }
147
148
149
150
151 protected void closeService(CmisService service) {
152 if (service != null) {
153 service.close();
154 }
155 }
156
157
158
159
160 protected CmisException convertException(Exception ex) {
161 CmisFaultType fault = new CmisFaultType();
162 fault.setMessage("Unknown exception");
163 fault.setCode(BigInteger.ZERO);
164 fault.setType(EnumServiceException.RUNTIME);
165
166 if (ex != null) {
167 fault.setMessage(ex.getMessage());
168
169 if (ex instanceof CmisBaseException) {
170 fault.setCode(((CmisBaseException) ex).getCode());
171 }
172
173 if (ex instanceof CmisConstraintException) {
174 fault.setType(EnumServiceException.CONSTRAINT);
175 } else if (ex instanceof CmisContentAlreadyExistsException) {
176 fault.setType(EnumServiceException.CONTENT_ALREADY_EXISTS);
177 } else if (ex instanceof CmisFilterNotValidException) {
178 fault.setType(EnumServiceException.FILTER_NOT_VALID);
179 } else if (ex instanceof CmisInvalidArgumentException) {
180 fault.setType(EnumServiceException.INVALID_ARGUMENT);
181 } else if (ex instanceof CmisNameConstraintViolationException) {
182 fault.setType(EnumServiceException.NAME_CONSTRAINT_VIOLATION);
183 } else if (ex instanceof CmisNotSupportedException) {
184 fault.setType(EnumServiceException.NOT_SUPPORTED);
185 } else if (ex instanceof CmisObjectNotFoundException) {
186 fault.setType(EnumServiceException.OBJECT_NOT_FOUND);
187 } else if (ex instanceof CmisPermissionDeniedException) {
188 fault.setType(EnumServiceException.PERMISSION_DENIED);
189 } else if (ex instanceof CmisStorageException) {
190 fault.setType(EnumServiceException.STORAGE);
191 } else if (ex instanceof CmisStreamNotSupportedException) {
192 fault.setType(EnumServiceException.STREAM_NOT_SUPPORTED);
193 } else if (ex instanceof CmisUpdateConflictException) {
194 fault.setType(EnumServiceException.UPDATE_CONFLICT);
195 } else if (ex instanceof CmisVersioningException) {
196 fault.setType(EnumServiceException.VERSIONING);
197 }
198
199 Node node = ExceptionHelper.getStacktraceAsNode(ex);
200 if (node != null) {
201 fault.getAny().add(node);
202 }
203 }
204
205 return new CmisException(fault.getMessage(), fault, ex);
206 }
207 }