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