diff options
author | Martin Stockhammer <martin_s@apache.org> | 2021-01-19 09:36:23 +0100 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2021-01-19 09:36:23 +0100 |
commit | b97724c6a70b18f5667b45a20375ff550bd9015c (patch) | |
tree | 0b662774fb60eca9252b0552b6a241ae750d83b5 /archiva-modules/archiva-base/archiva-repository-admin | |
parent | 3313a6cee8bc30e0a9b87c83998cfb219bce7306 (diff) | |
download | archiva-b97724c6a70b18f5667b45a20375ff550bd9015c.tar.gz archiva-b97724c6a70b18f5667b45a20375ff550bd9015c.zip |
Refactoring exceptions and adding REST V2 service
Diffstat (limited to 'archiva-modules/archiva-base/archiva-repository-admin')
7 files changed, 318 insertions, 16 deletions
diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/EntityExistsException.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/EntityExistsException.java new file mode 100644 index 000000000..9868427a9 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/EntityExistsException.java @@ -0,0 +1,78 @@ +package org.apache.archiva.admin.model;/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * This exception is thrown, if a entity that should be created, exists already. + * @author Martin Stockhammer <martin_s@apache.org> + * @since 3.0 + */ +public class EntityExistsException extends RepositoryAdminException +{ + private static final String KEY = "entity.exists"; + + public static EntityExistsException of(String... parameters) { + String message = getMessage( KEY, parameters ); + return new EntityExistsException( message, parameters ); + } + + public EntityExistsException( String s, String... parameters ) + { + super( s ); + setKey( KEY ); + setParameters( parameters ); + } + + public EntityExistsException( String s, String fieldName, String... parameters ) + { + super( s, fieldName ); + setKey( KEY ); + setParameters( parameters ); + } + + public EntityExistsException( String message, Throwable cause, String... parameters ) + { + super( message, cause ); + setKey( KEY ); + setParameters( parameters ); + } + + public EntityExistsException( String message, Throwable cause, String fieldName, String... parameters ) + { + super( message, cause, fieldName ); + setKey( KEY ); + setParameters( parameters ); + } +} diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/EntityNotFoundException.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/EntityNotFoundException.java new file mode 100644 index 000000000..90bf82d8e --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/EntityNotFoundException.java @@ -0,0 +1,79 @@ +package org.apache.archiva.admin.model;/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * This exception is thrown, if a requested entity does not exist. + * + * @author Martin Stockhammer <martin_s@apache.org> + * @since 3.0 + */ +public class EntityNotFoundException extends RepositoryAdminException +{ + public static final String KEY = "entity.not_found"; + + public static EntityNotFoundException of(String... parameters) { + String message = getMessage( KEY, parameters ); + return new EntityNotFoundException( message, parameters ); + } + + public EntityNotFoundException( String s, String... parameters ) + { + super( s ); + setKey( KEY ); + setParameters( parameters ); + } + + public EntityNotFoundException( String s, String fieldName, String... parameters ) + { + super( s, fieldName ); + setKey( KEY ); + setParameters( parameters ); + } + + public EntityNotFoundException( String message, Throwable cause, String... parameters ) + { + super( message, cause ); + setKey( KEY ); + setParameters( parameters ); + } + + public EntityNotFoundException( String message, Throwable cause, String fieldName, String... parameters ) + { + super( message, cause, fieldName ); + setKey( KEY ); + setParameters( parameters ); + } +} diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/RepositoryAdminException.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/RepositoryAdminException.java index 80f777015..c684c0e09 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/RepositoryAdminException.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/RepositoryAdminException.java @@ -19,6 +19,12 @@ package org.apache.archiva.admin.model; */ +import org.apache.commons.lang3.StringUtils; + +import java.text.MessageFormat; +import java.util.Locale; +import java.util.ResourceBundle; + /** * @author Olivier Lamy * @since 1.4-M1 @@ -27,6 +33,8 @@ public class RepositoryAdminException extends Exception { + private static final ResourceBundle bundle = ResourceBundle.getBundle( "org.apache.archiva.admin.model.error.AdminErrors", Locale.ROOT ); + /** * can return the field name of bean with issue * can be <code>null</code> @@ -34,6 +42,58 @@ public class RepositoryAdminException */ private String fieldName; + /** + * A unique identifier of this error + * @since 3.0 + */ + private String key; + private boolean keyExists = false; + + /** + * Message parameters + */ + String[] parameters = new String[0]; + + + public static RepositoryAdminException ofKey(String key, String... params) { + String message = getMessage( key, params ); + RepositoryAdminException ex = new RepositoryAdminException( message ); + ex.setKey( key ); + ex.setParameters( params ); + return ex; + } + + protected static String getMessage( String key, String[] params ) + { + return MessageFormat.format( bundle.getString( key ), params ); + } + + public static RepositoryAdminException ofKey(String key, Throwable cause, String... params) { + String message = getMessage( key, params ); + RepositoryAdminException ex = new RepositoryAdminException( message, cause ); + ex.setKey( key ); + ex.setParameters( params ); + return ex; + } + + + public static RepositoryAdminException ofKeyAndField(String key, String fieldName, String... params) { + String message = getMessage( key, params ); + RepositoryAdminException ex = new RepositoryAdminException( message, fieldName ); + ex.setKey( key ); + ex.setParameters( params ); + return ex; + } + + public static RepositoryAdminException ofKeyAndField(String key, Throwable cause, String fieldName, String... params) { + String message = getMessage( key, params ); + RepositoryAdminException ex = new RepositoryAdminException( message, cause, fieldName ); + ex.setKey( key ); + ex.setFieldName( fieldName ); + ex.setParameters( params ); + return ex; + } + public RepositoryAdminException( String s ) { super( s ); @@ -65,4 +125,32 @@ public class RepositoryAdminException { this.fieldName = fieldName; } + + public String getKey( ) + { + return key; + } + + public void setKey( String key ) + { + this.keyExists=!StringUtils.isEmpty( key ); + this.key = key; + } + + public boolean keyExists() { + return this.keyExists; + } + + public String[] getParameters( ) + { + return parameters; + } + + public void setParameters( String[] parameters ) + { + if (parameters==null) { + this.parameters = new String[0]; + } + this.parameters = parameters; + } } diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/RepositoryGroup.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/RepositoryGroup.java index 74bafcd7d..d4c0041b2 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/RepositoryGroup.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/beans/RepositoryGroup.java @@ -59,6 +59,8 @@ public class RepositoryGroup */ private String cronExpression; + private String location; + public RepositoryGroup() { // no op @@ -184,6 +186,16 @@ public class RepositoryGroup return this; } + public String getLocation( ) + { + return location; + } + + public void setLocation( String location ) + { + this.location = location; + } + @Override public boolean equals( Object other ) { diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/group/RepositoryGroupAdmin.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/group/RepositoryGroupAdmin.java index e98e8321a..e5411fea2 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/group/RepositoryGroupAdmin.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/group/RepositoryGroupAdmin.java @@ -19,6 +19,7 @@ package org.apache.archiva.admin.model.group; */ import org.apache.archiva.admin.model.AuditInformation; +import org.apache.archiva.admin.model.EntityNotFoundException; import org.apache.archiva.admin.model.RepositoryAdminException; import org.apache.archiva.admin.model.beans.RepositoryGroup; import org.apache.archiva.repository.storage.StorageAsset; @@ -35,8 +36,17 @@ public interface RepositoryGroupAdmin List<RepositoryGroup> getRepositoriesGroups() throws RepositoryAdminException; + /** + * Returns the repository group. If it is not found a {@link org.apache.archiva.admin.model.EntityNotFoundException} + * will be thrown. + * + * @param repositoryGroupId the identifier of the repository group + * @return the repository group object + * @throws RepositoryAdminException + * @throws EntityNotFoundException + */ RepositoryGroup getRepositoryGroup( String repositoryGroupId ) - throws RepositoryAdminException; + throws RepositoryAdminException, EntityNotFoundException; Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation ) throws RepositoryAdminException; diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/resources/org/apache/archiva/admin/model/error/AdminErrors.properties b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/resources/org/apache/archiva/admin/model/error/AdminErrors.properties new file mode 100644 index 000000000..7a9ddf897 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/resources/org/apache/archiva/admin/model/error/AdminErrors.properties @@ -0,0 +1,27 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +entity.exists=The entity {0} exists already +entity.not_found=The entity {0} was not found +repository_group.id.empty=The repository group id was empty +repository_group.id.max_length=The id "{0}" of the repository group exceeds {1} characters +repository_group.id.invalid_chars=The repository group id "{0}" contains invalid characters. Only the following are allowed: {1}. +repository_group.merged_index_ttl.min=Merged Index TTL must be greater than {0}. +repository_group.repository.not_found=The member repository with id "{0}" does not exist. Cannot be used in a repository group. +repository_group.registry.add_error=The registry could not add the repository "{0}": {1} +repository_group.registry.update_error=The registry could not update the repository "{0}": {1} diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/group/DefaultRepositoryGroupAdmin.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/group/DefaultRepositoryGroupAdmin.java index 1986c425e..3b810c7c9 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/group/DefaultRepositoryGroupAdmin.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/group/DefaultRepositoryGroupAdmin.java @@ -19,6 +19,8 @@ package org.apache.archiva.admin.repository.group; */ import org.apache.archiva.admin.model.AuditInformation; +import org.apache.archiva.admin.model.EntityExistsException; +import org.apache.archiva.admin.model.EntityNotFoundException; import org.apache.archiva.admin.model.RepositoryAdminException; import org.apache.archiva.admin.model.beans.ManagedRepository; import org.apache.archiva.admin.model.beans.RepositoryGroup; @@ -125,8 +127,14 @@ public class DefaultRepositoryGroupAdmin } @Override - public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) { - return convertRepositoryGroupObject( repositoryRegistry.getRepositoryGroup( repositoryGroupId ) ); + public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws EntityNotFoundException + { + org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId ); + if (group==null) { + throw new EntityNotFoundException( "Repository group does not exist" ); + } else { + return convertRepositoryGroupObject( group ); + } } @Override @@ -146,7 +154,8 @@ public class DefaultRepositoryGroupAdmin try { repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration); } catch (RepositoryException e) { - e.printStackTrace(); + log.error( "Could not add the repository group to the registry: {}", e.getMessage( ), e ); + throw RepositoryAdminException.ofKey( "repository_group.registry.add_error", e, repositoryGroup.getId(), e.getMessage() ); } triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation ); @@ -200,7 +209,8 @@ public class DefaultRepositoryGroupAdmin try { repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration); } catch (RepositoryException e) { - e.printStackTrace(); + log.error( "Could not update the repository group in the registry: {}", e.getMessage( ), e ); + throw RepositoryAdminException.ofKey( "repository_group.registry.update_error", e, repositoryGroup.getId(), e.getMessage() ); } org.apache.archiva.repository.RepositoryGroup rg = repositoryRegistry.getRepositoryGroup( repositoryGroup.getId( ) ); @@ -349,26 +359,24 @@ public class DefaultRepositoryGroupAdmin String repoGroupId = repositoryGroup.getId(); if ( StringUtils.isBlank( repoGroupId ) ) { - throw new RepositoryAdminException( "repositoryGroup id cannot be empty" ); + throw RepositoryAdminException.ofKey("repository_group.id.empty" ); } if ( repoGroupId.length() > 100 ) { - throw new RepositoryAdminException( - "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" ); + throw RepositoryAdminException.ofKey("repository_group.id.max_length",repoGroupId, Integer.toString( 100 )); } Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId ); if ( !matcher.matches() ) { - throw new RepositoryAdminException( - "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" ); + throw RepositoryAdminException.ofKey("repository_group.id.invalid_chars","alphanumeric, '.', '-','_'" ); } if ( repositoryGroup.getMergedIndexTtl() <= 0 ) { - throw new RepositoryAdminException( "Merged Index TTL must be greater than 0." ); + throw RepositoryAdminException.ofKey("repository_group.merged_index_ttl.min","0" ); } Configuration configuration = getArchivaConfiguration().getConfiguration(); @@ -377,18 +385,18 @@ public class DefaultRepositoryGroupAdmin { if ( !updateMode ) { - throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId + throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId + "], that id already exists as a repository group." ); } } else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) ) { - throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId + throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId + "], that id already exists as a managed repository." ); } else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) ) { - throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId + throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId + "], that id already exists as a remote repository." ); } @@ -402,8 +410,7 @@ public class DefaultRepositoryGroupAdmin { if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null ) { - throw new RepositoryAdminException( - "managedRepository with id " + id + " not exists so cannot be used in a repositoryGroup" ); + throw RepositoryAdminException.ofKey("repository_group.repository.not_found",id ); } } } @@ -427,6 +434,7 @@ public class DefaultRepositoryGroupAdmin } rg.setCronExpression( group.getSchedulingDefinition() ); rg.setMergedIndexTtl( group.getMergedIndexTTL() ); + rg.setLocation( group.getLocation().toString() ); return rg; } } |