1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.jboss.extension.resources.components;
17
18 import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
19
20 import javax.xml.stream.XMLStreamException;
21
22 import org.jboss.as.controller.AttributeDefinition;
23 import org.jboss.as.controller.OperationFailedException;
24 import org.jboss.as.controller.OperationStepHandler;
25 import org.jboss.as.controller.SimpleAttributeDefinition;
26 import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
27 import org.jboss.as.controller.parsing.ParseUtils;
28 import org.jboss.as.controller.registry.AttributeAccess;
29 import org.jboss.as.controller.registry.ManagementResourceRegistration;
30 import org.jboss.dmr.ModelNode;
31 import org.jboss.dmr.ModelType;
32 import org.jboss.staxmapper.XMLExtendedStreamReader;
33 import org.jboss.staxmapper.XMLExtendedStreamWriter;
34
35 import de.smartics.properties.api.core.security.SecurityConfiguration;
36 import de.smartics.properties.spi.config.config.PropertiesConfiguration;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public final class SecurityComponent extends AbstractComponent
56 {
57
58
59
60
61
62
63
64
65
66
67
68 public static final String ROOT_ELEMENT_NAME = "security";
69
70
71
72
73 public static final SimpleAttributeDefinition ALGORITHM =
74 new SimpleAttributeDefinitionBuilder("security.algorithm",
75 ModelType.STRING, true).setXmlName("algorithm")
76 .setAllowExpression(true)
77 .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
78
79
80
81
82 public static final SimpleAttributeDefinition TRANSFORMATION =
83 new SimpleAttributeDefinitionBuilder("security.transformation",
84 ModelType.STRING, true).setXmlName("transformation")
85 .setAllowExpression(true)
86 .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
87
88
89
90
91 public static final SimpleAttributeDefinition PROVIDER =
92 new SimpleAttributeDefinitionBuilder("security.provider",
93 ModelType.STRING, true).setXmlName("provider")
94 .setAllowExpression(true)
95 .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
96
97
98
99
100 public static final SimpleAttributeDefinition KEY =
101 new SimpleAttributeDefinitionBuilder("security.key", ModelType.STRING,
102 true).setXmlName("key").setAllowExpression(true)
103 .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES).build();
104
105 private static final AttributeDefinition[] DEFINITIONS = { ALGORITHM,
106 TRANSFORMATION,
107 PROVIDER, KEY };
108
109
110
111
112
113
114
115
116
117
118 public SecurityComponent(final OperationStepHandler writeHandler)
119 {
120 super(writeHandler);
121 }
122
123
124
125
126
127
128
129
130
131
132
133 public void registerAttributes(
134 final ManagementResourceRegistration resourceRegistration)
135 {
136 resourceRegistration.registerReadWriteAttribute(ALGORITHM, null,
137 writeHandler);
138 resourceRegistration.registerReadWriteAttribute(TRANSFORMATION, null,
139 writeHandler);
140 resourceRegistration.registerReadWriteAttribute(PROVIDER, null,
141 writeHandler);
142 resourceRegistration.registerReadWriteAttribute(KEY, null, writeHandler);
143 }
144
145 public void populateModel(final ModelNode operation, final ModelNode model)
146 throws OperationFailedException
147 {
148 ALGORITHM.validateAndSet(operation, model);
149 TRANSFORMATION.validateAndSet(operation, model);
150 PROVIDER.validateAndSet(operation, model);
151 KEY.validateAndSet(operation, model);
152 }
153
154 public void read(final XMLExtendedStreamReader reader,
155 final ModelNode addOperation) throws XMLStreamException
156 {
157 ParseUtils.requireNoAttributes(reader);
158
159 while (reader.hasNext() && reader.nextTag() != END_ELEMENT)
160 {
161 if (reader.isStartElement())
162 {
163 final String localName = reader.getLocalName();
164
165 if (SecurityComponent.ALGORITHM.getXmlName().equals(localName))
166 {
167 readAttribute(reader, addOperation, SecurityComponent.ALGORITHM);
168 }
169 else if (SecurityComponent.TRANSFORMATION.getXmlName()
170 .equals(localName))
171 {
172 readAttribute(reader, addOperation, SecurityComponent.TRANSFORMATION);
173 }
174 else if (SecurityComponent.PROVIDER.getXmlName().equals(localName))
175 {
176 readAttribute(reader, addOperation, SecurityComponent.PROVIDER);
177 }
178 else if (SecurityComponent.KEY.getXmlName().equals(localName))
179 {
180 readAttribute(reader, addOperation, SecurityComponent.KEY);
181 }
182 else
183 {
184 throw ParseUtils.unexpectedElement(reader);
185 }
186 }
187 }
188 }
189
190 public void write(final XMLExtendedStreamWriter writer, final ModelNode config)
191 throws XMLStreamException
192 {
193 if (isAtLeastOneDefined(config, DEFINITIONS))
194 {
195 writer.writeStartElement(SecurityComponent.ROOT_ELEMENT_NAME);
196
197 for (final AttributeDefinition definition : DEFINITIONS)
198 {
199 writeAttribute(writer, config, definition);
200 }
201
202 writer.writeEndElement();
203 }
204 }
205
206 public void apply(final PropertiesConfiguration propertiesConfig,
207 final ModelNode model)
208 {
209 final SecurityConfiguration securityConfig =
210 propertiesConfig.getSecurityConfiguration();
211
212 final String algorithm = getAttribute(model, ALGORITHM);
213 securityConfig.setAlgorithm(algorithm);
214
215 final String transformation = getAttribute(model, TRANSFORMATION);
216 securityConfig.setTransformation(transformation);
217
218 final String provider = getAttribute(model, PROVIDER);
219 securityConfig.setProvider(provider);
220
221 final String key = getAttribute(model, KEY);
222 securityConfig.setKey(key);
223 }
224
225
226
227 }