1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.client.bindings.spi.webservices;
20
21 import java.io.StringWriter;
22 import java.math.BigInteger;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.xml.transform.OutputKeys;
27 import javax.xml.transform.Transformer;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.dom.DOMSource;
30 import javax.xml.transform.stream.StreamResult;
31
32 import org.apache.chemistry.opencmis.client.bindings.impl.CmisBindingsHelper;
33 import org.apache.chemistry.opencmis.client.bindings.impl.RepositoryInfoCache;
34 import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
35 import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
36 import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
37 import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
38 import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
39 import org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException;
40 import org.apache.chemistry.opencmis.commons.exceptions.CmisFilterNotValidException;
41 import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
42 import org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException;
43 import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
44 import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
45 import org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException;
46 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
47 import org.apache.chemistry.opencmis.commons.exceptions.CmisStorageException;
48 import org.apache.chemistry.opencmis.commons.exceptions.CmisStreamNotSupportedException;
49 import org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException;
50 import org.apache.chemistry.opencmis.commons.exceptions.CmisVersioningException;
51 import org.apache.chemistry.opencmis.commons.impl.XMLUtils;
52 import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisException;
53 import org.w3c.dom.Node;
54 import org.w3c.dom.NodeList;
55
56
57
58
59 public abstract class AbstractWebServicesService {
60
61 private static final String ADDITIONAL_DATA_NS = "http://chemistry.apache.org/opencmis/exception";
62 private static final String ADDITIONAL_DATA_TAG = "additionalData";
63 private BindingSession session;
64
65
66
67
68 protected void setSession(BindingSession session) {
69 this.session = session;
70 }
71
72
73
74
75 protected BindingSession getSession() {
76 return session;
77 }
78
79
80
81
82 protected CmisBaseException convertException(CmisException ex) {
83 if ((ex == null) || (ex.getFaultInfo() == null)) {
84 return new CmisRuntimeException("CmisException has no fault!");
85 }
86
87 String msg = ex.getFaultInfo().getMessage();
88 BigInteger code = ex.getFaultInfo().getCode();
89
90 String errorContent = null;
91 Map<String, String> additionalData = null;
92 if (!ex.getFaultInfo().getAny().isEmpty()) {
93 StringBuilder sb = new StringBuilder(1024);
94
95 for (Object o : ex.getFaultInfo().getAny()) {
96 if (o != null) {
97 if (o instanceof Node) {
98 Node node = (Node) o;
99
100 if (ADDITIONAL_DATA_NS.equals(node.getNamespaceURI())
101 && ADDITIONAL_DATA_TAG.equals(node.getNodeName())) {
102 NodeList entries = node.getChildNodes();
103 int n = entries.getLength();
104 for (int i = 0; i < n; i++) {
105 Node entry = entries.item(i);
106 if (!"entry".equals(entry.getNodeName())) {
107 continue;
108 }
109
110 String key = null;
111 String value = null;
112
113 NodeList keyValueList = entry.getChildNodes();
114 int n2 = keyValueList.getLength();
115 for (int j = 0; j < n2; j++) {
116 Node item = keyValueList.item(j);
117 if ("key".equals(item.getNodeName())) {
118 key = item.getTextContent();
119 } else if ("value".equals(item.getNodeName())) {
120 value = item.getTextContent();
121 }
122 }
123
124 if (key == null || value == null) {
125 continue;
126 }
127
128 if (additionalData == null) {
129 additionalData = new HashMap<String, String>();
130 }
131
132 additionalData.put(key, value);
133 }
134 }
135
136 sb.append(getNodeAsString(node));
137 } else {
138 sb.append(o.toString());
139 }
140 sb.append('\n');
141 }
142 }
143 errorContent = sb.toString();
144 }
145
146 switch (ex.getFaultInfo().getType()) {
147 case CONSTRAINT:
148 return new CmisConstraintException(msg, code, errorContent, additionalData);
149 case CONTENT_ALREADY_EXISTS:
150 return new CmisContentAlreadyExistsException(msg, code, errorContent, additionalData);
151 case FILTER_NOT_VALID:
152 return new CmisFilterNotValidException(msg, code, errorContent, additionalData);
153 case INVALID_ARGUMENT:
154 return new CmisInvalidArgumentException(msg, code, errorContent, additionalData);
155 case NAME_CONSTRAINT_VIOLATION:
156 return new CmisNameConstraintViolationException(msg, code, errorContent, additionalData);
157 case NOT_SUPPORTED:
158 return new CmisNotSupportedException(msg, code, errorContent, additionalData);
159 case OBJECT_NOT_FOUND:
160 return new CmisObjectNotFoundException(msg, code, errorContent, additionalData);
161 case PERMISSION_DENIED:
162 return new CmisPermissionDeniedException(msg, code, errorContent, additionalData);
163 case RUNTIME:
164 return new CmisRuntimeException(msg, code, errorContent, additionalData);
165 case STORAGE:
166 return new CmisStorageException(msg, code, errorContent, additionalData);
167 case STREAM_NOT_SUPPORTED:
168 return new CmisStreamNotSupportedException(msg, code, errorContent, additionalData);
169 case UPDATE_CONFLICT:
170 return new CmisUpdateConflictException(msg, code, errorContent, additionalData);
171 case VERSIONING:
172 return new CmisVersioningException(msg, code, errorContent, additionalData);
173 default:
174 }
175
176 return new CmisRuntimeException("Unknown exception[" + ex.getFaultInfo().getType().value() + "]: " + msg);
177 }
178
179 private static String getNodeAsString(Node node) {
180 try {
181 Transformer transformer = XMLUtils.newTransformer();
182 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
183
184
185 StringWriter sw = new StringWriter(512);
186 transformer.transform(new DOMSource(node), new StreamResult(sw));
187 return sw.toString();
188 } catch (TransformerException e) {
189 assert false;
190 }
191
192 return "";
193 }
194
195
196
197
198 protected CmisVersion getCmisVersion(String repositoryId) {
199 if (CmisBindingsHelper.getForcedCmisVersion(session) != null) {
200 return CmisBindingsHelper.getForcedCmisVersion(session);
201 }
202
203 RepositoryInfoCache cache = CmisBindingsHelper.getRepositoryInfoCache(session);
204 RepositoryInfo info = cache.get(repositoryId);
205
206 if (info == null) {
207 info = CmisBindingsHelper.getSPI(session).getRepositoryService().getRepositoryInfo(repositoryId, null);
208 if (info != null) {
209 cache.put(info);
210 }
211 }
212
213
214 return info == null ? CmisVersion.CMIS_1_0 : info.getCmisVersion();
215 }
216 }