001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.platform.web.common.exceptionhandling;
022
023import java.util.Arrays;
024import java.util.List;
025
026import javax.servlet.ServletException;
027
028import org.nuxeo.ecm.core.api.DocumentSecurityException;
029import org.nuxeo.ecm.core.io.download.DownloadHelper;
030
031public class ExceptionHelper {
032
033    private ExceptionHelper() {
034    }
035
036    public static Throwable unwrapException(Throwable t) {
037        Throwable cause = null;
038        if (t instanceof ServletException) {
039            cause = ((ServletException) t).getRootCause();
040        } else if (t instanceof Exception) {
041            cause = t.getCause();
042        }
043
044        if (cause == null) {
045            return t;
046        } else {
047            return unwrapException(cause);
048        }
049    }
050
051    public static List<String> possibleSecurityErrorMessages = Arrays.asList("java.lang.SecurityException",
052            DocumentSecurityException.class.getName(), SecurityException.class.getName());
053
054    public static boolean isSecurityError(Throwable t) {
055        if (t == null) {
056            return false;
057        } else if (t instanceof DocumentSecurityException || t.getCause() instanceof DocumentSecurityException
058                || t.getCause() instanceof SecurityException) {
059            return true;
060        } else if (t.getMessage() != null) {
061            String message = t.getMessage();
062            for (String errorMessage : possibleSecurityErrorMessages) {
063                if (message.contains(errorMessage)) {
064                    return true;
065                }
066            }
067        }
068        return false;
069    }
070
071    /**
072     * @deprecated since 7.3, use {@link DownloadHelper#isClientAbortError} instead
073     */
074    @Deprecated
075    public static boolean isClientAbortError(Throwable t) {
076        return DownloadHelper.isClientAbortError(t);
077    }
078
079    /**
080     * @deprecated since 7.3, use {@link DownloadHelper#logClientAbort} instead
081     */
082    @Deprecated
083    public static void logClientAbort(Exception e) {
084        DownloadHelper.logClientAbort(e);
085    }
086
087}