This project has retired. For details please refer to its
Attic page.
AbstractWebServicesService 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.client.bindings.spi.webservices;
20
21 import java.io.StringWriter;
22 import java.math.BigInteger;
23
24 import javax.xml.transform.OutputKeys;
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.transform.TransformerFactory;
28 import javax.xml.transform.dom.DOMSource;
29 import javax.xml.transform.stream.StreamResult;
30
31 import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
32 import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
33 import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
34 import org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException;
35 import org.apache.chemistry.opencmis.commons.exceptions.CmisFilterNotValidException;
36 import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
37 import org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException;
38 import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
39 import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
40 import org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException;
41 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
42 import org.apache.chemistry.opencmis.commons.exceptions.CmisStorageException;
43 import org.apache.chemistry.opencmis.commons.exceptions.CmisStreamNotSupportedException;
44 import org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException;
45 import org.apache.chemistry.opencmis.commons.exceptions.CmisVersioningException;
46 import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisException;
47 import org.w3c.dom.Node;
48
49
50
51
52 public abstract class AbstractWebServicesService {
53
54 private BindingSession fSession;
55
56
57
58
59 protected void setSession(BindingSession session) {
60 fSession = session;
61 }
62
63
64
65
66 protected BindingSession getSession() {
67 return fSession;
68 }
69
70
71
72
73 protected CmisBaseException convertException(CmisException ex) {
74 if ((ex == null) || (ex.getFaultInfo() == null)) {
75 return new CmisRuntimeException("CmisException has no fault!");
76 }
77
78 String msg = ex.getFaultInfo().getMessage();
79 BigInteger code = ex.getFaultInfo().getCode();
80
81 String errorContent = null;
82 if (ex.getFaultInfo().getAny().size() > 0) {
83 StringBuilder sb = new StringBuilder();
84 for (Object o : ex.getFaultInfo().getAny()) {
85 if (o != null) {
86 if (o instanceof Node) {
87 sb.append(getNodeAsString((Node) o));
88 } else {
89 sb.append(o.toString());
90 }
91 sb.append('\n');
92 }
93 }
94 errorContent = sb.toString();
95 }
96
97 switch (ex.getFaultInfo().getType()) {
98 case CONSTRAINT:
99 return new CmisConstraintException(msg, code, errorContent);
100 case CONTENT_ALREADY_EXISTS:
101 return new CmisContentAlreadyExistsException(msg, code, errorContent);
102 case FILTER_NOT_VALID:
103 return new CmisFilterNotValidException(msg, code, errorContent);
104 case INVALID_ARGUMENT:
105 return new CmisInvalidArgumentException(msg, code, errorContent);
106 case NAME_CONSTRAINT_VIOLATION:
107 return new CmisNameConstraintViolationException(msg, code, errorContent);
108 case NOT_SUPPORTED:
109 return new CmisNotSupportedException(msg, code, errorContent);
110 case OBJECT_NOT_FOUND:
111 return new CmisObjectNotFoundException(msg, code, errorContent);
112 case PERMISSION_DENIED:
113 return new CmisPermissionDeniedException(msg, code, errorContent);
114 case RUNTIME:
115 return new CmisRuntimeException(msg, code, errorContent);
116 case STORAGE:
117 return new CmisStorageException(msg, code, errorContent);
118 case STREAM_NOT_SUPPORTED:
119 return new CmisStreamNotSupportedException(msg, code, errorContent);
120 case UPDATE_CONFLICT:
121 return new CmisUpdateConflictException(msg, code, errorContent);
122 case VERSIONING:
123 return new CmisVersioningException(msg, code, errorContent);
124 }
125
126 return new CmisRuntimeException("Unknown exception[" + ex.getFaultInfo().getType().value() + "]: " + msg);
127 }
128
129 private static String getNodeAsString(Node node) {
130 try {
131 TransformerFactory factory = TransformerFactory.newInstance();
132 Transformer transformrt = factory.newTransformer();
133 transformrt.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
134
135
136 StringWriter sw = new StringWriter();
137 transformrt.transform(new DOMSource(node), new StreamResult(sw));
138 return sw.toString();
139 } catch (TransformerException e) {
140 }
141
142 return "";
143 }
144 }