Merged in feature/validation_reflection (pull request #105)

Use reflection for validation
This commit is contained in:
Sebastian Sdorra
2018-11-08 09:37:36 +00:00
11 changed files with 35 additions and 68 deletions

View File

@@ -485,6 +485,7 @@
see https://blogs.oracle.com/darcy/entry/bootclasspath_older_source see https://blogs.oracle.com/darcy/entry/bootclasspath_older_source
--> -->
<compilerArgument>-Xlint:unchecked,-options</compilerArgument> <compilerArgument>-Xlint:unchecked,-options</compilerArgument>
<compilerArgument>-parameters</compilerArgument>
</configuration> </configuration>
</plugin> </plugin>
@@ -757,7 +758,7 @@
<guice.version>4.0</guice.version> <guice.version>4.0</guice.version>
<!-- event bus --> <!-- event bus -->
<legman.version>1.4.0</legman.version> <legman.version>1.4.2</legman.version>
<!-- webserver --> <!-- webserver -->
<jetty.version>9.2.10.v20150310</jetty.version> <jetty.version>9.2.10.v20150310</jetty.version>

View File

@@ -71,7 +71,7 @@ public class GroupCollectionResource {
/** /**
* Creates a new group. * Creates a new group.
* @param groupDto The group to be created. * @param group The group to be created.
* @return A response with the link to the new group (if created successfully). * @return A response with the link to the new group (if created successfully).
*/ */
@POST @POST
@@ -86,9 +86,9 @@ public class GroupCollectionResource {
}) })
@TypeHint(TypeHint.NO_CONTENT.class) @TypeHint(TypeHint.NO_CONTENT.class)
@ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created group")) @ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created group"))
public Response create(@Valid @Named("group") GroupDto groupDto) { public Response create(@Valid GroupDto group) {
return adapter.create(groupDto, return adapter.create(group,
() -> dtoToGroupMapper.map(groupDto), () -> dtoToGroupMapper.map(group),
group -> resourceLinks.group().self(group.getName())); g -> resourceLinks.group().self(g.getName()));
} }
} }

View File

@@ -83,7 +83,7 @@ public class GroupResource {
* <strong>Note:</strong> This method requires "group" privilege. * <strong>Note:</strong> This method requires "group" privilege.
* *
* @param name name of the group to be modified * @param name name of the group to be modified
* @param groupDto group object to modify * @param group group object to modify
*/ */
@PUT @PUT
@Path("") @Path("")
@@ -97,7 +97,7 @@ public class GroupResource {
@ResponseCode(code = 500, condition = "internal server error") @ResponseCode(code = 500, condition = "internal server error")
}) })
@TypeHint(TypeHint.NO_CONTENT.class) @TypeHint(TypeHint.NO_CONTENT.class)
public Response update(@PathParam("id") String name, @Valid @Named("group") GroupDto groupDto) { public Response update(@PathParam("id") String name, @Valid GroupDto group) {
return adapter.update(name, existing -> dtoToGroupMapper.map(groupDto)); return adapter.update(name, existing -> dtoToGroupMapper.map(group));
} }
} }

View File

@@ -74,8 +74,8 @@ public class MeResource {
}) })
@TypeHint(TypeHint.NO_CONTENT.class) @TypeHint(TypeHint.NO_CONTENT.class)
@Consumes(VndMediaType.PASSWORD_CHANGE) @Consumes(VndMediaType.PASSWORD_CHANGE)
public Response changePassword(@Valid @Named("passwordChange") PasswordChangeDto passwordChangeDto) { public Response changePassword(@Valid PasswordChangeDto passwordChange) {
userManager.changePasswordForLoggedInUser(passwordService.encryptPassword(passwordChangeDto.getOldPassword()), passwordService.encryptPassword(passwordChangeDto.getNewPassword())); userManager.changePasswordForLoggedInUser(passwordService.encryptPassword(passwordChange.getOldPassword()), passwordService.encryptPassword(passwordChange.getNewPassword()));
return Response.noContent().build(); return Response.noContent().build();
} }
} }

View File

