You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigureNetworkProxyAction.java 6.7KB

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