org.sonatype.nexus.rest.ValidationErrorsException Java Examples

The following examples show how to use org.sonatype.nexus.rest.ValidationErrorsException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserApiResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void validateRoles(final Set<String> roleIds) {
  ValidationErrorsException errors = new ValidationErrorsException();

  Set<String> localRoles;
  try {
    localRoles = securitySystem.listRoles(UserManager.DEFAULT_SOURCE).stream().map(r -> r.getRoleId())
        .collect(Collectors.toSet());
    for (String roleId : roleIds) {
      if (!localRoles.contains(roleId)) {
        errors.withError("roles", "Unable to locate roleId: " + roleId);
      }
    }
    if (errors.hasValidationErrors()) {
      throw errors;
    }
  }
  catch (NoSuchAuthorizationManagerException e) {
    log.error("Unable to locate default user manager", e);
    throw createWebException(Status.INTERNAL_SERVER_ERROR, "Unable to locate default user manager");
  }
}
 
Example #2
Source File: RawUploadHandlerTestSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_covertDoubleDotFilename() throws IOException {
  ComponentUpload component = new ComponentUpload();
  component.getFields().put("directory", "foo");

  AssetUpload asset = new AssetUpload();
  asset.getFields().put("filename", "foo/../../foo.jar");
  asset.setPayload(jarPayload);
  component.getAssetUploads().add(asset);

  try {
    underTest.handle(repository, component);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Path is not allowed to have '.' or '..' segments: '" + path("foo/foo/../../foo.jar") + "'"));
  }
}
 
Example #3
Source File: BlobStoreGroupDescriptor.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void validateOnlyEmptyOrNotWritableExistingMembersRemoved(final String name, final List<String> memberNames) {
  BlobStore blobStore = blobStoreManager.get(name);
  if (blobStore != null) {
    BlobStoreConfiguration currentConfiguration = blobStore.getBlobStoreConfiguration();
    if (currentConfiguration != null && currentConfiguration.getType().equals(BlobStoreGroup.TYPE)) {
      for (String existingMemberName : memberNames(currentConfiguration)) {
        if (!memberNames.contains(existingMemberName)) {
          BlobStore existingMember = blobStoreManager.get(existingMemberName);
          if (existingMember.isWritable() || !existingMember.isEmpty()) {
            throw new ValidationErrorsException(
                format("Blob Store '%s' cannot be removed from Blob Store Group '%s', " +
                    "use 'Admin - Remove a member from a blob store group' task instead",
                    existingMemberName, name));
          }
        }
      }
    }
  }
}
 
Example #4
Source File: OrientNpmUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_unauthorized() throws IOException {
  when(contentPermissionChecker.isPermitted(eq(REPO_NAME), eq(NpmFormat.NAME), eq(BreadActions.EDIT), any()))
      .thenReturn(false);

  ComponentUpload component = new ComponentUpload();
  AssetUpload asset = new AssetUpload();
  asset.setPayload(payload);
  component.getAssetUploads().add(asset);

  try {
    underTest.handle(repository, component);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(), is("Not authorized for requested path '@foo/bar/-/bar-1.5.3.tgz'"));
  }
}
 
Example #5
Source File: OrientPyPiUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_unauthorized() throws IOException {
  when(contentPermissionChecker.isPermitted(eq(REPO_NAME), eq(PyPiFormat.NAME), eq(BreadActions.EDIT), any()))
      .thenReturn(false);

  ComponentUpload component = new ComponentUpload();
  AssetUpload asset = new AssetUpload();
  asset.setPayload(payload);
  component.getAssetUploads().add(asset);

  try {
    underTest.handle(repository, component);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(), is("Not authorized for requested path 'packages/sample/1.2.0/sample-1.2.0-py2.7.egg'"));
  }
}
 
Example #6
Source File: RawUploadHandlerTestSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_dotFilename() throws IOException {
  ComponentUpload component = new ComponentUpload();
  component.getFields().put("directory", "foo");

  AssetUpload asset = new AssetUpload();
  asset.getFields().put("filename", ".");
  asset.setPayload(jarPayload);
  component.getAssetUploads().add(asset);

  try {
    underTest.handle(repository, component);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Path is not allowed to have '.' or '..' segments: '" + path("foo/.") + "'"));
  }
}
 
Example #7
Source File: RawUploadHandlerTestSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_convertDoubleDotDirectory() throws IOException {
  ComponentUpload component = new ComponentUpload();
  component.getFields().put("directory", "foo/..");

  AssetUpload asset = new AssetUpload();
  asset.getFields().put("filename", "foo.jar");
  asset.setPayload(jarPayload);
  component.getAssetUploads().add(asset);

  try {
    underTest.handle(repository, component);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Path is not allowed to have '.' or '..' segments: '" + path("foo/../foo.jar") + "'"));
  }
}
 
