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.

AbstractProxyConnectorAction.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package org.apache.maven.archiva.web.action.admin.connectors.proxy;
  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 org.apache.commons.collections.CollectionUtils;
  21. import org.apache.commons.collections.functors.NotPredicate;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.apache.maven.archiva.configuration.ArchivaConfiguration;
  24. import org.apache.maven.archiva.configuration.Configuration;
  25. import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
  26. import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
  27. import org.apache.maven.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
  28. import org.apache.maven.archiva.security.ArchivaRoleConstants;
  29. import org.apache.maven.archiva.web.action.PlexusActionSupport;
  30. import org.codehaus.plexus.redback.rbac.Resource;
  31. import org.codehaus.plexus.redback.struts2.interceptor.SecureAction;
  32. import org.codehaus.plexus.redback.struts2.interceptor.SecureActionBundle;
  33. import org.codehaus.plexus.redback.struts2.interceptor.SecureActionException;
  34. import org.codehaus.plexus.registry.RegistryException;
  35. import java.util.List;
  36. import java.util.Map;
  37. /**
  38. * AbstractProxyConnectorAction
  39. *
  40. * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
  41. * @version $Id$
  42. */
  43. public abstract class AbstractProxyConnectorAction
  44. extends PlexusActionSupport
  45. implements SecureAction
  46. {
  47. public static final String DIRECT_CONNECTION = "(direct connection)";
  48. /**
  49. * @plexus.requirement
  50. */
  51. protected ArchivaConfiguration archivaConfiguration;
  52. public SecureActionBundle getSecureActionBundle()
  53. throws SecureActionException
  54. {
  55. SecureActionBundle bundle = new SecureActionBundle();
  56. bundle.setRequiresAuthentication( true );
  57. bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
  58. return bundle;
  59. }
  60. public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
  61. {
  62. this.archivaConfiguration = archivaConfiguration;
  63. }
  64. protected void addProxyConnector( ProxyConnectorConfiguration proxyConnector )
  65. {
  66. getConfig().addProxyConnector( proxyConnector );
  67. }
  68. protected ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId )
  69. {
  70. if ( StringUtils.isBlank( sourceId ) )
  71. {
  72. return null;
  73. }
  74. if ( StringUtils.isBlank( targetId ) )
  75. {
  76. return null;
  77. }
  78. ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
  79. return (ProxyConnectorConfiguration) CollectionUtils.find( getConfig().getProxyConnectors(), selectedProxy );
  80. }
  81. protected Configuration getConfig()
  82. {
  83. return this.archivaConfiguration.getConfiguration();
  84. }
  85. protected Map<String, List<ProxyConnectorConfiguration>> createProxyConnectorMap()
  86. {
  87. return getConfig().getProxyConnectorAsMap();
  88. }
  89. protected void removeConnector( String sourceId, String targetId )
  90. {
  91. ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
  92. NotPredicate notSelectedProxy = new NotPredicate( selectedProxy );
  93. CollectionUtils.filter( getConfig().getProxyConnectors(), notSelectedProxy );
  94. }
  95. protected void removeProxyConnector( ProxyConnectorConfiguration connector )
  96. {
  97. getConfig().removeProxyConnector( connector );
  98. }
  99. protected String saveConfiguration()
  100. {
  101. try
  102. {
  103. archivaConfiguration.save( getConfig() );
  104. addActionMessage( "Successfully saved configuration" );
  105. }
  106. catch ( RegistryException e )
  107. {
  108. addActionError( "Unable to save configuration: " + e.getMessage() );
  109. return INPUT;
  110. }
  111. catch ( IndeterminateConfigurationException e )
  112. {
  113. addActionError( e.getMessage() );
  114. return INPUT;
  115. }
  116. return SUCCESS;
  117. }
  118. }