]> source.dussan.org Git - archiva.git/blob
4acc3ae144f71cc78c1f5a4dc8fd3aa30f1be209
[archiva.git] /
1 package org.apache.maven.archiva.web.action.admin.networkproxies;
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 com.opensymphony.xwork.Preparable;
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.NetworkProxyConfiguration;
30 import org.apache.maven.archiva.configuration.functors.NetworkProxySelectionPredicate;
31 import org.apache.maven.archiva.security.ArchivaRoleConstants;
32 import org.codehaus.plexus.redback.rbac.Resource;
33 import org.codehaus.plexus.redback.xwork.interceptor.SecureAction;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
36 import org.codehaus.plexus.registry.RegistryException;
37 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
38
39 /**
40  * ConfigureNetworkProxyAction
41  *
42  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43  * @version $Id$
44  * @plexus.component role="com.opensymphony.xwork.Action" role-hint="configureNetworkProxyAction"
45  */
46 public class ConfigureNetworkProxyAction
47     extends PlexusActionSupport
48     implements SecureAction, Preparable
49 {
50     /**
51      * @plexus.requirement
52      */
53     private ArchivaConfiguration archivaConfiguration;
54
55     private String mode;
56
57     private String proxyid;
58
59     private NetworkProxyConfiguration proxy;
60
61     public String add()
62     {
63         this.mode = "add";
64         return INPUT;
65     }
66
67     public String confirm()
68     {
69         return INPUT;
70     }
71
72     public String delete()
73     {
74         Configuration config = archivaConfiguration.getConfiguration();
75
76         String id = getProxyid();
77         if ( StringUtils.isBlank( id ) )
78         {
79             addActionError( "Unable to delete network proxy with blank id." );
80             return SUCCESS;
81         }
82
83         NetworkProxySelectionPredicate networkProxySelection = new NetworkProxySelectionPredicate( id );
84         NetworkProxyConfiguration proxyConfig = (NetworkProxyConfiguration) CollectionUtils.find( config
85             .getNetworkProxies(), networkProxySelection );
86         if ( proxyConfig == null )
87         {
88             addActionError( "Unable to remove network proxy, proxy with id [" + id + "] not found." );
89             return SUCCESS;
90         }
91
92         archivaConfiguration.getConfiguration().removeNetworkProxy( proxyConfig );
93         addActionMessage( "Successfully removed network proxy [" + id + "]" );
94         return saveConfiguration();
95     }
96
97     public String edit()
98     {
99         this.mode = "edit";
100         return INPUT;
101     }
102
103     public String getMode()
104     {
105         return mode;
106     }
107
108     public NetworkProxyConfiguration getProxy()
109     {
110         return proxy;
111     }
112
113     public String getProxyid()
114     {
115         return proxyid;
116     }
117
118     public SecureActionBundle getSecureActionBundle()
119         throws SecureActionException
120     {
121         SecureActionBundle bundle = new SecureActionBundle();
122
123         bundle.setRequiresAuthentication( true );
124         bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
125
126         return bundle;
127     }
128
129     public String input()
130     {
131         return INPUT;
132     }
133
134     public void prepare()
135         throws Exception
136     {
137         String id = getProxyid();
138
139         if ( StringUtils.isNotBlank( id ) )
140         {
141             proxy = findNetworkProxy( id );
142         }
143
144         if ( proxy == null )
145         {
146             proxy = new NetworkProxyConfiguration();
147         }
148     }
149
150     public String save()
151     {
152         String mode = getMode();
153
154         String id = getProxy().getId();
155
156         if ( StringUtils.equalsIgnoreCase( "edit", mode ) )
157         {
158             removeNetworkProxy( id );
159         }
160         else
161         {
162             if ( findNetworkProxy( id ) != null )
163             {
164                 addActionError( "Unable to add new repository with id [" + id + "], that id already exists." );
165                 return INPUT;
166             }
167         }
168
169         addNetworkProxy( getProxy() );
170         return saveConfiguration();
171     }
172
173     public void setMode( String mode )
174     {
175         this.mode = mode;
176     }
177
178     public void setProxy( NetworkProxyConfiguration proxy )
179     {
180         this.proxy = proxy;
181     }
182
183     public void setProxyid( String proxyid )
184     {
185         this.proxyid = proxyid;
186     }
187
188     private void addNetworkProxy( NetworkProxyConfiguration proxy )
189     {
190         archivaConfiguration.getConfiguration().addNetworkProxy( proxy );
191     }
192
193     private NetworkProxyConfiguration findNetworkProxy( String id )
194     {
195         Configuration config = archivaConfiguration.getConfiguration();
196
197         NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id );
198
199         return (NetworkProxyConfiguration) CollectionUtils.find( config.getNetworkProxies(), selectedProxy );
200     }
201
202     private void removeNetworkProxy( String id )
203     {
204         NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id );
205         NotPredicate notSelectedProxy = new NotPredicate( selectedProxy );
206         CollectionUtils.filter( archivaConfiguration.getConfiguration().getNetworkProxies(), notSelectedProxy );
207     }
208
209     private String saveConfiguration()
210     {
211         try
212         {
213             archivaConfiguration.save( archivaConfiguration.getConfiguration() );
214             addActionMessage( "Successfully saved configuration" );
215         }
216         catch ( RegistryException e )
217         {
218             addActionError( "Unable to save configuration: " + e.getMessage() );
219             return INPUT;
220         }
221         catch ( IndeterminateConfigurationException e )
222         {
223             addActionError( e.getMessage() );
224             return INPUT;
225         }
226
227         return SUCCESS;
228     }
229 }