mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
use javax validation
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
public class AutoCompleteBadParamException extends Exception {
|
||||
|
||||
public static final String PARAMETER_IS_REQUIRED = "The parameter is required.";
|
||||
public static final String INVALID_PARAMETER_LENGTH = "Invalid parameter length.";
|
||||
|
||||
public AutoCompleteBadParamException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.ext.ExceptionMapper;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
@Provider
|
||||
public class AutoCompleteBadParamExceptionMapper implements ExceptionMapper<AutoCompleteBadParamException> {
|
||||
|
||||
@Override
|
||||
public Response toResponse(AutoCompleteBadParamException exception) {
|
||||
return Response.status(Response.Status.BAD_REQUEST)
|
||||
.entity(exception.getMessage())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,14 @@ package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.user.UserManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.Size;
|
||||
import javax.ws.rs.DefaultValue;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
@@ -18,14 +18,16 @@ import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static sonia.scm.api.v2.resources.AutoCompleteBadParamException.INVALID_PARAMETER_LENGTH;
|
||||
import static sonia.scm.api.v2.resources.AutoCompleteBadParamException.PARAMETER_IS_REQUIRED;
|
||||
|
||||
@Path(AutoCompleteResource.PATH)
|
||||
public class AutoCompleteResource {
|
||||
public static final String PATH = "v2/autocomplete/";
|
||||
public static final String DEFAULT_LIMIT = "5";
|
||||
public static final int MIN_SEARCHED_CHARS = 1;
|
||||
public static final int MIN_SEARCHED_CHARS = 2;
|
||||
|
||||
public static final String PARAMETER_IS_REQUIRED = "The parameter is required.";
|
||||
public static final String INVALID_PARAMETER_LENGTH = "Invalid parameter length.";
|
||||
|
||||
|
||||
private ReducedObjectModelToDtoMapper mapper;
|
||||
|
||||
@@ -51,9 +53,8 @@ public class AutoCompleteResource {
|
||||
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"user:autocomplete\" privilege"),
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
public Response searchUser(@QueryParam("filter") String filter,
|
||||
@DefaultValue(DEFAULT_LIMIT) @QueryParam("limit") Integer limit) throws AutoCompleteBadParamException {
|
||||
validateParams(filter);
|
||||
public Response searchUser(@NotEmpty(message = PARAMETER_IS_REQUIRED) @Size(min = MIN_SEARCHED_CHARS, message = INVALID_PARAMETER_LENGTH) @QueryParam("filter") String filter,
|
||||
@DefaultValue(DEFAULT_LIMIT) @QueryParam("limit") Integer limit) {
|
||||
return Response.ok(userManager.getFiltered(filter, limit)
|
||||
.stream()
|
||||
.map(mapper::map)
|
||||
@@ -71,9 +72,8 @@ public class AutoCompleteResource {
|
||||
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"group:autocomplete\" privilege"),
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
public Response searchGroup(@Valid @QueryParam("filter") String filter,
|
||||
@DefaultValue(DEFAULT_LIMIT) @QueryParam("limit") Integer limit) throws AutoCompleteBadParamException {
|
||||
validateParams(filter);
|
||||
public Response searchGroup(@NotEmpty(message = PARAMETER_IS_REQUIRED) @Size(min = MIN_SEARCHED_CHARS, message = INVALID_PARAMETER_LENGTH) @QueryParam("filter") String filter,
|
||||
@DefaultValue(DEFAULT_LIMIT) @QueryParam("limit") Integer limit) {
|
||||
return Response.ok(groupManager.getFiltered(filter, limit)
|
||||
.stream()
|
||||
.map(mapper::map)
|
||||
@@ -91,9 +91,8 @@ public class AutoCompleteResource {
|
||||
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"repository:autocomplete\" privilege"),
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
public Response searchRepo(@Valid @QueryParam("filter") String filter,
|
||||
@DefaultValue(DEFAULT_LIMIT) @QueryParam("limit") Integer limit) throws AutoCompleteBadParamException {
|
||||
validateParams(filter);
|
||||
public Response searchRepo(@NotEmpty(message = PARAMETER_IS_REQUIRED) @Size(min = MIN_SEARCHED_CHARS, message = INVALID_PARAMETER_LENGTH) @QueryParam("filter") String filter,
|
||||
@DefaultValue(DEFAULT_LIMIT) @QueryParam("limit") Integer limit) {
|
||||
return Response.ok(repositoryManager.getFiltered(filter, limit)
|
||||
.stream()
|
||||
.map(mapper::map)
|
||||
@@ -101,13 +100,5 @@ public class AutoCompleteResource {
|
||||
.build();
|
||||
}
|
||||
|
||||
void validateParams(String filter) throws AutoCompleteBadParamException {
|
||||
if (StringUtils.isBlank(filter)) {
|
||||
throw new AutoCompleteBadParamException(PARAMETER_IS_REQUIRED);
|
||||
}
|
||||
if (filter.length() <= MIN_SEARCHED_CHARS) {
|
||||
throw new AutoCompleteBadParamException(INVALID_PARAMETER_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user