From 8b969db208351367fc0a77fc9f42a547e073c8a6 Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Mon, 18 Apr 2016 15:34:13 +0300 Subject: [PATCH] CODENVY-282 check permissions before trying to start any workspace --- .../eclipse/che/ide/util/ExceptionUtils.java | 30 ++++++-- .../che/ide/workspace/WorkspaceComponent.java | 74 ++++++++++++------- 2 files changed, 68 insertions(+), 36 deletions(-) diff --git a/core/commons/che-core-commons-gwt/src/main/java/org/eclipse/che/ide/util/ExceptionUtils.java b/core/commons/che-core-commons-gwt/src/main/java/org/eclipse/che/ide/util/ExceptionUtils.java index 73433c9a3961..4136570934f6 100644 --- a/core/commons/che-core-commons-gwt/src/main/java/org/eclipse/che/ide/util/ExceptionUtils.java +++ b/core/commons/che-core-commons-gwt/src/main/java/org/eclipse/che/ide/util/ExceptionUtils.java @@ -15,7 +15,6 @@ package org.eclipse.che.ide.util; import org.eclipse.che.ide.commons.exception.ServerException; -import org.eclipse.che.ide.dto.DtoFactory; import java.util.Collections; import java.util.Map; @@ -23,8 +22,6 @@ /** Utility class for common Exception related operations. */ public class ExceptionUtils { - private static DtoFactory dtoFactory = new DtoFactory(); - public static final int MAX_CAUSE = 10; public static String getStackTraceAsString(Throwable e) { @@ -37,7 +34,7 @@ public static String getThrowableAsString(Throwable e, String newline, String in } // For each cause, print the requested number of entries of its stack // trace, being careful to avoid getting stuck in an infinite loop. - StringBuffer s = new StringBuffer(newline); + StringBuilder s = new StringBuilder(newline); Throwable currentCause = e; String causedBy = ""; @@ -50,11 +47,11 @@ public static String getThrowableAsString(Throwable e, String newline, String in s.append(currentCause.getMessage()); StackTraceElement[] stackElems = currentCause.getStackTrace(); if (stackElems != null) { - for (int i = 0; i < stackElems.length; ++i) { + for (StackTraceElement stackElem : stackElems) { s.append(newline); s.append(indent); s.append("at "); - s.append(stackElems[i].toString()); + s.append(stackElem.toString()); } } @@ -70,7 +67,7 @@ public static String getThrowableAsString(Throwable e, String newline, String in } /** - * Returns error code of the exception if it is of type {@clink ServerException} and has error code set, or -1 otherwise. + * Returns error code of the exception if it is of type {@link ServerException} and has error code set, or -1 otherwise. * * @param exception * passed exception @@ -87,7 +84,7 @@ public static int getErrorCode(Throwable exception) { } /** - * Returns attributes of the exception if it is of type {@clink ServerException} and has attributes set, or empty map otherwise. + * Returns attributes of the exception if it is of type {@link ServerException} and has attributes set, or empty map otherwise. * * @param exception * passed exception @@ -102,4 +99,21 @@ public static Map getAttributes(Throwable exception) { return Collections.emptyMap(); } } + + /** + * Returns HTTP status the exception if it is of type {@link ServerException} or -1 otherwise. + * + * @param exception + * passed exception + * @return status code + */ + public static int getStatusCode(Throwable exception) { + if (exception instanceof ServerException) { + return ((ServerException)exception).getHTTPStatus(); + } else if (exception instanceof org.eclipse.che.ide.websocket.rest.exceptions.ServerException) { + return ((org.eclipse.che.ide.websocket.rest.exceptions.ServerException)exception).getHTTPStatus(); + } else { + return -1; + } + } } diff --git a/core/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java b/core/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java index 38fe0c765bc6..67af217a6a52 100644 --- a/core/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java +++ b/core/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java @@ -34,6 +34,7 @@ import org.eclipse.che.ide.CoreLocalizationConstant; import org.eclipse.che.ide.actions.WorkspaceSnapshotCreator; import org.eclipse.che.ide.api.app.AppContext; +import org.eclipse.che.ide.api.event.HttpSessionDestroyedEvent; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.notification.StatusNotification; import org.eclipse.che.ide.api.preferences.PreferencesManager; @@ -41,11 +42,13 @@ import org.eclipse.che.ide.api.component.Component; import org.eclipse.che.ide.dto.DtoFactory; import org.eclipse.che.ide.rest.DtoUnmarshallerFactory; +import org.eclipse.che.ide.rest.HTTPStatus; import org.eclipse.che.ide.ui.dialogs.CancelCallback; import org.eclipse.che.ide.ui.dialogs.ConfirmCallback; import org.eclipse.che.ide.ui.dialogs.DialogFactory; import org.eclipse.che.ide.ui.loaders.initialization.InitialLoadingInfo; import org.eclipse.che.ide.ui.loaders.initialization.LoaderPresenter; +import org.eclipse.che.ide.util.ExceptionUtils; import org.eclipse.che.ide.util.loging.Log; import org.eclipse.che.ide.websocket.MessageBus; import org.eclipse.che.ide.websocket.MessageBusProvider; @@ -182,39 +185,54 @@ public void setCurrentWorkspace(WorkspaceDto workspace) { */ public void startWorkspaceById(final WorkspaceDto workspace, final Callback callback) { this.callback = callback; - loader.show(initialLoadingInfo); - initialLoadingInfo.setOperationStatus(WORKSPACE_BOOTING.getValue(), IN_PROGRESS); - - if (messageBus != null) { - messageBus.cancelReconnection(); - } - messageBus = messageBusProvider.createMessageBus(workspace.getId()); - - messageBus.addOnOpenHandler(new ConnectionOpenedHandler() { + workspaceServiceClient.getWorkspace(workspace.getId()).then(new Operation() { @Override - public void onOpen() { - messageBus.removeOnOpenHandler(this); - subscribeToWorkspaceStatusWebSocket(workspace); - - if (!RUNNING.equals(workspace.getStatus())) { - workspaceServiceClient.getSnapshot(workspace.getId()).then(new Operation>() { - @Override - public void apply(List snapshots) throws OperationException { - if (snapshots.isEmpty()) { - handleWsStart(workspaceServiceClient.startById(workspace.getId(), - workspace.getConfig().getDefaultEnv())); - } else { - showRecoverWorkspaceConfirmDialog(workspace); - } + public void apply(WorkspaceDto arg) throws OperationException { + loader.show(initialLoadingInfo); + initialLoadingInfo.setOperationStatus(WORKSPACE_BOOTING.getValue(), IN_PROGRESS); + + if (messageBus != null) { + messageBus.cancelReconnection(); + } + messageBus = messageBusProvider.createMessageBus(workspace.getId()); + + messageBus.addOnOpenHandler(new ConnectionOpenedHandler() { + @Override + public void onOpen() { + messageBus.removeOnOpenHandler(this); + subscribeToWorkspaceStatusWebSocket(workspace); + + if (!RUNNING.equals(workspace.getStatus())) { + workspaceServiceClient.getSnapshot(workspace.getId()).then(new Operation>() { + @Override + public void apply(List snapshots) throws OperationException { + if (snapshots.isEmpty()) { + handleWsStart(workspaceServiceClient.startById(workspace.getId(), + workspace.getConfig().getDefaultEnv())); + } else { + showRecoverWorkspaceConfirmDialog(workspace); + } + } + }); + } else { + initialLoadingInfo.setOperationStatus(WORKSPACE_BOOTING.getValue(), SUCCESS); + setCurrentWorkspace(workspace); + eventBus.fireEvent(new WorkspaceStartedEvent(workspace)); } - }); - } else { - initialLoadingInfo.setOperationStatus(WORKSPACE_BOOTING.getValue(), SUCCESS); - setCurrentWorkspace(workspace); - eventBus.fireEvent(new WorkspaceStartedEvent(workspace)); + } + }); + } + }).catchError(new Operation() { + @Override + public void apply(PromiseError err) throws OperationException { + Log.error(getClass(), err.getCause()); + if (ExceptionUtils.getStatusCode(err.getCause()) == HTTPStatus.FORBIDDEN) { + eventBus.fireEvent(new HttpSessionDestroyedEvent()); } } }); + + } /**