Java Code Examples for org.opensaml.xml.util.DatatypeHelper#safeEquals()

The following examples show how to use org.opensaml.xml.util.DatatypeHelper#safeEquals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NamespaceManager.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Remove a Namespace from a set of Namespaces.  Equivalence of Namespace instances will be based 
 * on namespace URI and prefix only. The <code>alwaysDeclare</code> property will be ignored for
 * purpose of equivalence.
 * 
 * @param namespaces the set of namespaces
 * @param oldNamespace the namespace to add to the set
 */
private void removeNamespace(Set<Namespace> namespaces, Namespace oldNamespace) {
    if (oldNamespace == null) {
        return;
    }
    
    Iterator<Namespace> iter = namespaces.iterator();
    while (iter.hasNext()) {
        Namespace namespace = iter.next();
        if (DatatypeHelper.safeEquals(namespace.getNamespaceURI(), oldNamespace.getNamespaceURI()) &&
                DatatypeHelper.safeEquals(namespace.getNamespacePrefix(), oldNamespace.getNamespacePrefix())) {
            iter.remove();
        }
    }
    
}
 
Example 2
Source File: AbstractMetadataProvider.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the entities descriptor with the given name.
 * 
 * @param name name of the entities descriptor
 * @param rootDescriptor the root descriptor to search in
 * 
 * @return the EntitiesDescriptor with the given name
 */
protected EntitiesDescriptor getEntitiesDescriptorByName(String name, EntitiesDescriptor rootDescriptor) {
    EntitiesDescriptor descriptor = null;

    if (DatatypeHelper.safeEquals(name, rootDescriptor.getName()) && isValid(rootDescriptor)) {
        descriptor = rootDescriptor;
    } else {
        List<EntitiesDescriptor> childDescriptors = rootDescriptor.getEntitiesDescriptors();
        if (childDescriptors == null || childDescriptors.isEmpty()) {
            return null;
        }

        for (EntitiesDescriptor childDescriptor : childDescriptors) {
            childDescriptor = getEntitiesDescriptorByName(name, childDescriptor);
            if (childDescriptor != null) {
                descriptor = childDescriptor;
            }
        }
    }

    return descriptor;
}
 
Example 3
Source File: HTTPSOAP11Decoder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Checks that, if any SOAP headers, require understand that they are in the understood header list.
 * 
 * @param headers SOAP headers to check
 * 
 * @throws MessageDecodingException thrown if a SOAP header requires understanding but is not understood by the
 *             decoder
 */
protected void checkUnderstoodSOAPHeaders(List<XMLObject> headers) throws MessageDecodingException {
    if (headers == null || headers.isEmpty()) {
        return;
    }

    AttributeExtensibleXMLObject attribExtensObject;
    for (XMLObject header : headers) {
        if (header instanceof AttributeExtensibleXMLObject) {
            attribExtensObject = (AttributeExtensibleXMLObject) header;
            if (DatatypeHelper.safeEquals("1", attribExtensObject.getUnknownAttributes().get(soapMustUnderstand))) {
                if (!understoodHeaders.contains(header.getElementQName())) {
                    throw new MessageDecodingException("SOAP decoder encountered a header, "
                            + header.getElementQName()
                            + ", that requires understanding however this decoder does not understand that header");
                }
            }
        }
    }
}
 
Example 4
Source File: AuthnResponseEndpointSelector.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Selects the endpoint by way of the assertion consumer service index given in the AuthnRequest.
 * 
 * @param request the AuthnRequest
 * @param endpoints list of endpoints to select from
 * 
 * @return the selected endpoint
 */
