se.swedenconnect.opensaml.saml2.metadata.build.ExtensionsBuilder Maven / Gradle / Ivy
/*
* Copyright 2021 Sweden Connect
*
* 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 se.swedenconnect.opensaml.saml2.metadata.build;
import java.util.Arrays;
import java.util.List;
import org.opensaml.core.xml.XMLObject;
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.saml2.metadata.Extensions;
import se.swedenconnect.opensaml.common.builder.AbstractSAMLObjectBuilder;
/**
* Builder for metadata {@link Extensions} objects.
*
* @author Martin Lindström ([email protected])
*/
public class ExtensionsBuilder extends AbstractSAMLObjectBuilder {
/**
* Default constructor.
*/
public ExtensionsBuilder() {
super();
}
/**
* Creates a builder instance.
*
* @return a builder instance
*/
public static ExtensionsBuilder builder() {
return new ExtensionsBuilder();
}
/**
* Adds the extensions (overwrites any previous extensions).
*
* @param extensions
* the extension objects
* @return the builder
*/
public ExtensionsBuilder extensions(final List extensions) {
// First clear all previous values ...
this.object().getUnknownXMLObjects().clear();
for (final XMLObject obj : extensions) {
try {
this.object().getUnknownXMLObjects().add(XMLObjectSupport.cloneXMLObject(obj));
}
catch (MarshallingException | UnmarshallingException e) {
throw new RuntimeException(e);
}
}
return this;
}
/**
* See {@link #extensions(List)}.
*
* @param extensions
* the extension objects
* @return the builder
*/
public ExtensionsBuilder extensions(final XMLObject... extensions) {
return this.extensions(extensions != null ? Arrays.asList(extensions) : null);
}
/**
* Adds one, or more, extensions to this {@code Extensions} object.
*
* @param extension
* the extension(s) to add
* @return the builder
*/
public ExtensionsBuilder extension(final XMLObject... extension) {
if (extension == null) {
return this;
}
for (final XMLObject obj : extension) {
try {
this.object().getUnknownXMLObjects().add(XMLObjectSupport.cloneXMLObject(obj));
}
catch (MarshallingException | UnmarshallingException e) {
throw new RuntimeException(e);
}
}
return this;
}
/** {@inheritDoc} */
@Override
protected Class getObjectType() {
return Extensions.class;
}
}