This project has retired. For details please refer to its Attic page.
AbstractWebServicesService xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Base class for all Web Services clients.
51   */
52  public abstract class AbstractWebServicesService {
53  
54      private BindingSession fSession;
55  
56      /**
57       * Sets the current session.
58       */
59      protected void setSession(BindingSession session) {
60          fSession = session;
61      }
62  
63      /**
64       * Gets the current session.
65       */
66      protected BindingSession getSession() {
67          return fSession;
68      }
69  
70      /**
71       * Converts a Web Services Exception into a CMIS Client exception.
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             // transformrt.setOutputProperty(OutputKeys.INDENT, "yes");
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 }