mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
use javax validation
This commit is contained in:
@@ -186,7 +186,7 @@ public class Repository extends BasicPropertiesAware implements ModelObject, Per
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getDisplayName() {
|
public String getDisplayName() {
|
||||||
return getNamespaceAndName().toString();
|
return getNamespace() + "/" + getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ package sonia.scm.xml;
|
|||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -241,7 +242,7 @@ public abstract class AbstractXmlDAO<I extends ModelObject,
|
|||||||
int size = db.values().size();
|
int size = db.values().size();
|
||||||
AssertUtil.assertIsNotEmpty(searched);
|
AssertUtil.assertIsNotEmpty(searched);
|
||||||
return ImmutableList.copyOf(db.values().stream()
|
return ImmutableList.copyOf(db.values().stream()
|
||||||
.filter(item -> item.getId().contains(searched) || (item.getDisplayName() != null && item.getDisplayName().contains(searched)))
|
.filter(item -> StringUtils.containsIgnoreCase(item.getId(), searched) || (item.getDisplayName() != null && StringUtils.containsIgnoreCase(item.getDisplayName() , searched)))
|
||||||
.limit(limit <= 0 ? size : limit)
|
.limit(limit <= 0 ? size : limit)
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.ResponseCode;
|
||||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
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.group.GroupManager;
|
||||||
import sonia.scm.repository.RepositoryManager;
|
import sonia.scm.repository.RepositoryManager;
|
||||||
import sonia.scm.user.UserManager;
|
import sonia.scm.user.UserManager;
|
||||||
import sonia.scm.web.VndMediaType;
|
import sonia.scm.web.VndMediaType;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.validation.Valid;
|
import javax.validation.constraints.Size;
|
||||||
import javax.ws.rs.DefaultValue;
|
import javax.ws.rs.DefaultValue;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
@@ -18,14 +18,16 @@ import javax.ws.rs.QueryParam;
|
|||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.util.stream.Collectors;
|
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)
|
@Path(AutoCompleteResource.PATH)
|
||||||
public class AutoCompleteResource {
|
public class AutoCompleteResource {
|
||||||
public static final String PATH = "v2/autocomplete/";
|
public static final String PATH = "v2/autocomplete/";
|
||||||
public static final String DEFAULT_LIMIT = "5";
|
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;
|
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 = 403, condition = "not authorized, the current user does not have the \"user:autocomplete\" privilege"),
|
||||||
@ResponseCode(code = 500, condition = "internal server error")
|
@ResponseCode(code = 500, condition = "internal server error")
|
||||||
})
|
})
|
||||||
public Response searchUser(@QueryParam("filter") String 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) throws AutoCompleteBadParamException {
|
@DefaultValue(DEFAULT_LIMIT) @QueryParam("limit") Integer limit) {
|
||||||
validateParams(filter);
|
|
||||||
return Response.ok(userManager.getFiltered(filter, limit)
|
return Response.ok(userManager.getFiltered(filter, limit)
|
||||||
.stream()
|
.stream()
|
||||||
.map(mapper::map)
|
.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 = 403, condition = "not authorized, the current user does not have the \"group:autocomplete\" privilege"),
|
||||||
@ResponseCode(code = 500, condition = "internal server error")
|
@ResponseCode(code = 500, condition = "internal server error")
|
||||||
})
|
})
|
||||||
public Response searchGroup(@Valid @QueryParam("filter") String 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) throws AutoCompleteBadParamException {
|
@DefaultValue(DEFAULT_LIMIT) @QueryParam("limit") Integer limit) {
|
||||||
validateParams(filter);
|
|
||||||
return Response.ok(groupManager.getFiltered(filter, limit)
|
return Response.ok(groupManager.getFiltered(filter, limit)
|
||||||
.stream()
|
.stream()
|
||||||
.map(mapper::map)
|
.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 = 403, condition = "not authorized, the current user does not have the \"repository:autocomplete\" privilege"),
|
||||||
@ResponseCode(code = 500, condition = "internal server error")
|
@ResponseCode(code = 500, condition = "internal server error")
|
||||||
})
|
})
|
||||||
public Response searchRepo(@Valid @QueryParam("filter") String 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) throws AutoCompleteBadParamException {
|
@DefaultValue(DEFAULT_LIMIT) @QueryParam("limit") Integer limit) {
|
||||||
validateParams(filter);
|
|
||||||
return Response.ok(repositoryManager.getFiltered(filter, limit)
|
return Response.ok(repositoryManager.getFiltered(filter, limit)
|
||||||
.stream()
|
.stream()
|
||||||
.map(mapper::map)
|
.map(mapper::map)
|
||||||
@@ -101,13 +100,5 @@ public class AutoCompleteResource {
|
|||||||
.build();
|
.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ public class DispatcherMock {
|
|||||||
dispatcher.getProviderFactory().registerProvider(InternalRepositoryExceptionMapper.class);
|
dispatcher.getProviderFactory().registerProvider(InternalRepositoryExceptionMapper.class);
|
||||||
dispatcher.getProviderFactory().registerProvider(ChangePasswordNotAllowedExceptionMapper.class);
|
dispatcher.getProviderFactory().registerProvider(ChangePasswordNotAllowedExceptionMapper.class);
|
||||||
dispatcher.getProviderFactory().registerProvider(InvalidPasswordExceptionMapper.class);
|
dispatcher.getProviderFactory().registerProvider(InvalidPasswordExceptionMapper.class);
|
||||||
dispatcher.getProviderFactory().registerProvider(AutoCompleteBadParamExceptionMapper.class);
|
|
||||||
return dispatcher;
|
return dispatcher;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user