001/*
002 * Copyright (c) 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.client;
013
014import java.io.PrintStream;
015import java.io.PrintWriter;
016import java.io.StringWriter;
017
018import javax.ws.rs.core.Response;
019
020/**
021 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
022 */
023public class RemoteException extends AutomationException {
024
025    private static final long serialVersionUID = 1L;
026
027    protected final int status;
028
029    protected final String type;
030
031    protected final String info;
032
033    protected final Throwable remoteCause;
034
035    public RemoteException(int status, String type, String message, Throwable cause) {
036        super(message, cause);
037        this.status = status;
038        this.type = type;
039        info = extractInfo(cause);
040        remoteCause = cause;
041    }
042
043    public RemoteException(int status, String type, String message, String info) {
044        super(message);
045        this.status = status;
046        this.type = type;
047        this.info = info;
048        remoteCause = null;
049    }
050
051    public int getStatus() {
052        return status;
053    }
054
055    public String getType() {
056        return type;
057    }
058
059    protected static String extractInfo(Throwable t) {
060        if (t == null) {
061            return "";
062        }
063        StringWriter sw = new StringWriter();
064        PrintWriter pw = new PrintWriter(sw);
065        t.printStackTrace(pw);
066        return sw.getBuffer().toString();
067    }
068
069    public Throwable getRemoteCause() {
070        return remoteCause;
071    }
072
073    public String getRemoteStackTrace() {
074        return status + " - " + getMessage() + "\n" + info;
075    }
076
077    @Override
078    public void printStackTrace(PrintStream s) {
079        super.printStackTrace(s);
080        s.println("====== Remote Stack Trace:");
081        s.print(getRemoteStackTrace());
082    }
083
084    @Override
085    public void printStackTrace(PrintWriter s) {
086        super.printStackTrace(s);
087        s.println("====== Remote Stack Trace:");
088        s.print(getRemoteStackTrace());
089    }
090
091    public static RemoteException wrap(Throwable t) {
092        return wrap(t, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
093    }
094
095    public static RemoteException wrap(Throwable t, int status) {
096        return wrap(t.getMessage(), t, status);
097    }
098
099    public static RemoteException wrap(String message, Throwable t) {
100        return wrap(message, t, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
101    }
102
103    public static RemoteException wrap(String message, Throwable t, int status) {
104        RemoteException e = new RemoteException(status, t.getClass().getName(), message, t);
105        e.initCause(t);
106        return e;
107    }
108
109}