use of org.apache.cxf.headers.Header in project cxf by apache.
the class MAPCodec method unmarshalMAPs.
/**
* Decode the MAPs from protocol-specific headers.
*
* @param message the SOAP message
* @param the decoded MAPs
* @exception SOAPFaultException if decoded MAPs are invalid
*/
public AddressingProperties unmarshalMAPs(SoapMessage message) {
// REVISIT generate MessageAddressingHeaderRequired fault if an
// expected header is missing
AddressingProperties maps = null;
Unmarshaller unmarshaller = null;
try {
List<Header> header = message.getHeaders();
if (header != null) {
LOG.log(Level.FINE, "Inbound WS-Addressing headers");
Set<Element> referenceParameterHeaders = null;
QName invalidCardinalityQName = null;
Iterator<Header> iter = header.iterator();
while (iter.hasNext()) {
Header hdr = iter.next();
if (hdr.getObject() instanceof Element) {
Element headerElement = (Element) hdr.getObject();
String headerURI = headerElement.getNamespaceURI();
// headers.
if (org.apache.cxf.ws.addressing.VersionTransformer.isSupported(headerURI)) {
if (unmarshaller == null) {
JAXBContext jaxbContext = org.apache.cxf.ws.addressing.VersionTransformer.getExposedJAXBContext(headerURI);
unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(null);
}
if (maps == null) {
maps = new AddressingProperties();
maps.exposeAs(headerURI);
}
String localName = headerElement.getLocalName();
if (Names.WSA_MESSAGEID_NAME.equals(localName)) {
invalidCardinalityQName = maps.getMessageID() != null ? Names.WSA_MESSAGEID_QNAME : null;
maps.setMessageID(decodeAsNative(headerURI, AttributedURIType.class, headerElement, unmarshaller));
} else if (Names.WSA_TO_NAME.equals(localName)) {
invalidCardinalityQName = maps.getTo() != null ? Names.WSA_TO_QNAME : null;
AttributedURIType addr = decodeAsNative(headerURI, AttributedURIType.class, headerElement, unmarshaller);
maps.setTo(EndpointReferenceUtils.getEndpointReference(addr));
} else if (Names.WSA_FROM_NAME.equals(localName)) {
invalidCardinalityQName = maps.getFrom() != null ? Names.WSA_FROM_QNAME : null;
maps.setFrom(decodeAsNative(headerURI, EndpointReferenceType.class, headerElement, unmarshaller));
} else if (Names.WSA_REPLYTO_NAME.equals(localName)) {
invalidCardinalityQName = maps.getReplyTo() != null ? Names.WSA_REPLYTO_QNAME : null;
maps.setReplyTo(decodeAsNative(headerURI, EndpointReferenceType.class, headerElement, unmarshaller));
} else if (Names.WSA_FAULTTO_NAME.equals(localName)) {
invalidCardinalityQName = maps.getFaultTo() != null ? Names.WSA_FAULTTO_QNAME : null;
maps.setFaultTo(decodeAsNative(headerURI, EndpointReferenceType.class, headerElement, unmarshaller));
} else if (Names.WSA_RELATESTO_NAME.equals(localName)) {
maps.setRelatesTo(decodeAsNative(headerURI, RelatesToType.class, headerElement, unmarshaller));
} else if (Names.WSA_ACTION_NAME.equals(localName)) {
invalidCardinalityQName = maps.getAction() != null ? Names.WSA_ACTION_QNAME : null;
maps.setAction(decodeAsNative(headerURI, AttributedURIType.class, headerElement, unmarshaller));
}
} else if (null != headerElement.getAttribute(IS_REFERENCE_PARAM_ATTR_NAME)) {
if (null == referenceParameterHeaders) {
referenceParameterHeaders = new HashSet<>();
}
referenceParameterHeaders.add(headerElement);
} else if (headerURI.contains(Names.WSA_NAMESPACE_PATTERN)) {
LOG.log(Level.WARNING, "UNSUPPORTED_VERSION_MSG", headerURI);
}
}
}
if (maps != null && !MessageUtils.isRequestor(message) && maps.getReplyTo() == null) {
// per spec, if unspecified, replyTo is anon
AttributedURIType address = ContextUtils.getAttributedURI(Names.WSA_ANONYMOUS_ADDRESS);
EndpointReferenceType replyTo = ContextUtils.WSA_OBJECT_FACTORY.createEndpointReferenceType();
replyTo.setAddress(address);
maps.setReplyTo(replyTo);
}
if (null != referenceParameterHeaders && null != maps) {
decodeReferenceParameters(referenceParameterHeaders, maps, unmarshaller);
}
if (invalidCardinalityQName != null) {
storeInvalidCardinalityFault(message, invalidCardinalityQName);
}
}
} catch (JAXBException je) {
LOG.log(Level.WARNING, "SOAP_HEADER_DECODE_FAILURE_MSG", je);
} finally {
JAXBUtils.closeUnmarshaller(unmarshaller);
}
return maps;
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class DecoupledFaultHandlerTest method testOnewayFault.
@Test
public void testOnewayFault() {
DecoupledFaultHandler handler = new DecoupledFaultHandler() {
protected Destination createDecoupledDestination(Exchange exchange, EndpointReferenceType epr) {
assertEquals("http://bar", epr.getAddress().getValue());
return EasyMock.createMock(Destination.class);
}
};
SoapMessage message = new SoapMessage(new MessageImpl());
QName qname = new QName("http://cxf.apache.org/mustunderstand", "TestMU");
message.getHeaders().add(new Header(qname, new Object()));
AddressingProperties maps = new AddressingProperties();
EndpointReferenceType faultTo = new EndpointReferenceType();
faultTo.setAddress(new AttributedURIType());
faultTo.getAddress().setValue("http://bar");
maps.setFaultTo(faultTo);
message.put(ContextUtils.getMAPProperty(false, false, false), maps);
Exchange exchange = new ExchangeImpl();
message.setExchange(exchange);
exchange.setInMessage(message);
exchange.setOneWay(true);
handler.handleFault(message);
assertTrue(message.getHeaders().isEmpty());
assertFalse(exchange.isOneWay());
assertSame(message, exchange.getOutMessage());
assertNotNull(exchange.getDestination());
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class SamlTokenInterceptor method processToken.
protected void processToken(SoapMessage message) {
Header h = findSecurityHeader(message, false);
if (h == null) {
return;
}
Element el = (Element) h.getObject();
Element child = DOMUtils.getFirstElement(el);
while (child != null) {
if ("Assertion".equals(child.getLocalName()) && (WSS4JConstants.SAML_NS.equals(child.getNamespaceURI()) || WSS4JConstants.SAML2_NS.equals(child.getNamespaceURI()))) {
try {
List<WSSecurityEngineResult> samlResults = processToken(child, message);
if (samlResults != null) {
List<WSHandlerResult> results = CastUtils.cast((List<?>) message.get(WSHandlerConstants.RECV_RESULTS));
if (results == null) {
results = new ArrayList<>();
message.put(WSHandlerConstants.RECV_RESULTS, results);
}
boolean signed = false;
for (WSSecurityEngineResult result : samlResults) {
SamlAssertionWrapper wrapper = (SamlAssertionWrapper) result.get(WSSecurityEngineResult.TAG_SAML_ASSERTION);
if (wrapper.isSigned()) {
signed = true;
break;
}
}
assertTokens(message, SPConstants.SAML_TOKEN, signed);
Integer key = WSConstants.ST_UNSIGNED;
if (signed) {
key = WSConstants.ST_SIGNED;
}
WSHandlerResult rResult = new WSHandlerResult(null, samlResults, Collections.singletonMap(key, samlResults));
results.add(0, rResult);
// Check version against policy
AssertionInfoMap aim = message.get(AssertionInfoMap.class);
for (AssertionInfo ai : PolicyUtils.getAllAssertionsByLocalname(aim, SPConstants.SAML_TOKEN)) {
SamlToken samlToken = (SamlToken) ai.getAssertion();
for (WSSecurityEngineResult result : samlResults) {
SamlAssertionWrapper assertionWrapper = (SamlAssertionWrapper) result.get(WSSecurityEngineResult.TAG_SAML_ASSERTION);
if (!checkVersion(aim, samlToken, assertionWrapper)) {
ai.setNotAsserted("Wrong SAML Version");
}
TLSSessionInfo tlsInfo = message.get(TLSSessionInfo.class);
Certificate[] tlsCerts = null;
if (tlsInfo != null) {
tlsCerts = tlsInfo.getPeerCertificates();
}
if (!DOMSAMLUtil.checkHolderOfKey(assertionWrapper, null, tlsCerts)) {
ai.setNotAsserted("Assertion fails holder-of-key requirements");
continue;
}
if (!DOMSAMLUtil.checkSenderVouches(assertionWrapper, tlsCerts, null, null)) {
ai.setNotAsserted("Assertion fails sender-vouches requirements");
continue;
}
}
}
if (signed) {
Principal principal = (Principal) samlResults.get(0).get(WSSecurityEngineResult.TAG_PRINCIPAL);
SecurityContext sc = message.get(SecurityContext.class);
if (sc == null || sc.getUserPrincipal() == null) {
message.put(SecurityContext.class, new DefaultSecurityContext(principal, null));
}
}
}
} catch (WSSecurityException ex) {
throw WSS4JUtils.createSoapFault(message, message.getVersion(), ex);
}
}
child = DOMUtils.getNextElement(child);
}
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class UsernameTokenInterceptor method addToken.
protected void addToken(SoapMessage message) {
UsernameToken tok = assertTokens(message);
Header h = findSecurityHeader(message, true);
Element el = (Element) h.getObject();
Document doc = el.getOwnerDocument();
WSSecUsernameToken utBuilder = addUsernameToken(message, doc, tok);
if (utBuilder == null) {
AssertionInfoMap aim = message.get(AssertionInfoMap.class);
Collection<AssertionInfo> ais = PolicyUtils.getAllAssertionsByLocalname(aim, SPConstants.USERNAME_TOKEN);
for (AssertionInfo ai : ais) {
if (ai.isAsserted()) {
ai.setAsserted(false);
}
}
return;
}
utBuilder.prepare();
el.appendChild(utBuilder.getUsernameTokenElement());
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class RMSoapInInterceptor method unmarshalRMProperties.
/**
* Decode the RM properties from protocol-specific headers.
*
* @param message the SOAP message
* @return the RM properties
*/
public RMProperties unmarshalRMProperties(SoapMessage message) {
RMProperties rmps = (RMProperties) message.get(RMContextUtils.getRMPropertiesKey(false));
if (rmps == null) {
rmps = new RMProperties();
}
List<Header> headers = message.getHeaders();
if (headers != null) {
decodeHeaders(message, headers, rmps);
}
return rmps;
}
Aggregations