use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class ElementDataWriter method write.
public void write(Object obj, MessagePartInfo part, Element output) {
AegisType type = databinding.getType(part);
if (type == null) {
throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG));
}
Context context = new Context(databinding.getAegisContext());
context.setAttachments(attachments);
type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
try {
W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter(output);
ElementWriter writer = new ElementWriter(domWriter);
MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
if (type.isNillable() && type.isWriteOuter() && obj == null) {
w2.writeXsiNil();
w2.close();
return;
}
type.writeObject(obj, w2, context);
w2.close();
} catch (DatabindingException e) {
throw new RuntimeException(e);
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class JaxWsEndpointImpl method buildWsdlExtensibilities.
private void buildWsdlExtensibilities(BindingInfo bindingInfo) {
Addressing addressing = getAddressing();
if (addressing != null) {
ExtensionRegistry extensionRegistry = getBus().getExtension(WSDLManager.class).getExtensionRegistry();
try {
ExtensibilityElement el = extensionRegistry.createExtension(javax.wsdl.Binding.class, JAXWSAConstants.WSAW_USINGADDRESSING_QNAME);
el.setRequired(addressing.required());
bindingInfo.addExtensor(el);
StringBuilder polRefId = new StringBuilder(bindingInfo.getName().getLocalPart());
polRefId.append("_WSAM_Addressing_Policy");
UnknownExtensibilityElement uel = new UnknownExtensibilityElement();
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.writeStartElement("wsp", "PolicyReference", URI_POLICY_NS);
writer.writeAttribute("URI", "#" + polRefId.toString());
writer.writeEndElement();
Element pr = writer.getDocument().getDocumentElement();
uel.setElement(pr);
uel.setElementType(DOMUtils.getElementQName(pr));
bindingInfo.addExtensor(uel);
writer = new W3CDOMStreamWriter();
writer.writeStartElement("wsp", "Policy", URI_POLICY_NS);
writer.writeAttribute("wsu", URI_WSU_NS, "Id", polRefId.toString());
writer.writeStartElement("wsam", "Addressing", JAXWSAConstants.NS_WSAM);
if (!addressing.required()) {
writer.writeAttribute("wsp", URI_POLICY_NS, "Optional", "true");
}
writer.writeStartElement("wsp", "Policy", URI_POLICY_NS);
String s = getAddressingRequirement(addressing);
if (s != null) {
writer.writeEmptyElement("wsam", s, JAXWSAConstants.NS_WSAM);
}
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndElement();
pr = writer.getDocument().getDocumentElement();
uel = new UnknownExtensibilityElement();
uel.setElement(pr);
uel.setElementType(DOMUtils.getElementQName(pr));
if (bindingInfo.getService().getDescription() == null) {
DescriptionInfo description = new DescriptionInfo();
description.setName(bindingInfo.getService().getName());
bindingInfo.getService().setDescription(description);
}
bindingInfo.getService().getDescription().addExtensor(uel);
} catch (Exception e) {
// ignore
e.printStackTrace();
}
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class ProviderImpl method createW3CEndpointReference.
// CHECKSTYLE:OFF - spec requires a bunch of params
public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters, List<Element> elements, Map<QName, String> attributes) {
// CHECKSTYLE:ON
if (serviceName != null && portName != null && wsdlDocumentLocation != null && interfaceName == null) {
Bus bus = BusFactory.getThreadDefaultBus();
WSDLManager wsdlManager = bus.getExtension(WSDLManager.class);
try {
Definition def = wsdlManager.getDefinition(wsdlDocumentLocation);
interfaceName = def.getService(serviceName).getPort(portName.getLocalPart()).getBinding().getPortType().getQName();
} catch (Exception e) {
// do nothing
}
}
if (serviceName == null && portName == null && address == null) {
throw new IllegalStateException("Address in an EPR cannot be null, " + " when serviceName or portName is null");
}
try {
final W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.setPrefix(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.NS_WSA);
writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_ERF_NAME, JAXWSAConstants.NS_WSA);
writer.writeNamespace(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.NS_WSA);
writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_ADDRESS_NAME, JAXWSAConstants.NS_WSA);
address = address == null ? "" : address;
writer.writeCharacters(address);
writer.writeEndElement();
if (referenceParameters != null) {
writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_REFERENCEPARAMETERS_NAME, JAXWSAConstants.NS_WSA);
for (Element ele : referenceParameters) {
StaxUtils.writeElement(ele, writer, true);
}
writer.writeEndElement();
}
if (wsdlDocumentLocation != null || interfaceName != null || serviceName != null || (metadata != null && !metadata.isEmpty())) {
writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_METADATA_NAME, JAXWSAConstants.NS_WSA);
writer.writeNamespace(JAXWSAConstants.WSAW_PREFIX, JAXWSAConstants.NS_WSAW);
writer.writeNamespace(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.NS_WSAM);
if (wsdlDocumentLocation != null) {
boolean includeLocationOnly = false;
org.apache.cxf.message.Message message = PhaseInterceptorChain.getCurrentMessage();
if (message != null) {
includeLocationOnly = MessageUtils.getContextualBoolean(message, "org.apache.cxf.wsa.metadata.wsdlLocationOnly", false);
}
String attrubuteValue = serviceName != null && !includeLocationOnly ? serviceName.getNamespaceURI() + " " + wsdlDocumentLocation : wsdlDocumentLocation;
writer.writeNamespace(JAXWSAConstants.WSDLI_PFX, JAXWSAConstants.NS_WSDLI);
writer.writeAttribute(JAXWSAConstants.WSDLI_PFX, JAXWSAConstants.NS_WSDLI, JAXWSAConstants.WSDLI_WSDLLOCATION, attrubuteValue);
}
if (interfaceName != null) {
writer.writeStartElement(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.WSAM_INTERFACE_NAME, JAXWSAConstants.NS_WSAM);
String portTypePrefix = interfaceName.getPrefix();
if (portTypePrefix == null || portTypePrefix.isEmpty()) {
portTypePrefix = "ns1";
}
writer.writeNamespace(portTypePrefix, interfaceName.getNamespaceURI());
writer.writeCharacters(portTypePrefix + ":" + interfaceName.getLocalPart());
writer.writeEndElement();
}
if (serviceName != null) {
String serviceNamePrefix = (serviceName.getPrefix() == null || serviceName.getPrefix().length() == 0) ? "ns2" : serviceName.getPrefix();
writer.writeStartElement(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.WSAM_SERVICENAME_NAME, JAXWSAConstants.NS_WSAM);
if (portName != null) {
writer.writeAttribute(JAXWSAConstants.WSAM_ENDPOINT_NAME, portName.getLocalPart());
}
writer.writeNamespace(serviceNamePrefix, serviceName.getNamespaceURI());
writer.writeCharacters(serviceNamePrefix + ":" + serviceName.getLocalPart());
writer.writeEndElement();
}
if (wsdlDocumentLocation != null) {
writer.writeStartElement(WSDLConstants.WSDL_PREFIX, WSDLConstants.QNAME_DEFINITIONS.getLocalPart(), WSDLConstants.NS_WSDL11);
writer.writeNamespace(WSDLConstants.WSDL_PREFIX, WSDLConstants.NS_WSDL11);
writer.writeStartElement(WSDLConstants.WSDL_PREFIX, WSDLConstants.QNAME_IMPORT.getLocalPart(), WSDLConstants.QNAME_IMPORT.getNamespaceURI());
if (serviceName != null) {
writer.writeAttribute(WSDLConstants.ATTR_NAMESPACE, serviceName.getNamespaceURI());
}
writer.writeAttribute(WSDLConstants.ATTR_LOCATION, wsdlDocumentLocation);
writer.writeEndElement();
writer.writeEndElement();
}
if (metadata != null) {
for (Element e : metadata) {
StaxUtils.writeElement(e, writer, true);
}
}
writer.writeEndElement();
}
if (elements != null) {
for (Element e : elements) {
StaxUtils.writeElement(e, writer, true);
}
}
writer.writeEndElement();
writer.flush();
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<W3CEndpointReference>() {
public W3CEndpointReference run() throws Exception {
Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
try {
return (W3CEndpointReference) unmarshaller.unmarshal(writer.getDocument());
} finally {
JAXBUtils.closeUnmarshaller(unmarshaller);
}
}
});
} catch (PrivilegedActionException pae) {
Exception e = pae.getException();
if (e instanceof JAXBException) {
throw (JAXBException) e;
}
throw new SecurityException(e);
}
} catch (Exception e) {
throw new WebServiceException(new Message("ERROR_UNMARSHAL_ENDPOINTREFERENCE", LOG).toString(), e);
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class STSRESTTest method testValidateJWTAndIssueSAML.
@org.junit.Test
public void testValidateJWTAndIssueSAML() throws Exception {
WebClient client = webClient().path("jwt").accept(MediaType.TEXT_PLAIN);
// 1. Get a token via GET
String token = client.get(String.class);
assertNotNull(token);
// 2. Now validate it in the STS using POST
client = webClient().query("action", "validate").type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
// Create RequestSecurityToken
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.writeStartElement("wst", "RequestSecurityToken", WST_NS_05_12);
writer.writeStartElement("wst", "RequestType", WST_NS_05_12);
writer.writeCharacters(WST_NS_05_12 + "/Validate");
writer.writeEndElement();
writer.writeStartElement("wst", "TokenType", WST_NS_05_12);
writer.writeCharacters(SAML2_TOKEN_TYPE);
writer.writeEndElement();
writer.writeStartElement("wst", "ValidateTarget", WST_NS_05_12);
writer.writeStartElement("TokenWrapper");
writer.writeCharacters(token);
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndElement();
RequestSecurityTokenResponseType securityResponse = client.post(new DOMSource(writer.getDocument().getDocumentElement()), RequestSecurityTokenResponseType.class);
assertTrue(getValidationStatus(securityResponse));
// Check the token
validateSAMLSecurityTokenResponse(securityResponse, true);
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class STSRESTTest method testIssueSAML2TokenViaPOST.
@org.junit.Test
public void testIssueSAML2TokenViaPOST() throws Exception {
WebClient client = webClient().type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
// Create RequestSecurityToken
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writer.writeStartElement("wst", "RequestSecurityToken", WST_NS_05_12);
writer.writeStartElement("wst", "RequestType", WST_NS_05_12);
writer.writeCharacters(WST_NS_05_12 + "/Issue");
writer.writeEndElement();
writer.writeStartElement("wst", "TokenType", WST_NS_05_12);
writer.writeCharacters(SAML2_TOKEN_TYPE);
writer.writeEndElement();
writer.writeEndElement();
RequestSecurityTokenResponseType securityResponse = client.post(new DOMSource(writer.getDocument().getDocumentElement()), RequestSecurityTokenResponseType.class);
validateSAMLSecurityTokenResponse(securityResponse, true);
}
Aggregations