]> source.dussan.org Git - archiva.git/blob
979c6b88fd0766dfc4d68cd5b43a51a70ff14a95
[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.archiva.security.common.ArchivaRoleConstants;
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.collections.functors.NotPredicate;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
29 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
30 import org.apache.maven.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
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     @Inject
55     protected ArchivaConfiguration archivaConfiguration;
56
57     public SecureActionBundle getSecureActionBundle()
58         throws SecureActionException
59     {
60         SecureActionBundle bundle = new SecureActionBundle();
61
62         bundle.setRequiresAuthentication( true );
63         bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
64
65         return bundle;
66     }
67
68     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
69     {
70         this.archivaConfiguration = archivaConfiguration;
71     }
72
73     protected void addProxyConnector( ProxyConnectorConfiguration proxyConnector )
74     {
75         getConfig().addProxyConnector( proxyConnector );
76     }
77
78     protected ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId )
79     {
80         if ( StringUtils.isBlank( sourceId ) )
81         {
82             return null;
83         }
84
85         if ( StringUtils.isBlank( targetId ) )
86         {
87             return null;
88         }
89
90         ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
91         return (ProxyConnectorConfiguration) CollectionUtils.find( getConfig().getProxyConnectors(), selectedProxy );
92     }
93
94     protected Configuration getConfig()
95     {
96         return this.archivaConfiguration.getConfiguration();
97     }
98
99     protected Map<String, List<ProxyConnectorConfiguration>> createProxyConnectorMap()
100     {
101         return getConfig().getProxyConnectorAsMap();
102     }
103
104     protected void removeConnector( String sourceId, String targetId )
105     {
106         ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
107         NotPredicate notSelectedProxy = new NotPredicate( selectedProxy );
108         CollectionUtils.filter( getConfig().getProxyConnectors(), notSelectedProxy );
109     }
110
111     protected void removeProxyConnector( ProxyConnectorConfiguration connector )
112     {
113         getConfig().removeProxyConnector( connector );
114     }
115
116     protected String saveConfiguration()
117     {
118         try
119         {
120             archivaConfiguration.save( getConfig() );
121             addActionMessage( "Successfully saved configuration" );
122         }
123         catch ( RegistryException e )
124         {
125             addActionError( "Unable to save configuration: " + e.getMessage() );
126             return INPUT;
127         }
128         catch ( IndeterminateConfigurationException e )
129         {
130             addActionError( e.getMessage() );
131             return INPUT;
132         }
133
134         return SUCCESS;
135     }
136 }