]> source.dussan.org Git - archiva.git/blob
b9557c030449cd040a9300a90a66afb78db67eac
[archiva.git] /
1 package org.apache.archiva.rest.services;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.ProxyConnector;
23 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
24 import org.apache.archiva.policies.Policy;
25 import org.apache.archiva.rest.api.model.PolicyInformation;
26 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
27 import org.apache.archiva.rest.api.services.ProxyConnectorService;
28 import org.springframework.context.ApplicationContext;
29 import org.springframework.stereotype.Service;
30
31 import javax.inject.Inject;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35
36 /**
37  * @author Olivier Lamy
38  */
39 @Service( "proxyConnectorService#rest" )
40 public class DefaultProxyConnectorService
41     extends AbstractRestService
42     implements ProxyConnectorService
43 {
44     @Inject
45     private ProxyConnectorAdmin proxyConnectorAdmin;
46
47     private List<Policy> allPolicies;
48
49     @Inject
50     public DefaultProxyConnectorService( ApplicationContext applicationContext )
51     {
52         allPolicies = new ArrayList<>( getBeansOfType( applicationContext, Policy.class ).values() );
53     }
54
55     @Override
56     public List<ProxyConnector> getProxyConnectors()
57         throws ArchivaRestServiceException
58     {
59         try
60         {
61             List<ProxyConnector> proxyConnectors = proxyConnectorAdmin.getProxyConnectors();
62             return proxyConnectors == null ? Collections.<ProxyConnector>emptyList() : proxyConnectors;
63         }
64         catch ( RepositoryAdminException e )
65         {
66             throw new ArchivaRestServiceException( e.getMessage(), e );
67         }
68     }
69
70     @Override
71     public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
72         throws ArchivaRestServiceException
73     {
74         try
75         {
76             return proxyConnectorAdmin.getProxyConnector( sourceRepoId, targetRepoId );
77         }
78         catch ( RepositoryAdminException e )
79         {
80             throw new ArchivaRestServiceException( e.getMessage(), e );
81         }
82     }
83
84     @Override
85     public Boolean addProxyConnector( ProxyConnector proxyConnector )
86         throws ArchivaRestServiceException
87     {
88         if ( proxyConnector == null )
89         {
90             return Boolean.FALSE;
91         }
92         try
93         {
94             return proxyConnectorAdmin.addProxyConnector( proxyConnector, getAuditInformation() );
95         }
96         catch ( RepositoryAdminException e )
97         {
98             throw new ArchivaRestServiceException( e.getMessage(), e );
99         }
100     }
101
102     @Override
103     public Boolean deleteProxyConnector( ProxyConnector proxyConnector )
104         throws ArchivaRestServiceException
105     {
106         if ( proxyConnector == null )
107         {
108             return Boolean.FALSE;
109         }
110         try
111         {
112             return proxyConnectorAdmin.deleteProxyConnector( proxyConnector, getAuditInformation() );
113         }
114         catch ( RepositoryAdminException e )
115         {
116             throw new ArchivaRestServiceException( e.getMessage(), e );
117         }
118     }
119
120     @Override
121     public Boolean removeProxyConnector( String sourceRepoId, String targetRepoId )
122         throws ArchivaRestServiceException
123     {
124         ProxyConnector proxyConnector = getProxyConnector( sourceRepoId, targetRepoId );
125         if ( proxyConnector == null )
126         {
127             throw new ArchivaRestServiceException(
128                 "proxyConnector with sourceRepoId:" + sourceRepoId + " and targetRepoId:" + targetRepoId
129                     + " not exists", null );
130         }
131         return deleteProxyConnector( proxyConnector );
132     }
133
134     @Override
135     public Boolean updateProxyConnector( ProxyConnector proxyConnector )
136         throws ArchivaRestServiceException
137     {
138         if ( proxyConnector == null )
139         {
140             return Boolean.FALSE;
141         }
142         try
143         {
144             return proxyConnectorAdmin.updateProxyConnector( proxyConnector, getAuditInformation() );
145         }
146         catch ( RepositoryAdminException e )
147         {
148             throw new ArchivaRestServiceException( e.getMessage(), e );
149         }
150     }
151
152     @Override
153     public List<PolicyInformation> getAllPolicyInformations()
154         throws ArchivaRestServiceException
155     {
156         List<PolicyInformation> policyInformations = new ArrayList<>( allPolicies.size() );
157
158         for ( Policy policy : allPolicies )
159         {
160             policyInformations.add(
161                 new PolicyInformation( policy.getOptions(), policy.getDefaultOption(), policy.getId(),
162                                        policy.getName() ) );
163         }
164
165         return policyInformations;
166     }
167
168     public ProxyConnectorAdmin getProxyConnectorAdmin()
169     {
170         return proxyConnectorAdmin;
171     }
172
173     public void setProxyConnectorAdmin( ProxyConnectorAdmin proxyConnectorAdmin )
174     {
175         this.proxyConnectorAdmin = proxyConnectorAdmin;
176     }
177 }
178
179