This project has retired. For details please refer to its Attic page.
DefaultProxyConnectorRuleService xref
View Javadoc
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.ProxyConnectorRule;
23  import org.apache.archiva.admin.model.proxyconnectorrule.ProxyConnectorRuleAdmin;
24  import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
25  import org.apache.archiva.rest.api.services.ProxyConnectorRuleService;
26  import org.apache.commons.lang.StringUtils;
27  import org.springframework.stereotype.Service;
28  
29  import javax.inject.Inject;
30  import java.util.List;
31  
32  /**
33   * @author Olivier Lamy
34   */
35  @Service ("proxyConnectorRuleService#rest")
36  public class DefaultProxyConnectorRuleService
37      extends AbstractRestService
38      implements ProxyConnectorRuleService
39  {
40  
41      @Inject
42      private ProxyConnectorRuleAdmin proxyConnectorRuleAdmin;
43  
44      @Override
45      public List<ProxyConnectorRule> getProxyConnectorRules()
46          throws ArchivaRestServiceException
47      {
48          try
49          {
50              return proxyConnectorRuleAdmin.getProxyConnectorRules();
51          }
52          catch ( RepositoryAdminException e )
53          {
54              throw new ArchivaRestServiceException( e.getMessage(), e );
55          }
56      }
57  
58      private void validateProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
59          throws ArchivaRestServiceException
60      {
61          if ( StringUtils.isEmpty( proxyConnectorRule.getPattern() ) )
62          {
63              ArchivaRestServiceException e = new ArchivaRestServiceException( "pattern cannot be empty", null );
64              e.setErrorKey( "proxy-connector-rule.pattern.empty" );
65              throw e;
66          }
67  
68          if ( proxyConnectorRule.getProxyConnectors() == null || proxyConnectorRule.getProxyConnectors().isEmpty() )
69          {
70              ArchivaRestServiceException e =
71                  new ArchivaRestServiceException( "proxyConnector rule must have proxyConnectors.", null );
72              e.setErrorKey( "proxy-connector-rule.pattern.connectors.empty" );
73              throw e;
74          }
75  
76          for ( ProxyConnectorRule proxyConnectorRule1 : getProxyConnectorRules() )
77          {
78              if ( StringUtils.equals( proxyConnectorRule.getPattern(), proxyConnectorRule1.getPattern() )
79                  && proxyConnectorRule.getProxyConnectorRuleType() == proxyConnectorRule1.getProxyConnectorRuleType() )
80              {
81                  ArchivaRestServiceException e =
82                      new ArchivaRestServiceException( "same ProxyConnector rule already exists.", null );
83                  e.setErrorKey( "proxy-connector-rule.pattern.already.exists" );
84                  throw e;
85              }
86          }
87      }
88  
89      @Override
90      public Boolean addProxyConnectorRule( ProxyConnectorRule proxyConnectorRule )
91          throws ArchivaRestServiceException
92      {
93  
94          validateProxyConnectorRule( proxyConnectorRule );
95  
96          try
97          {
98              proxyConnectorRuleAdmin.addProxyConnectorRule( proxyConnectorRule, getAuditInformation() );
99              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 }