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

com.sun.xml.wss.saml.assertion.saml20.jaxb20.Conditions Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

/*
 * $Id: Conditions.java,v 1.2 2010-10-21 15:38:03 snajper Exp $
 */

package com.sun.xml.wss.saml.assertion.saml20.jaxb20;

import com.sun.xml.wss.saml.SAMLException;
import com.sun.xml.wss.logging.LogDomainConstants;
import com.sun.xml.wss.logging.LogStringsMessages;
import com.sun.xml.wss.saml.internal.saml20.jaxb20.ConditionsType;
import com.sun.xml.wss.saml.util.SAML20JAXBUtil;
import com.sun.xml.wss.util.DateUtils;
import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import jakarta.xml.bind.JAXBContext;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;

/**
 * The validity of an Assertion MAY be subject to a set of
 * Conditions. Each Condition evaluates to a value that
 * is Valid, Invalid or Indeterminate.
 */
public class Conditions extends ConditionsType
        implements com.sun.xml.wss.saml.Conditions {

    protected static final Logger log =
            Logger.getLogger(
            LogDomainConstants.WSS_API_DOMAIN,
            LogDomainConstants.WSS_API_DOMAIN_BUNDLE);

    private Date notBeforeField = null;
    private Date notOnOrAfterField = null;

    /**
    Constructor taking in nothing (SAML spec allows it)
     */
    public Conditions() {
        super();
    }
    @SuppressWarnings("unchecked")
    private void setConditionOrAudienceRestrictionOrOneTimeUse(List condition) {
        this.conditionOrAudienceRestrictionOrOneTimeUse = condition;
    }

    /**
     * Constructs an instance of Conditions.
     *
     * @param notBefore specifies the earliest time instant at which the
     *        assertion is valid.
     * @param notOnOrAfter specifies the time instant at which the assertion
     *        has expired.
     * @param ar the AudienceRestrictionCondition to be
     *        added. Can be null, if no audience restriction.
     */
    public Conditions(
            GregorianCalendar notBefore,
            GregorianCalendar notOnOrAfter,
            List condition,
            List ar,
            List oneTimeUse,
            List proxyRestriction) {

        DatatypeFactory factory = null;
        try {
            factory = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException e) {
            factory = null;
        }

        if (factory != null) {
            setNotBefore(factory.newXMLGregorianCalendar(notBefore));
            setNotOnOrAfter(factory.newXMLGregorianCalendar(notOnOrAfter));
        }

        if (condition != null) {
            setConditionOrAudienceRestrictionOrOneTimeUse(condition);
        }
        if (ar != null) {
            setConditionOrAudienceRestrictionOrOneTimeUse(ar);
        }
        if (oneTimeUse != null) {
            setConditionOrAudienceRestrictionOrOneTimeUse(oneTimeUse);
        }
        if (proxyRestriction != null) {
            setConditionOrAudienceRestrictionOrOneTimeUse(proxyRestriction);
        }
    }

    public Conditions(ConditionsType cType){
            setNotBefore(cType.getNotBefore());
            setNotOnOrAfter(cType.getNotOnOrAfter());
            setConditionOrAudienceRestrictionOrOneTimeUse(cType.getConditionOrAudienceRestrictionOrOneTimeUse());

    }

   @Override
   public Date getNotBeforeDate(){
        try {
            if(notBeforeField != null){
                return notBeforeField;
            }
            if(super.getNotBefore() != null){
                notBeforeField = DateUtils.stringToDate(super.getNotBefore().toString());
            }
        } catch (ParseException ex) {
            log.log(Level.SEVERE, LogStringsMessages.WSS_0430_SAML_GET_NOT_BEFORE_DATE_OR_GET_NOT_ON_OR_AFTER_DATE_PARSE_FAILED(), ex);
        }
            return notBeforeField;
        }

        @Override
        public Date getNotOnOrAfterDate(){
        try {
            if(notOnOrAfterField != null){
                return notOnOrAfterField;
            }
            if(super.getNotOnOrAfter() != null){
                notOnOrAfterField = DateUtils.stringToDate(super.getNotOnOrAfter().toString());
            }
        } catch (ParseException ex) {
            log.log(Level.SEVERE, LogStringsMessages.WSS_0430_SAML_GET_NOT_BEFORE_DATE_OR_GET_NOT_ON_OR_AFTER_DATE_PARSE_FAILED(), ex);
        }
            return notOnOrAfterField;
        }
        @Override
        @SuppressWarnings("unchecked")
        public List getConditions(){
            return (List)(Object) super.getConditionOrAudienceRestrictionOrOneTimeUse();
        }

    /**
     * Constructs a Conditions element from an existing XML block.
     *
     * @param element A org.w3c.dom.Element representing
     *        DOM tree for Conditions object
     * @exception SAMLException if it could not process the Element properly,
     *            implying that there is an error in the sender or in the
     *            element definition.
     */
    public static ConditionsType fromElement(org.w3c.dom.Element element)
            throws SAMLException {
        try {
            JAXBContext jc = SAML20JAXBUtil.getJAXBContext();

            jakarta.xml.bind.Unmarshaller u = jc.createUnmarshaller();
            return (ConditionsType) u.unmarshal(element);
        } catch (Exception ex) {
            throw new SAMLException(ex.getMessage());
        }
    }
}