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.webdav;
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.client.methods.DavMethod;
025import org.apache.jackrabbit.webdav.xml.DomUtil;
026import org.w3c.dom.Element;
027
028/**
029 * Converts WebDAV exceptions into FileSystemExceptions.
030 *
031 * @since 2.0
032 */
033public final class ExceptionConverter {
034
035    /**
036     * Generates a new instance of FileSystemException.
037     *
038     * @param cause The cause of the new exception.
039     * @return A new FileSystemException.
040     * @throws FileSystemException If an Exception is caught while generating a new instance.
041     */
042    public static FileSystemException generate(final DavException cause) throws FileSystemException {
043        return generate(cause, null);
044    }
045
046    /**
047     * Generates a new instance of FileSystemException.
048     *
049     * @param cause The cause of the new exception.
050     * @param davMethod Ignored.
051     * @return A new FileSystemException.
052     * @throws FileSystemException If an Exception is caught while generating a new instance.
053     */
054    public static FileSystemException generate(final DavException cause, final DavMethod davMethod) throws FileSystemException {
055        String msg = cause.getMessage();
056        if (cause.hasErrorCondition()) {
057            try {
058                final Element error = cause.toXml(DomUtil.BUILDER_FACTORY.newDocumentBuilder().newDocument());
059                if (DomUtil.matches(error, DavException.XML_ERROR, DavConstants.NAMESPACE) && DomUtil.hasChildElement(error, "exception", null)) {
060                    final Element exc = DomUtil.getChildElement(error, "exception", null);
061                    if (DomUtil.hasChildElement(exc, "message", null)) {
062                        msg = DomUtil.getChildText(exc, "message", null);
063                    }
064                    if (DomUtil.hasChildElement(exc, "class", null)) {
065                        final Class<?> cl = Class.forName(DomUtil.getChildText(exc, "class", null));
066                        final Constructor<?> excConstr = cl.getConstructor(String.class);
067                        final Object o = excConstr.newInstance(msg);
068                        if (o instanceof FileSystemException) {
069                            return (FileSystemException) o;
070                        }
071                        if (o instanceof Exception) {
072                            return new FileSystemException(msg, (Exception) o);
073                        }
074                    }
075                }
076            } catch (final Exception e) {
077                throw new FileSystemException(e);
078            }
079        }
080
081        return new FileSystemException(msg);
082    }
083
084    // avoid instantiation.
085    private ExceptionConverter() {
086    }
087}