protected Endpoint selectEndpointByACSIndex(AuthnRequest request, List<IndexedEndpoint> endpoints) {
    Integer acsIndex = request.getAssertionConsumerServiceIndex();
    for (IndexedEndpoint endpoint : endpoints) {
        if (endpoint == null || !getSupportedIssuerBindings().contains(endpoint.getBinding())) {
            log.debug(
                    "Endpoint '{}' with binding '{}' discarded because it requires an unsupported outbound binding.",
                    endpoint.getLocation(), endpoint.getBinding());
            continue;
        }

        if (DatatypeHelper.safeEquals(acsIndex, endpoint.getIndex())) {
            return endpoint;
        } else {
            log.debug("Endpoint '{}' with index '{}' discard because it does have the required index '{}'",
                    new Object[] {endpoint.getLocation(), endpoint.getIndex(), acsIndex});
        }
    }

    log.warn("Relying party '{}' requested the response to be returned to endpoint with ACS index '{}' "
            + "however no endpoint, with that index and using a supported binding, can be found "
            + " in the relying party's metadata ", getEntityMetadata().getEntityID(), acsIndex);
    return null;
}
 
Example 5
Source File: HTTPSOAP11Decoder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Checks that, if any SOAP headers, require understand that they are in the understood header list.
 * 
 * @param headers SOAP headers to check
 * 
 * @throws MessageDecodingException thrown if a SOAP header requires understanding but is not understood by the
 *             decoder
 */
protected void checkUnderstoodSOAPHeaders(List<XMLObject> headers) throws MessageDecodingException {
    if (headers == null || headers.isEmpty()) {
        return;
    }

    AttributeExtensibleXMLObject attribExtensObject;
    for (XMLObject header : headers) {
        if (header instanceof AttributeExtensibleXMLObject) {
            attribExtensObject = (AttributeExtensibleXMLObject) header;
            if (DatatypeHelper.safeEquals("1", attribExtensObject.getUnknownAttributes().get(soapMustUnderstand))) {
                if (!understoodHeaders.contains(header.getElementQName())) {
                    throw new MessageDecodingException("SOAP decoder encountered a  header, "
                            + header.getElementQName()
                            + ", that requires undestanding however this decoder does not understand that header");
                }
            }
        }
    }
}
 
Example 6
Source File: Namespace.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Checks if the given object is the same as this Namespace. This is true if:
 * <ul>
 * <li>The given object is of type {@link Namespace}</li>
 * <li>The given object's namespace URI is the same as this object's namespace URI</li>
 * <li>The given object's namespace prefix is the same as this object's namespace prefix</li>
 * </ul>
 * 
 * @param obj {@inheritDoc}
 * 
 * @return {@inheritDoc}
 */
public boolean equals(Object obj) {    
    if(obj == this){
        return true;
    }
    
    if (obj instanceof Namespace) {
        Namespace otherNamespace = (Namespace) obj;
        if (DatatypeHelper.safeEquals(otherNamespace.getNamespaceURI(), getNamespaceURI())){
            if (DatatypeHelper.safeEquals(otherNamespace.getNamespacePrefix(), getNamespacePrefix())){
                return otherNamespace.alwaysDeclare() == alwaysDeclare();
            }
        }
    }

    return false;
}
 
Example 7
Source File: X509CRLImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public void setValue(String newValue) {
    // Dump our cached DOM if the new value really is new
    String currentCert = B64_CRL_STORE.get(b64CRLIndex);
    String b64Cert = prepareForAssignment(currentCert, newValue);

    // This is a new value, remove the old one, add the new one
    if (!DatatypeHelper.safeEquals(currentCert, b64Cert)) {
        B64_CRL_STORE.remove(b64CRLIndex);
        b64CRLIndex = B64_CRL_STORE.put(b64Cert);
    }
}
 
Example 8
Source File: CryptoBinaryImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public void setValue(String newValue) {
    if (bigIntValue != null 
            && (!DatatypeHelper.safeEquals(getValue(), newValue) || newValue == null)) {
        // Just clear the cached value, my not be needed in big int form again,
        // let it be lazily recreated if necessary
        bigIntValue = null;
    }
    super.setValue(newValue);
}
 
Example 9
Source File: SimpleRetrievalMethodEncryptedKeyResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public Iterable<EncryptedKey> resolve(EncryptedData encryptedData) {
    List<EncryptedKey> resolvedEncKeys = new ArrayList<EncryptedKey>();

    if (encryptedData.getKeyInfo() == null) {
        return resolvedEncKeys;
    }

    for (RetrievalMethod rm : encryptedData.getKeyInfo().getRetrievalMethods()) {
        if (!DatatypeHelper.safeEquals(rm.getType(), EncryptionConstants.TYPE_ENCRYPTED_KEY)) {
            continue;
        }
        if (rm.getTransforms() != null) {
            log.warn("EncryptedKey RetrievalMethod has transforms, can not process");
            continue;
        }

        EncryptedKey encKey = dereferenceURI(rm);
        if (encKey == null) {
            continue;
        }

        if (matchRecipient(encKey.getRecipient())) {
            resolvedEncKeys.add(encKey);
        }
    }

    return resolvedEncKeys;
}
 
