use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class WSS4JInInterceptor method handleMessageInternal.
@SuppressWarnings("deprecation")
private void handleMessageInternal(SoapMessage msg) throws Fault {
boolean utWithCallbacks = MessageUtils.getContextualBoolean(msg, SecurityConstants.VALIDATE_TOKEN, true);
translateProperties(msg);
RequestData reqData = new CXFRequestData();
WSSConfig config = (WSSConfig) msg.getContextualProperty(WSSConfig.class.getName());
WSSecurityEngine engine;
if (config != null) {
engine = new WSSecurityEngine();
engine.setWssConfig(config);
} else {
engine = getSecurityEngine(utWithCallbacks);
if (engine == null) {
engine = new WSSecurityEngine();
}
config = engine.getWssConfig();
}
reqData.setWssConfig(config);
reqData.setEncryptionSerializer(new StaxSerializer());
// Add Audience Restrictions for SAML
reqData.setAudienceRestrictions(SAMLUtils.getAudienceRestrictions(msg, true));
SOAPMessage doc = getSOAPMessage(msg);
boolean doDebug = LOG.isLoggable(Level.FINE);
SoapVersion version = msg.getVersion();
if (doDebug) {
LOG.fine("WSS4JInInterceptor: enter handleMessage()");
}
/*
* The overall try, just to have a finally at the end to perform some
* housekeeping.
*/
try {
reqData.setMsgContext(msg);
reqData.setAttachmentCallbackHandler(new AttachmentCallbackHandler(msg));
setAlgorithmSuites(msg, reqData);
reqData.setCallbackHandler(getCallback(reqData, utWithCallbacks));
computeAction(msg, reqData);
String action = getAction(msg, version);
List<Integer> actions = WSSecurityUtil.decodeAction(action);
String actor = (String) getOption(ConfigurationConstants.ACTOR);
if (actor == null) {
actor = (String) msg.getContextualProperty(SecurityConstants.ACTOR);
}
reqData.setActor(actor);
// Configure replay caching
configureReplayCaches(reqData, actions, msg);
TLSSessionInfo tlsInfo = msg.get(TLSSessionInfo.class);
if (tlsInfo != null) {
Certificate[] tlsCerts = tlsInfo.getPeerCertificates();
reqData.setTlsCerts(tlsCerts);
}
/*
* Get and check the Signature specific parameters first because
* they may be used for encryption too.
*/
doReceiverAction(actions, reqData);
// explicitly specified by the user)
if (getString(ConfigurationConstants.EXPAND_XOP_INCLUDE_FOR_SIGNATURE, msg) == null && getString(ConfigurationConstants.EXPAND_XOP_INCLUDE, msg) == null) {
reqData.setExpandXopInclude(AttachmentUtil.isMtomEnabled(msg));
}
/*get chance to check msg context enableRevocation setting
*when use policy based ws-security where the WSHandler configuration
*isn't available
*/
boolean enableRevocation = reqData.isRevocationEnabled() || MessageUtils.isTrue(SecurityUtils.getSecurityPropertyValue(SecurityConstants.ENABLE_REVOCATION, msg));
reqData.setEnableRevocation(enableRevocation);
Element soapBody = SAAJUtils.getBody(doc);
if (soapBody != null) {
engine.setCallbackLookup(new CXFCallbackLookup(soapBody.getOwnerDocument(), soapBody));
}
Element elem = WSSecurityUtil.getSecurityHeader(doc.getSOAPHeader(), actor, version.getVersion() != 1.1);
elem = (Element) DOMUtils.getDomElement(elem);
Node originalNode = null;
if (elem != null) {
originalNode = elem.cloneNode(true);
}
WSHandlerResult wsResult = engine.processSecurityHeader(elem, reqData);
importNewDomToSAAJ(doc, elem, originalNode, wsResult);
Element header = SAAJUtils.getHeader(doc);
Element body = SAAJUtils.getBody(doc);
header = (Element) DOMUtils.getDomElement(header);
body = (Element) DOMUtils.getDomElement(body);
if (!(wsResult.getResults() == null || wsResult.getResults().isEmpty())) {
// security header found
if (reqData.isEnableSignatureConfirmation()) {
checkSignatureConfirmation(reqData, wsResult);
}
checkActions(msg, reqData, wsResult.getResults(), actions, SAAJUtils.getBody(doc));
doResults(msg, actor, header, body, wsResult, utWithCallbacks);
} else {
// no security header found
if (doc.getSOAPPart().getEnvelope().getBody().hasFault() && isRequestor(msg)) {
LOG.warning("The request is a SOAP Fault, but it is not secured");
// We allow lax action matching here for backwards compatibility
// with manually configured WSS4JInInterceptors that previously
// allowed faults to pass through even if their actions aren't
// a strict match against those configured. In the WS-SP case,
// we will want to still call doResults as it handles asserting
// certain assertions that do not require a WS-S header such as
// a sp:TransportBinding assertion. In the case of WS-SP,
// the unasserted assertions will provide confirmation that
// security was not sufficient.
// checkActions(msg, reqData, wsResult, actions);
doResults(msg, actor, header, body, wsResult, utWithCallbacks);
} else {
checkActions(msg, reqData, wsResult.getResults(), actions, SAAJUtils.getBody(doc));
doResults(msg, actor, header, body, wsResult, utWithCallbacks);
}
}
if (SAAJUtils.getBody(doc) != null) {
advanceBody(msg, body);
}
SAAJInInterceptor.replaceHeaders(doc, msg);
if (doDebug) {
LOG.fine("WSS4JInInterceptor: exit handleMessage()");
}
msg.put(SECURITY_PROCESSED, Boolean.TRUE);
} catch (WSSecurityException e) {
throw WSS4JUtils.createSoapFault(msg, version, e);
} catch (XMLStreamException e) {
throw new SoapFault(new Message("STAX_EX", LOG), e, version.getSender());
} catch (SOAPException e) {
throw new SoapFault(new Message("SAAJ_EX", LOG), e, version.getSender());
} finally {
reqData = null;
}
}
use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class ReadHeadersInterceptor method handleMessage.
// CHECKSTYLE:OFF MethodLength
public void handleMessage(SoapMessage message) {
if (isGET(message)) {
LOG.fine("ReadHeadersInterceptor skipped in HTTP GET method");
return;
}
/*
* Reject OPTIONS, and any other noise that is not allowed in SOAP.
*/
final String verb = (String) message.get(org.apache.cxf.message.Message.HTTP_REQUEST_METHOD);
if (verb != null && !"POST".equals(verb)) {
Fault formula405 = new Fault("HTTP verb was not GET or POST", LOG);
formula405.setStatusCode(405);
throw formula405;
}
XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
boolean closeNeeded = false;
if (xmlReader == null) {
InputStream in = message.getContent(InputStream.class);
if (in == null) {
throw new RuntimeException("Can't find input stream in message");
}
xmlReader = StaxUtils.createXMLStreamReader(in);
closeNeeded = true;
}
try {
if (xmlReader.getEventType() == XMLStreamConstants.START_ELEMENT || xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
SoapVersion soapVersion = readVersion(xmlReader, message);
if (soapVersion == Soap12.getInstance() && version == Soap11.getInstance()) {
message.setVersion(version);
throw new SoapFault(new Message("INVALID_11_VERSION", LOG), version.getVersionMismatch());
}
XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, message.getVersion().getBody());
Node nd = message.getContent(Node.class);
W3CDOMStreamWriter writer = message.get(W3CDOMStreamWriter.class);
Document doc = null;
if (writer != null) {
StaxUtils.copy(filteredReader, writer);
doc = writer.getDocument();
} else if (nd instanceof Document) {
doc = (Document) nd;
StaxUtils.readDocElements(doc, doc, filteredReader, false, false);
} else {
final boolean addNC = MessageUtils.getContextualBoolean(message, "org.apache.cxf.binding.soap.addNamespaceContext", false);
Map<String, String> bodyNC = addNC ? new HashMap<String, String>() : null;
if (addNC) {
// add the Envelope-Level declarations
addCurrentNamespaceDecls(xmlReader, bodyNC);
}
HeadersProcessor processor = new HeadersProcessor(soapVersion);
doc = processor.process(filteredReader);
if (doc != null) {
message.setContent(Node.class, doc);
} else {
message.put(ENVELOPE_EVENTS, processor.getEnvAttributeAndNamespaceEvents());
message.put(BODY_EVENTS, processor.getBodyAttributeAndNamespaceEvents());
message.put(ENVELOPE_PREFIX, processor.getEnvelopePrefix());
message.put(BODY_PREFIX, processor.getBodyPrefix());
}
if (addNC) {
// add the Body-level declarations
addCurrentNamespaceDecls(xmlReader, bodyNC);
message.put("soap.body.ns.context", bodyNC);
}
}
// Find header
if (doc != null) {
Element element = doc.getDocumentElement();
QName header = soapVersion.getHeader();
List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(element, header.getNamespaceURI(), header.getLocalPart());
for (Element elem : elemList) {
Element hel = DOMUtils.getFirstElement(elem);
while (hel != null) {
// which otherwise would be lost.
if (elem.hasAttributes()) {
NamedNodeMap nnp = elem.getAttributes();
for (int ct = 0; ct < nnp.getLength(); ct++) {
Node attr = nnp.item(ct);
Node headerAttrNode = hel.hasAttributes() ? hel.getAttributes().getNamedItemNS(attr.getNamespaceURI(), attr.getLocalName()) : null;
if (headerAttrNode == null) {
Attr attribute = hel.getOwnerDocument().createAttributeNS(attr.getNamespaceURI(), attr.getNodeName());
attribute.setNodeValue(attr.getNodeValue());
hel.setAttributeNodeNS(attribute);
}
}
}
HeaderProcessor p = bus == null ? null : bus.getExtension(HeaderManager.class).getHeaderProcessor(hel.getNamespaceURI());
Object obj;
DataBinding dataBinding = null;
if (p == null || p.getDataBinding() == null) {
obj = hel;
} else {
dataBinding = p.getDataBinding();
DataReader<Node> dataReader = dataBinding.createReader(Node.class);
dataReader.setAttachments(message.getAttachments());
dataReader.setProperty(DataReader.ENDPOINT, message.getExchange().getEndpoint());
dataReader.setProperty(Message.class.getName(), message);
obj = dataReader.read(hel);
}
SoapHeader shead = new SoapHeader(new QName(hel.getNamespaceURI(), hel.getLocalName()), obj, dataBinding);
String mu = hel.getAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
String act = hel.getAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
if (!StringUtils.isEmpty(act)) {
shead.setActor(act);
}
shead.setMustUnderstand(Boolean.valueOf(mu) || "1".equals(mu));
// mark header as inbound header.(for distinguishing between the direction to
// avoid piggybacking of headers from request->server->response.
shead.setDirection(SoapHeader.Direction.DIRECTION_IN);
message.getHeaders().add(shead);
hel = DOMUtils.getNextElement(hel);
}
}
}
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message)) {
message.getInterceptorChain().add(new CheckClosingTagsInterceptor());
}
}
} catch (XMLStreamException e) {
throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e, message.getVersion().getSender());
} finally {
if (closeNeeded) {
try {
StaxUtils.close(xmlReader);
} catch (XMLStreamException e) {
throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e, message.getVersion().getSender());
}
}
}
}
use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class ReadHeadersInterceptor method readVersion.
public static SoapVersion readVersion(XMLStreamReader xmlReader, SoapMessage message) {
String ns = xmlReader.getNamespaceURI();
String lcname = xmlReader.getLocalName();
if (ns == null || "".equals(ns)) {
throw new SoapFault(new Message("NO_NAMESPACE", LOG, lcname), Soap11.getInstance().getVersionMismatch());
}
SoapVersion soapVersion = SoapVersionFactory.getInstance().getSoapVersion(ns);
if (soapVersion == null) {
throw new SoapFault(new Message("INVALID_VERSION", LOG, ns, lcname), Soap11.getInstance().getVersionMismatch());
}
if (!"Envelope".equals(lcname)) {
throw new SoapFault(new Message("INVALID_ENVELOPE", LOG, lcname), soapVersion.getSender());
}
message.setVersion(soapVersion);
return soapVersion;
}
use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class SoapOutInterceptor method handleHeaderPart.
private boolean handleHeaderPart(boolean preexistingHeaders, SoapMessage message, String soapPrefix) {
// add MessagePart to soapHeader if necessary
boolean endedHeader = false;
Exchange exchange = message.getExchange();
BindingOperationInfo bop = exchange.getBindingOperationInfo();
if (bop == null) {
return endedHeader;
}
XMLStreamWriter xtw = message.getContent(XMLStreamWriter.class);
boolean startedHeader = false;
BindingOperationInfo unwrappedOp = bop;
if (bop.isUnwrapped()) {
unwrappedOp = bop.getWrappedOperation();
}
boolean client = isRequestor(message);
BindingMessageInfo bmi = client ? unwrappedOp.getInput() : unwrappedOp.getOutput();
BindingMessageInfo wrappedBmi = client ? bop.getInput() : bop.getOutput();
if (bmi == null) {
return endedHeader;
}
if (wrappedBmi.getMessageInfo().getMessagePartsNumber() > 0) {
MessageContentsList objs = MessageContentsList.getContentsList(message);
if (objs == null) {
return endedHeader;
}
SoapVersion soapVersion = message.getVersion();
List<SoapHeaderInfo> headers = bmi.getExtensors(SoapHeaderInfo.class);
if (headers == null) {
return endedHeader;
}
for (SoapHeaderInfo header : headers) {
MessagePartInfo part = header.getPart();
if (wrappedBmi != bmi) {
part = wrappedBmi.getMessageInfo().addMessagePart(part.getName());
}
if (part.getIndex() >= objs.size()) {
// The optional out of band header is not a part of parameters of the method
continue;
}
Object arg = objs.get(part);
if (arg == null) {
continue;
}
objs.remove(part);
if (!(startedHeader || preexistingHeaders)) {
try {
xtw.writeStartElement(soapPrefix, soapVersion.getHeader().getLocalPart(), soapVersion.getNamespace());
} catch (XMLStreamException e) {
throw new SoapFault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), e, soapVersion.getSender());
}
startedHeader = true;
}
DataWriter<XMLStreamWriter> dataWriter = getDataWriter(message);
dataWriter.write(arg, header.getPart(), xtw);
}
if (startedHeader || preexistingHeaders) {
try {
xtw.writeEndElement();
endedHeader = true;
} catch (XMLStreamException e) {
throw new SoapFault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), e, soapVersion.getSender());
}
}
}
return endedHeader;
}
use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.
the class SoapOutInterceptor method writeSoapEnvelopeStart.
private void writeSoapEnvelopeStart(final SoapMessage message) {
final SoapVersion soapVersion = message.getVersion();
try {
XMLStreamWriter xtw = message.getContent(XMLStreamWriter.class);
String soapPrefix = xtw.getPrefix(soapVersion.getNamespace());
if (StringUtils.isEmpty(soapPrefix)) {
soapPrefix = "soap";
}
if (message.hasAdditionalEnvNs()) {
Map<String, String> nsMap = message.getEnvelopeNs();
for (Map.Entry<String, String> entry : nsMap.entrySet()) {
if (soapVersion.getNamespace().equals(entry.getValue())) {
soapPrefix = entry.getKey();
}
}
xtw.setPrefix(soapPrefix, soapVersion.getNamespace());
xtw.writeStartElement(soapPrefix, soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
xtw.writeNamespace(soapPrefix, soapVersion.getNamespace());
for (Map.Entry<String, String> entry : nsMap.entrySet()) {
if (!soapVersion.getNamespace().equals(entry.getValue())) {
xtw.writeNamespace(entry.getKey(), entry.getValue());
}
}
} else {
xtw.setPrefix(soapPrefix, soapVersion.getNamespace());
xtw.writeStartElement(soapPrefix, soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
String s2 = xtw.getPrefix(soapVersion.getNamespace());
if (StringUtils.isEmpty(s2) || soapPrefix.equals(s2)) {
xtw.writeNamespace(soapPrefix, soapVersion.getNamespace());
} else {
soapPrefix = s2;
}
}
boolean preexistingHeaders = message.hasHeaders();
if (preexistingHeaders) {
xtw.writeStartElement(soapPrefix, soapVersion.getHeader().getLocalPart(), soapVersion.getNamespace());
List<Header> hdrList = message.getHeaders();
for (Header header : hdrList) {
XMLStreamWriter writer = xtw;
if (xtw instanceof W3CDOMStreamWriter) {
Element nd = ((W3CDOMStreamWriter) xtw).getCurrentNode();
if (header.getObject() instanceof Element && nd.isSameNode(((Element) header.getObject()).getParentNode())) {
continue;
}
}
if (header instanceof SoapHeader) {
SoapHeader soapHeader = (SoapHeader) header;
writer = new SOAPHeaderWriter(xtw, soapHeader, soapVersion, soapPrefix);
}
DataBinding b = header.getDataBinding();
if (b == null) {
HeaderProcessor hp = bus.getExtension(HeaderManager.class).getHeaderProcessor(header.getName().getNamespaceURI());
if (hp != null) {
b = hp.getDataBinding();
}
}
if (b != null) {
MessagePartInfo part = new MessagePartInfo(header.getName(), null);
part.setConcreteName(header.getName());
b.createWriter(XMLStreamWriter.class).write(header.getObject(), part, writer);
} else {
Element node = (Element) header.getObject();
StaxUtils.copy(node, writer);
}
}
}
boolean endedHeader = handleHeaderPart(preexistingHeaders, message, soapPrefix);
if (preexistingHeaders && !endedHeader) {
xtw.writeEndElement();
}
xtw.writeStartElement(soapPrefix, soapVersion.getBody().getLocalPart(), soapVersion.getNamespace());
// Interceptors followed such as Wrapped/RPC/Doc Interceptor will write SOAP body
} catch (XMLStreamException e) {
throw new SoapFault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), e, soapVersion.getSender());
}
}
Aggregations