]> source.dussan.org Git - archiva.git/blob
70632e31105373ae51a9ee203db71f10110c72fc
[archiva.git] /
1 package org.apache.maven.archiva.web.action.admin.connectors.proxy;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.collections.functors.NotPredicate;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.Configuration;
27 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
28 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.maven.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
30 import org.apache.archiva.security.ArchivaRoleConstants;
31 import org.apache.maven.archiva.web.action.AbstractActionSupport;
32 import org.codehaus.plexus.redback.rbac.Resource;
33 import org.codehaus.plexus.registry.RegistryException;
34
35 import java.util.List;
36 import java.util.Map;
37 import org.codehaus.redback.integration.interceptor.SecureAction;
38 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
39 import org.codehaus.redback.integration.interceptor.SecureActionException;
40
41 import javax.inject.Inject;
42
43 /**
44  * AbstractProxyConnectorAction 
45  *
46  * @version $Id$
47  */
48 public abstract class AbstractProxyConnectorAction
49     extends AbstractActionSupport
50     implements SecureAction
51 {
52     public static final String DIRECT_CONNECTION = "(direct connection)";
53
54     /**
55      * plexus.requirement
56      */
57     @Inject
58     protected ArchivaConfiguration archivaConfiguration;
59
60     public SecureActionBundle getSecureActionBundle()
61         throws SecureActionException
62     {
63         SecureActionBundle bundle = new SecureActionBundle();
64
65         bundle.setRequiresAuthentication( true );
66         bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
67
68         return bundle;
69     }
70
71     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
72     {
73         this.archivaConfiguration = archivaConfiguration;
74     }
75
76     protected void addProxyConnector( ProxyConnectorConfiguration proxyConnector )
77     {
78         getConfig().addProxyConnector( proxyConnector );
79     }
80
81     protected ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId )
82     {
83         if ( StringUtils.isBlank( sourceId ) )
84         {
85             return null;
86         }
87
88         if ( StringUtils.isBlank( targetId ) )
89         {
90             return null;
91         }
92
93         ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
94         return (ProxyConnectorConfiguration) CollectionUtils.find( getConfig().getProxyConnectors(), selectedProxy );
95     }
96
97     protected Configuration getConfig()
98     {
99         return this.archivaConfiguration.getConfiguration();
100     }
101
102     protected Map<String, List<ProxyConnectorConfiguration>> createProxyConnectorMap()
103     {
104         return getConfig().getProxyConnectorAsMap();
105     }
106
107     protected void removeConnector( String sourceId, String targetId )
108     {
109         ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
110         NotPredicate notSelectedProxy = new NotPredicate( selectedProxy );
111         CollectionUtils.filter( getConfig().getProxyConnectors(), notSelectedProxy );
112     }
113
114     protected void removeProxyConnector( ProxyConnectorConfiguration connector )
115     {
116         getConfig().removeProxyConnector( connector );
117     }
118
119     protected String saveConfiguration()
120     {
121         try
122         {
123             archivaConfiguration.save( getConfig() );
124             addActionMessage( "Successfully saved configuration" );
125         }
126         catch ( RegistryException e )
127         {
128             addActionError( "Unable to save configuration: " + e.getMessage() );
129             return INPUT;
130         }
131         catch ( IndeterminateConfigurationException e )
132         {
133             addActionError( e.getMessage() );
134             return INPUT;
135         }
136
137         return SUCCESS;
138     }
139 }