Skip to content

Commit

Permalink
CLDIDE-2663: Add accept named factory link
Browse files Browse the repository at this point in the history
  • Loading branch information
akorneta committed Jan 4, 2016
1 parent 53104ec commit 1c9ec31
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.workspace.EnvironmentState;
import org.eclipse.che.api.core.rest.Service;
import org.eclipse.che.api.core.rest.shared.dto.Link;
import org.eclipse.che.api.factory.server.builder.FactoryBuilder;
import org.eclipse.che.api.factory.server.snippet.SnippetGenerator;
import org.eclipse.che.api.factory.shared.dto.Author;
import org.eclipse.che.api.factory.shared.dto.Factory;
import org.eclipse.che.api.machine.shared.dto.CommandDto;
import org.eclipse.che.api.user.server.dao.UserDao;
import org.eclipse.che.api.workspace.server.DtoConverter;
import org.eclipse.che.api.workspace.server.WorkspaceManager;
import org.eclipse.che.api.workspace.server.model.impl.EnvironmentStateImpl;
Expand Down Expand Up @@ -100,6 +102,7 @@ public class FactoryService extends Service {
private final LinksHelper linksHelper;
private final FactoryBuilder factoryBuilder;
private final WorkspaceManager workspaceManager;
private final UserDao userDao;

@Inject
public FactoryService(FactoryStore factoryStore,
Expand All @@ -108,14 +111,16 @@ public FactoryService(FactoryStore factoryStore,
FactoryEditValidator factoryEditValidator,
LinksHelper linksHelper,
FactoryBuilder factoryBuilder,
WorkspaceManager workspaceManager) {
WorkspaceManager workspaceManager,
UserDao userDao) {
this.factoryStore = factoryStore;
this.createValidator = createValidator;
this.acceptValidator = acceptValidator;
this.factoryEditValidator = factoryEditValidator;
this.linksHelper = linksHelper;
this.factoryBuilder = factoryBuilder;
this.workspaceManager = workspaceManager;
this.userDao = userDao;
}

/**
Expand Down Expand Up @@ -185,11 +190,8 @@ public Factory saveFactory(Iterator<FileItem> formData, @Context UriInfo uriInfo
}
processDefaults(factory);
createValidator.validateOnCreate(factory);
String factoryId = factoryStore.saveFactory(factory, images);
return factory.withLinks(linksHelper.createLinks(factoryStore.getFactory(factoryId),
images,
uriInfo,
EnvironmentContext.getCurrent().getUser().getName()));
final Factory storedFactory = factoryStore.getFactory(factoryStore.saveFactory(factory, images));
return storedFactory.withLinks(createLinks(storedFactory, images, uriInfo));
} catch (IOException e) {
LOG.error(e.getLocalizedMessage(), e);
throw new ServerException(e.getLocalizedMessage(), e);
Expand Down Expand Up @@ -231,15 +233,8 @@ public Factory saveFactory(Factory factory)
}
processDefaults(factory);
createValidator.validateOnCreate(factory);
final String factoryId = factoryStore.saveFactory(factory, null);
try {
factory.setLinks(linksHelper.createLinks(factoryStore.getFactory(factoryId),
uriInfo,
EnvironmentContext.getCurrent().getUser().getName()));
} catch (UnsupportedEncodingException e) {
throw new ServerException(e.getLocalizedMessage(), e);
}
return factory;
final Factory storedFactory = factoryStore.getFactory(factoryStore.saveFactory(factory, null));
return storedFactory.withLinks(createLinks(storedFactory, null, uriInfo));
}

/**
Expand Down Expand Up @@ -279,15 +274,7 @@ public Factory getFactory(@ApiParam(value = "Factory ID")
@Context
UriInfo uriInfo) throws NotFoundException, ServerException, BadRequestException {
final Factory factory = factoryStore.getFactory(id);

try {
factory.setLinks(linksHelper.createLinks(factory,
factoryStore.getFactoryImages(id, null),
uriInfo,
factory.getCreator().getName()));
} catch (UnsupportedEncodingException e) {
throw new ServerException(e.getLocalizedMessage());
}
factory.setLinks(createLinks(factory, factoryStore.getFactoryImages(id, null), uriInfo));
if (validate) {
acceptValidator.validateOnAccept(factory);
}
Expand Down Expand Up @@ -351,16 +338,7 @@ public Factory updateFactory(@ApiParam(value = "Factory id")

// access granted, user can update the factory
factoryStore.updateFactory(id, newFactory);

// create links
try {
newFactory.setLinks(linksHelper.createLinks(newFactory,
factoryStore.getFactoryImages(id, null),
uriInfo,
newFactory.getCreator().getName()));
} catch (UnsupportedEncodingException e) {
throw new ServerException(e.getLocalizedMessage());
}
newFactory.setLinks(createLinks(newFactory, factoryStore.getFactoryImages(id, null), uriInfo));
return newFactory;
}

Expand Down Expand Up @@ -606,6 +584,26 @@ public Response getFactoryJson(@ApiParam(value = "Workspace ID")
.build();
}

/**
* Creates list of factory links,
* if factory is named it will be generated accept named link,
* if images set is not null and not empty it will be generate links for them
*/
private List<Link> createLinks(Factory factory, Set<FactoryImage> images, UriInfo uriInfo) throws NotFoundException, ServerException {
try {
String username = null;
if (!isNullOrEmpty(factory.getName())) {
username = userDao.getById(factory.getCreator().getUserId()).getName();
}
return images != null && !images.isEmpty()
? linksHelper.createLinks(factory, images, uriInfo, username)
: linksHelper.createLinks(factory, uriInfo, username);

} catch (UnsupportedEncodingException e) {
throw new ServerException(e.getLocalizedMessage(), e);
}
}

/**
* Checks if it is possible to create the factory from specified the user's workspace,
* in case when it impossible {@link BadRequestException} will be thrown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.eclipse.che.api.factory.shared.dto.ButtonAttributes;
import org.eclipse.che.api.factory.shared.dto.Factory;
import org.eclipse.che.api.machine.shared.dto.CommandDto;
import org.eclipse.che.api.user.server.dao.UserDao;
import org.eclipse.che.api.user.server.dao.User;
import org.eclipse.che.api.workspace.server.WorkspaceManager;
import org.eclipse.che.api.workspace.server.model.impl.UsersWorkspaceImpl;
import org.eclipse.che.api.workspace.shared.dto.EnvironmentDto;
Expand Down Expand Up @@ -113,6 +115,9 @@ public class FactoryServiceTest {
@Mock
private WorkspaceManager workspaceManager;

@Mock
private UserDao userDao;

private FactoryBuilder factoryBuilder;

private FactoryService factoryService;
Expand All @@ -123,13 +128,15 @@ public void setUp() throws Exception {
dto = DtoFactory.getInstance();
factoryBuilder = spy(new FactoryBuilder(new SourceStorageParametersValidator()));
doNothing().when(factoryBuilder).checkValid(any(Factory.class));
when(userDao.getById(anyString())).thenReturn(new User().withName(JettyHttpServer.ADMIN_USER_NAME));
factoryService = new FactoryService(factoryStore,
createValidator,
acceptValidator,
editValidator,
new LinksHelper(),
factoryBuilder,
workspaceManager);
workspaceManager,
userDao);
}

@Filter
Expand Down Expand Up @@ -427,7 +434,7 @@ public void shouldBeAbleToGetFactory(ITestContext context) throws Exception {

Link expectedCreateProjectByName = dto.createDto(Link.class);
expectedCreateProjectByName.setProduces("text/html");
expectedCreateProjectByName.setHref(getServerUrl(context) + "/f?name=" + factoryName + "&user=" + userId);
expectedCreateProjectByName.setHref(getServerUrl(context) + "/f?name=" + factoryName + "&user=" + JettyHttpServer.ADMIN_USER_NAME);
expectedCreateProjectByName.setRel("accept-named");
expectedLinks.add(expectedCreateProjectByName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,38 @@ public UserInRoleDescriptor inRole(@Required @Description("role inside a scope")

}

/**
* Get's user by name.
*
* @param name
* user name
* @return found user
* @throws NotFoundException
* when user with given name doesn't exist
* @throws ServerException
* when some error occurred while retrieving user
*/
@GET
@Path("/name/{name}")
@GenerateLink(rel = "get user by name")
@RolesAllowed({"user", "system/admin", "system/manager"})
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get user by name",
notes = "Get user by its name in the system. Roles allowed: system/admin, system/manager.",
response = UserDescriptor.class)
@ApiResponses({@ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Internal Server Error")})
public UserDescriptor getByName(@ApiParam(value = "User email", required = true)
@PathParam("name")
@Required
String name,
@Context
SecurityContext context) throws NotFoundException, ServerException {
final User user = userDao.getByName(name);
return toDescriptor(user, context);
}

private User fromEntity(NewUser newUser) throws ForbiddenException {
if (newUser == null) {
throw new ForbiddenException("New user required");
Expand Down

0 comments on commit 1c9ec31

Please sign in to comment.