This project has retired. For details please refer to its Attic page.
Source code
001package 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
021import org.apache.archiva.admin.model.RepositoryAdminException;
022import org.apache.archiva.admin.model.beans.ProxyConnectorRule;
023import org.apache.archiva.admin.model.proxyconnectorrule.ProxyConnectorRuleAdmin;
024import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
025import org.apache.archiva.rest.api.services.ProxyConnectorRuleService;
026import org.apache.commons.lang.StringUtils;
027import org.springframework.stereotype.Service;
028
029import javax.inject.Inject;
030import java.util.List;
031
032/**
033 * @author Olivier Lamy
034 */
035@Service ("proxyConnectorRuleService#rest")
036public class DefaultProxyConnectorRuleService
037    extends AbstractRestService
038    implements ProxyConnectorRuleService
039{
040
041    @Inject
042    private ProxyConnectorRuleAdmin proxyConnectorRuleAdmin;
043
044    @Override
045    public List<ProxyConnectorRule> getProxyConnectorRules()
046        throws ArchivaRestServiceException
047    {
048        try
049        {
050            return proxyConnectorRuleAdmin.getProxyConnectorRules();
051        }
052        catch ( RepositoryAdminException e )
053        {
054            throw new ArchivaRestServiceException( e.getMessage(), e );
055        }
056    }
057
058    private void validateProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
059        throws ArchivaRestServiceException
060    {
061        if ( StringUtils.isEmpty( proxyConnectorRule.getPattern() ) )
062        {
063            ArchivaRestServiceException e = new ArchivaRestServiceException( "pattern cannot be empty", null );
064            e.setErrorKey( "proxy-connector-rule.pattern.empty" );
065            throw e;
066        }
067
068        if ( proxyConnectorRule.getProxyConnectors() == null || proxyConnectorRule.getProxyConnectors().isEmpty() )
069        {
070            ArchivaRestServiceException e =
071                new ArchivaRestServiceException( "proxyConnector rule must have proxyConnectors.", null );
072            e.setErrorKey( "proxy-connector-rule.pattern.connectors.empty" );
073            throw e;
074        }
075
076        for ( ProxyConnectorRule proxyConnectorRule1 : getProxyConnectorRules() )
077        {
078            if ( StringUtils.equals( proxyConnectorRule.getPattern(), proxyConnectorRule1.getPattern() )
079                && proxyConnectorRule.getProxyConnectorRuleType() == proxyConnectorRule1.getProxyConnectorRuleType() )
080            {
081                ArchivaRestServiceException e =
082                    new ArchivaRestServiceException( "same ProxyConnector rule already exists.", null );
083                e.setErrorKey( "proxy-connector-rule.pattern.already.exists" );
084                throw e;
085            }
086        }
087    }
088
089    @Override
090    public Boolean addProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
091        throws ArchivaRestServiceException
092    {
093
094        validateProxyConnectorRule( proxyConnectorRule );
095
096        try
097        {
098            proxyConnectorRuleAdmin.addProxyConnectorRule( proxyConnectorRule, getAuditInformation() );
099            return Boolean.TRUE;
100        }
101        catch ( RepositoryAdminException e )
102        {
103            throw new ArchivaRestServiceException( e.getMessage(), e );
104        }
105    }
106
107    @Override
108    public Boolean deleteProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
109        throws ArchivaRestServiceException
110    {
111        try
112        {
113            proxyConnectorRuleAdmin.deleteProxyConnectorRule( proxyConnectorRule, getAuditInformation() );
114            return Boolean.TRUE;
115        }
116        catch ( RepositoryAdminException e )
117        {
118            throw new ArchivaRestServiceException( e.getMessage(), e );
119        }
120    }
121
122    @Override
123    public Boolean updateProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
124        throws ArchivaRestServiceException
125    {
126        try
127        {
128            proxyConnectorRuleAdmin.updateProxyConnectorRule( proxyConnectorRule, getAuditInformation() );
129            return Boolean.TRUE;
130        }
131        catch ( RepositoryAdminException e )
132        {
133            throw new ArchivaRestServiceException( e.getMessage(), e );
134        }
135    }
136}