@@ -74,7 +74,7 @@ public class PermissionRootResource {
@TypeHint(TypeHint.NO_CONTENT.class) @TypeHint(TypeHint.NO_CONTENT.class)
@Consumes(VndMediaType.PERMISSION) @Consumes(VndMediaType.PERMISSION)
@Path("") @Path("")
public Response create(@PathParam("namespace") String namespace, @PathParam("name") String name,@Valid @Named("permission") PermissionDto permission) { public Response create(@PathParam("namespace") String namespace, @PathParam("name") String name,@Valid PermissionDto permission) {
log.info("try to add new permission: {}", permission); log.info("try to add new permission: {}", permission);
Repository repository = load(namespace, name); Repository repository = load(namespace, name);
RepositoryPermissions.permissionWrite(repository).check(); RepositoryPermissions.permissionWrite(repository).check();
@@ -161,7 +161,7 @@ public class PermissionRootResource {
public Response update(@PathParam("namespace") String namespace, public Response update(@PathParam("namespace") String namespace,
@PathParam("name") String name, @PathParam("name") String name,
@PathParam("permission-name") String permissionName, @PathParam("permission-name") String permissionName,
@Valid @Named("permission") PermissionDto permission) { @Valid PermissionDto permission) {
log.info("try to update the permission with name: {}. the modified permission is: {}", permissionName, permission); log.info("try to update the permission with name: {}. the modified permission is: {}", permissionName, permission);
Repository repository = load(namespace, name); Repository repository = load(namespace, name);
RepositoryPermissions.permissionWrite(repository).check(); RepositoryPermissions.permissionWrite(repository).check();

View File

@@ -72,7 +72,7 @@ public class RepositoryCollectionResource {
* <strong>Note:</strong> This method requires "repository" privilege. The namespace of the given repository will * <strong>Note:</strong> This method requires "repository" privilege. The namespace of the given repository will
* be ignored and set by the configured namespace strategy. * be ignored and set by the configured namespace strategy.
* *
* @param repositoryDto The repository to be created. * @param repository The repository to be created.
* @return A response with the link to the new repository (if created successfully). * @return A response with the link to the new repository (if created successfully).
*/ */
@POST @POST
@@ -87,9 +87,9 @@ public class RepositoryCollectionResource {
}) })
@TypeHint(TypeHint.NO_CONTENT.class) @TypeHint(TypeHint.NO_CONTENT.class)
@ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created repository")) @ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created repository"))
public Response create(@Valid @Named("repository") RepositoryDto repositoryDto) { public Response create(@Valid RepositoryDto repository) {
return adapter.create(repositoryDto, return adapter.create(repository,
() -> dtoToRepositoryMapper.map(repositoryDto, null), () -> dtoToRepositoryMapper.map(repository, null),
repository -> resourceLinks.repository().self(repository.getNamespace(), repository.getName())); r -> resourceLinks.repository().self(r.getNamespace(), r.getName()));
} }
} }

View File

@@ -126,7 +126,7 @@ public class RepositoryResource {
* *
* @param namespace the namespace of the repository to be modified * @param namespace the namespace of the repository to be modified
* @param name the name of the repository to be modified * @param name the name of the repository to be modified
* @param repositoryDto repository object to modify * @param repository repository object to modify
*/ */
@PUT @PUT
@Path("") @Path("")
@@ -140,10 +140,10 @@ public class RepositoryResource {
@ResponseCode(code = 500, condition = "internal server error") @ResponseCode(code = 500, condition = "internal server error")
}) })
@TypeHint(TypeHint.NO_CONTENT.class) @TypeHint(TypeHint.NO_CONTENT.class)
public Response update(@PathParam("namespace") String namespace, @PathParam("name") String name, @Valid @Named("repository") RepositoryDto repositoryDto) { public Response update(@PathParam("namespace") String namespace, @PathParam("name") String name, @Valid RepositoryDto repository) {
return adapter.update( return adapter.update(
loadBy(namespace, name), loadBy(namespace, name),
existing -> processUpdate(repositoryDto, existing), existing -> processUpdate(repository, existing),
nameAndNamespaceStaysTheSame(namespace, name) nameAndNamespaceStaysTheSame(namespace, name)
); );
} }

View File

@@ -1,29 +0,0 @@
package sonia.scm.api.v2.resources;
import javax.inject.Named;
import javax.validation.ParameterNameProvider;
import javax.ws.rs.ext.Provider;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Provider
public class ResourceParameterNameProvider implements ParameterNameProvider {
@Override
public List<String> getParameterNames(Constructor<?> constructor) {
return Arrays.stream(constructor.getParameters()).map(this::getParameterName).collect(Collectors.toList());
}
@Override
public List<String> getParameterNames(Method method) {
return Arrays.stream(method.getParameters()).map(this::getParameterName).collect(Collectors.toList());
}
private String getParameterName(Parameter parameter) {
return Optional.ofNullable(parameter.getAnnotation(Named.class)).map(Named::value).orElse(parameter.getName());
}
}

