Added: websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/AbstractServiceCall.html ============================================================================== --- websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/AbstractServiceCall.html (added) +++ websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/AbstractServiceCall.html Tue May 28 07:22:41 2013 @@ -0,0 +1,210 @@ + + + + +AbstractServiceCall xref + + + +
View Javadoc
+
+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.server.shared;
+20  
+21  import java.io.IOException;
+22  import java.math.BigInteger;
+23  import java.util.Date;
+24  import java.util.GregorianCalendar;
+25  
+26  import javax.servlet.http.HttpServletRequest;
+27  import javax.servlet.http.HttpServletResponse;
+28  
+29  import org.apache.chemistry.opencmis.commons.data.CacheHeaderContentStream;
+30  import org.apache.chemistry.opencmis.commons.data.ContentLengthContentStream;
+31  import org.apache.chemistry.opencmis.commons.data.ContentStream;
+32  import org.apache.chemistry.opencmis.commons.data.LastModifiedContentStream;
+33  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
+34  import org.apache.chemistry.opencmis.commons.impl.CmisEnumHelper;
+35  import org.apache.chemistry.opencmis.commons.impl.DateTimeHelper;
+36  
+37  public abstract class AbstractServiceCall implements ServiceCall {
+38  
+39      /**
+40       * Extracts a string parameter.
+41       */
+42      public String getStringParameter(HttpServletRequest request, String name) {
+43          return HttpUtils.getStringParameter(request, name);
+44      }
+45  
+46      /**
+47       * Extracts a boolean parameter (with default).
+48       */
+49      public boolean getBooleanParameter(HttpServletRequest request, String name, boolean def) {
+50          String value = getStringParameter(request, name);
+51          if ((value == null) || (value.length() == 0)) {
+52              return def;
+53          }
+54  
+55          return Boolean.valueOf(value);
+56      }
+57  
+58      /**
+59       * Extracts a boolean parameter.
+60       */
+61      public Boolean getBooleanParameter(HttpServletRequest request, String name) {
+62          String value = getStringParameter(request, name);
+63          if ((value == null) || (value.length() == 0)) {
+64              return null;
+65          }
+66  
+67          return Boolean.valueOf(value);
+68      }
+69  
+70      /**
+71       * Extracts an integer parameter (with default).
+72       */
+73      public BigInteger getBigIntegerParameter(HttpServletRequest request, String name, long def) {
+74          BigInteger result = getBigIntegerParameter(request, name);
+75          if (result == null) {
+76              result = BigInteger.valueOf(def);
+77          }
+78  
+79          return result;
+80      }
+81  
+82      /**
+83       * Extracts an integer parameter.
+84       */
+85      public BigInteger getBigIntegerParameter(HttpServletRequest request, String name) {
+86          String value = getStringParameter(request, name);
+87          if ((value == null) || (value.length() == 0)) {
+88              return null;
+89          }
+90  
+91          try {
+92              return new BigInteger(value);
+93          } catch (Exception e) {
+94              throw new CmisInvalidArgumentException("Invalid parameter '" + name + "'!");
+95          }
+96      }
+97  
+98      /**
+99       * Extracts an enum parameter.
+100      */
+101     public <T extends Enum<T>> T getEnumParameter(HttpServletRequest request, String name, Class<T> clazz) {
+102         return CmisEnumHelper.fromValue(getStringParameter(request, name), clazz);
+103     }
+104 
+105     /**
+106      * Closes a content stream.
+107      */
+108     public void closeContentStream(ContentStream contentStream) {
+109         if (contentStream != null && contentStream.getStream() != null) {
+110             try {
+111                 contentStream.getStream().close();
+112             } catch (IOException e) {
+113                 // we tried our best
+114             }
+115         }
+116     }
+117 
+118     /**
+119      * Sets certain HTTP headers if the server implementation requested them.
+120      * 
+121      * @return <code>true</code> if the request has been served by this method
+122      *         (e.g. status code 304 was send), <code>false</code> if the
+123      *         content should be served.
+124      */
+125     public boolean sendContentStreamHeaders(ContentStream content, HttpServletRequest request,
+126             HttpServletResponse response) throws IOException {
+127 
+128         // check if Last-Modified header should be set
+129         if (content instanceof LastModifiedContentStream) {
+130             GregorianCalendar lastModified = ((LastModifiedContentStream) content).getLastModified();
+131             if (lastModified != null) {
+132                 long lastModifiedSecs = (long) Math.floor((double) lastModified.getTimeInMillis() / 1000);
+133 
+134                 Date modifiedSince = DateTimeHelper.parseHttpDateTime(request.getHeader("If-Modified-Since"));
+135                 if (modifiedSince != null) {
+136                     long modifiedSinceSecs = (long) Math.floor((double) modifiedSince.getTime() / 1000);
+137 
+138                     if (modifiedSinceSecs >= lastModifiedSecs) {
+139                         // close stream
+140                         content.getStream().close();
+141 
+142                         // send not modified status code
+143                         response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+144                         response.setContentLength(0);
+145                         return true;
+146                     }
+147                 }
+148 
+149                 response.setHeader("Last-Modified", DateTimeHelper.formatHttpDateTime(lastModifiedSecs * 1000));
+150             }
+151         }
+152 
+153         // check if cache headers should be set
+154         if (content instanceof CacheHeaderContentStream) {
+155             CacheHeaderContentStream chcs = (CacheHeaderContentStream) content;
+156 
+157             if (chcs.getETag() != null) {
+158                 String etag = request.getHeader("If-None-Match");
+159                 if (etag != null && !etag.equals("*")) {
+160                     if (etag.length() > 2 && etag.startsWith("\"") && etag.endsWith("\"")) {
+161                         etag = etag.substring(1, etag.length() - 1);
+162                     }
+163 
+164                     if (chcs.getETag().equals(etag)) {
+165                         // close stream
+166                         content.getStream().close();
+167 
+168                         // send not modified status code
+169                         response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+170                         response.setContentLength(0);
+171                         return true;
+172                     }
+173                 }
+174 
+175                 response.setHeader("ETag", "\"" + chcs.getETag() + "\"");
+176             }
+177 
+178             if (chcs.getCacheControl() != null) {
+179                 response.setHeader("Cache-Control", chcs.getCacheControl());
+180             }
+181 
+182             if (chcs.getExpires() != null) {
+183                 response.setHeader("Expires", DateTimeHelper.formatHttpDateTime(chcs.getExpires()));
+184             }
+185         }
+186 
+187         // check if Content-Length header should be set
+188         if (content instanceof ContentLengthContentStream) {
+189             if (content.getBigLength() != null && content.getBigLength().signum() >= 0) {
+190                 response.setHeader("Content-Length", content.getBigLength().toString());
+191             }
+192         }
+193 
+194         return false;
+195     }
+196 }
+
+
+ + Added: websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/BasicAuthCallContextHandler.html ============================================================================== --- websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/BasicAuthCallContextHandler.html (added) +++ websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/BasicAuthCallContextHandler.html Tue May 28 07:22:41 2013 @@ -0,0 +1,89 @@ + + + + +BasicAuthCallContextHandler xref + + + +
View Javadoc
+
+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.server.shared;
+20  
+21  import java.io.Serializable;
+22  import java.util.HashMap;
+23  import java.util.Locale;
+24  import java.util.Map;
+25  
+26  import javax.servlet.http.HttpServletRequest;
+27  
+28  import org.apache.chemistry.opencmis.commons.impl.Base64;
+29  import org.apache.chemistry.opencmis.commons.server.CallContext;
+30  
+31  /**
+32   * Call Context handler that handles basic authentication.
+33   */
+34  public class BasicAuthCallContextHandler implements CallContextHandler, Serializable {
+35  
+36      private static final long serialVersionUID = 1L;
+37  
+38      /**
+39       * Constructor.
+40       */
+41      public BasicAuthCallContextHandler() {
+42      }
+43  
+44      public Map<String, String> getCallContextMap(HttpServletRequest request) {
+45          Map<String, String> result = null;
+46  
+47          String authHeader = request.getHeader("Authorization");
+48          if ((authHeader != null) && (authHeader.trim().toLowerCase(Locale.ENGLISH).startsWith("basic "))) {
+49              int x = authHeader.lastIndexOf(' ');
+50              if (x == -1) {
+51                  return result;
+52              }
+53  
+54              String credentials = null;
+55              try {
+56                  credentials = new String(Base64.decode(authHeader.substring(x + 1).getBytes("ISO-8859-1")),
+57                          "ISO-8859-1");
+58              } catch (Exception e) {
+59                  return result;
+60              }
+61  
+62              x = credentials.indexOf(':');
+63              if (x == -1) {
+64                  return result;
+65              }
+66  
+67              // extract user and password and add them to map
+68              result = new HashMap<String, String>();
+69              result.put(CallContext.USERNAME, credentials.substring(0, x));
+70              result.put(CallContext.PASSWORD, credentials.substring(x + 1));
+71          }
+72  
+73          return result;
+74      }
+75  }
+
+
+ + Added: websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/CallContextHandler.html ============================================================================== --- websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/CallContextHandler.html (added) +++ websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/CallContextHandler.html Tue May 28 07:22:41 2013 @@ -0,0 +1,50 @@ + + + + +CallContextHandler xref + + + +
View Javadoc
+
+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.server.shared;
+20  
+21  import java.util.Map;
+22  
+23  import javax.servlet.http.HttpServletRequest;
+24  
+25  import org.apache.chemistry.opencmis.commons.server.CallContext;
+26  
+27  /**
+28   * Call Context handler interface.
+29   */
+30  public interface CallContextHandler {
+31  
+32      /**
+33       * Returns key-value pairs that will be added to the {@link CallContext}.
+34       */
+35      Map<String, String> getCallContextMap(HttpServletRequest request);
+36  }
+
+
+ + Added: websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/CappedInputStream.html ============================================================================== --- websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/CappedInputStream.html (added) +++ websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/CappedInputStream.html Tue May 28 07:22:41 2013 @@ -0,0 +1,153 @@ + + + + +CappedInputStream xref + + + +
View Javadoc
+
+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.server.shared;
+20  
+21  import java.io.IOException;
+22  import java.io.InputStream;
+23  import java.io.UnsupportedEncodingException;
+24  
+25  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
+26  
+27  /**
+28   * A stream that counts bytes and throws an exception if the given maximum is
+29   * reached. Counted bytes can be deducted to excludes parts of stream from the
+30   * length limitation.
+31   */
+32  public class CappedInputStream extends InputStream {
+33  
+34      private InputStream stream;
+35      private long max;
+36      private long counter;
+37  
+38      public CappedInputStream(InputStream stream, long max) {
+39          this.stream = stream;
+40          this.max = max;
+41          this.counter = 0;
+42      }
+43  
+44      /**
+45       * Returns the counter;
+46       */
+47      public long getCounter() {
+48          return counter;
+49      }
+50  
+51      /**
+52       * Deducts the byte counter.
+53       */
+54      public void deductBytes(int byteCount) {
+55          counter -= byteCount;
+56      }
+57  
+58      /**
+59       * Deducts the byte counter.
+60       */
+61      public void deductString(String s, String encoding) throws UnsupportedEncodingException {
+62          if (encoding == null) {
+63              counter -= s.getBytes("UTF-8").length;
+64          } else {
+65              counter -= s.getBytes(encoding).length;
+66          }
+67      }
+68  
+69      private void checkLength() throws IOException {
+70          if (counter > max) {
+71              throw new CmisInvalidArgumentException("Limit exceeded!");
+72          }
+73      }
+74  
+75      @Override
+76      public int available() throws IOException {
+77          return stream.available();
+78      }
+79  
+80      @Override
+81      public synchronized void mark(int readlimit) {
+82      }
+83  
+84      @Override
+85      public synchronized void reset() throws IOException {
+86      }
+87  
+88      @Override
+89      public boolean markSupported() {
+90          return false;
+91      }
+92  
+93      @Override
+94      public int read() throws IOException {
+95          checkLength();
+96  
+97          int b = stream.read();
+98          if (b > -1) {
+99              counter++;
+100         }
+101 
+102         return b;
+103     }
+104 
+105     @Override
+106     public int read(byte[] b, int off, int len) throws IOException {
+107         checkLength();
+108 
+109         int l = stream.read(b, off, len);
+110         counter += l;
+111 
+112         return l;
+113     }
+114 
+115     @Override
+116     public int read(byte[] b) throws IOException {
+117         checkLength();
+118 
+119         int l = stream.read(b);
+120         counter += l;
+121 
+122         return l;
+123     }
+124 
+125     @Override
+126     public long skip(long n) throws IOException {
+127         checkLength();
+128 
+129         long l = stream.skip(n);
+130         counter += l;
+131 
+132         return l;
+133     }
+134 
+135     @Override
+136     public void close() throws IOException {
+137         stream.close();
+138     }
+139 }
+
+
+ + Added: websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/Dispatcher.html ============================================================================== --- websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/Dispatcher.html (added) +++ websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/Dispatcher.html Tue May 28 07:22:41 2013 @@ -0,0 +1,118 @@ + + + + +Dispatcher xref + + + +
View Javadoc
+
+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.server.shared;
+20  
+21  import java.io.Serializable;
+22  import java.util.HashMap;
+23  
+24  import javax.servlet.http.HttpServletRequest;
+25  import javax.servlet.http.HttpServletResponse;
+26  
+27  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
+28  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
+29  import org.apache.chemistry.opencmis.commons.server.CallContext;
+30  import org.apache.chemistry.opencmis.commons.server.CmisService;
+31  import org.slf4j.Logger;
+32  import org.slf4j.LoggerFactory;
+33  
+34  /**
+35   * Dispatcher for the AtomPub and Browser binding servlet.
+36   */
+37  public class Dispatcher implements Serializable {
+38  
+39      private static final long serialVersionUID = 1L;
+40  
+41      public static final String BASE_URL_ATTRIBUTE = "org.apache.chemistry.opencmis.baseurl";
+42  
+43      public static final String METHOD_GET = "GET";
+44      public static final String METHOD_POST = "POST";
+45      public static final String METHOD_PUT = "PUT";
+46      public static final String METHOD_DELETE = "DELETE";
+47  
+48      private static final Logger LOG = LoggerFactory.getLogger(Dispatcher.class.getName());
+49  
+50      private final boolean caseSensitive;
+51      private final HashMap<String, ServiceCall> serviceCallMap;
+52  
+53      public Dispatcher() {
+54          this(true);
+55      }
+56  
+57      public Dispatcher(boolean caseSensitive) {
+58          this.caseSensitive = caseSensitive;
+59          serviceCallMap = new HashMap<String, ServiceCall>();
+60      }
+61  
+62      /**
+63       * Connects a resource and HTTP method with an object that handles the call.
+64       */
+65      public void addResource(String resource, String httpMethod, ServiceCall serviceCall) {
+66          serviceCallMap.put(getKey(resource, httpMethod), serviceCall);
+67      }
+68  
+69      /**
+70       * Handles the a call.
+71       * 
+72       * @return <code>true</code> if an object was found that can handle the
+73       *         request, <code>false</code> otherwise.
+74       */
+75      public boolean dispatch(String resource, String httpMethod, CallContext context, CmisService service,
+76              String repositoryId, HttpServletRequest request, HttpServletResponse response) {
+77          ServiceCall serviceCall = serviceCallMap.get(getKey(resource, httpMethod));
+78          if (serviceCall == null) {
+79              return false;
+80          }
+81  
+82          if (LOG.isDebugEnabled()) {
+83              LOG.debug(repositoryId + " / " + resource + ", " + httpMethod + " -> " + serviceCall.getClass().getName());
+84          }
+85  
+86          try {
+87              serviceCall.serve(context, service, repositoryId, request, response);
+88          } catch (CmisBaseException ce) {
+89              throw ce;
+90          } catch (Exception e) {
+91              throw new CmisRuntimeException(e.getMessage(), e);
+92          }
+93  
+94          return true;
+95      }
+96  
+97      /**
+98       * Generates a map key from a resource and an HTTP method.
+99       */
+100     private String getKey(String resource, String httpMethod) {
+101         String s = resource + "/" + httpMethod;
+102         return (caseSensitive ? s : s.toUpperCase());
+103     }
+104 }
+
+
+ + Added: websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/ExceptionHelper.html ============================================================================== --- websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/ExceptionHelper.html (added) +++ websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/shared/ExceptionHelper.html Tue May 28 07:22:41 2013 @@ -0,0 +1,93 @@ + + + + +ExceptionHelper xref + + + +
View Javadoc
+
+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.server.shared;
+20  
+21  import java.io.PrintWriter;
+22  import java.io.StringWriter;
+23  
+24  import org.apache.chemistry.opencmis.commons.impl.XMLUtils;
+25  import org.w3c.dom.Document;
+26  import org.w3c.dom.Element;
+27  import org.w3c.dom.Node;
+28  
+29  public final class ExceptionHelper {
+30  
+31      public static final String STACK_TRACE_PROPERTY = "org.apache.chemistry.opencmis.stacktrace.disable";
+32  
+33      private static final boolean SEND_STACK_TRACE;
+34  
+35      static {
+36          SEND_STACK_TRACE = System.getProperty(STACK_TRACE_PROPERTY) == null;
+37      }
+38  
+39      private ExceptionHelper() {
+40      }
+41  
+42      /**
+43       * Returns the stack trace as string.
+44       */
+45      public static String getStacktraceAsString(Throwable t) {
+46          if (!SEND_STACK_TRACE || t == null) {
+47              return null;
+48          }
+49  
+50          StringWriter sw = new StringWriter();
+51          PrintWriter pw = new PrintWriter(sw);
+52  
+53          t.printStackTrace(pw);
+54  
+55          return sw.toString();
+56      }
+57  
+58      /**
+59       * Returns the stack trace as DOM node.
+60       */
+61      public static Node getStacktraceAsNode(Throwable t) {
+62          try {
+63              String st = getStacktraceAsString(t);
+64              if (st != null) {
+65                  Document doc = XMLUtils.newDomDocument();
+66  
+67                  Element node = doc.createElementNS("http://chemistry.apache.org/opencmis/exception", "stacktrace");
+68                  doc.appendChild(node);
+69  
+70                  node.appendChild(doc.createTextNode(st));
+71  
+72                  return node;
+73              }
+74          } catch (Exception e) {
+75          }
+76  
+77          return null;
+78      }
+79  }
+
+
+ +