Example 10
Source File: NamespaceManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add a Namespace to a set of Namespaces.  Namespaces with identical URI and prefix will be treated as equivalent.
 * An <code>alwaysDeclare</code> property of true will take precedence over a value of false.
 * 
 * @param namespaces the set of namespaces
 * @param newNamespace the namespace to add to the set
 */
private void addNamespace(Set<Namespace> namespaces, Namespace newNamespace) {
    if (newNamespace == null) {
        return;
    }
    
    if (namespaces.size() == 0) {
        namespaces.add(newNamespace);
        return;
    }
    
    for (Namespace namespace : namespaces) {
        if (DatatypeHelper.safeEquals(namespace.getNamespaceURI(), newNamespace.getNamespaceURI()) &&
                DatatypeHelper.safeEquals(namespace.getNamespacePrefix(), newNamespace.getNamespacePrefix())) {
            if (newNamespace.alwaysDeclare() && !namespace.alwaysDeclare()) {
                // An alwaysDeclare=true trumps false.
                // Don't modify the existing object in the set, merely swap them.
                namespaces.remove(namespace);
                namespaces.add(newNamespace);
                return;
            } else {
                // URI and prefix match, alwaysDeclare does also, so just leave the original
                return;
            }
        }
    }
    
    namespaces.add(newNamespace);
}
 
Example 11
Source File: WSAddressingHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the <code>wsa:IsReferenceParameter</code> attribute from a given SOAP object.
 * 
 * @param soapObject the SOAP object to add the attribute to
 * 
 * @return value of the IsReferenceParameter attribute, or false if not present
 */
public static boolean getWSAIsReferenceParameter(XMLObject soapObject) {
    if (soapObject instanceof IsReferenceParameterBearing) {
        XSBooleanValue value = ((IsReferenceParameterBearing)soapObject).isWSAIsReferenceParameterXSBoolean();
        if (value != null) {
            return value.getValue();
        }
    }
    if (soapObject instanceof AttributeExtensibleXMLObject) {
        String valueStr = DatatypeHelper.safeTrimOrNullString(((AttributeExtensibleXMLObject)soapObject)
                .getUnknownAttributes().get(IsReferenceParameterBearing.WSA_IS_REFERENCE_PARAMETER_ATTR_NAME)); 
        return DatatypeHelper.safeEquals("1", valueStr) || DatatypeHelper.safeEquals("true", valueStr);
    }
    return false;
}
 
