Search in sources :

Example 26 with BadRequestException

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());
    }
}
Also used : Fields(jetbrains.buildServer.server.rest.model.Fields) SUser(jetbrains.buildServer.users.SUser) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation)

Example 27 with BadRequestException

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);
    }
}
Also used : BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException)

Example 28 with BadRequestException

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());
}
Also used : Requirement(jetbrains.buildServer.requirements.Requirement) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) BuildTypeImpl(jetbrains.buildServer.serverSide.impl.BuildTypeImpl) Test(org.testng.annotations.Test) BaseFinderTest(jetbrains.buildServer.server.rest.data.BaseFinderTest)

Example 29 with BadRequestException

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();
}
Also used : SUser(jetbrains.buildServer.users.SUser) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) BufferedImage(java.awt.image.BufferedImage) ApiOperation(io.swagger.annotations.ApiOperation)

Example 30 with BadRequestException

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);
}
Also used : AccessDeniedException(jetbrains.buildServer.serverSide.auth.AccessDeniedException) SUser(jetbrains.buildServer.users.SUser) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) BufferedImage(java.awt.image.BufferedImage) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)128 NotNull (org.jetbrains.annotations.NotNull)52 ApiOperation (io.swagger.annotations.ApiOperation)39 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)35 ServiceLocator (jetbrains.buildServer.ServiceLocator)28 BuildTypeOrTemplate (jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate)22 Nullable (org.jetbrains.annotations.Nullable)22 SUser (jetbrains.buildServer.users.SUser)19 Fields (jetbrains.buildServer.server.rest.model.Fields)17 LocatorProcessException (jetbrains.buildServer.server.rest.errors.LocatorProcessException)16 Collectors (java.util.stream.Collectors)10 SProject (jetbrains.buildServer.serverSide.SProject)9 StringUtil (jetbrains.buildServer.util.StringUtil)9 Date (java.util.Date)8 jetbrains.buildServer.serverSide (jetbrains.buildServer.serverSide)8 java.util (java.util)7 Locator (jetbrains.buildServer.server.rest.data.Locator)7 OperationException (jetbrains.buildServer.server.rest.errors.OperationException)7 PagerData (jetbrains.buildServer.server.rest.model.PagerData)7 Permission (jetbrains.buildServer.serverSide.auth.Permission)7