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.ProxyConnector;
023    import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
024    import org.apache.archiva.policies.Policy;
025    import org.apache.archiva.rest.api.model.PolicyInformation;
026    import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
027    import org.apache.archiva.rest.api.services.ProxyConnectorService;
028    import org.springframework.context.ApplicationContext;
029    import org.springframework.stereotype.Service;
030    
031    import javax.inject.Inject;
032    import java.util.ArrayList;
033    import java.util.Collections;
034    import java.util.List;
035    
036    /**
037     * @author Olivier Lamy
038     */
039    @Service( "proxyConnectorService#rest" )
040    public class DefaultProxyConnectorService
041        extends AbstractRestService
042        implements ProxyConnectorService
043    {
044        @Inject
045        private ProxyConnectorAdmin proxyConnectorAdmin;
046    
047        private List<Policy> allPolicies;
048    
049        @Inject
050        public DefaultProxyConnectorService( ApplicationContext applicationContext )
051        {
052            allPolicies = new ArrayList<Policy>( getBeansOfType( applicationContext, Policy.class ).values() );
053        }
054    
055        public List<ProxyConnector> getProxyConnectors()
056            throws ArchivaRestServiceException
057        {
058            try
059            {
060                List<ProxyConnector> proxyConnectors = proxyConnectorAdmin.getProxyConnectors();
061                return proxyConnectors == null ? Collections.<ProxyConnector>emptyList() : proxyConnectors;
062            }
063            catch ( RepositoryAdminException e )
064            {
065                throw new ArchivaRestServiceException( e.getMessage(), e );
066            }
067        }
068    
069        public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
070            throws ArchivaRestServiceException
071        {
072            try
073            {
074                return proxyConnectorAdmin.getProxyConnector( sourceRepoId, targetRepoId );
075            }
076            catch ( RepositoryAdminException e )
077            {
078                throw new ArchivaRestServiceException( e.getMessage(), e );
079            }
080        }
081    
082        public Boolean addProxyConnector( ProxyConnector proxyConnector )
083            throws ArchivaRestServiceException
084        {
085            if ( proxyConnector == null )
086            {
087                return Boolean.FALSE;
088            }
089            try
090            {
091                return proxyConnectorAdmin.addProxyConnector( proxyConnector, getAuditInformation() );
092            }
093            catch ( RepositoryAdminException e )
094            {
095                throw new ArchivaRestServiceException( e.getMessage(), e );
096            }
097        }
098    
099        public Boolean deleteProxyConnector( ProxyConnector proxyConnector )
100            throws ArchivaRestServiceException
101        {
102            if ( proxyConnector == null )
103            {
104                return Boolean.FALSE;
105            }
106            try
107            {
108                return proxyConnectorAdmin.deleteProxyConnector( proxyConnector, getAuditInformation() );
109            }
110            catch ( RepositoryAdminException e )
111            {
112                throw new ArchivaRestServiceException( e.getMessage(), e );
113            }
114        }
115    
116        public Boolean removeProxyConnector( String sourceRepoId, String targetRepoId )
117            throws ArchivaRestServiceException
118        {
119            ProxyConnector proxyConnector = getProxyConnector( sourceRepoId, targetRepoId );
120            if ( proxyConnector == null )
121            {
122                throw new ArchivaRestServiceException(
123                    "proxyConnector with sourceRepoId:" + sourceRepoId + " and targetRepoId:" + targetRepoId
124                        + " not exists", null );
125            }
126            return deleteProxyConnector( proxyConnector );
127        }
128    
129        public Boolean updateProxyConnector( ProxyConnector proxyConnector )
130            throws ArchivaRestServiceException
131        {
132            if ( proxyConnector == null )
133            {
134                return Boolean.FALSE;
135            }
136            try
137            {
138                return proxyConnectorAdmin.updateProxyConnector( proxyConnector, getAuditInformation() );
139            }
140            catch ( RepositoryAdminException e )
141            {
142                throw new ArchivaRestServiceException( e.getMessage(), e );
143            }
144        }
145    
146        public List<PolicyInformation> getAllPolicyInformations()
147            throws ArchivaRestServiceException
148        {
149            List<PolicyInformation> policyInformations = new ArrayList<PolicyInformation>( allPolicies.size() );
150    
151            for ( Policy policy : allPolicies )
152            {
153                policyInformations.add(
154                    new PolicyInformation( policy.getOptions(), policy.getDefaultOption(), policy.getId(),
155                                           policy.getName() ) );
156            }
157    
158            return policyInformations;
159        }
160    
161        public ProxyConnectorAdmin getProxyConnectorAdmin()
162        {
163            return proxyConnectorAdmin;
164        }
165    
166        public void setProxyConnectorAdmin( ProxyConnectorAdmin proxyConnectorAdmin )
167        {
168            this.proxyConnectorAdmin = proxyConnectorAdmin;
169        }
170    }
171    
172