ImportSAML2MetaData.java revision 1a09ec0264aacab68f85b04c150957647557a35d
7083N/A/**
7083N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7083N/A *
7083N/A * Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved
7083N/A *
7083N/A * The contents of this file are subject to the terms
7083N/A * of the Common Development and Distribution License
7083N/A * (the License). You may not use this file except in
7083N/A * compliance with the License.
7083N/A *
7083N/A * You can obtain a copy of the License at
7083N/A * https://opensso.dev.java.net/public/CDDLv1.0.html or
7083N/A * opensso/legal/CDDLv1.0.txt
7083N/A * See the License for the specific language governing
7083N/A * permission and limitations under the License.
7083N/A *
7083N/A * When distributing Covered Code, include this CDDL
7083N/A * Header Notice in each file and include the License file
7083N/A * at opensso/legal/CDDLv1.0.txt.
7083N/A * If applicable, add the following below the CDDL Header,
7083N/A * with the fields enclosed by brackets [] replaced by
7083N/A * your own identifying information:
7083N/A * "Portions Copyrighted [year] [name of copyright owner]"
7083N/A *
7083N/A * $Id: ImportSAML2MetaData.java,v 1.5 2008/07/08 01:12:01 exu Exp $
7083N/A *
7083N/A * Portions Copyrighted 2011-2014 ForgeRock AS.
7083N/A */
7083N/Apackage com.sun.identity.workflow;
7083N/A
7083N/Aimport com.sun.identity.saml2.meta.SAML2MetaException;
7083N/Aimport com.sun.identity.saml2.meta.SAML2MetaManager;
7083N/Aimport com.sun.identity.saml2.meta.SAML2MetaUtils;
7083N/Aimport com.sun.identity.saml2.jaxb.entityconfig.BaseConfigType;
7083N/Aimport com.sun.identity.saml2.jaxb.entityconfig.EntityConfigElement;
7083N/Aimport com.sun.identity.shared.debug.Debug;
7083N/Aimport com.sun.identity.shared.xml.XMLUtils;
7083N/Aimport java.util.List;
7083N/Aimport javax.xml.bind.JAXBException;
7083N/Aimport org.w3c.dom.Document;
7083N/A
7083N/A/**
7083N/A * Import SAML2 Metadata.
7083N/A */
7083N/Apublic class ImportSAML2MetaData {
7083N/A
7083N/A private static final Debug DEBUG = Debug.getInstance("workflow");
7083N/A
7083N/A private ImportSAML2MetaData() {
7083N/A }
7083N/A
7083N/A /**
7083N/A * Imports meta and extended metadata.
7083N/A *
7083N/A * @param realm Realm of the entity.
7083N/A * @param metadata Meta data.
7083N/A * @param extended extended data.
7083N/A * @return realm and entity ID.
7083N/A */
7083N/A public static String[] importData(
7083N/A String realm,
7083N/A String metadata,
7083N/A String extended
7083N/A ) throws WorkflowException {
7083N/A String entityID = null;
7083N/A
7083N/A try {
SAML2MetaManager metaManager = new SAML2MetaManager();
EntityConfigElement configElt = null;
if (extended != null) {
Object obj = SAML2MetaUtils.convertStringToJAXB(extended);
configElt = (obj instanceof EntityConfigElement) ?
(EntityConfigElement)obj : null;
if (configElt != null && configElt.isHosted()) {
List config =
configElt.getIDPSSOConfigOrSPSSOConfigOrAuthnAuthorityConfig();
if (!config.isEmpty()) {
BaseConfigType bConfig = (BaseConfigType)
config.iterator().next();
realm = SAML2MetaUtils.getRealmByMetaAlias(
bConfig.getMetaAlias());
}
}
}
// Load the metadata if it has been provided
if (metadata != null) {
entityID = importSAML2MetaData(metaManager, realm, metadata);
}
// Load the extended metadata if it has been provided
if (configElt != null) {
metaManager.createEntityConfig(realm, configElt);
}
} catch (SAML2MetaException e) {
DEBUG.error("An error occurred while importing the SAML metadata", e);
throw new WorkflowException(e.getMessage());
} catch (JAXBException e) {
DEBUG.error("An error occurred while importing the SAML metadata", e);
throw new WorkflowException(e.getMessage());
}
String[] results = {realm, entityID};
return results;
}
private static String importSAML2MetaData(SAML2MetaManager metaManager, String realm,
String metadata)
throws SAML2MetaException, JAXBException, WorkflowException {
String result = null;
Document doc = XMLUtils.toDOMDocument(metadata, DEBUG);
if (doc == null) {
throw new WorkflowException(
"import-entity-exception-invalid-descriptor", null);
} else {
List<String> entityIds = SAML2MetaUtils.importSAML2Document(metaManager, realm, doc);
if (entityIds.isEmpty()) {
throw new WorkflowException(
"import-entity-exception-invalid-descriptor", null);
} else {
result = entityIds.iterator().next();
}
}
return result;
}
}