001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.provider.webdav4;
018
019import java.lang.reflect.Constructor;
020
021import org.apache.commons.vfs2.FileSystemException;
022import org.apache.jackrabbit.webdav.DavConstants;
023import org.apache.jackrabbit.webdav.DavException;
024import org.apache.jackrabbit.webdav.xml.DomUtil;
025import org.w3c.dom.Element;
026
027/**
028 * Converts WebDAV exceptions into FileSystemExceptions.
029 *
030 * @since 2.5.0
031 */
032public final class ExceptionConverter {
033
034    /**
035     * Generates a new instance of FileSystemException.
036     *
037     * @param cause The cause of the new exception.
038     * @return A new FileSystemException.
039     * @throws FileSystemException If an Exception is caught while generating a new instance.
040     */
041    public static FileSystemException generate(final DavException cause) throws FileSystemException {
042        String msg = cause.getMessage();
043        if (cause.hasErrorCondition()) {
044            try {
045                final Element error = cause.toXml(DomUtil.createDocument());
046                if (DomUtil.matches(error, DavException.XML_ERROR, DavConstants.NAMESPACE) && DomUtil.hasChildElement(error, "exception", null)) {
047                    final Element exc = DomUtil.getChildElement(error, "exception", null);
048                    if (DomUtil.hasChildElement(exc, "message", null)) {
049                        msg = DomUtil.getChildText(exc, "message", null);
050                    }
051                    if (DomUtil.hasChildElement(exc, "class", null)) {
052                        final Class<?> cl = Class.forName(DomUtil.getChildText(exc, "class", null));
053                        final Constructor<?> excConstr = cl.getConstructor(String.class);
054                        final Object o = excConstr.newInstance(msg);
055                        if (o instanceof FileSystemException) {
056                            return (FileSystemException) o;
057                        }
058                        if (o instanceof Exception) {
059                            return new FileSystemException(msg, (Exception) o);
060                        }
061                    }
062                }
063            } catch (final Exception e) {
064                throw new FileSystemException(e);
065            }
066        }
067
068        return new FileSystemException(msg);
069    }
070
071    /** Disallow instantiation. */
072    private ExceptionConverter() {
073    }
074}