/*
 * Copyright 2012 SURFnet bv, The Netherlands
 *
 * 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 nl.surfnet.mockoleth.saml.xml;

import java.util.Map;

import org.opensaml.Configuration;
import org.opensaml.saml2.core.Attribute;
import org.opensaml.saml2.core.AttributeStatement;
import org.opensaml.saml2.core.AttributeValue;
import org.opensaml.saml2.core.impl.AttributeBuilder;
import org.opensaml.saml2.core.impl.AttributeStatementBuilder;
import org.opensaml.xml.XMLObjectBuilderFactory;
import org.opensaml.xml.schema.XSString;
import org.opensaml.xml.schema.impl.XSStringBuilder;

public class AttributeStatementGenerator {

    private final XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();

    /*
	public AttributeStatement generateAttributeStatement(Collection<GrantedAuthority> authorities) {
		
		AttributeStatementBuilder attributeStatementBuilder = (AttributeStatementBuilder) builderFactory.getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME);
		AttributeStatement attributeStatement = attributeStatementBuilder.buildObject();

		//Response/Assertion/AttributeStatement/Attribute
		AttributeBuilder attributeBuilder = (AttributeBuilder)  builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);
		Attribute attribute = attributeBuilder.buildObject();
		attribute.setName(GrantedAuthority.class.getName());
		
		//urn:oasis:names:tc:SAML:2.0:attrname-format:basic
		attribute.setNameFormat(Attribute.BASIC);
		
		
		for (GrantedAuthority grantedAuthority : authorities) {
			//this was convoluted to figure out
			//Response/Assertion/AttributeStatement/Attribute/AttributeValue
			XSStringBuilder stringBuilder = (XSStringBuilder) Configuration.getBuilderFactory().getBuilder(XSString.TYPE_NAME);
			XSString stringValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
			stringValue.setValue(grantedAuthority.getAuthority());

	
			attribute.getAttributeValues().add(stringValue);
			
		}
		
		attributeStatement.getAttributes().add(attribute);
		
		return attributeStatement;
	} */

    public AttributeStatement generateAttributeStatement(final Map<String, String> attributes) {
        AttributeStatementBuilder attributeStatementBuilder = (AttributeStatementBuilder) builderFactory.getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME);
        AttributeStatement attributeStatement = attributeStatementBuilder.buildObject();

        AttributeBuilder attributeBuilder = (AttributeBuilder) builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);
        XSStringBuilder stringBuilder = (XSStringBuilder) Configuration.getBuilderFactory().getBuilder(XSString.TYPE_NAME);

        for (Map.Entry<String, String> entry : attributes.entrySet()) {
            Attribute attribute = attributeBuilder.buildObject();
            attribute.setName(entry.getKey());
            // attribute.setNameFormat(Attribute.BASIC);
            XSString stringValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
            stringValue.setValue(entry.getValue());
            attribute.getAttributeValues().add(stringValue);
            attributeStatement.getAttributes().add(attribute);
        }

        return attributeStatement;
    }
}

@@ -1147,16 +1147,53 @@
 erator %7B
+%0A    private static long counter = 0;
 %0A%0A    pr
@@ -1285,1349 +1285,8 @@
 );%0A%0A
-    /*%0A%09public AttributeStatement generateAttributeStatement(Collection%3CGrantedAuthority%3E authorities) %7B%0A%09%09%0A%09%09AttributeStatementBuilder attributeStatementBuilder = (AttributeStatementBuilder) builderFactory.getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME);%0A%09%09AttributeStatement attributeStatement = attributeStatementBuilder.buildObject();%0A%0A%09%09//Response/Assertion/AttributeStatement/Attribute%0A%09%09AttributeBuilder attributeBuilder = (AttributeBuilder)  builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);%0A%09%09Attribute attribute = attributeBuilder.buildObject();%0A%09%09attribute.setName(GrantedAuthority.class.getName());%0A%09%09%0A%09%09//urn:oasis:names:tc:SAML:2.0:attrname-format:basic%0A%09%09attribute.setNameFormat(Attribute.BASIC);%0A%09%09%0A%09%09%0A%09%09for (GrantedAuthority grantedAuthority : authorities) %7B%0A%09%09%09//this was convoluted to figure out%0A%09%09%09//Response/Assertion/AttributeStatement/Attribute/AttributeValue%0A%09%09%09XSStringBuilder stringBuilder = (XSStringBuilder) Configuration.getBuilderFactory().getBuilder(XSString.TYPE_NAME);%0A%09%09%09XSString stringValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);%0A%09%09%09stringValue.setValue(grantedAuthority.getAuthority());%0A%0A%09%0A%09%09%09attribute.getAttributeValues().add(stringValue);%0A%09%09%09%0A%09%09%7D%0A%09%09%0A%09%09attributeStatement.getAttributes().add(attribute);%0A%09%09%0A%09%09return attributeStatement;%0A%09%7D */%0A%0A
     
@@ -2062,65 +2062,8 @@
 ));%0A
-            // attribute.setNameFormat(Attribute.BASIC);%0A
     
@@ -2193,44 +2193,177 @@
     
-s
+S
 tring
-V
+ v
 alue
-.setValue(entry.g
+ = entry.getValue();%0A            if (%22random%22.equals(value)) %7B%0A                value = %22john.doe.%22 + counter;%0A            %7D%0A            stringValue.s
 etValue(
 ));%0A
@@ -2358,17 +2358,21 @@
 etValue(
-)
+value
 );%0A     

Randomize a random value