summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/GuiErrorCallback.java
blob: 4267ea31354cb7813f475e0328dc855e825dab26 (plain) (tree)
1
2
3
4
5
6
7
8
9




                                  
                                    
                                                       

                                                             
                                 




















                                                                                           
                                                             
                                     


                                                            

                                                                        
                                                                                         










                                                                                                                                                                        
                                                                                              




                                                                    
                         

















                                                                                                                                                
         
 
package org.openslx.dozmod.thrift;

import java.awt.Frame;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.openslx.bwlp.thrift.iface.AuthorizationError;
import org.openslx.bwlp.thrift.iface.TAuthorizationException;
import org.openslx.dozmod.Config;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.Gui.GuiCallable;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.thrifthelper.ThriftManager.ErrorCallback;

public class GuiErrorCallback implements ErrorCallback {

	private static final Logger LOGGER = Logger.getLogger(GuiErrorCallback.class);

	private final Frame parent;

	private final String serverString;

	public GuiErrorCallback(Frame parent, String serverString) {
		this.parent = parent;
		this.serverString = serverString;
	}

	@Override
	public boolean thriftError(int failCount, final String method, final Throwable t) {
		// if it's not a transport exception, do not retry
		if (t != null  && !(t instanceof TException))
			return false;
		// if it's the first fail, retry immediately
		if (failCount == 1)
			return true;
		// Some methods are non-critical, so don't show a pop-up
		if (ThriftError.failSilently(method))
			return failCount == 2; // As it's silent, give it a second try...
		if (t instanceof TAuthorizationException) {
			final TAuthorizationException taex = (TAuthorizationException) t;
			if (taex.getNumber() == AuthorizationError.NOT_AUTHENTICATED || taex.getNumber() == AuthorizationError.INVALID_TOKEN) {
				// TODO somehow reauth the user :)
				return Gui.syncExec(new GuiCallable<Boolean>() {
					@Override
					public Boolean run() {
						if (Gui.showMessageBox(parent, "Ungültiges Sitzungstoken oder fehlerhafte Authentifizierung am " + serverString + "!" +
								//"\n" + errMsg + "\n" +
								"\nBitte starten Sie das Programm neu. Jetzt beenden?", MessageType.ERROR_RETRY, LOGGER, t)) {
							// user confirmed exit
							Config.saveCurrentSession("", "", "");
							Gui.exit(0);
						}
						return false;
					}
				});
			}
		}
		if (t instanceof TTransportException) {
			// Otherwise, ask user if we should retry
			final TTransportException tex = (TTransportException) t;
			return Gui.syncExec(new GuiCallable<Boolean>() {
				@Override
				public Boolean run() {
					String errMsg = null;
					if (tex != null) {
						errMsg = " (Fehler " + tex.getType() + ")";
					}
					return Gui.showMessageBox(parent, "Die Kommunikation mit " + serverString + " ist"
							+ " gestört. Der Aufruf der Funktion " + method + " ist fehlgeschlagen" + errMsg
							+ ".\n\n" + "Möchten Sie den Aufruf wiederholen?", MessageType.ERROR_RETRY, LOGGER, t);
				}
			});
		}
		return false;
	}
}