use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class AbstractSTSClient method cancel.
/**
* Make an "Cancel" invocation and return the response as a STSResponse Object
*/
protected STSResponse cancel(SecurityToken token) throws Exception {
createClient();
client.getRequestContext().clear();
client.getRequestContext().putAll(ctx);
client.getRequestContext().put(SecurityConstants.TOKEN, token);
BindingOperationInfo boi = findOperation("/RST/Cancel");
boolean attachTokenDirectly = true;
if (boi == null) {
attachTokenDirectly = false;
boi = findOperation("/RST/Issue");
Policy cancelPolicy = new Policy();
ExactlyOne one = new ExactlyOne();
cancelPolicy.addPolicyComponent(one);
All all = new All();
one.addPolicyComponent(all);
all.addAssertion(getAddressingAssertion());
final SecureConversationToken secureConversationToken = new SecureConversationToken(SPConstants.SPVersion.SP12, SPConstants.IncludeTokenType.INCLUDE_TOKEN_ALWAYS_TO_RECIPIENT, null, null, null, null);
secureConversationToken.setOptional(true);
class InternalProtectionToken extends ProtectionToken {
InternalProtectionToken(SPVersion version, Policy nestedPolicy) {
super(version, nestedPolicy);
super.setToken(secureConversationToken);
}
}
DefaultSymmetricBinding binding = new DefaultSymmetricBinding(SPConstants.SPVersion.SP12, new Policy());
all.addAssertion(binding);
all.addAssertion(getAddressingAssertion());
binding.setProtectionToken(new InternalProtectionToken(SPConstants.SPVersion.SP12, new Policy()));
binding.setIncludeTimestamp(true);
binding.setOnlySignEntireHeadersAndBody(true);
binding.setProtectTokens(false);
String addrNamespace = addressingNamespace;
if (addrNamespace == null) {
addrNamespace = "http://www.w3.org/2005/08/addressing";
}
List<Header> headers = new ArrayList<>();
headers.add(new Header("To", addrNamespace));
headers.add(new Header("From", addrNamespace));
headers.add(new Header("FaultTo", addrNamespace));
headers.add(new Header("ReplyTo", addrNamespace));
headers.add(new Header("Action", addrNamespace));
headers.add(new Header("MessageID", addrNamespace));
headers.add(new Header("RelatesTo", addrNamespace));
SignedParts parts = new SignedParts(SPConstants.SPVersion.SP12, true, null, headers, false);
parts.setOptional(true);
all.addPolicyComponent(parts);
client.getRequestContext().put(PolicyConstants.POLICY_OVERRIDE, cancelPolicy);
}
if (isSecureConv) {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, namespace + "/RST/SCT/Cancel");
} else {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, namespace + "/RST/Cancel");
}
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.writeStartElement("wst", "RequestSecurityToken", namespace);
writer.writeNamespace("wst", namespace);
writer.writeStartElement("wst", "RequestType", namespace);
writer.writeCharacters(namespace + "/Cancel");
writer.writeEndElement();
writer.writeStartElement("wst", "CancelTarget", namespace);
Element el;
if (attachTokenDirectly) {
el = token.getToken();
} else {
el = token.getUnattachedReference();
if (el == null) {
el = token.getAttachedReference();
}
}
StaxUtils.copy(el, writer);
writer.writeEndElement();
writer.writeEndElement();
Object[] obj = client.invoke(boi, new DOMSource(writer.getDocument().getDocumentElement()));
return new STSResponse((DOMSource) obj[0], null);
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class AbstractSTSClient method renew.
/**
* Make an "Renew" invocation and return the response as a STSResponse Object
*/
public STSResponse renew(SecurityToken tok) throws Exception {
createClient();
BindingOperationInfo boi = findOperation("/RST/Renew");
client.getRequestContext().putAll(ctx);
client.getRequestContext().remove(SecurityConstants.TOKEN_ID);
if (isSecureConv) {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, namespace + "/RST/SCT/Renew");
} else {
client.getRequestContext().put(SoapBindingConstants.SOAP_ACTION, namespace + "/RST/Renew");
}
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.writeStartElement("wst", "RequestSecurityToken", namespace);
writer.writeNamespace("wst", namespace);
if (context != null) {
writer.writeAttribute(null, "Context", context);
}
String sptt = null;
if (template != null && DOMUtils.getFirstElement(template) != null) {
if (this.useSecondaryParameters()) {
writer.writeStartElement("wst", "SecondaryParameters", namespace);
}
Element tl = DOMUtils.getFirstElement(template);
while (tl != null) {
StaxUtils.copy(tl, writer);
if ("TokenType".equals(tl.getLocalName())) {
sptt = DOMUtils.getContent(tl);
}
tl = DOMUtils.getNextElement(tl);
}
if (this.useSecondaryParameters()) {
writer.writeEndElement();
}
}
if (isSpnego) {
tokenType = STSUtils.getTokenTypeSCT(namespace);
}
if (sptt == null) {
addTokenType(writer);
}
addRequestType("/Renew", writer);
if (enableAppliesTo) {
addAppliesTo(writer, tok.getIssuerAddress());
}
if (isSecureConv || enableLifetime) {
addLifetime(writer);
}
writer.writeStartElement("wst", "RenewTarget", namespace);
StaxUtils.copy(tok.getToken(), writer);
writer.writeEndElement();
// Write out renewal semantics
writeRenewalSemantics(writer);
writer.writeEndElement();
Object[] obj = client.invoke(boi, new DOMSource(writer.getDocument().getDocumentElement()));
@SuppressWarnings("unchecked") Collection<Attachment> attachments = (Collection<Attachment>) client.getResponseContext().get(Message.ATTACHMENTS);
return new STSResponse((DOMSource) obj[0], null, null, null, attachments);
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter 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.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class SAAJOutInterceptor method handleMessage.
public void handleMessage(SoapMessage message) throws Fault {
SOAPMessage saaj = message.getContent(SOAPMessage.class);
try {
if (message.hasHeaders() && saaj != null && saaj.getSOAPPart().getEnvelope().getHeader() == null) {
// creating an empty SOAPHeader at this point in the
// pre-existing SOAPMessage avoids the <soap:body> and
// <soap:header> appearing in reverse order when the envolope
// is written to the wire
//
saaj.getSOAPPart().getEnvelope().addHeader();
}
} catch (SOAPException e) {
throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, message.getVersion().getSender());
}
if (saaj == null) {
SoapVersion version = message.getVersion();
try {
MessageFactory factory = getFactory(message);
SOAPMessage soapMessage = factory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
XMLStreamWriter origWriter = (XMLStreamWriter) message.get(ORIGINAL_XML_WRITER);
if (origWriter == null) {
origWriter = message.getContent(XMLStreamWriter.class);
}
message.put(ORIGINAL_XML_WRITER, origWriter);
W3CDOMStreamWriter writer = new SAAJStreamWriter(soapPart);
// Replace stax writer with DomStreamWriter
message.setContent(XMLStreamWriter.class, writer);
message.setContent(SOAPMessage.class, soapMessage);
message.setContent(Node.class, soapMessage.getSOAPPart());
} catch (SOAPException e) {
throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, version.getSender());
}
} else if (!message.containsKey(ORIGINAL_XML_WRITER)) {
// as the SOAPMessage already has everything in place, we do not need XMLStreamWriter to write
// anything for us, so we just set XMLStreamWriter's output to a dummy output stream.
XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
message.put(ORIGINAL_XML_WRITER, origWriter);
XMLStreamWriter dummyWriter = StaxUtils.createXMLStreamWriter(new OutputStream() {
public void write(int b) throws IOException {
}
public void write(byte[] b, int off, int len) throws IOException {
}
});
message.setContent(XMLStreamWriter.class, dummyWriter);
}
// Add a final interceptor to write the message
message.getInterceptorChain().add(SAAJOutEndingInterceptor.INSTANCE);
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class LogicalHandlerFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
if (binding.getHandlerChain().isEmpty()) {
return;
}
HandlerChainInvoker invoker = getInvoker(message);
if (invoker.getLogicalHandlers().isEmpty()) {
return;
}
XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
Document doc = DOMUtils.newDocument();
message.setContent(Node.class, doc);
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(doc);
// set up the namespace context
try {
writer.setNamespaceContext(origWriter.getNamespaceContext());
} catch (XMLStreamException ex) {
// don't set the namespaceContext
}
// Replace stax writer with DomStreamWriter
message.setContent(XMLStreamWriter.class, writer);
message.put(ORIGINAL_WRITER, origWriter);
message.getInterceptorChain().add(ending);
}
Aggregations