Skip to content

Commit

Permalink
CODENVY-282 check permissions before trying to start any workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
mshaposhnik committed Apr 18, 2016
1 parent e94589e commit c6724e4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,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) {
Expand Down Expand Up @@ -102,4 +100,21 @@ public static Map<String, String> getAttributes(Throwable exception) {
return Collections.emptyMap();
}
}

/**
* Returns HTTP status the exception if it is of type {@clink 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@
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;
import org.eclipse.che.ide.context.BrowserQueryFieldRenderer;
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;
Expand Down Expand Up @@ -182,39 +185,54 @@ public void setCurrentWorkspace(WorkspaceDto workspace) {
*/
public void startWorkspaceById(final WorkspaceDto workspace, final Callback<Component, Exception> 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<WorkspaceDto>() {
@Override
public void onOpen() {
messageBus.removeOnOpenHandler(this);
subscribeToWorkspaceStatusWebSocket(workspace);

if (!RUNNING.equals(workspace.getStatus())) {
workspaceServiceClient.getSnapshot(workspace.getId()).then(new Operation<List<SnapshotDto>>() {
@Override
public void apply(List<SnapshotDto> 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<List<SnapshotDto>>() {
@Override
public void apply(List<SnapshotDto> 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<PromiseError>() {
@Override
public void apply(PromiseError err) throws OperationException {
Log.error(getClass(), err.getCause());
if (ExceptionUtils.getStatusCode(err.getCause()) == HTTPStatus.FORBIDDEN) {
eventBus.fireEvent(new HttpSessionDestroyedEvent());
}
}
});


}

/**
Expand Down

0 comments on commit c6724e4

Please sign in to comment.