Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Red Hat Inc. and/or its affiliates and other contributors
   * as indicated by the @author tags. All rights reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   * use this file except in compliance with the License. You may obtain a copy of
   * the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and limitations under
  * the License.
  */
 package org.keycloak.subsystem.as7;
 
 import  org.jboss.as.controller.AttributeDefinition;
 import  org.jboss.as.controller.PathAddress;
 import  org.jboss.as.controller.PathElement;
 import  org.jboss.as.controller.SimpleAttributeDefinition;
 import  org.jboss.as.controller.descriptions.ModelDescriptionConstants;
 import  org.jboss.as.controller.parsing.ParseUtils;
 import  org.jboss.as.controller.persistence.SubsystemMarshallingContext;
 import  org.jboss.dmr.ModelNode;
 import  org.jboss.dmr.Property;
 import  org.jboss.staxmapper.XMLElementReader;
 import  org.jboss.staxmapper.XMLElementWriter;
 import  org.jboss.staxmapper.XMLExtendedStreamReader;
 import  org.jboss.staxmapper.XMLExtendedStreamWriter;
 
 import java.util.List;

The subsystem parser, which uses stax to read and write to and from xml
 
 class KeycloakSubsystemParser implements XMLStreamConstants, XMLElementReader<List<ModelNode>>, XMLElementWriter<SubsystemMarshallingContext> {

    
 
     @Override
     public void readElement(final XMLExtendedStreamReader readerfinal List<ModelNode> listthrows XMLStreamException {
         // Require no attributes
         ParseUtils.requireNoAttributes(reader);
         ModelNode addKeycloakSub = Util.createAddOperation(PathAddress.pathAddress(.));
         list.add(addKeycloakSub);
 
         while (reader.hasNext() && nextTag(reader) != ) {
             if (reader.getLocalName().equals(.)) {
                 readRealm(readerlist);
             }
             else if (reader.getLocalName().equals(.)) {
                 readDeployment(readerlist);
             }
         }
     }
 
     // used for debugging
     private int nextTag(XMLExtendedStreamReader readerthrows XMLStreamException {
         return reader.nextTag();
     }
 
     private void readRealm(XMLExtendedStreamReader readerList<ModelNode> listthrows XMLStreamException {
         String realmName = readNameAttribute(reader);
         ModelNode addRealm = new ModelNode();
         addRealm.get(ModelDescriptionConstants.OP).set(ModelDescriptionConstants.ADD);
         PathAddress addr = PathAddress.pathAddress(PathElement.pathElement(ModelDescriptionConstants.SUBSYSTEM, .),
                                                    PathElement.pathElement(.realmName));
         addRealm.get(ModelDescriptionConstants.OP_ADDR).set(addr.toModelNode());
 
         while (reader.hasNext() && nextTag(reader) != ) {
             String tagName = reader.getLocalName();
             SimpleAttributeDefinition def = RealmDefinition.lookup(tagName);
             if (def == nullthrow new XMLStreamException("Unknown realm tag " + tagName);
             def.parseAndSetParameter(reader.getElementText(), addRealmreader);
         }
 
         if (!SharedAttributeDefinitons.validateTruststoreSetIfRequired(addRealm)) {
             //TODO: externalize the message
             throw new XMLStreamException("truststore and truststore-password must be set if ssl-required is not none and disable-trust-maanger is false.");
         }
 
         list.add(addRealm);
     }
 
     private void readDeployment(XMLExtendedStreamReader readerList<ModelNode> resourcesToAddthrows XMLStreamException {
         String name = readNameAttribute(reader);
         ModelNode addSecureDeployment = new ModelNode();
         addSecureDeployment.get(ModelDescriptionConstants.OP).set(ModelDescriptionConstants.ADD);
         PathAddress addr = PathAddress.pathAddress(PathElement.pathElement(ModelDescriptionConstants.SUBSYSTEM, .),
                 PathElement.pathElement(.name));
         addSecureDeployment.get(ModelDescriptionConstants.OP_ADDR).set(addr.toModelNode());
         List<ModelNode> credentialsToAdd = new ArrayList<ModelNode>();
        while (reader.hasNext() && nextTag(reader) != ) {
            String tagName = reader.getLocalName();
            if (tagName.equals(.)) {
                readCredential(readeraddrcredentialsToAdd);
                continue;
            }
            SimpleAttributeDefinition def = SecureDeploymentDefinition.lookup(tagName);
            if (def == nullthrow new XMLStreamException("Unknown secure-deployment tag " + tagName);
            def.parseAndSetParameter(reader.getElementText(), addSecureDeploymentreader);
        }


        
TODO need to check realm-ref first. if (!SharedAttributeDefinitons.validateTruststoreSetIfRequired(addSecureDeployment)) { //TODO: externalize the message throw new XMLStreamException("truststore and truststore-password must be set if ssl-required is not none and disable-trust-maanger is false."); }
        // Must add credentials after the deployment is added.
        resourcesToAdd.add(addSecureDeployment);
        resourcesToAdd.addAll(credentialsToAdd);
    }
    public void readCredential(XMLExtendedStreamReader reader, PathAddress parentList<ModelNode> credentialsToAddthrows XMLStreamException {
        String name = readNameAttribute(reader);
        ModelNode addCredential = new ModelNode();
        addCredential.get(ModelDescriptionConstants.OP).set(ModelDescriptionConstants.ADD);
        PathAddress addr = PathAddress.pathAddress(parent, PathElement.pathElement(.name));
        addCredential.get(ModelDescriptionConstants.OP_ADDR).set(addr.toModelNode());
        addCredential.get(..getName()).set(reader.getElementText());
        credentialsToAdd.add(addCredential);
    }
    // expects that the current tag will have one single attribute called "name"
    private String readNameAttribute(XMLExtendedStreamReader readerthrows XMLStreamException {
        String name = null;
        for (int i = 0; i < reader.getAttributeCount(); i++) {
            String attr = reader.getAttributeLocalName(i);
            if (attr.equals("name")) {
                name = reader.getAttributeValue(i);
                continue;
            }
            throw ParseUtils.unexpectedAttribute(readeri);
        }
        if (name == null) {
            throw ParseUtils.missingRequired(reader, Collections.singleton("name"));
        }
        return name;
    }

    
    @Override
    public void writeContent(final XMLExtendedStreamWriter writerfinal SubsystemMarshallingContext contextthrows XMLStreamException {
        context.startSubsystemElement(.false);
        writeRealms(writercontext);
        writeSecureDeployments(writercontext);
        writer.writeEndElement();
    }
    private void writeRealms(XMLExtendedStreamWriter writer, SubsystemMarshallingContext contextthrows XMLStreamException {
        if (!context.getModelNode().get(.).isDefined()) {
            return;
        }
        for (Property realm : context.getModelNode().get(.).asPropertyList()) {
            writer.writeStartElement(.);
            writer.writeAttribute("name"realm.getName());
            ModelNode realmElements = realm.getValue();
            for (AttributeDefinition element : .) {
                element.marshallAsElement(realmElementswriter);
            }
            writer.writeEndElement();
        }
    }
    private void writeSecureDeployments(XMLExtendedStreamWriter writer, SubsystemMarshallingContext contextthrows XMLStreamException {
        if (!context.getModelNode().get(.).isDefined()) {
            return;
        }
        for (Property deployment : context.getModelNode().get(.).asPropertyList()) {
            writer.writeStartElement(.);
            writer.writeAttribute("name"deployment.getName());
            ModelNode deploymentElements = deployment.getValue();
            for (AttributeDefinition element : .) {
                element.marshallAsElement(deploymentElementswriter);
            }
            ModelNode credentials = deploymentElements.get(.);
            if (credentials.isDefined()) {
                writeCredentials(writercredentials);
            }
            writer.writeEndElement();
        }
    }
    private void writeCredentials(XMLExtendedStreamWriter writer, ModelNode credentialsthrows XMLStreamException {
        for (Property credential : credentials.asPropertyList()) {
            writer.writeStartElement(.);
            writer.writeAttribute("name"credential.getName());
            String credentialValue = credential.getValue().get(..getName()).asString();
            writeCharacters(writercredentialValue);
            writer.writeEndElement();
        }
    }
    // code taken from org.jboss.as.controller.AttributeMarshaller
    private void writeCharacters(XMLExtendedStreamWriter writerString contentthrows XMLStreamException {
        if (content.indexOf('\n') > -1) {
            // Multiline content. Use the overloaded variant that staxmapper will format
            writer.writeCharacters(content);
        } else {
            // Staxmapper will just output the chars without adding newlines if this is used
            char[] chars = content.toCharArray();
            writer.writeCharacters(chars, 0, chars.length);
        }
    }
New to GrepCode? Check out our FAQ X