use of org.apache.cxf.headers.Header in project cxf by apache.
the class BinarySecurityTokenInterceptor 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 (WSS4JConstants.BINARY_TOKEN_LN.equals(child.getLocalName()) && WSS4JConstants.WSSE_NS.equals(child.getNamespaceURI())) {
try {
List<WSSecurityEngineResult> bstResults = processToken(child, message);
if (bstResults != null) {
List<WSHandlerResult> results = CastUtils.cast((List<?>) message.get(WSHandlerConstants.RECV_RESULTS));
if (results == null) {
results = new ArrayList<>();
message.put(WSHandlerConstants.RECV_RESULTS, results);
}
WSHandlerResult rResult = new WSHandlerResult(null, bstResults, Collections.singletonMap(WSConstants.BST, bstResults));
results.add(0, rResult);
assertTokens(message);
Principal principal = (Principal) bstResults.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 WSDiscoveryClient method addAddressing.
private void addAddressing(BindingProvider p, boolean addSeq, String action) {
AddressingProperties addrProperties = new AddressingProperties();
if (action != null) {
AttributedURIType act = new AttributedURIType();
act.setValue(action);
addrProperties.setAction(act);
}
if (adHoc) {
EndpointReferenceType to = new EndpointReferenceType();
addrProperties.exposeAs(version.getAddressingNamespace());
AttributedURIType epr = new AttributedURIType();
epr.setValue(version.getToAddress());
to.setAddress(epr);
addrProperties.setTo(to);
if (addSeq) {
AppSequenceType s = new AppSequenceType();
s.setInstanceId(instanceId);
s.setMessageNumber(msgId.getAndIncrement());
JAXBElement<AppSequenceType> seq = new ObjectFactory().createAppSequence(s);
Header h = new Header(seq.getName(), seq, new JAXBDataBinding(getJAXBContext()));
List<Header> headers = new ArrayList<>();
headers.add(h);
p.getRequestContext().put(Header.HEADER_LIST, headers);
}
} else {
addrProperties.exposeAs(version.getAddressingNamespace());
}
p.getRequestContext().put(JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES, addrProperties);
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class SoapHeaderInterceptor method validateHeader.
private void validateHeader(final SoapMessage message, MessagePartInfo mpi, Schema schema) {
Header param = findHeader(message, mpi);
if (param != null && param.getDataBinding() == null) {
Node source = (Node) param.getObject();
if (!(source instanceof Element)) {
return;
}
if (schema != null) {
final Element el = (Element) source;
DOMSource ds = new DOMSource(el);
try {
Validator v = schema.newValidator();
ErrorHandler errorHandler = new ErrorHandler() {
public void warning(SAXParseException exception) throws SAXException {
}
public void error(SAXParseException exception) throws SAXException {
String msg = exception.getMessage();
if (msg.contains(el.getLocalName()) && (msg.contains(":" + message.getVersion().getAttrNameRole()) || msg.contains(":" + message.getVersion().getAttrNameMustUnderstand()))) {
return;
}
throw exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}
};
v.setErrorHandler(errorHandler);
v.validate(ds);
} catch (SAXException e) {
throw new Fault("COULD_NOT_VALIDATE_SOAP_HEADER_CAUSED_BY", LOG, e, e.getClass().getCanonicalName(), e.getMessage());
} catch (IOException e) {
throw new Fault("COULD_NOT_VALIDATE_SOAP_HEADER_CAUSED_BY", LOG, e, e.getClass().getCanonicalName(), e.getMessage());
}
}
}
}
use of org.apache.cxf.headers.Header 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());
}
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class SAAJInInterceptorTest method testHandleHeader.
@Test
public void testHandleHeader() {
try {
prepareSoapMessage("../test-soap-header.xml");
} catch (IOException ioe) {
fail("Failed in creating soap message");
}
staxIntc.handleMessage(soapMessage);
rhi.handleMessage(soapMessage);
sbi.handleMessage(soapMessage);
saajIntc.handleMessage(soapMessage);
// check the xmlReader should be placed on the first entry of the body
// element
XMLStreamReader xmlReader = soapMessage.getContent(XMLStreamReader.class);
assertEquals("check the first entry of body", "itinerary", xmlReader.getLocalName());
List<Header> eleHeaders = soapMessage.getHeaders();
List<Element> headerChilds = new ArrayList<>();
Iterator<Header> iter = eleHeaders.iterator();
while (iter.hasNext()) {
Header hdr = iter.next();
if (hdr.getObject() instanceof Element) {
headerChilds.add((Element) hdr.getObject());
}
}
assertEquals(2, headerChilds.size());
}
Aggregations