View File

@@ -76,7 +76,7 @@ public class UserCollectionResource {
* *
* <strong>Note:</strong> This method requires "user" privilege. * <strong>Note:</strong> This method requires "user" privilege.
* *
* @param userDto The user to be created. * @param user The user to be created.
* @return A response with the link to the new user (if created successfully). * @return A response with the link to the new user (if created successfully).
*/ */
@POST @POST
@@ -91,7 +91,7 @@ public class UserCollectionResource {
}) })
@TypeHint(TypeHint.NO_CONTENT.class) @TypeHint(TypeHint.NO_CONTENT.class)
@ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created user")) @ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created user"))
public Response create(@Valid @Named("user") UserDto userDto) { public Response create(@Valid UserDto user) {
return adapter.create(userDto, () -> dtoToUserMapper.map(userDto, passwordService.encryptPassword(userDto.getPassword())), user -> resourceLinks.user().self(user.getName())); return adapter.create(user, () -> dtoToUserMapper.map(user, passwordService.encryptPassword(user.getPassword())), u -> resourceLinks.user().self(u.getName()));
} }
} }

View File

@@ -87,7 +87,7 @@ public class UserResource {
* <strong>Note:</strong> This method requires "user" privilege. * <strong>Note:</strong> This method requires "user" privilege.
* *
* @param name name of the user to be modified * @param name name of the user to be modified
* @param userDto user object to modify * @param user user object to modify
*/ */
@PUT @PUT
@Path("") @Path("")
@@ -101,8 +101,8 @@ public class UserResource {
@ResponseCode(code = 500, condition = "internal server error") @ResponseCode(code = 500, condition = "internal server error")
}) })
@TypeHint(TypeHint.NO_CONTENT.class) @TypeHint(TypeHint.NO_CONTENT.class)
public Response update(@PathParam("id") String name, @Valid @Named("user") UserDto userDto) { public Response update(@PathParam("id") String name, @Valid UserDto user) {
return adapter.update(name, existing -> dtoToUserMapper.map(userDto, existing.getPassword())); return adapter.update(name, existing -> dtoToUserMapper.map(user, existing.getPassword()));
} }
/** /**
@@ -114,7 +114,7 @@ public class UserResource {
* <strong>Note:</strong> This method requires "user:changeOwnPassword" privilege to modify the own password. * <strong>Note:</strong> This method requires "user:changeOwnPassword" privilege to modify the own password.
* *
* @param name name of the user to be modified * @param name name of the user to be modified
* @param passwordOverwriteDto change password object to modify password. the old password is here not required * @param passwordOverwrite change password object to modify password. the old password is here not required
*/ */
@PUT @PUT
@Path("password") @Path("password")
@@ -128,8 +128,8 @@ public class UserResource {
@ResponseCode(code = 500, condition = "internal server error") @ResponseCode(code = 500, condition = "internal server error")
}) })
@TypeHint(TypeHint.NO_CONTENT.class) @TypeHint(TypeHint.NO_CONTENT.class)
public Response overwritePassword(@PathParam("id") String name, @Valid @Named("passwordChange") PasswordOverwriteDto passwordOverwriteDto) { public Response overwritePassword(@PathParam("id") String name, @Valid PasswordOverwriteDto passwordOverwrite) {
userManager.overwritePassword(name, passwordService.encryptPassword(passwordOverwriteDto.getNewPassword())); userManager.overwritePassword(name, passwordService.encryptPassword(passwordOverwrite.getNewPassword()));
return Response.noContent().build(); return Response.noContent().build();
} }
} }

View File

@@ -1,14 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<validation-config <validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration" xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration"
http://jboss.org/xml/ns/javax/validation/configuration
validation-configuration-1.1.xsd"
version="1.1"> version="1.1">
<default-provider>org.hibernate.validator.HibernateValidator</default-provider>
<message-interpolator>org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator</message-interpolator> <parameter-name-provider>org.hibernate.validator.parameternameprovider.ReflectionParameterNameProvider</parameter-name-provider>
<traversable-resolver>org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver</traversable-resolver>
<constraint-validator-factory>org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl</constraint-validator-factory>
<parameter-name-provider>sonia.scm.api.v2.resources.ResourceParameterNameProvider</parameter-name-provider>
</validation-config> </validation-config>