Example #8
Source File: RawUploadHandlerTestSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_doubleDotDirectory() throws IOException {
  ComponentUpload component = new ComponentUpload();
  component.getFields().put("directory", "..");

  AssetUpload asset = new AssetUpload();
  asset.getFields().put("filename", "foo.jar");
  asset.setPayload(jarPayload);
  component.getAssetUploads().add(asset);

  try {
    underTest.handle(repository, component);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Path is not allowed to have '.' or '..' segments: '" + path("../foo.jar") + "'"));
  }
}
 
Example #9
Source File: RawUploadHandlerTestSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_dotDirectory() throws IOException {
  ComponentUpload component = new ComponentUpload();
  component.getFields().put("directory", ".");

  AssetUpload asset = new AssetUpload();
  asset.getFields().put("filename", "foo.jar");
  asset.setPayload(jarPayload);
  component.getAssetUploads().add(asset);

  try {
    underTest.handle(repository, component);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Path is not allowed to have '.' or '..' segments: '" + path("./foo.jar") + "'"));
  }
}
 
Example #10
Source File: OrientPyPiUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_dot() throws IOException {
  metadata.put(PyPiAttributes.P_NAME, "./foo/../bar");
  metadata.put(PyPiAttributes.P_VERSION, "./foo/../bar");
  metadata.put(PyPiAttributes.P_ARCHIVE_TYPE, "zip");

  ComponentUpload component = new ComponentUpload();
  AssetUpload asset = new AssetUpload();
  asset.setPayload(payload);
  component.getAssetUploads().add(asset);

  try {
    underTest.handle(repository, component);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(), is("Path is not allowed to have '.' or '..' segments: 'packages/-/foo/-/bar/./foo/../bar/sample-1.2.0-py2.7.egg'"));
  }
}
 
Example #11
Source File: RawUploadHandlerTestSupport.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_doubleDotFilename() throws IOException {
  ComponentUpload component = new ComponentUpload();
  component.getFields().put("directory", "foo");

  AssetUpload asset = new AssetUpload();
  asset.getFields().put("filename", "..");
  asset.setPayload(jarPayload);
  component.getAssetUploads().add(asset);

  try {
    underTest.handle(repository, component);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Path is not allowed to have '.' or '..' segments: '" + path("foo/..") + "'"));
  }
}
 
Example #12
Source File: BlobStoreGroupDescriptor.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void validateEligibleMembers(final String name, final List<String> memberNames) {
  for (String memberName : memberNames) {
    BlobStore member = blobStoreManager.get(memberName);
    if (!member.isGroupable()) {
      BlobStoreConfiguration memberConfig = member.getBlobStoreConfiguration();
      throw new ValidationErrorsException(
          format("Blob Store '%s' is of type '%s' and is not eligible to be a group member", memberName,
              memberConfig.getType()));
    }

    // target member may not be a member of a different group
    Predicate<String> sameGroup = name::equals;
    blobStoreManager.getParent(memberName).filter(sameGroup.negate()).ifPresent(groupName -> {
      throw new ValidationErrorsException(
          format("Blob Store '%s' is already a member of Blob Store Group '%s'", memberName, groupName));
    });

    // target member may not be set as repository storage
    int repoCount = blobStoreUtil.usageCount(memberName);
    if (repoCount > 0) {
      throw new ValidationErrorsException(format(
          "Blob Store '%s' is set as storage for %s repositories and is not eligible to be a group member",
          memberName, repoCount));
    }
  }
}
 
Example #13
Source File: MavenUploadHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@VisibleForTesting
void validatePom(final Model model) {
  if (model == null) {
    throw new ValidationErrorsException("The provided POM file is invalid.");
  }

  String groupId = getGroupId(model);
  String version = getVersion(model);
  String artifactId = getArtifactId(model);

  if (groupId == null || artifactId == null || version == null ||
      groupId.startsWith(MAVEN_POM_PROPERTY_PREFIX) ||
      artifactId.startsWith(MAVEN_POM_PROPERTY_PREFIX) ||
      version.startsWith(MAVEN_POM_PROPERTY_PREFIX)) {
    throw new ValidationErrorsException(
        format("The provided POM file is invalid.  Could not retrieve valid G:A:V parameters (%s:%s:%s)", groupId,
            artifactId, version));
  }
}
 
