All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.litsec.opensaml.core.AbstractSAMLObjectBuilder Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
/*
 * The opensaml-ext project is an open-source package that extends OpenSAML
 * with useful extensions and utilities.
 *
 * More details on 
 * Copyright (C) 2017 Litsec AB
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see .
 */
package se.litsec.opensaml.core;

import java.io.InputStream;

import org.opensaml.core.xml.io.MarshallingException;
import org.opensaml.core.xml.io.UnmarshallingException;
import org.opensaml.core.xml.util.XMLObjectSupport;
import org.opensaml.saml.common.SAMLObject;

import net.shibboleth.utilities.java.support.xml.XMLParserException;
import se.litsec.opensaml.utils.ObjectUtils;

/**
 * Abstract base class for the builder pattern.
 * 
 * @author Martin Lindström ([email protected])
 *
 * @param 
 *          the type
 */
public abstract class AbstractSAMLObjectBuilder implements SAMLObjectBuilder {

  /** The object that is being built. */
  private T object;

  /**
   * Constructor setting up the object to build.
   */
  public AbstractSAMLObjectBuilder() {
    this.object = ObjectUtils.createSamlObject(this.getObjectType());
  }

  /**
   * Constructor setting up the builder with a template object. Users of the instance may now change, add or delete, the
   * elements and attributes of the template object using the assignment methods of the builder.
   * 

* Note that the supplied object is cloned, so any modifications have no effect on the passed object. *

* * @param template * the template object * @throws UnmarshallingException * for unmarshalling errors * @throws MarshallingException * for marshalling errors */ public AbstractSAMLObjectBuilder(T template) throws MarshallingException, UnmarshallingException { this.object = XMLObjectSupport.cloneXMLObject(template); } /** * Constructor setting up the builder with a template object that is read from an input stream. Users of the instance * may now change, add or delete, the elements and attributes of the template object using the assignment methods of * the builder. * * @param resource * the template resource * @throws UnmarshallingException * for unmarshalling errors * @throws XMLParserException * for XML parsing errors */ public AbstractSAMLObjectBuilder(InputStream resource) throws XMLParserException, UnmarshallingException { this.object = ObjectUtils.unmarshall(resource, this.getObjectType()); } /** * The default implementation of this method assumes that the object has been built during assignment of its * attributes and elements so it simply returns the object. *

* Implementations that need to perform additional processing during the build step should override this method. *

*/ @Override public T build() { return this.object(); } /** * Returns the object type. * * @return the object type */ protected abstract Class getObjectType(); /** * Returns the object being built. * * @return the object */ public final T object() { return this.object; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy