use of org.apache.cxf.message.Exchange in project cxf by apache.
the class PolicyInInterceptor method handle.
protected void handle(Message msg) {
Exchange exchange = msg.getExchange();
Bus bus = exchange.getBus();
Endpoint e = exchange.getEndpoint();
if (null == e) {
LOG.fine("No endpoint.");
return;
}
EndpointInfo ei = e.getEndpointInfo();
PolicyEngine pe = bus.getExtension(PolicyEngine.class);
if (null == pe) {
return;
}
List<Interceptor<? extends Message>> interceptors = new ArrayList<Interceptor<? extends Message>>();
Collection<Assertion> assertions = new ArrayList<>();
// 1. Check overridden policy
Policy p = (Policy) msg.getContextualProperty(PolicyConstants.POLICY_OVERRIDE);
if (p != null) {
EndpointPolicyImpl endpi = new EndpointPolicyImpl(p);
EffectivePolicyImpl effectivePolicy = new EffectivePolicyImpl();
effectivePolicy.initialise(endpi, pe, true, msg);
msg.put(EffectivePolicy.class, effectivePolicy);
PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
interceptors.addAll(effectivePolicy.getInterceptors());
assertions.addAll(effectivePolicy.getChosenAlternative());
} else if (MessageUtils.isRequestor(msg)) {
// 2. Process client policy
BindingOperationInfo boi = exchange.getBindingOperationInfo();
if (boi == null) {
Conduit conduit = exchange.getConduit(msg);
EndpointPolicy ep = pe.getClientEndpointPolicy(ei, conduit, msg);
if (ep != null) {
interceptors.addAll(ep.getInterceptors(msg));
assertions.addAll(ep.getVocabulary(msg));
}
} else {
// We do not know the underlying message type yet - so we pre-emptively add interceptors
// that can deal with any resposes or faults returned to this client endpoint.
EffectivePolicy ep = pe.getEffectiveClientResponsePolicy(ei, boi, msg);
if (ep != null) {
interceptors.addAll(ep.getInterceptors());
// insert assertions of endpoint's vocabulary into message
if (ep.getPolicy() != null) {
msg.put(AssertionInfoMap.class, new AssertionInfoMap(ep.getPolicy()));
msg.getInterceptorChain().add(PolicyVerificationInInterceptor.INSTANCE);
}
}
}
} else {
// 3. Process server policy
Destination destination = exchange.getDestination();
// We do not know the underlying message type yet - so we pre-emptively add interceptors
// that can deal with any messages to this endpoint
EndpointPolicy ep = pe.getServerEndpointPolicy(ei, destination, msg);
if (ep != null) {
interceptors.addAll(ep.getInterceptors(msg));
assertions.addAll(ep.getVocabulary(msg));
}
}
// add interceptors into message chain
for (Interceptor<? extends Message> i : interceptors) {
msg.getInterceptorChain().add(i);
}
// Insert assertions of endpoint's vocabulary into message
if (!assertions.isEmpty()) {
msg.put(AssertionInfoMap.class, new AssertionInfoMap(assertions));
msg.getInterceptorChain().add(PolicyVerificationInInterceptor.INSTANCE);
}
}
use of org.apache.cxf.message.Exchange in project cxf by apache.
the class PolicyOutInterceptor method handle.
protected void handle(Message msg) {
Exchange exchange = msg.getExchange();
Bus bus = exchange.getBus();
BindingOperationInfo boi = exchange.getBindingOperationInfo();
if (null == boi) {
LOG.fine("No binding operation info.");
return;
}
Endpoint e = exchange.getEndpoint();
if (null == e) {
LOG.fine("No endpoint.");
return;
}
EndpointInfo ei = e.getEndpointInfo();
PolicyEngine pe = bus.getExtension(PolicyEngine.class);
if (null == pe) {
return;
}
Collection<Assertion> assertions = new ArrayList<>();
// 1. Check overridden policy
Policy p = (Policy) msg.getContextualProperty(PolicyConstants.POLICY_OVERRIDE);
if (p != null) {
EndpointPolicyImpl endpi = new EndpointPolicyImpl(p);
EffectivePolicyImpl effectivePolicy = new EffectivePolicyImpl();
effectivePolicy.initialise(endpi, pe, false, msg);
msg.put(EffectivePolicy.class, effectivePolicy);
PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
addInterceptors(effectivePolicy.getInterceptors(), msg);
assertions.addAll(effectivePolicy.getChosenAlternative());
} else if (MessageUtils.isRequestor(msg)) {
// 2. Process client policy
Conduit conduit = exchange.getConduit(msg);
// add the required interceptors
EffectivePolicy effectivePolicy = pe.getEffectiveClientRequestPolicy(ei, boi, conduit, msg);
msg.put(EffectivePolicy.class, effectivePolicy);
if (effectivePolicy != null) {
PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
addInterceptors(effectivePolicy.getInterceptors(), msg);
assertions.addAll(effectivePolicy.getChosenAlternative());
}
} else {
// 3. Process server policy
Destination destination = exchange.getDestination();
List<List<Assertion>> incoming = CastUtils.cast((List<?>) exchange.get("ws-policy.validated.alternatives"));
EffectivePolicy effectivePolicy = pe.getEffectiveServerResponsePolicy(ei, boi, destination, incoming, msg);
msg.put(EffectivePolicy.class, effectivePolicy);
if (effectivePolicy != null) {
PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
addInterceptors(effectivePolicy.getInterceptors(), msg);
assertions.addAll(effectivePolicy.getChosenAlternative());
}
}
// insert assertions of endpoint's fault vocabulary into message
if (null != assertions && !assertions.isEmpty()) {
if (LOG.isLoggable(Level.FINEST)) {
StringBuilder buf = new StringBuilder();
buf.append("Chosen alternative: ");
String nl = SystemPropertyAction.getProperty("line.separator");
buf.append(nl);
for (Assertion a : assertions) {
PolicyUtils.printPolicyComponent(a, buf, 1);
}
LOG.finest(buf.toString());
}
msg.put(AssertionInfoMap.class, new AssertionInfoMap(assertions));
msg.getInterceptorChain().add(PolicyVerificationOutInterceptor.INSTANCE);
}
}
use of org.apache.cxf.message.Exchange in project cxf by apache.
the class PolicyVerificationInInterceptor method handle.
/**
* Determines the effective policy, and checks if one of its alternatives
* is supported.
*
* @param message
* @throws PolicyException if none of the alternatives is supported
*/
protected void handle(Message message) {
AssertionInfoMap aim = message.get(AssertionInfoMap.class);
if (null == aim) {
return;
}
Exchange exchange = message.getExchange();
BindingOperationInfo boi = exchange.getBindingOperationInfo();
if (null == boi) {
LOG.fine("No binding operation info.");
return;
}
Endpoint e = exchange.getEndpoint();
if (null == e) {
LOG.fine("No endpoint.");
return;
}
Bus bus = exchange.getBus();
PolicyEngine pe = bus.getExtension(PolicyEngine.class);
if (null == pe) {
return;
}
if (MessageUtils.isPartialResponse(message)) {
LOG.fine("Not verifying policies on inbound partial response.");
return;
}
getTransportAssertions(message);
EffectivePolicy effectivePolicy = message.get(EffectivePolicy.class);
if (effectivePolicy == null) {
EndpointInfo ei = e.getEndpointInfo();
if (MessageUtils.isRequestor(message)) {
effectivePolicy = pe.getEffectiveClientResponsePolicy(ei, boi, message);
} else {
effectivePolicy = pe.getEffectiveServerRequestPolicy(ei, boi, message);
}
}
try {
List<List<Assertion>> usedAlternatives = aim.checkEffectivePolicy(effectivePolicy.getPolicy());
if (usedAlternatives != null && !usedAlternatives.isEmpty() && message.getExchange() != null) {
message.getExchange().put("ws-policy.validated.alternatives", usedAlternatives);
}
} catch (PolicyException ex) {
LOG.log(Level.SEVERE, "Inbound policy verification failed: " + ex.getMessage());
// exception to pass jaxws2.2 tests
if (ex.getMessage().indexOf("Addressing") > -1) {
throw new Fault("A required header representing a Message Addressing Property " + "is not present", LOG).setFaultCode(new QName("http://www.w3.org/2005/08/addressing", "MessageAddressingHeaderRequired"));
}
throw ex;
}
LOG.fine("Verified policies for inbound message.");
}
use of org.apache.cxf.message.Exchange in project cxf by apache.
the class ServerPolicyOutFaultInterceptor method handle.
protected void handle(Message msg) {
if (MessageUtils.isRequestor(msg)) {
LOG.fine("Is a requestor.");
return;
}
Exchange exchange = msg.getExchange();
assert null != exchange;
BindingOperationInfo boi = exchange.getBindingOperationInfo();
if (null == boi) {
LOG.fine("No binding operation info.");
return;
}
Endpoint e = exchange.getEndpoint();
if (null == e) {
LOG.fine("No endpoint.");
return;
}
EndpointInfo ei = e.getEndpointInfo();
Bus bus = exchange.getBus();
PolicyEngine pe = bus.getExtension(PolicyEngine.class);
if (null == pe) {
return;
}
Destination destination = exchange.getDestination();
Exception ex = exchange.get(Exception.class);
List<Interceptor<? extends Message>> faultInterceptors = new ArrayList<Interceptor<? extends Message>>();
Collection<Assertion> assertions = new ArrayList<>();
// 1. Check overridden policy
Policy p = (Policy) msg.getContextualProperty(PolicyConstants.POLICY_OVERRIDE);
if (p != null) {
EndpointPolicyImpl endpi = new EndpointPolicyImpl(p);
EffectivePolicyImpl effectivePolicy = new EffectivePolicyImpl();
effectivePolicy.initialise(endpi, pe, false, true, msg);
PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
faultInterceptors.addAll(effectivePolicy.getInterceptors());
assertions.addAll(effectivePolicy.getChosenAlternative());
} else {
// 2. Process effective server policy
BindingFaultInfo bfi = getBindingFaultInfo(msg, ex, boi);
if (bfi == null && msg.get(FaultMode.class) != FaultMode.UNCHECKED_APPLICATION_FAULT && msg.get(FaultMode.class) != FaultMode.CHECKED_APPLICATION_FAULT) {
return;
}
EffectivePolicy effectivePolicy = pe.getEffectiveServerFaultPolicy(ei, boi, bfi, destination, msg);
if (effectivePolicy != null) {
faultInterceptors.addAll(effectivePolicy.getInterceptors());
assertions.addAll(effectivePolicy.getChosenAlternative());
}
}
// add interceptors into message chain
for (Interceptor<? extends Message> oi : faultInterceptors) {
msg.getInterceptorChain().add(oi);
LOG.log(Level.FINE, "Added interceptor of type {0}", oi.getClass().getSimpleName());
}
// insert assertions of the chosen alternative into the message
if (null != assertions && !assertions.isEmpty()) {
msg.put(AssertionInfoMap.class, new AssertionInfoMap(assertions));
}
}
use of org.apache.cxf.message.Exchange in project cxf by apache.
the class WSPolicyFeature method createMessage.
private MessageImpl createMessage(Endpoint e, Bus b) {
MessageImpl m = new MessageImpl();
Exchange ex = new ExchangeImpl();
m.setExchange(ex);
ex.put(Endpoint.class, e);
ex.put(Bus.class, b);
ex.put(Service.class, e.getService());
return m;
}
Aggregations