use of javax.xml.ws.handler.MessageContext in project cxf by apache.
the class ContextPropertiesMappingTest method testCreateWebServiceContext.
@Test
public void testCreateWebServiceContext() {
Exchange exchange = new ExchangeImpl();
Message inMessage = new MessageImpl();
Message outMessage = new MessageImpl();
inMessage.putAll(message);
exchange.setInMessage(inMessage);
exchange.setOutMessage(outMessage);
MessageContext ctx = new WrappedMessageContext(exchange.getInMessage(), Scope.APPLICATION);
Object requestHeader = ctx.get(MessageContext.HTTP_REQUEST_HEADERS);
assertNotNull("the request header should not be null", requestHeader);
assertEquals("we should get the request header", requestHeader, HEADER);
Object responseHeader = ctx.get(MessageContext.HTTP_RESPONSE_HEADERS);
assertNull("the response header should be null", responseHeader);
Object outMessageHeader = outMessage.get(Message.PROTOCOL_HEADERS);
assertEquals("the outMessage PROTOCOL_HEADERS should be update", responseHeader, outMessageHeader);
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);
assertTrue("no inbound attachments expected", ((Map<?, ?>) inAttachments).isEmpty());
}
use of javax.xml.ws.handler.MessageContext in project cxf by apache.
the class SecurityTokenServiceProvider method createSOAPFault.
private SoapFault createSOAPFault(Throwable ex) {
String faultString = "Internal STS error";
QName faultCode = null;
if (ex != null) {
if (ex instanceof STSException && ((STSException) ex).getFaultCode() != null) {
faultCode = ((STSException) ex).getFaultCode();
}
faultString = ex.getMessage();
}
MessageContext messageContext = context.getMessageContext();
SoapVersion soapVersion = (SoapVersion) messageContext.get(SoapVersion.class.getName());
SoapFault fault;
if (soapVersion.getVersion() == 1.1 && faultCode != null) {
fault = new SoapFault(faultString, faultCode);
} else {
fault = new SoapFault(faultString, soapVersion.getSender());
if (soapVersion.getVersion() != 1.1 && faultCode != null) {
fault.setSubCode(faultCode);
}
}
return fault;
}
use of javax.xml.ws.handler.MessageContext in project cxf by apache.
the class AttachmentStreamSourceXMLProvider method invoke.
public StreamSource invoke(StreamSource source) {
MessageContext mc = wsContext.getMessageContext();
String httpMethod = (String) mc.get(MessageContext.HTTP_REQUEST_METHOD);
if ("POST".equals(httpMethod)) {
int count = 0;
// we really want to verify that a root part is a proper XML as expected
try {
Document doc = StaxUtils.read(source);
count = Integer.parseInt(doc.getDocumentElement().getAttribute("count"));
} catch (Exception ex) {
// ignore
}
Map<String, DataHandler> dataHandlers = CastUtils.cast((Map<?, ?>) mc.get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS));
StringBuilder buf = new StringBuilder();
buf.append("<response>");
int i = 0;
for (Map.Entry<String, DataHandler> entry : dataHandlers.entrySet()) {
if (i++ > count) {
break;
}
try (ByteArrayOutputStream bous = new ByteArrayOutputStream()) {
InputStream is = entry.getValue().getInputStream();
IOUtils.copy(is, bous);
buf.append("<att contentId=\"" + entry.getKey() + "\">");
buf.append(Base64Utility.encode(bous.toByteArray()));
buf.append("</att>");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
buf.append("</response>");
Map<String, List<String>> respHeaders = CastUtils.cast((Map<?, ?>) mc.get(MessageContext.HTTP_RESPONSE_HEADERS));
if (respHeaders == null) {
respHeaders = new HashMap<>();
mc.put(MessageContext.HTTP_RESPONSE_HEADERS, respHeaders);
}
List<String> contentTypeValues = new ArrayList<>();
contentTypeValues.add("application/xml+custom");
respHeaders.put(Message.CONTENT_TYPE, contentTypeValues);
Map<String, DataHandler> outDataHandlers = CastUtils.cast((Map<?, ?>) mc.get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS));
byte[] data = new byte[50];
for (int x = 0; x < data.length; x++) {
data[x] = (byte) (x + '0');
}
DataHandler foo = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
outDataHandlers.put("foo", foo);
return new StreamSource(new StringReader(buf.toString()));
}
return source;
}
use of javax.xml.ws.handler.MessageContext in project cxf by apache.
the class HWSoapMessageDocProvider method invoke.
public SOAPMessage invoke(SOAPMessage request) {
try {
final MessageContext messageContext = ctx.getMessageContext();
ContinuationProvider contProvider = (ContinuationProvider) messageContext.get(ContinuationProvider.class.getName());
final Continuation continuation = contProvider.getContinuation();
if (continuation.isNew()) {
continuation.suspend(5000);
new Thread(new Runnable() {
public void run() {
try {
continuation.resume();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
return null;
} else if (!continuation.isResumed()) {
continuation.reset();
throw new RuntimeException("time out");
} else {
return resumeMessage(request);
}
} catch (SOAPFaultException e) {
throw e;
}
}
use of javax.xml.ws.handler.MessageContext in project cxf by apache.
the class TwoWayJMSImplBase method addToReply.
private void addToReply(String key, String value) {
MessageContext mc = wsContext.getMessageContext();
JMSMessageHeadersType responseHeaders = (JMSMessageHeadersType) mc.get(JMSConstants.JMS_SERVER_RESPONSE_HEADERS);
responseHeaders.putProperty(key, value);
}
Aggregations