Example #14
Source File: MavenUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_unauthorized() throws IOException {
  when(contentPermissionChecker.isPermitted(eq(REPO_NAME), eq(Maven2Format.NAME), eq(BreadActions.EDIT), any()))
      .thenReturn(false);

  ComponentUpload componentUpload = new ComponentUpload();

  componentUpload.getFields().put("groupId", "org.apache.maven");
  componentUpload.getFields().put("artifactId", "tomcat");
  componentUpload.getFields().put("version", "5.0.28");

  AssetUpload assetUpload = new AssetUpload();
  assetUpload.getFields().put("extension", "jar");
  assetUpload.setPayload(jarPayload);
  componentUpload.getAssetUploads().add(assetUpload);

  try {
    underTest.handle(repository, componentUpload);
    fail("Expected validation exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Not authorized for requested path 'org/apache/maven/tomcat/5.0.28/tomcat-5.0.28.jar'"));
  }
}
 
Example #15
Source File: MavenUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_snapshotToRelease() throws Exception {
  when(mavenFacet.getVersionPolicy()).thenReturn(VersionPolicy.RELEASE);
  when(versionPolicyValidator.validArtifactPath(any(), any())).thenReturn(false);

  ComponentUpload componentUpload = new ComponentUpload();

  componentUpload.getFields().put("groupId", "org.apache.maven");
  componentUpload.getFields().put("artifactId", "tomcat");
  componentUpload.getFields().put("version", "5.0.28-SNAPSHOT");

  AssetUpload assetUpload = new AssetUpload();
  assetUpload.getFields().put("extension", "jar");
  assetUpload.setPayload(jarPayload);
  componentUpload.getAssetUploads().add(assetUpload);

  try {
    underTest.handle(repository, componentUpload);
    fail("Expected version policy mismatch exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Version policy mismatch, cannot upload SNAPSHOT content to RELEASE repositories for file '0'"));
  }
}
 
Example #16
Source File: MavenUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_snapshot_not_supported() throws Exception {
  when(mavenFacet.getVersionPolicy()).thenReturn(VersionPolicy.SNAPSHOT);
  when(versionPolicyValidator.validArtifactPath(any(), any())).thenReturn(false);

  ComponentUpload componentUpload = new ComponentUpload();

  componentUpload.getFields().put("groupId", "org.apache.maven");
  componentUpload.getFields().put("artifactId", "tomcat");
  componentUpload.getFields().put("version", "5.0.28-SNAPSHOT");

  AssetUpload assetUpload = new AssetUpload();
  assetUpload.getFields().put("extension", "jar");
  assetUpload.setPayload(jarPayload);
  componentUpload.getAssetUploads().add(assetUpload);

  try {
    underTest.handle(repository, componentUpload);
    fail("Expected version policy mismatch exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Upload to snapshot repositories not supported, use the maven client."));
  }
}
 
Example #17
Source File: MavenUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_nullCoordinates() throws Exception {
  ComponentUpload componentUpload = new ComponentUpload();

  //note the slashes here are causing the MavenPathParser to choke
  componentUpload.getFields().put("groupId", "a</groupId>");
  componentUpload.getFields().put("artifactId", "a</artifactId>");
  componentUpload.getFields().put("version", "a</version>");
  componentUpload.getFields().put("packaging", "a</packaging>");
  componentUpload.getFields().put("generate-pom", "true");

  AssetUpload assetUpload = new AssetUpload();
  assetUpload.getFields().put("extension", "jar");
  assetUpload.setPayload(jarPayload);
  componentUpload.getAssetUploads().add(assetUpload);

  try {
    underTest.handle(repository, componentUpload);
    fail("Expected invalid coordinates exception");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Cannot generate maven coordinate from assembled path 'a</groupId>/a</artifactId>/a</version>/a</artifactId>-a</version>.jar'"));
  }
}
 
Example #18
Source File: ValidatingComponentUploadTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testValidate_noViolations() {
  when(uploadDefinition.getAssetFields()).thenReturn(
      Collections.singletonList(new UploadFieldDefinition("foo", false, STRING)));
  when(uploadDefinition.getComponentFields()).thenReturn(
      Collections.singletonList(new UploadFieldDefinition("bar", false, STRING)));

  when(assetUpload.getPayload()).thenReturn(payload);
  when(assetUpload.getFields()).thenReturn(singletonMap("foo", "fooValue"));
  when(assetUpload.getField("foo")).thenReturn("fooValue");

  when(componentUpload.getAssetUploads()).thenReturn(Collections.singletonList(assetUpload));
  when(componentUpload.getFields()).thenReturn(singletonMap("bar", "barValue"));
  when(componentUpload.getField("bar")).thenReturn("barValue");

  try {
    ValidatingComponentUpload validated = new ValidatingComponentUpload(uploadDefinition, componentUpload);
    validated.getComponentUpload();
  }
  catch (ValidationErrorsException e) {
    fail(format("Unexpected validation exception thrown '%s'", e));
  }
}
 
