Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made RuntimeDto return machine token #6732

Merged
merged 2 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
import org.eclipse.che.api.workspace.shared.dto.stack.StackSourceDto;
import org.eclipse.che.api.workspace.shared.stack.Stack;
import org.eclipse.che.api.workspace.shared.stack.StackSource;
import org.eclipse.che.commons.env.EnvironmentContext;
import org.eclipse.che.commons.subject.Subject;
import org.eclipse.che.dto.server.DtoFactory;

/**
Expand All @@ -61,7 +59,6 @@ public final class DtoConverter {

/** Converts {@link Workspace} to {@link WorkspaceDto}. */
public static WorkspaceDto asDto(Workspace workspace) {
Subject subject = EnvironmentContext.getCurrent().getSubject();
WorkspaceDto workspaceDto =
newDto(WorkspaceDto.class)
.withId(workspace.getId())
Expand All @@ -73,7 +70,6 @@ public static WorkspaceDto asDto(Workspace workspace) {

if (workspace.getRuntime() != null) {
RuntimeDto runtime = asDto(workspace.getRuntime());
runtime.setUserToken(subject.getToken());
workspaceDto.setRuntime(runtime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import org.eclipse.che.api.core.BadRequestException;
import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.core.ForbiddenException;
Expand All @@ -54,10 +52,13 @@
import org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl;
import org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl;
import org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl;
import org.eclipse.che.api.workspace.server.token.MachineTokenException;
import org.eclipse.che.api.workspace.server.token.MachineTokenProvider;
import org.eclipse.che.api.workspace.shared.dto.CommandDto;
import org.eclipse.che.api.workspace.shared.dto.EnvironmentDto;
import org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto;
import org.eclipse.che.api.workspace.shared.dto.RecipeDto;
import org.eclipse.che.api.workspace.shared.dto.RuntimeDto;
import org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto;
import org.eclipse.che.api.workspace.shared.dto.WorkspaceDto;
import org.eclipse.che.commons.env.EnvironmentContext;
Expand All @@ -73,25 +74,20 @@
public class WorkspaceService extends Service {

private final WorkspaceManager workspaceManager;
private final MachineTokenProvider machineTokenProvider;
private final WorkspaceLinksGenerator linksGenerator;
private final String apiEndpoint;
// private final boolean cheWorkspaceAutoSnapshot;
// private final boolean cheWorkspaceAutoRestore;
@Context private SecurityContext securityContext;

@Inject
public WorkspaceService(
@Named("che.api") String apiEndpoint,
WorkspaceManager workspaceManager,
WorkspaceLinksGenerator linksGenerator
// @Named(CHE_WORKSPACE_AUTO_SNAPSHOT) boolean cheWorkspaceAutoSnapshot,
// @Named(CHE_WORKSPACE_AUTO_RESTORE) boolean cheWorkspaceAutoRestore
) {
MachineTokenProvider machineTokenProvider,
WorkspaceLinksGenerator linksGenerator) {
this.apiEndpoint = apiEndpoint;
this.workspaceManager = workspaceManager;
this.machineTokenProvider = machineTokenProvider;
this.linksGenerator = linksGenerator;
// this.cheWorkspaceAutoSnapshot = cheWorkspaceAutoSnapshot;
// this.cheWorkspaceAutoRestore = cheWorkspaceAutoRestore;
}

@POST
Expand Down Expand Up @@ -158,7 +154,7 @@ public Response create(
if (startAfterCreate) {
workspaceManager.startWorkspace(workspace.getId(), null, new HashMap<>());
}
return Response.status(201).entity(asDtoWithLinks(workspace)).build();
return Response.status(201).entity(asDtoWithLinksAndToken(workspace)).build();
}

@GET
Expand Down Expand Up @@ -191,7 +187,7 @@ public WorkspaceDto getByKey(
String key)
throws NotFoundException, ServerException, ForbiddenException, BadRequestException {
validateKey(key);
return asDtoWithLinks(workspaceManager.getWorkspace(key));
return asDtoWithLinksAndToken(workspaceManager.getWorkspace(key));
}

@GET
Expand Down Expand Up @@ -278,7 +274,7 @@ public WorkspaceDto update(
ConflictException {
requiredNotNull(update, "Workspace configuration");
relativizeRecipeLinks(update.getConfig());
return doUpdate(id, update);
return asDtoWithLinksAndToken(doUpdate(id, update));
}

@DELETE
Expand Down Expand Up @@ -331,7 +327,7 @@ public WorkspaceDto startById(

Map<String, String> options = new HashMap<>();
if (restore != null) options.put("restore", restore.toString());
return asDtoWithLinks(workspaceManager.startWorkspace(workspaceId, envName, options));
return asDtoWithLinksAndToken(workspaceManager.startWorkspace(workspaceId, envName, options));
}

@POST
Expand Down Expand Up @@ -380,7 +376,7 @@ public WorkspaceDto startFromConfig(
try {
Workspace workspace =
workspaceManager.startWorkspace(config, namespace, isTemporary, new HashMap<>());
return asDtoWithLinks(workspace);
return asDtoWithLinksAndToken(workspace);
} catch (ValidationException x) {
throw new BadRequestException(x.getMessage());
}
Expand Down Expand Up @@ -436,7 +432,7 @@ public WorkspaceDto addCommand(
requiredNotNull(newCommand, "Command");
WorkspaceImpl workspace = workspaceManager.getWorkspace(id);
workspace.getConfig().getCommands().add(new CommandImpl(newCommand));
return doUpdate(id, workspace);
return asDtoWithLinksAndToken(doUpdate(id, workspace));
}

@PUT
Expand Down Expand Up @@ -469,7 +465,7 @@ public WorkspaceDto updateCommand(
format("Workspace '%s' doesn't contain command '%s'", id, cmdName));
}
commands.add(new CommandImpl(update));
return doUpdate(id, workspace);
return asDtoWithLinksAndToken(doUpdate(id, workspace));
}

@DELETE
Expand Down Expand Up @@ -526,7 +522,7 @@ public WorkspaceDto addEnvironment(
relativizeRecipeLinks(newEnvironment);
WorkspaceImpl workspace = workspaceManager.getWorkspace(id);
workspace.getConfig().getEnvironments().put(envName, new EnvironmentImpl(newEnvironment));
return doUpdate(id, workspace);
return asDtoWithLinksAndToken(doUpdate(id, workspace));
}

@PUT
Expand Down Expand Up @@ -559,7 +555,7 @@ public WorkspaceDto updateEnvironment(
throw new NotFoundException(
format("Workspace '%s' doesn't contain environment '%s'", id, envName));
}
return doUpdate(id, workspace);
return asDtoWithLinksAndToken(doUpdate(id, workspace));
}

@DELETE
Expand Down Expand Up @@ -609,7 +605,7 @@ public WorkspaceDto addProject(
requiredNotNull(newProject, "New project config");
final WorkspaceImpl workspace = workspaceManager.getWorkspace(id);
workspace.getConfig().getProjects().add(new ProjectConfigImpl(newProject));
return doUpdate(id, workspace);
return asDtoWithLinksAndToken(doUpdate(id, workspace));
}

@PUT
Expand Down Expand Up @@ -642,7 +638,7 @@ public WorkspaceDto updateProject(
format("Workspace '%s' doesn't contain project with path '%s'", id, normalizedPath));
}
projects.add(new ProjectConfigImpl(update));
return doUpdate(id, workspace);
return asDtoWithLinksAndToken(doUpdate(id, workspace));
}

@DELETE
Expand Down Expand Up @@ -679,8 +675,6 @@ public void deleteProject(
@ApiResponses({@ApiResponse(code = 200, message = "The response contains server settings")})
public Map<String, String> getSettings() {
return new HashMap<>();
// return ImmutableMap.of(CHE_WORKSPACE_AUTO_SNAPSHOT, Boolean.toString(cheWorkspaceAutoSnapshot),
// CHE_WORKSPACE_AUTO_RESTORE, Boolean.toString(cheWorkspaceAutoRestore));
}

private static Map<String, String> parseAttrs(List<String> attributes)
Expand Down Expand Up @@ -782,10 +776,10 @@ private void relativizeRecipeLinks(EnvironmentDto environment) {
}
}

private WorkspaceDto doUpdate(String id, Workspace update)
private Workspace doUpdate(String id, Workspace update)
throws BadRequestException, ConflictException, NotFoundException, ServerException {
try {
return asDtoWithLinks(workspaceManager.updateWorkspace(id, update));
return workspaceManager.updateWorkspace(id, update);
} catch (ValidationException x) {
throw new BadRequestException(x.getMessage());
}
Expand All @@ -798,7 +792,18 @@ private List<WorkspaceDto> withLinks(List<WorkspaceDto> workspaces) throws Serve
return workspaces;
}

private WorkspaceDto asDtoWithLinks(Workspace workspace) throws ServerException {
return asDto(workspace).withLinks(linksGenerator.genLinks(workspace));
private WorkspaceDto asDtoWithLinksAndToken(Workspace workspace) throws ServerException {
WorkspaceDto workspaceDto = asDto(workspace).withLinks(linksGenerator.genLinks(workspace));

RuntimeDto runtimeDto = workspaceDto.getRuntime();
if (runtimeDto != null) {
try {
runtimeDto.setUserToken(machineTokenProvider.getToken(workspace.getId()));
} catch (MachineTokenException e) {
throw new ServerException(e.getMessage(), e);
}
}

return workspaceDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@
public class RuntimeImpl implements Runtime {

private final String activeEnv;
private String owner;
private String userToken;
private Map<String, ? extends Machine> machines;
private final String owner;
private final Map<String, ? extends Machine> machines;
private List<WarningImpl> warnings;

public RuntimeImpl(String activeEnv, Map<String, ? extends Machine> machines, String owner) {
this.activeEnv = activeEnv;
this.machines = machines;
this.owner = owner;
// this.userToken = userToken;
}

public RuntimeImpl(Runtime runtime) {
Expand All @@ -47,7 +45,6 @@ public RuntimeImpl(Runtime runtime) {
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> new MachineImpl(e.getValue())));
this.owner = runtime.getOwner();
// this.userToken = null;
this.warnings =
runtime.getWarnings().stream().map(WarningImpl::new).collect(Collectors.toList());
}
Expand All @@ -70,14 +67,6 @@ public List<WarningImpl> getWarnings() {
return warnings;
}

public String getUserToken() {
return userToken;
}

public void setUserToken(String userToken) {
this.userToken = userToken;
}

@Override
public Map<String, ? extends Machine> getMachines() {
return machines;
Expand All @@ -89,19 +78,14 @@ public boolean equals(Object o) {
if (!(o instanceof RuntimeImpl)) return false;
RuntimeImpl that = (RuntimeImpl) o;
return Objects.equals(activeEnv, that.activeEnv)
&&
// Objects.equals(rootFolder, that.rootFolder) &&
// Objects.equals(devMachine, that.devMachine) &&
Objects.equals(machines, that.machines);
&& Objects.equals(machines, that.machines)
&& Objects.equals(owner, that.owner)
&& Objects.equals(warnings, that.warnings);
}

@Override
public int hashCode() {
return Objects.hash(
activeEnv,
// rootFolder,
// devMachine,
machines);
return Objects.hash(activeEnv, machines, owner, warnings);
}

@Override
Expand All @@ -110,11 +94,13 @@ public String toString() {
+ "activeEnv='"
+ activeEnv
+ '\''
+
// ", rootFolder='" + rootFolder + '\'' +
// ", devMachine=" + devMachine +
", machines="
+ ", owner='"
+ owner
+ '\''
+ ", machines="
+ machines
+ ", warnings="
+ warnings
+ '}';
}
}
Loading