PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/security/src/main/java/org/jboss/as/security/JASPIAuthenticationModulesAttributeDefinition.java

#
Java | 154 lines | 106 code | 23 blank | 25 comment | 10 complexity | 10e00c39bb5251a40b08d82a1c2f89c2 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.security;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.*;
  24. import java.util.Locale;
  25. import java.util.ResourceBundle;
  26. import javax.xml.stream.XMLStreamException;
  27. import javax.xml.stream.XMLStreamReader;
  28. import javax.xml.stream.XMLStreamWriter;
  29. import org.jboss.as.controller.ListAttributeDefinition;
  30. import org.jboss.as.controller.OperationFailedException;
  31. import org.jboss.as.controller.descriptions.ResourceDescriptionResolver;
  32. import org.jboss.as.controller.operations.validation.ModelTypeValidator;
  33. import org.jboss.as.controller.operations.validation.ParameterValidator;
  34. import org.jboss.as.controller.operations.validation.ParametersOfValidator;
  35. import org.jboss.as.controller.operations.validation.ParametersValidator;
  36. import org.jboss.as.controller.operations.validation.StringLengthValidator;
  37. import org.jboss.as.controller.registry.AttributeAccess;
  38. import org.jboss.dmr.ModelNode;
  39. import org.jboss.dmr.ModelType;
  40. /**
  41. * @author Jason T. Greene
  42. */
  43. public class JASPIAuthenticationModulesAttributeDefinition extends ListAttributeDefinition {
  44. private static final ParameterValidator validator;
  45. private static final ParameterValidator fieldValidator;
  46. static {
  47. final ParametersValidator delegate = new ParametersValidator();
  48. delegate.registerValidator(CODE, new StringLengthValidator(1));
  49. delegate.registerValidator(Constants.MODULE_OPTIONS, new ModelTypeValidator(ModelType.OBJECT, true));
  50. delegate.registerValidator(Constants.LOGIN_MODULE_STACK_REF, new StringLengthValidator(1, true));
  51. validator = new ParametersOfValidator(delegate);
  52. fieldValidator = delegate;
  53. }
  54. public JASPIAuthenticationModulesAttributeDefinition() {
  55. super(Constants.AUTH_MODULES, Constants.AUTH_MODULE, false, 1, Integer.MAX_VALUE, validator, null, null, AttributeAccess.Flag.RESTART_ALL_SERVICES);
  56. }
  57. @Override
  58. protected void addValueTypeDescription(ModelNode node, ResourceBundle bundle) {
  59. // This method being used indicates a misuse of this class
  60. throw SecurityMessages.MESSAGES.unsupportedOperationExceptionUseResourceDesc();
  61. }
  62. @Override
  63. protected void addAttributeValueTypeDescription(ModelNode node, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
  64. final ModelNode valueType = getNoTextValueTypeDescription(node);
  65. valueType.get(CODE, DESCRIPTION).set(resolver.getResourceAttributeValueTypeDescription(getName(), locale, bundle, CODE));
  66. valueType.get(Constants.MODULE_OPTIONS, DESCRIPTION).set(resolver.getResourceAttributeValueTypeDescription(getName(), locale, bundle, Constants.MODULE_OPTIONS));
  67. valueType.get(Constants.LOGIN_MODULE_STACK_REF, DESCRIPTION).set(resolver.getResourceAttributeValueTypeDescription(getName(), locale, bundle, Constants.LOGIN_MODULE_STACK_REF));
  68. }
  69. @Override
  70. protected void addOperationParameterValueTypeDescription(ModelNode node, String operationName, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
  71. final ModelNode valueType = getNoTextValueTypeDescription(node);
  72. valueType.get(CODE, DESCRIPTION).set(resolver.getOperationParameterValueTypeDescription(operationName, getName(), locale, bundle, CODE));
  73. valueType.get(Constants.MODULE_OPTIONS, DESCRIPTION).set(resolver.getOperationParameterValueTypeDescription(operationName, getName(), locale, bundle, Constants.MODULE_OPTIONS));
  74. valueType.get(Constants.LOGIN_MODULE_STACK_REF, DESCRIPTION).set(resolver.getOperationParameterValueTypeDescription(operationName, getName(), locale, bundle, Constants.LOGIN_MODULE_STACK_REF));
  75. }
  76. @Override
  77. public void marshallAsElement(ModelNode resourceModel, XMLStreamWriter writer) throws XMLStreamException {
  78. if (resourceModel.hasDefined(getName()) && resourceModel.asInt() > 0) {
  79. final ModelNode modules = resourceModel.get(getName());
  80. for (ModelNode module : modules.asList()) {
  81. writer.writeStartElement(getXmlName());
  82. writer.writeAttribute(Attribute.CODE.getLocalName(), module.get(CODE).asString());
  83. if (module.hasDefined(Constants.LOGIN_MODULE_STACK_REF)) {
  84. writer.writeAttribute(Attribute.LOGIN_MODULE_STACK_REF.getLocalName(), module.get(Constants.LOGIN_MODULE_STACK_REF).asString());
  85. }
  86. if (module.hasDefined(Constants.MODULE_OPTIONS)) {
  87. for (ModelNode option : module.get(Constants.MODULE_OPTIONS).asList()) {
  88. writer.writeEmptyElement(Element.MODULE_OPTION.getLocalName());
  89. writer.writeAttribute(Attribute.NAME.getLocalName(), option.asProperty().getName());
  90. writer.writeAttribute(Attribute.VALUE.getLocalName(), option.asProperty().getValue().asString());
  91. }
  92. }
  93. writer.writeEndElement();
  94. }
  95. }
  96. }
  97. public static ModelNode parseField(String name, String value, XMLStreamReader reader) throws XMLStreamException {
  98. final String trimmed = value == null ? null : value.trim();
  99. ModelNode node;
  100. if (trimmed != null ) {
  101. node = new ModelNode().set(trimmed);
  102. } else {
  103. node = new ModelNode();
  104. }
  105. try {
  106. fieldValidator.validateParameter(name, node);
  107. } catch (OperationFailedException e) {
  108. throw SecurityMessages.MESSAGES.xmlStreamException(e.getFailureDescription().toString(), reader.getLocation());
  109. }
  110. return node;
  111. }
  112. private ModelNode getNoTextValueTypeDescription(final ModelNode parent) {
  113. final ModelNode valueType = parent.get(VALUE_TYPE);
  114. final ModelNode code = valueType.get(CODE);
  115. code.get(DESCRIPTION); // placeholder
  116. code.get(TYPE).set(ModelType.STRING);
  117. code.get(NILLABLE).set(false);
  118. code.get(MIN_LENGTH).set(1);
  119. final ModelNode moduleOptions = valueType.get(Constants.MODULE_OPTIONS);
  120. moduleOptions.get(DESCRIPTION); // placeholder
  121. moduleOptions.get(TYPE).set(ModelType.OBJECT);
  122. moduleOptions.get(VALUE_TYPE).set(ModelType.STRING);
  123. moduleOptions.get(NILLABLE).set(true);
  124. final ModelNode ref = valueType.get(Constants.LOGIN_MODULE_STACK_REF);
  125. ref.get(DESCRIPTION); // placeholder
  126. ref.get(TYPE).set(ModelType.STRING);
  127. ref.get(NILLABLE).set(true);
  128. ref.get(MIN_LENGTH).set(1);
  129. return valueType;
  130. }
  131. }