org.opensaml.soap.soap11.profile.impl.AddSOAPFault Maven / Gradle / Ivy
/*
* Licensed to the University Corporation for Advanced Internet Development,
* Inc. (UCAID) under one or more contributor license agreements. See the
* NOTICE file distributed with this work for additional information regarding
* copyright ownership. The UCAID licenses this file to You 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 org.opensaml.soap.soap11.profile.impl;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.xml.namespace.QName;
import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
import net.shibboleth.utilities.java.support.component.ComponentSupport;
import net.shibboleth.utilities.java.support.logic.Constraint;
import net.shibboleth.utilities.java.support.primitive.StringSupport;
import org.opensaml.core.xml.XMLObjectBuilder;
import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
import org.opensaml.messaging.context.MessageContext;
import org.opensaml.profile.action.AbstractProfileAction;
import org.opensaml.profile.context.EventContext;
import org.opensaml.profile.context.ProfileRequestContext;
import org.opensaml.profile.context.navigate.CurrentOrPreviousEventLookup;
import org.opensaml.soap.messaging.SOAPMessagingSupport;
import org.opensaml.soap.soap11.Fault;
import org.opensaml.soap.soap11.FaultCode;
import org.opensaml.soap.soap11.FaultString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
/**
* Action that resolves or builds a SOAP 1.1 {@link Fault} object, and stores it in the outbound message context.
*
*
* An attempt will first be made to resolve a pre-existing fault instance from the {@link ProfileRequestContext}, via
* an optionally configured lookup strategy. This is to accommodate SOAP-aware components which may choose to
* emit a specific, locally determined fault. The default strategy is {@link MessageContextFaultStrategy}.
*
*
*
* If no context fault instance is resolved, a new instance will be built using strategy functions which lookup
* the {@link FaultCode} {@link QName} and the {@link FaultString} {@link String} values. If no value is produced,
* the former defaults to {@link FaultCode#SERVER}. The latter defaults to null.
*
*
* @event {@link org.opensaml.profile.action.EventIds#PROCEED_EVENT_ID}
*/
public class AddSOAPFault extends AbstractProfileAction {
/** Class logger. */
@Nonnull private Logger log = LoggerFactory.getLogger(AddSOAPFault.class);
/** Strategy for resolving a fault instance directly from the request context. */
@Nullable private Function contextFaultStrategy;
/** Predicate determining whether detailed error information is permitted. */
@Nonnull private Predicate detailedErrorsCondition;
/** Optional method to obtain fault code. */
@Nullable private Function faultCodeLookupStrategy;
/** Optional method to obtain a fault string. */
@Nullable private Function faultStringLookupStrategy;
/** Default fault codes to insert. */
@Nonnull @NonnullElements private QName defaultFaultCode;
/** A default fault string to include. */
@Nullable private String faultString;
/** Whether to include detailed status information. */
private boolean detailedErrors;
/** Whether to set the outbound message context's message property to null. */
private boolean nullifyOutboundMessage;
/** Constructor. */
public AddSOAPFault() {
detailedErrorsCondition = Predicates.alwaysFalse();
defaultFaultCode = FaultCode.SERVER;
detailedErrors = false;
contextFaultStrategy = new MessageContextFaultStrategy();
nullifyOutboundMessage = true;
}
/**
* Set the flag indicating whether to set the outbound message context's message property to null.
*
* Default is: true
*
* @param flag true if should nullify, false if not
*/
public void setNullifyOutboundMessage(boolean flag) {
nullifyOutboundMessage = flag;
}
/**
* Set the optional strategy used to resolve a {@link Fault} instance directly
* from the request context.
*
* @param strategy strategy used to resolve the fault instance
*/
public void setContextFaultStrategy(@Nullable final Function strategy) {
ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
contextFaultStrategy = strategy;
}
/**
* Set the predicate used to determine the detailed errors condition.
*
* @param condition predicate for detailed errors condition
*/
public void setDetailedErrorsCondition(@Nonnull final Predicate condition) {
ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
detailedErrorsCondition = Constraint.isNotNull(condition, "Detailed errors condition cannot be null");
}
/**
* Set the optional strategy used to obtain a faultcode to include.
*
* @param strategy strategy used to obtain faultcode
*/
public void setFaultCodeLookupStrategy(@Nullable final Function strategy) {
ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
faultCodeLookupStrategy = strategy;
}
/**
* Set the optional strategy used to obtain a faultstring to include.
*
* @param strategy strategy used to obtain a fault string
*/
public void setFaultStringLookupStrategy(@Nullable final Function strategy) {
ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
faultStringLookupStrategy = strategy;
}
/**
* Set the default faultcode to insert.
*
* @param code faultcode
*/
public void setFaultCode(@Nonnull QName code) {
ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
defaultFaultCode = Constraint.isNotNull(code, "Faultcode cannot be null");
}
/**
* Set a default faultstring to use in the event that error detail is off,
* or no specific message is obtained.
*
* @param message default faultstring
*/
public void setFaultString(@Nullable final String message) {
ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
faultString = StringSupport.trimOrNull(message);
}
/** {@inheritDoc} */
@Override
protected boolean doPreExecute(@Nonnull final ProfileRequestContext profileRequestContext) {
ComponentSupport.ifNotInitializedThrowUninitializedComponentException(this);
detailedErrors = detailedErrorsCondition.apply(profileRequestContext);
log.debug("{} Detailed errors are {}", getLogPrefix(), detailedErrors ? "enabled" : "disabled");
if (profileRequestContext.getOutboundMessageContext() != null && nullifyOutboundMessage) {
profileRequestContext.getOutboundMessageContext().setMessage(null);
} else {
profileRequestContext.setOutboundMessageContext(new MessageContext © 2015 - 2025 Weber Informatics LLC | Privacy Policy