Skip to content

Commit

Permalink
Make backward compliance for the workspace keys with ":" and optional…
Browse files Browse the repository at this point in the history
… namespace value (eclipse-che#4123)

* Make backward compliance for the workspace keys with ":" and optional namespace value
Broken by eclipse-che#4061
This backward compliance will be soon removed once deprecation time is over
Change-Id: I884e8adef2f01774f00a9f6c08ca834fc22b5917
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
  • Loading branch information
benoitf committed Feb 15, 2017
1 parent 02a60a2 commit a1d230c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public WorkspaceImpl createWorkspace(WorkspaceConfig config,
*
* <p> Key rules:
* <ul>
* <li>@Deprecated : If it contains <b>:</b> character then that key is combination of namespace and workspace name
* <li>@Deprecated : <b></>:workspace_name</b> is valid abstract key and current user name will be used as namespace
* <li>If it doesn't contain <b>/</b> character then that key is id(e.g. workspace123456)
* <li>If it contains <b>/</b> character then that key is combination of namespace and workspace name
* </ul>
Expand Down Expand Up @@ -895,13 +897,27 @@ private WorkspaceImpl doCreateWorkspace(WorkspaceConfig config,
}

private WorkspaceImpl getByKey(String key) throws NotFoundException, ServerException {

int lastColonIndex = key.indexOf(":");
int lastSlashIndex = key.lastIndexOf("/");
if (lastSlashIndex == -1) {
if (lastSlashIndex == -1 && lastColonIndex == -1) {
// key is id
return workspaceDao.get(key);
}
final String namespace = key.substring(0, lastSlashIndex);
final String wsName = key.substring(lastSlashIndex + 1);

final String namespace;
final String wsName;
if (lastColonIndex == 0) {
// no namespace, use current user namespace
namespace = EnvironmentContext.getCurrent().getSubject().getUserName();
wsName = key.substring(1);
} else if (lastColonIndex > 0) {
wsName = key.substring(lastColonIndex + 1);
namespace = key.substring(0, lastColonIndex);
} else {
namespace = key.substring(0, lastSlashIndex);
wsName = key.substring(lastSlashIndex + 1);
}
return workspaceDao.get(wsName, namespace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public Response create(@ApiParam(value = "The configuration to create the new wo
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get the workspace by the composite key",
notes = "Composite key can be just workspace ID or in the " +
"namespace:workspace_name form, where namespace is optional (e.g :workspace_name is valid key too." +
"namespace/workspace_name form, where namespace can contain '/' character.")
@ApiResponses({@ApiResponse(code = 200, message = "The response contains requested workspace entity"),
@ApiResponse(code = 404, message = "The workspace with specified id does not exist"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ public void shouldBeAbleToGetWorkspaceByKey() throws Exception {
assertEquals(result, workspace);
}


@Test
public void shouldBeAbleToGetWorkspaceByKeyPreviousFormat() throws Exception {
WorkspaceImpl workspace = createAndMockWorkspace();
WorkspaceImpl result = workspaceManager.getWorkspace(workspace.getNamespace() + ":" + workspace.getConfig().getName());
assertEquals(result, workspace);
}

@Test
public void shouldBeAbleToGetWorkspaceByKeyNamespaceOptionalPreviousFormat() throws Exception {
WorkspaceImpl workspace = createAndMockWorkspace();
WorkspaceImpl result = workspaceManager.getWorkspace(":" + workspace.getConfig().getName());
assertEquals(result, workspace);
}

@Test
public void shouldBeAbleToGetWorkspaceByKeyWithoutOwner() throws Exception {
WorkspaceImpl workspace = createAndMockWorkspace();
WorkspaceImpl result = workspaceManager.getWorkspace(":" + workspace.getConfig().getName());
assertEquals(result, workspace);
}



@Test
public void shouldBeAbleToGetWorkspacesAvailableForUser() throws Exception {
// given
Expand Down

0 comments on commit a1d230c

Please sign in to comment.