Example 12
Source File: AbstractXMLObjectUnmarshaller.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unmarshalls the attributes from the given DOM Attr into the given XMLObject. If the attribute is an XML namespace
 * declaration the attribute is passed to
 * {@link AbstractXMLObjectUnmarshaller#unmarshallNamespaceAttribute(XMLObject, Attr)}. If it is an schema type
 * decleration (xsi:type) it is ignored because this attribute is handled by {@link #buildXMLObject(Element)}. All
 * other attributes are passed to the {@link #processAttribute(XMLObject, Attr)}
 * 
 * @param attribute the attribute to be unmarshalled
 * @param xmlObject the XMLObject that will recieve information from the DOM attribute
 * 
 * @throws UnmarshallingException thrown if there is a problem unmarshalling an attribute
 */
protected void unmarshallAttribute(XMLObject xmlObject, Attr attribute) throws UnmarshallingException {
    QName attribName = XMLHelper.getNodeQName(attribute);
    log.trace("Pre-processing attribute {}", attribName);
    String attributeNamespace = DatatypeHelper.safeTrimOrNullString(attribute.getNamespaceURI());
    
    if (DatatypeHelper.safeEquals(attributeNamespace, XMLConstants.XMLNS_NS)) {
        unmarshallNamespaceAttribute(xmlObject, attribute);
    } else if (DatatypeHelper.safeEquals(attributeNamespace, XMLConstants.XSI_NS)) {
        unmarshallSchemaInstanceAttributes(xmlObject, attribute);
    } else {
        if (log.isTraceEnabled()) {
            log.trace("Attribute {} is neither a schema type nor namespace, calling processAttribute()",
                    XMLHelper.getNodeQName(attribute));
        }
        String attributeNSURI = attribute.getNamespaceURI();
        String attributeNSPrefix;
        if (attributeNSURI != null) {
            attributeNSPrefix = attribute.lookupPrefix(attributeNSURI);
            if (attributeNSPrefix == null && XMLConstants.XML_NS.equals(attributeNSURI)) {
                attributeNSPrefix = XMLConstants.XML_PREFIX;
            }
            xmlObject.getNamespaceManager().registerAttributeName(attribName);
        }

        checkIDAttribute(attribute);

        processAttribute(xmlObject, attribute);
    }
}
 
Example 13
Source File: AbstractXMLObjectUnmarshaller.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unmarshalls a namespace declaration attribute.
 * 
 * @param xmlObject the xmlObject to recieve the namespace decleration
 * @param attribute the namespace decleration attribute
 */
protected void unmarshallNamespaceAttribute(XMLObject xmlObject, Attr attribute) {
    if (log.isTraceEnabled()) {
        log.trace("{} is a namespace declaration, adding it to the list of namespaces on the XMLObject",
                XMLHelper.getNodeQName(attribute));
    }
    Namespace namespace;
    if(DatatypeHelper.safeEquals(attribute.getLocalName(), XMLConstants.XMLNS_PREFIX)){
        namespace = new Namespace(attribute.getValue(), null);
    }else{
        namespace = new Namespace(attribute.getValue(), attribute.getLocalName());
    }
    namespace.setAlwaysDeclare(true);
    xmlObject.getNamespaceManager().registerNamespaceDeclaration(namespace);
}
 
Example 14
Source File: AbstractXMLObjectMarshaller.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates the xmlns attributes for any namespaces set on the given XMLObject.
 * 
 * @param xmlObject the XMLObject
 * @param domElement the DOM element the namespaces will be added to
 */
protected void marshallNamespaces(XMLObject xmlObject, Element domElement) {
    log.trace("Marshalling namespace attributes for XMLObject {}", xmlObject.getElementQName());
    Set<Namespace> namespaces = xmlObject.getNamespaces();

    for (Namespace namespace : namespaces) {
        if (!namespace.alwaysDeclare()) {
            if (DatatypeHelper.safeEquals(namespace.getNamespacePrefix(), XMLConstants.XML_PREFIX)
                    || DatatypeHelper.safeEquals(namespace.getNamespaceURI(), XMLConstants.XML_NS)) {
                // the "xml" namespace never needs to be declared
                continue;
            }

            String declared = XMLHelper.lookupNamespaceURI(domElement, namespace.getNamespacePrefix());
            if (declared != null && namespace.getNamespaceURI().equals(declared)) {
                log.trace("Namespace {} has already been declared on an ancestor of {} no need to add it here",
                        namespace, xmlObject.getElementQName());
                continue;
            }
        }
        log.trace("Adding namespace declaration {} to {}", namespace, xmlObject.getElementQName());
        String nsURI = DatatypeHelper.safeTrimOrNullString(namespace.getNamespaceURI());
        String nsPrefix = DatatypeHelper.safeTrimOrNullString(namespace.getNamespacePrefix());

        XMLHelper.appendNamespaceDeclaration(domElement, nsURI, nsPrefix);
    }
}
 
Example 15
Source File: BaseObligationHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public boolean equals(Object obj) {
    if (obj == this) {
        return true;
    }

    if (obj instanceof BaseObligationHandler) {
        return DatatypeHelper.safeEquals(getObligationId(), ((BaseObligationHandler) obj).getObligationId());
    }

    return false;
}
 
Example 16
Source File: X509CertificateImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public void setValue(String newValue) {
    // Dump our cached DOM if the new value really is new
    String currentCert = B64_CERT_STORE.get(b64CertIndex);
    String b64Cert = prepareForAssignment(currentCert, newValue);

    // This is a new value, remove the old one, add the new one
    if (!DatatypeHelper.safeEquals(currentCert, b64Cert)) {
        B64_CERT_STORE.remove(b64CertIndex);
        b64CertIndex = B64_CERT_STORE.put(b64Cert);
    }
}
 
Example 17
Source File: AbstractXMLObject.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A helper function for derived classes. The mutator/setter method for any ID-typed attributes should call this
 * method in order to handle getting the old value removed from the ID-to-XMLObject mapping, and the new value added
 * to the mapping.
 * 
 * @param oldID the old value of the ID-typed attribute
 * @param newID the new value of the ID-typed attribute
 */
protected void registerOwnID(String oldID, String newID) {
    String newString = DatatypeHelper.safeTrimOrNullString(newID);

    if (!DatatypeHelper.safeEquals(oldID, newString)) {
        if (oldID != null) {
            idIndex.deregisterIDMapping(oldID);
        }

        if (newString != null) {
            idIndex.registerIDMapping(newString, this);
        }
    }
}
 
Example 18
Source File: AuthnResponseEndpointSelector.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Selects the endpoint by way of the assertion consumer service URL given in the AuthnRequest.
 * 
 * @param request the AuthnRequest
 * @param endpoints list of endpoints to select from
 * 
 * @return the selected endpoint
 */
protected Endpoint selectEndpointByACSURL(AuthnRequest request, List<IndexedEndpoint> endpoints) {
    String acsBinding = DatatypeHelper.safeTrimOrNullString(request.getProtocolBinding());

    for (IndexedEndpoint endpoint : endpoints) {
        if (!getSupportedIssuerBindings().contains(endpoint.getBinding())) {
            log.debug(
                    "Endpoint '{}' with binding '{}' discarded because that is not a supported outbound binding.",
                    endpoint.getLocation(), endpoint.getBinding());
            continue;
        }

        if (acsBinding != null) {
            if (!DatatypeHelper.safeEquals(acsBinding, endpoint.getBinding())) {
                log.debug(
                        "Endpoint '{}' with binding '{}' discarded because it does not meet protocol binding selection criteria",
                        endpoint.getLocation(), endpoint.getBinding());
                continue;
            }
        }

        String responseLocation = DatatypeHelper.safeTrim(endpoint.getResponseLocation());
        if (responseLocation != null){
                if(DatatypeHelper.safeEquals(responseLocation, request.getAssertionConsumerServiceURL())) {
                    return endpoint;
                }
        }else{    
            String location = DatatypeHelper.safeTrim(endpoint.getLocation());
            if (location != null && DatatypeHelper.safeEquals(location, request.getAssertionConsumerServiceURL())) {
                return endpoint;
            }
        }

        log.debug("Endpoint with Location '{}' discarded because neither its Location nor ResponseLocation match ACS URL '{}'",
                endpoint.getLocation(), request.getAssertionConsumerServiceURL());
    }

    log.warn("Relying party '{}' requested the response to be returned to endpoint with ACS URL '{}' "
            + " and binding '{}' however no endpoint, with that URL and using a supported binding, "
            + " can be found in the relying party's metadata ", new Object[] {getEntityMetadata().getEntityID(),
            request.getAssertionConsumerServiceURL(), (acsBinding == null) ? "any" : acsBinding});
    return null;
}
 
Example 19
Source File: AbstractXMLObject.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * A helper function for derived classes. This 'nornmalizes' newString and then if it is different from oldString
 * invalidates the DOM. It returns the normalized value so subclasses just have to go. this.foo =
 * prepareForAssignment(this.foo, foo);
 * 
 * @param oldValue - the current value
 * @param newValue - the new value
 * 
 * @return the value that should be assigned
 */
protected String prepareForAssignment(String oldValue, String newValue) {
    String newString = DatatypeHelper.safeTrimOrNullString(newValue);

    if (!DatatypeHelper.safeEquals(oldValue, newString)) {
        releaseThisandParentDOM();
    }

    return newString;
}