001    package org.apache.archiva.rest.services;
002    /*
003     * Licensed to the Apache Software Foundation (ASF) under one
004     * or more contributor license agreements.  See the NOTICE file
005     * distributed with this work for additional information
006     * regarding copyright ownership.  The ASF licenses this file
007     * to you under the Apache License, Version 2.0 (the
008     * "License"); you may not use this file except in compliance
009     * with the License.  You may obtain a copy of the License at
010     *
011     *   http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing,
014     * software distributed under the License is distributed on an
015     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016     * KIND, either express or implied.  See the License for the
017     * specific language governing permissions and limitations
018     * under the License.
019     */
020    
021    import org.apache.archiva.admin.model.RepositoryAdminException;
022    import org.apache.archiva.admin.model.beans.ProxyConnectorRule;
023    import org.apache.archiva.admin.model.proxyconnectorrule.ProxyConnectorRuleAdmin;
024    import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
025    import org.apache.archiva.rest.api.services.ProxyConnectorRuleService;
026    import org.apache.commons.lang.StringUtils;
027    import org.springframework.stereotype.Service;
028    
029    import javax.inject.Inject;
030    import java.util.List;
031    
032    /**
033     * @author Olivier Lamy
034     */
035    @Service ("proxyConnectorRuleService#rest")
036    public class DefaultProxyConnectorRuleService
037        extends AbstractRestService
038        implements ProxyConnectorRuleService
039    {
040    
041        @Inject
042        private ProxyConnectorRuleAdmin proxyConnectorRuleAdmin;
043    
044        public List<ProxyConnectorRule> getProxyConnectorRules()
045            throws ArchivaRestServiceException
046        {
047            try
048            {
049                return proxyConnectorRuleAdmin.getProxyConnectorRules();
050            }
051            catch ( RepositoryAdminException e )
052            {
053                throw new ArchivaRestServiceException( e.getMessage(), e );
054            }
055        }
056    
057        private void validateProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
058            throws ArchivaRestServiceException
059        {
060            if ( StringUtils.isEmpty( proxyConnectorRule.getPattern() ) )
061            {
062                ArchivaRestServiceException e = new ArchivaRestServiceException( "pattern cannot be empty", null );
063                e.setErrorKey( "proxy-connector-rule.pattern.empty" );
064                throw e;
065            }
066    
067            if ( proxyConnectorRule.getProxyConnectors() == null || proxyConnectorRule.getProxyConnectors().isEmpty() )
068            {
069                ArchivaRestServiceException e =
070                    new ArchivaRestServiceException( "proxyConnector rule must have proxyConnectors.", null );
071                e.setErrorKey( "proxy-connector-rule.pattern.connectors.empty" );
072                throw e;
073            }
074    
075            for ( ProxyConnectorRule proxyConnectorRule1 : getProxyConnectorRules() )
076            {
077                if ( StringUtils.equals( proxyConnectorRule.getPattern(), proxyConnectorRule1.getPattern() )
078                    && proxyConnectorRule.getProxyConnectorRuleType() == proxyConnectorRule1.getProxyConnectorRuleType() )
079                {
080                    ArchivaRestServiceException e =
081                        new ArchivaRestServiceException( "same ProxyConnector rule already exists.", null );
082                    e.setErrorKey( "proxy-connector-rule.pattern.already.exists" );
083                    throw e;
084                }
085            }
086        }
087    
088        public Boolean addProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
089            throws ArchivaRestServiceException
090        {
091    
092            validateProxyConnectorRule( proxyConnectorRule );
093    
094            try
095            {
096                proxyConnectorRuleAdmin.addProxyConnectorRule( proxyConnectorRule, getAuditInformation() );
097                return Boolean.TRUE;
098            }
099            catch ( RepositoryAdminException e )
100            {
101                throw new ArchivaRestServiceException( e.getMessage(), e );
102            }
103        }
104    
105        public Boolean deleteProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
106            throws ArchivaRestServiceException
107        {
108            try
109            {
110                proxyConnectorRuleAdmin.deleteProxyConnectorRule( proxyConnectorRule, getAuditInformation() );
111                return Boolean.TRUE;
112            }
113            catch ( RepositoryAdminException e )
114            {
115                throw new ArchivaRestServiceException( e.getMessage(), e );
116            }
117        }
118    
119        public Boolean updateProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
120            throws ArchivaRestServiceException
121        {
122            try
123            {
124                proxyConnectorRuleAdmin.updateProxyConnectorRule( proxyConnectorRule, getAuditInformation() );
125                return Boolean.TRUE;
126            }
127            catch ( RepositoryAdminException e )
128            {
129                throw new ArchivaRestServiceException( e.getMessage(), e );
130            }
131        }
132    }