use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class UserRequest method createToken.
@POST
@Path("/{userLocator}/tokens/{name}")
@Produces({ "application/xml", "application/json" })
@ApiOperation(value = "Create a new authentication token for the matching user.", nickname = "addUserToken", hidden = true)
public Token createToken(@ApiParam(format = LocatorName.USER) @PathParam("userLocator") String userLocator, @PathParam("name") @NotNull final String name, @QueryParam("fields") String fields) {
if (TeamCityProperties.getBooleanOrTrue(UserFinder.REST_CHECK_ADDITIONAL_PERMISSIONS_ON_USERS_AND_GROUPS)) {
myUserFinder.checkViewAllUsersPermission();
}
final TokenAuthenticationModel tokenAuthenticationModel = myBeanContext.getSingletonService(TokenAuthenticationModel.class);
final SUser user = myUserFinder.getItem(userLocator, true);
try {
final AuthenticationToken token = tokenAuthenticationModel.createToken(user.getId(), name, new Date(PermanentTokenConstants.NO_EXPIRE.getTime()));
return new Token(token, token.getValue(), new Fields(fields), myBeanContext);
} catch (AuthenticationTokenStorage.CreationException e) {
throw new BadRequestException(e.getMessage());
}
}
use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class BuildTypeOrTemplate method remove.
public void remove(@Nullable SUser user, String reason) {
try {
ConfigAction action = myBuildTypeIdentity.createConfigAction(user, reason);
myBuildTypeIdentity.scheduleRemove(action);
} catch (TemplateCannotBeRemovedException e) {
throw new BadRequestException("Cannot remove template with id '" + getId() + "': " + e.getMessage(), e);
}
}
use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class BuildTypeRequestTest method testUpdatingAgentRequirements.
@Test
public void testUpdatingAgentRequirements() {
BuildTypeImpl buildType1 = registerBuildType("buildType1", "projectName");
buildType1.addRequirement(new Requirement("prop1", null, RequirementType.EXISTS));
String disabledId = "id2";
buildType1.addRequirement(new Requirement(disabledId, "prop2", null, RequirementType.EXISTS));
buildType1.setEnabled(disabledId, false);
buildType1.addRequirement(new Requirement("prop3", null, RequirementType.EXISTS));
final String btLocator = "id:" + buildType1.getExternalId();
assertEquals(3, myBuildTypeRequest.getAgentRequirements(btLocator, "$long,agent-requirement($long)").propEntities.size());
{
PropEntityAgentRequirement submitted = new PropEntityAgentRequirement();
submitted.type = "not-exists";
submitted.disabled = true;
submitted.properties = new Properties();
submitted.properties.properties = Arrays.asList(new Property(new SimpleParameter("property-name", "aaa"), false, Fields.LONG, myFixture));
String newId = myBuildTypeRequest.addAgentRequirement(btLocator, "$long", submitted).id;
assertEquals(4, myBuildTypeRequest.getAgentRequirements(btLocator, "$long,agent-requirement($long)").propEntities.size());
assertTrue(myBuildTypeRequest.getAgentRequirements(btLocator, "$long,agent-requirement($long)").propEntities.get(3).disabled);
myBuildTypeRequest.deleteAgentRequirement(btLocator, newId);
assertEquals(3, myBuildTypeRequest.getAgentRequirements(btLocator, "$long,agent-requirement($long)").propEntities.size());
}
{
PropEntitiesAgentRequirement submitted = new PropEntitiesAgentRequirement();
PropEntityAgentRequirement submitted1 = new PropEntityAgentRequirement();
submitted1.type = "agentRequirementType1";
PropEntityAgentRequirement submitted2 = new PropEntityAgentRequirement();
submitted2.type = "agentRequirementType1";
submitted.propEntities = Arrays.asList(submitted1, submitted2);
checkException(BadRequestException.class, new Runnable() {
public void run() {
myBuildTypeRequest.replaceAgentRequirements(btLocator, "$long", // error will be reported: BadRequestException: No name is specified. Make sure 'property-name' property is present and has not empty value
submitted);
}
}, null);
assertEquals(3, myBuildTypeRequest.getAgentRequirements(btLocator, "$long,agent-requirement($long)").propEntities.size());
assertEquals(3, buildType1.getRequirements().size());
assertFalse(buildType1.isEnabled(disabledId));
}
{
PropEntityAgentRequirement submitted = new PropEntityAgentRequirement();
submitted.type = "agentRequirementType1";
checkException(BadRequestException.class, new Runnable() {
public void run() {
myBuildTypeRequest.addAgentRequirement(btLocator, "$long", submitted);
}
}, null);
assertEquals(3, myBuildTypeRequest.getAgentRequirements(btLocator, "$long,agent-requirement($long)").propEntities.size());
assertEquals(3, buildType1.getRequirements().size());
assertFalse(buildType1.isEnabled(disabledId));
}
myBuildTypeRequest.replaceAgentRequirements(btLocator, "$long", new PropEntitiesAgentRequirement());
assertEquals(0, buildType1.getRequirements().size());
}
use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class AvatarRequest method getAvatarWithHash.
@GET
@Produces(MediaType.IMAGE_PNG_VALUE)
@Path("/{userLocator}/{size}/avatar.{hash}.png")
@ApiOperation("Get a users avatar")
public Response getAvatarWithHash(@Context HttpServletResponse response, @ApiParam(format = LocatorName.USER) @PathParam("userLocator") String userLocator, @ApiParam(value = "avatar's size", allowableValues = "range[" + MIN_AVATAR_SIZE + ", " + MAX_AVATAR_SIZE + "]") @PathParam("size") Integer size, @PathParam("hash") String hash) throws IOException {
if (size < MIN_AVATAR_SIZE || size > MAX_AVATAR_SIZE) {
throw new BadRequestException("\"size\" must be bigger or equal than " + MIN_AVATAR_SIZE + " and lower or equal than " + MAX_AVATAR_SIZE);
}
final SUser user = myUserFinder.getItem(userLocator);
if (!hash.equals(user.getPropertyValue(AVATAR_HASH)))
throw new NotFoundException("Avatar with hash - " + hash + " not found");
final BufferedImage image = myUserAvatarsManager.getAvatar(user, size);
if (image == null)
throw new NotFoundException("avatar (username: " + user.getUsername() + ") not found");
response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_CONTROL_MAX_AGE + CACHE_CONTROL_NEVER_EXPIRES);
ImageIO.write(image, "png", response.getOutputStream());
return Response.ok().build();
}
use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class AvatarRequest method putAvatar.
@PUT
@Path("/{userLocator}")
@Consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
@ApiOperation("Update a users avatar")
public void putAvatar(@Context HttpServletRequest request, @FormDataParam("avatar") InputStream avatar, @ApiParam(format = LocatorName.USER) @PathParam("userLocator") String userLocator) throws IOException {
final SUser currentUser = myUserFinder.getCurrentUser();
if (currentUser == null)
throw new AccessDeniedException(null, "Log in to your account");
final SUser targetUser = myUserFinder.getItem(userLocator);
ServerAuthUtil.canEditUser(currentUser, targetUser);
// check avatar file size
final long avatarMaxSize = getAvatarMaxSize();
if (request.getContentLength() >= avatarMaxSize) {
throw new BadRequestException(String.format("The size of the avatar must be less than or equal to %d kilobytes (%d bytes)", avatarMaxSize / 1024, avatarMaxSize));
}
final BufferedImage image = ImageIO.read(avatar);
myUserAvatarsManager.saveAvatar(targetUser, image);
}
Aggregations