use of org.apache.cxf.jaxws.context.WrappedMessageContext in project cxf by apache.
the class SAMLTokenValidatorCachedRealmTest method createValidatorParameters.
private TokenValidatorParameters createValidatorParameters() throws WSSecurityException {
TokenValidatorParameters parameters = new TokenValidatorParameters();
TokenRequirements tokenRequirements = new TokenRequirements();
tokenRequirements.setTokenType(STSConstants.STATUS);
parameters.setTokenRequirements(tokenRequirements);
KeyRequirements keyRequirements = new KeyRequirements();
parameters.setKeyRequirements(keyRequirements);
parameters.setPrincipal(new CustomTokenPrincipal("alice"));
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
parameters.setMessageContext(msgCtx);
// Add STSProperties object
StaticSTSProperties stsProperties = new StaticSTSProperties();
Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
stsProperties.setEncryptionCrypto(crypto);
stsProperties.setSignatureCrypto(crypto);
stsProperties.setEncryptionUsername("myservicekey");
stsProperties.setSignatureUsername("mystskey");
stsProperties.setCallbackHandler(new PasswordCallbackHandler());
stsProperties.setIssuer("STS-2");
parameters.setStsProperties(stsProperties);
parameters.setTokenStore(tokenStore);
return parameters;
}
use of org.apache.cxf.jaxws.context.WrappedMessageContext in project camel by apache.
the class CxfProducer method prepareRequest.
protected Map<String, Object> prepareRequest(Exchange camelExchange, org.apache.cxf.message.Exchange cxfExchange) throws Exception {
// create invocation context
WrappedMessageContext requestContext = new WrappedMessageContext(new HashMap<String, Object>(), null, Scope.APPLICATION);
camelExchange.setProperty(Message.MTOM_ENABLED, String.valueOf(endpoint.isMtomEnabled()));
// set data format mode in exchange
DataFormat dataFormat = endpoint.getDataFormat();
camelExchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, dataFormat);
LOG.trace("Set Camel Exchange property: {}={}", DataFormat.class.getName(), dataFormat);
if (endpoint.getMergeProtocolHeaders()) {
camelExchange.setProperty(CxfConstants.CAMEL_CXF_PROTOCOL_HEADERS_MERGED, Boolean.TRUE);
}
// set data format mode in the request context
requestContext.put(DataFormat.class.getName(), dataFormat);
// don't let CXF ClientImpl close the input stream
if (dataFormat.dealias() == DataFormat.RAW) {
cxfExchange.put(Client.KEEP_CONDUIT_ALIVE, true);
LOG.trace("Set CXF Exchange property: {}={}", Client.KEEP_CONDUIT_ALIVE, true);
}
// bind the request CXF exchange
endpoint.getCxfBinding().populateCxfRequestFromExchange(cxfExchange, camelExchange, requestContext);
// add appropriate cookies from the cookie store to the protocol headers
if (endpoint.getCookieHandler() != null) {
try {
Map<String, List<String>> transportHeaders = CastUtils.cast((Map<?, ?>) requestContext.get(Message.PROTOCOL_HEADERS));
boolean added;
if (transportHeaders == null) {
transportHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
added = true;
} else {
added = false;
}
transportHeaders.putAll(endpoint.getCookieHandler().loadCookies(camelExchange, endpoint.getRequestUri(camelExchange)));
if (added && transportHeaders.size() > 0) {
requestContext.put(Message.PROTOCOL_HEADERS, transportHeaders);
}
} catch (IOException e) {
LOG.warn("Cannot load cookies", e);
}
}
// Remove protocol headers from scopes. Otherwise, response headers can be
// overwritten by request headers when SOAPHandlerInterceptor tries to create
// a wrapped message context by the copyScoped() method.
requestContext.getScopes().remove(Message.PROTOCOL_HEADERS);
return requestContext.getWrappedMap();
}
use of org.apache.cxf.jaxws.context.WrappedMessageContext in project cxf by apache.
the class ContextPropertiesMappingTest method testCreateWebServiceContextWithInAttachments.
@Test
public void testCreateWebServiceContextWithInAttachments() {
Exchange exchange = new ExchangeImpl();
Message inMessage = new MessageImpl();
Collection<Attachment> attachments = new LinkedList<>();
DataSource source = new ByteDataSource(new byte[0], "text/xml");
DataHandler handler1 = new DataHandler(source);
attachments.add(new AttachmentImpl("part1", handler1));
DataHandler handler2 = new DataHandler(source);
attachments.add(new AttachmentImpl("part2", handler2));
inMessage.setAttachments(attachments);
inMessage.putAll(message);
exchange.setInMessage(inMessage);
exchange.setOutMessage(new MessageImpl());
MessageContext ctx = new WrappedMessageContext(exchange.getInMessage(), Scope.APPLICATION);
Object inAttachments = ctx.get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
assertNotNull("inbound attachments object must be initialized", inAttachments);
assertTrue("inbound attachments must be in a Map", inAttachments instanceof Map);
Map<String, DataHandler> dataHandlers = CastUtils.cast((Map<?, ?>) inAttachments);
assertEquals("two inbound attachments expected", 2, dataHandlers.size());
assertTrue("part1 attachment is missing", dataHandlers.containsKey("part1"));
// should do as it's the same instance
assertTrue("part1 handler is missing", dataHandlers.get("part1") == handler1);
assertTrue("part2 attachment is missing", dataHandlers.containsKey("part2"));
assertTrue("part2 handler is missing", dataHandlers.get("part2") == handler2);
}
use of org.apache.cxf.jaxws.context.WrappedMessageContext in project cxf by apache.
the class HandlerChainInvokerTest method setUp.
@Before
public void setUp() {
AbstractHandlerBase.clear();
@SuppressWarnings("rawtypes") List<Handler> handlers = new ArrayList<>();
for (int i = 0; i < logicalHandlers.length; i++) {
logicalHandlers[i] = new TestLogicalHandler();
handlers.add(logicalHandlers[i]);
}
for (int i = 0; i < protocolHandlers.length; i++) {
protocolHandlers[i] = new TestProtocolHandler();
handlers.add(protocolHandlers[i]);
}
invoker = new HandlerChainInvoker(handlers);
message = new MessageImpl();
Exchange e = new ExchangeImpl();
message.setExchange(e);
lmc = new LogicalMessageContextImpl(message);
pmc = new WrappedMessageContext(message);
/*
payload = new DOMSource();
message.setContent(Source.class, payload);*/
}
use of org.apache.cxf.jaxws.context.WrappedMessageContext in project cxf by apache.
the class HandlerChainInvoker method setFaultMessage.
/*
* When the message direction is reversed, if the message is not already a
* fault message then it is replaced with a fault message
*/
private void setFaultMessage(MessageContext mc, Exception exception) {
Message msg = ((WrappedMessageContext) mc).getWrappedMessage();
msg.setContent(Exception.class, exception);
msg.removeContent(XMLStreamReader.class);
msg.removeContent(Source.class);
try {
SoapVersion version = null;
if (msg instanceof SoapMessage) {
version = ((SoapMessage) msg).getVersion();
}
SOAPMessage soapMessage = SAAJFactoryResolver.createMessageFactory(version).createMessage();
msg.setContent(SOAPMessage.class, soapMessage);
SOAPBody body = SAAJUtils.getBody(soapMessage);
SOAPFault soapFault = body.addFault();
if (exception instanceof SOAPFaultException) {
SOAPFaultException sf = (SOAPFaultException) exception;
soapFault.setFaultString(sf.getFault().getFaultString());
SAAJUtils.setFaultCode(soapFault, sf.getFault().getFaultCodeAsQName());
soapFault.setFaultActor(sf.getFault().getFaultActor());
if (sf.getFault().hasDetail()) {
Node nd = soapMessage.getSOAPPart().importNode(sf.getFault().getDetail(), true);
nd = nd.getFirstChild();
soapFault.addDetail();
while (nd != null) {
soapFault.getDetail().appendChild(nd);
nd = nd.getNextSibling();
}
}
} else if (exception instanceof Fault) {
SoapFault sf = SoapFault.createFault((Fault) exception, ((SoapMessage) msg).getVersion());
soapFault.setFaultString(sf.getReason());
SAAJUtils.setFaultCode(soapFault, sf.getFaultCode());
if (sf.hasDetails()) {
soapFault.addDetail();
Node nd = soapMessage.getSOAPPart().importNode(sf.getDetail(), true);
nd = nd.getFirstChild();
while (nd != null) {
soapFault.getDetail().appendChild(nd);
nd = nd.getNextSibling();
}
}
} else {
SAAJUtils.setFaultCode(soapFault, new QName("http://cxf.apache.org/faultcode", "HandlerFault"));
soapFault.setFaultString(exception.getMessage());
}
} catch (SOAPException e) {
e.printStackTrace();
// do nothing
}
}
Aggregations