Search in sources :

Example 11 with MessageContext

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());
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) HashMap(java.util.HashMap) Map(java.util.Map) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 12 with MessageContext

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;
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) SoapFault(org.apache.cxf.binding.soap.SoapFault) QName(javax.xml.namespace.QName) MessageContext(javax.xml.ws.handler.MessageContext)

Example 13 with MessageContext

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;
}
Also used : InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Document(org.w3c.dom.Document) IOException(java.io.IOException) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) MessageContext(javax.xml.ws.handler.MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 14 with MessageContext

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;
    }
}
Also used : Continuation(org.apache.cxf.continuations.Continuation) ContinuationProvider(org.apache.cxf.continuations.ContinuationProvider) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) MessageContext(javax.xml.ws.handler.MessageContext) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException)

Example 15 with MessageContext

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);
}
Also used : JMSMessageHeadersType(org.apache.cxf.transport.jms.JMSMessageHeadersType) MessageContext(javax.xml.ws.handler.MessageContext)

Aggregations

MessageContext (javax.xml.ws.handler.MessageContext)46 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 QName (javax.xml.namespace.QName)10 WrappedMessageContext (org.apache.cxf.jaxws.context.WrappedMessageContext)9 Exchange (org.apache.cxf.message.Exchange)9 MessageImpl (org.apache.cxf.message.MessageImpl)9 HandlerChainInvoker (org.apache.cxf.jaxws.handler.HandlerChainInvoker)8 List (java.util.List)7 Header (org.apache.cxf.headers.Header)7 IOException (java.io.IOException)6 Handler (javax.xml.ws.handler.Handler)6 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpSession (javax.servlet.http.HttpSession)5 JAXBException (javax.xml.bind.JAXBException)5 SOAPMessage (javax.xml.soap.SOAPMessage)5 Binding (javax.xml.ws.Binding)5