Example #19
Source File: UploadManagerImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private UploadHandler getUploadHandler(final Repository repository)
{
  if (!(repository.getType() instanceof HostedType)) {
    throw new ValidationErrorsException(
        format("Uploading components to a '%s' type repository is unsupported, must be '%s'",
            repository.getType().getValue(), HostedType.NAME));
  }

  String repositoryFormat = repository.getFormat().toString();
  UploadHandler uploadHandler = uploadHandlers.get(repositoryFormat);

  if (uploadHandler == null) {
    throw new ValidationErrorsException(
        format("Uploading components to '%s' repositories is unsupported", repositoryFormat));
  }

  return uploadHandler;
}
 
Example #20
Source File: UploadManagerImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public UploadResponse handle(final Repository repository, final HttpServletRequest request) throws IOException {
  checkNotNull(repository);
  checkNotNull(request);

  if (!repository.getConfiguration().isOnline()) {
    throw new ValidationErrorsException("Repository offline");
  }

  UploadHandler uploadHandler = getUploadHandler(repository);
  ComponentUpload upload = create(repository, request);
  logUploadDetails(upload, repository);

  try {
    return uploadHandler.handle(repository, uploadHandler.getValidatingComponentUpload(upload).getComponentUpload());
  }
  finally {
    for (AssetUpload assetUpload : upload.getAssetUploads()) {
      assetUpload.getPayload().close();
    }
  }
}
 
Example #21
Source File: UploadHandler.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Use the <code>ContentPermissionChecker</code> to verify the current user has EDIT permission for the repository,
 * path and coordinates. An <code>AuthorizationException</code> will be thrown if the action is not permitted.
 *
 * @param repositoryName the name of the repository the asset is being uploaded to
 * @param format the format name
 * @param path the path within the repository that will represent the asset (should not be prefixed with a slash)
 * @param coordinates a map containing the coordinate fields and their values
 */
default void ensurePermitted(final String repositoryName,
                             final String format,
                             final String path,
                             final Map<String, String> coordinates)
{
  boolean dotSegment = Stream.of(path.split("/")).anyMatch(segment -> segment.equals(".") || segment.equals(".."));
  if (dotSegment) {
    throw new ValidationErrorsException(format("Path is not allowed to have '.' or '..' segments: '%s'", path));
  }

  VariableSource variableSource = getVariableResolverAdapter().fromCoordinates(format, path, coordinates);
  if (!contentPermissionChecker().isPermitted(repositoryName, format, BreadActions.EDIT, variableSource)) {
    throw new ValidationErrorsException(format("Not authorized for requested path '%s'", path));
  }
}
 
Example #22
Source File: MavenUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_doubleDotInArtifactId() throws IOException {
  ComponentUpload componentUpload = new ComponentUpload();

  componentUpload.getFields().put("groupId", "groupId");
  componentUpload.getFields().put("artifactId", "/../g/a/v/a-v.jar");
  componentUpload.getFields().put("version", "version");

  AssetUpload assetUpload = new AssetUpload();
  assetUpload.getFields().put("extension", "jar");
  assetUpload.setPayload(jarPayload);
  componentUpload.getAssetUploads().add(assetUpload);

  try {
    underTest.handle(repository, componentUpload);
    fail("Expected ValidationErrorsException");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Path is not allowed to have '.' or '..' segments: 'groupId//../g/a/v/a-v.jar/version//../g/a/v/a-v.jar-version.jar'"));
  }
}
 
Example #23
Source File: MavenUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_doubleDotInVersion() throws IOException {
  ComponentUpload componentUpload = new ComponentUpload();

  componentUpload.getFields().put("groupId", "groupId");
  componentUpload.getFields().put("artifactId", "artifactId");
  componentUpload.getFields().put("version", "/../g/a/v/a-v.jar");

  AssetUpload assetUpload = new AssetUpload();
  assetUpload.getFields().put("extension", "jar");
  assetUpload.setPayload(jarPayload);
  componentUpload.getAssetUploads().add(assetUpload);

  try {
    underTest.handle(repository, componentUpload);
    fail("Expected ValidationErrorsException");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Path is not allowed to have '.' or '..' segments: 'groupId/artifactId//../g/a/v/a-v.jar/artifactId-/../g/a/v/a-v.jar.jar'"));
  }
}
 
