001/*
002 * (C) Copyright 2006-2014 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.client;
020
021import java.io.PrintStream;
022import java.io.PrintWriter;
023import java.io.StringWriter;
024
025import javax.ws.rs.core.Response;
026
027/**
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public class RemoteException extends AutomationException {
031
032    private static final long serialVersionUID = 1L;
033
034    protected final int status;
035
036    protected final String type;
037
038    protected final String info;
039
040    protected final Throwable remoteCause;
041
042    public RemoteException(int status, String type, String message, Throwable cause) {
043        super(message, cause);
044        this.status = status;
045        this.type = type;
046        info = extractInfo(cause);
047        remoteCause = cause;
048    }
049
050    public RemoteException(int status, String type, String message, String info) {
051        super(message);
052        this.status = status;
053        this.type = type;
054        this.info = info;
055        remoteCause = null;
056    }
057
058    public int getStatus() {
059        return status;
060    }
061
062    public String getType() {
063        return type;
064    }
065
066    protected static String extractInfo(Throwable t) {
067        if (t == null) {
068            return "";
069        }
070        StringWriter sw = new StringWriter();
071        PrintWriter pw = new PrintWriter(sw);
072        t.printStackTrace(pw);
073        return sw.getBuffer().toString();
074    }
075
076    public Throwable getRemoteCause() {
077        return remoteCause;
078    }
079
080    public String getRemoteStackTrace() {
081        return status + " - " + getMessage() + "\n" + info;
082    }
083
084    @Override
085    public void printStackTrace(PrintStream s) {
086        super.printStackTrace(s);
087        s.println("====== Remote Stack Trace:");
088        s.print(getRemoteStackTrace());
089    }
090
091    @Override
092    public void printStackTrace(PrintWriter s) {
093        super.printStackTrace(s);
094        s.println("====== Remote Stack Trace:");
095        s.print(getRemoteStackTrace());
096    }
097
098    public static RemoteException wrap(Throwable t) {
099        return wrap(t, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
100    }
101
102    public static RemoteException wrap(Throwable t, int status) {
103        return wrap(t.getMessage(), t, status);
104    }
105
106    public static RemoteException wrap(String message, Throwable t) {
107        return wrap(message, t, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
108    }
109
110    public static RemoteException wrap(String message, Throwable t, int status) {
111        RemoteException e = new RemoteException(status, t.getClass().getName(), message, t);
112        e.initCause(t);
113        return e;
114    }
115
116}