/* * Copyright 2003 Federal Chancellery Austria * * 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 at.gv.egovernment.moa.id.auth.builder; import java.io.IOException; import java.text.MessageFormat; import java.util.Iterator; import java.util.List; import javax.xml.transform.TransformerException; import org.w3c.dom.Element; import at.gv.egovernment.moa.id.ParseException; import at.gv.egovernment.moa.id.auth.data.ExtendedSAMLAttribute; import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.DOMUtils; import at.gv.egovernment.moa.util.StringUtils; /** * Base class for building authentication the AUTHBlock and final OA data SAML assertions. * Encapsulates methods used by the two specific builders * {@link at.gv.egovernment.moa.id.auth.builder.AuthenticationBlockAssertionBuilder AuthenticationBlockAssertionBuilder} * and * {@link at.gv.egovernment.moa.id.auth.builder.AuthenticationDataAssertionBuilder AuthenticationDataAssertionBuilder} * * @author Harald Bratko */ public class AuthenticationAssertionBuilder { /** the NewLine representation in Java*/ protected static String NL = "\n"; protected static String SAML_ATTRIBUTE = " " + NL + " {2}" + NL + " "+ NL; /** * Empty constructor */ public AuthenticationAssertionBuilder() { } /** * Builds the SAML attributes to be appended to the AUTHBlock or to the SAML assertion * delivered to the online application. * The method traverses through the list of given SAML attribute objects and builds an * XML structure (String representation) for each of the attributes. * * @param extendedSAMLAttributes The SAML attributes to be appended to the AUTHBlock or * to the SAML assertion delivered to the online application. * @return A string representation including the XML structures of * the SAML attributes. * * @throws ParseException If an error occurs on serializing an SAML attribute. */ protected String buildExtendedSAMLAttributes(List extendedSAMLAttributes) throws ParseException { StringBuffer sb = new StringBuffer(); if (extendedSAMLAttributes!=null) { Iterator it = extendedSAMLAttributes.iterator(); while (it.hasNext()) { ExtendedSAMLAttribute extendedSAMLAttribute = (ExtendedSAMLAttribute)it.next(); Object value = extendedSAMLAttribute.getValue(); String name = extendedSAMLAttribute.getName(); String namespace = extendedSAMLAttribute.getNameSpace(); if (value instanceof String) { sb.append(MessageFormat.format( SAML_ATTRIBUTE, new Object[] {name, namespace, value})); } else if (value instanceof Element) { try { String serializedValue = DOMUtils.serializeNode((Element)(value)); serializedValue = StringUtils.removeXMLDeclaration(serializedValue); sb.append(MessageFormat.format( SAML_ATTRIBUTE, new Object[] {name, namespace, serializedValue})); } catch (TransformerException e) { Logger.error("Error on serializing SAML attribute \"" + name + " (namespace: \"" + namespace + "\"."); throw new ParseException("parser.05", new Object[] { name, namespace}); } catch (IOException e) { Logger.error("Error on serializing SAML attribute \"" + name + " (namespace: \"" + namespace + "\"."); throw new ParseException("parser.05", new Object[] { name, namespace}); } } } } return sb.toString(); } }