Example #24
Source File: MavenUploadHandlerTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testHandle_doubleDotInExtension() throws IOException {
  ComponentUpload componentUpload = new ComponentUpload();

  componentUpload.getFields().put("groupId", "groupId");
  componentUpload.getFields().put("artifactId", "artifactId");
  componentUpload.getFields().put("version", "version");

  AssetUpload assetUpload = new AssetUpload();
  assetUpload.getFields().put("extension", "/../g/a/v/a-v.jar");
  assetUpload.setPayload(jarPayload);
  componentUpload.getAssetUploads().add(assetUpload);

  try {
    underTest.handle(repository, componentUpload);
    fail("Expected ValidationErrorsException");
  }
  catch (ValidationErrorsException e) {
    assertThat(e.getValidationErrors().size(), is(1));
    assertThat(e.getValidationErrors().get(0).getMessage(),
        is("Path is not allowed to have '.' or '..' segments: 'groupId/artifactId/version/artifactId-version./../g/a/v/a-v.jar'"));
  }
}
 
Example #25
Source File: ValidationErrorsResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@PUT
@Path("/manual/multiple")
@Consumes({APPLICATION_XML, APPLICATION_JSON})
@Produces({APPLICATION_XML, APPLICATION_JSON})
public UserXO putWithMultipleManualValidations(final UserXO user) {
  log.info("PUT user: {}", user);

  final ValidationErrorsException validationErrors = new ValidationErrorsException();
  if (user.getName() == null) {
    validationErrors.withError("name", "Name cannot be null");
  }
  if (user.getDescription() == null) {
    validationErrors.withError("description", "Description cannot be null");
  }

  if (validationErrors.hasValidationErrors()) {
    throw validationErrors;
  }

  return user;
}
 
Example #26
Source File: ValidationErrorsResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@PUT
@Path("/manual/single")
@Consumes({APPLICATION_XML, APPLICATION_JSON})
@Produces({APPLICATION_XML, APPLICATION_JSON})
public UserXO putWithSingleManualValidation(final UserXO user) {
  log.info("PUT user: {}", user);

  if (user.getName() == null) {
    throw new ValidationErrorsException("name", "Name cannot be null");
  }
  if (user.getDescription() == null) {
    throw new ValidationErrorsException("description", "Description cannot be null");
  }

  return user;
}
 
Example #27
Source File: UploadManagerImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private UploadHandler getUploadHandler(final Repository repository)
{
  if (!(repository.getType() instanceof HostedType)) {
    throw new ValidationErrorsException(
        format("Uploading components to a '%s' type repository is unsupported, must be '%s'",
            repository.getType().getValue(), HostedType.NAME));
  }

  String repositoryFormat = repository.getFormat().toString();
  UploadHandler uploadHandler = uploadHandlers.get(repositoryFormat);

  if (uploadHandler == null) {
    throw new ValidationErrorsException(
        format("Uploading components to '%s' repositories is unsupported", repositoryFormat));
  }

  return uploadHandler;
}
 
Example #28
Source File: BlobStoreGroupDescriptor.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void validateConfig(final BlobStoreConfiguration config) {
  super.validateConfig(config);
  validateEnabled();
  String name = config.getName();

  String fillPolicy = config.attributes(CONFIG_KEY).get(FILL_POLICY_KEY, String.class);
  if (StringUtils.isBlank(fillPolicy)) {
    throw new ValidationErrorsException("Blob store group requires a fill policy configuration");
  }

  List<String> memberNames = config.attributes(CONFIG_KEY).get(MEMBERS_KEY, List.class);
  validateNotEmptyOrSelfReferencing(name, memberNames);
  validateEligibleMembers(name, memberNames);
  validateOnlyEmptyOrNotWritableExistingMembersRemoved(name, memberNames);
}
 
Example #29
Source File: PasswordValidatorTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testValidate_failValidation() {
  PasswordValidator underTest = new PasswordValidator("[a]+", null);

  thrown.expect(ValidationErrorsException.class);
  thrown.expectMessage("Password does not match corporate policy");

  underTest.validate("foo");
}
 
Example #30
Source File: OrientRoutingRuleStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static void validate(final RoutingRule rule, final String id, final String message) {
  try {
    OrientRoutingRuleStore.validate(rule);
    fail("Expected exception");
  }
  catch (ValidationErrorsException e) {
    assertValidationException(e, id, message);
  }
}