/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 */
22package org.jboss.as.security;
23
24import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.*;
25
26import java.util.Locale;
27import java.util.ResourceBundle;
28
29import javax.xml.stream.XMLStreamException;
30import javax.xml.stream.XMLStreamReader;
31import javax.xml.stream.XMLStreamWriter;
32
33import org.jboss.as.controller.ListAttributeDefinition;
34import org.jboss.as.controller.OperationFailedException;
35import org.jboss.as.controller.descriptions.ResourceDescriptionResolver;
36import org.jboss.as.controller.operations.validation.ModelTypeValidator;
37import org.jboss.as.controller.operations.validation.ParameterValidator;
38import org.jboss.as.controller.operations.validation.ParametersOfValidator;
39import org.jboss.as.controller.operations.validation.ParametersValidator;
40import org.jboss.as.controller.operations.validation.StringLengthValidator;
41import org.jboss.as.controller.registry.AttributeAccess;
42import org.jboss.dmr.ModelNode;
43import org.jboss.dmr.ModelType;
44
45/**
46 * @author Jason T. Greene
47 */
48public class JASPIAuthenticationModulesAttributeDefinition extends ListAttributeDefinition {
49
50 private static final ParameterValidator validator;
51 private static final ParameterValidator fieldValidator;
52
53
54 static {
55 final ParametersValidator delegate = new ParametersValidator();
56 delegate.registerValidator(CODE, new StringLengthValidator(1));
57 delegate.registerValidator(Constants.MODULE_OPTIONS, new ModelTypeValidator(ModelType.OBJECT, true));
58 delegate.registerValidator(Constants.LOGIN_MODULE_STACK_REF, new StringLengthValidator(1, true));
59
60 validator = new ParametersOfValidator(delegate);
61 fieldValidator = delegate;
62 }
63
64
65 public JASPIAuthenticationModulesAttributeDefinition() {
66 super(Constants.AUTH_MODULES, Constants.AUTH_MODULE, false, 1, Integer.MAX_VALUE, validator, null, null, AttributeAccess.Flag.RESTART_ALL_SERVICES);
67 }
68
69 @Override
70 protected void addValueTypeDescription(ModelNode node, ResourceBundle bundle) {
71 // This method being used indicates a misuse of this class
72 throw SecurityMessages.MESSAGES.unsupportedOperationExceptionUseResourceDesc();
73 }
74
75 @Override
76 protected void addAttributeValueTypeDescription(ModelNode node, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
77 final ModelNode valueType = getNoTextValueTypeDescription(node);
78 valueType.get(CODE, DESCRIPTION).set(resolver.getResourceAttributeValueTypeDescription(getName(), locale, bundle, CODE));
79 valueType.get(Constants.MODULE_OPTIONS, DESCRIPTION).set(resolver.getResourceAttributeValueTypeDescription(getName(), locale, bundle, Constants.MODULE_OPTIONS));
80 valueType.get(Constants.LOGIN_MODULE_STACK_REF, DESCRIPTION).set(resolver.getResourceAttributeValueTypeDescription(getName(), locale, bundle, Constants.LOGIN_MODULE_STACK_REF));
81 }
82
83 @Override
84 protected void addOperationParameterValueTypeDescription(ModelNode node, String operationName, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
85 final ModelNode valueType = getNoTextValueTypeDescription(node);
86 valueType.get(CODE, DESCRIPTION).set(resolver.getOperationParameterValueTypeDescription(operationName, getName(), locale, bundle, CODE));
87 valueType.get(Constants.MODULE_OPTIONS, DESCRIPTION).set(resolver.getOperationParameterValueTypeDescription(operationName, getName(), locale, bundle, Constants.MODULE_OPTIONS));
88 valueType.get(Constants.LOGIN_MODULE_STACK_REF, DESCRIPTION).set(resolver.getOperationParameterValueTypeDescription(operationName, getName(), locale, bundle, Constants.LOGIN_MODULE_STACK_REF));
89 }
90
91 @Override
92 public void marshallAsElement(ModelNode resourceModel, XMLStreamWriter writer) throws XMLStreamException {
93 if (resourceModel.hasDefined(getName()) && resourceModel.asInt() > 0) {
94 final ModelNode modules = resourceModel.get(getName());
95 for (ModelNode module : modules.asList()) {
96 writer.writeStartElement(getXmlName());
97 writer.writeAttribute(Attribute.CODE.getLocalName(), module.get(CODE).asString());
98 if (module.hasDefined(Constants.LOGIN_MODULE_STACK_REF)) {
99 writer.writeAttribute(Attribute.LOGIN_MODULE_STACK_REF.getLocalName(), module.get(Constants.LOGIN_MODULE_STACK_REF).asString());
100 }
101
102 if (module.hasDefined(Constants.MODULE_OPTIONS)) {
103 for (ModelNode option : module.get(Constants.MODULE_OPTIONS).asList()) {
104 writer.writeEmptyElement(Element.MODULE_OPTION.getLocalName());
105 writer.writeAttribute(Attribute.NAME.getLocalName(), option.asProperty().getName());
106 writer.writeAttribute(Attribute.VALUE.getLocalName(), option.asProperty().getValue().asString());
107 }
108 }
109 writer.writeEndElement();
110 }
111 }
112
113 }
114
115 public static ModelNode parseField(String name, String value, XMLStreamReader reader) throws XMLStreamException {
116 final String trimmed = value == null ? null : value.trim();
117 ModelNode node;
118 if (trimmed != null ) {
119 node = new ModelNode().set(trimmed);
120 } else {
121 node = new ModelNode();
122 }
123
124 try {
125 fieldValidator.validateParameter(name, node);
126 } catch (OperationFailedException e) {
127 throw SecurityMessages.MESSAGES.xmlStreamException(e.getFailureDescription().toString(), reader.getLocation());
128 }
129 return node;
130 }
131
132 private ModelNode getNoTextValueTypeDescription(final ModelNode parent) {
133 final ModelNode valueType = parent.get(VALUE_TYPE);
134 final ModelNode code = valueType.get(CODE);
135 code.get(DESCRIPTION); // placeholder
136 code.get(TYPE).set(ModelType.STRING);
137 code.get(NILLABLE).set(false);
138 code.get(MIN_LENGTH).set(1);
139
140 final ModelNode moduleOptions = valueType.get(Constants.MODULE_OPTIONS);
141 moduleOptions.get(DESCRIPTION); // placeholder
142 moduleOptions.get(TYPE).set(ModelType.OBJECT);
143 moduleOptions.get(VALUE_TYPE).set(ModelType.STRING);
144 moduleOptions.get(NILLABLE).set(true);
145
146 final ModelNode ref = valueType.get(Constants.LOGIN_MODULE_STACK_REF);
147 ref.get(DESCRIPTION); // placeholder
148 ref.get(TYPE).set(ModelType.STRING);
149 ref.get(NILLABLE).set(true);
150 ref.get(MIN_LENGTH).set(1);
151
152 return valueType;
153 }
154}