001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.web.common.exceptionhandling;
020
021import java.util.Arrays;
022import java.util.List;
023
024import javax.servlet.ServletException;
025
026import org.nuxeo.ecm.core.api.DocumentSecurityException;
027import org.nuxeo.ecm.core.io.download.DownloadHelper;
028
029public class ExceptionHelper {
030
031    private ExceptionHelper() {
032    }
033
034    public static Throwable unwrapException(Throwable t) {
035        Throwable cause = null;
036        if (t instanceof ServletException) {
037            cause = ((ServletException) t).getRootCause();
038        } else if (t instanceof Exception) {
039            cause = t.getCause();
040        }
041
042        if (cause == null) {
043            return t;
044        } else {
045            return unwrapException(cause);
046        }
047    }
048
049    public static List<String> possibleSecurityErrorMessages = Arrays.asList("java.lang.SecurityException",
050            DocumentSecurityException.class.getName(), SecurityException.class.getName());
051
052    public static boolean isSecurityError(Throwable t) {
053        if (t == null) {
054            return false;
055        } else if (t instanceof DocumentSecurityException || t.getCause() instanceof DocumentSecurityException
056                || t.getCause() instanceof SecurityException) {
057            return true;
058        } else if (t.getMessage() != null) {
059            String message = t.getMessage();
060            for (String errorMessage : possibleSecurityErrorMessages) {
061                if (message.contains(errorMessage)) {
062                    return true;
063                }
064            }
065        }
066        return false;
067    }
068
069    /**
070     * @deprecated since 7.3, use {@link DownloadHelper#isClientAbortError} instead
071     */
072    @Deprecated
073    public static boolean isClientAbortError(Throwable t) {
074        return DownloadHelper.isClientAbortError(t);
075    }
076
077    /**
078     * @deprecated since 7.3, use {@link DownloadHelper#logClientAbort} instead
079     */
080    @Deprecated
081    public static void logClientAbort(Exception e) {
082        DownloadHelper.logClientAbort(e);
083    }
084
085}