javax.xml.ws.handler.MessageContext.Scope Java Examples

The following examples show how to use javax.xml.ws.handler.MessageContext.Scope. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContextPropertiesMappingTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #2
Source File: JaxWsClientProxy.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Map<String, Object> getResponseContext() {
    if (client == null) {
        throw new IllegalStateException("The client has been closed.");
    }
    return new WrappedMessageContext(this.getClient().getResponseContext(),
                                                      null,
                                                      Scope.APPLICATION);
}
 
Example #3
Source File: ContextPropertiesMappingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #4
Source File: WebServiceContextImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetHttpRequestHeadersScope() {
    MessageImpl msg = new MessageImpl();
    MessageContext context = new WrappedMessageContext(msg);
    Map<String, List<String>> headers = new HashMap<>();
    List<String> values = new ArrayList<>();
    values.add("Value1");
    headers.put("Header1", values);
    context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    context.setScope(MessageContext.HTTP_REQUEST_HEADERS, Scope.APPLICATION);
}
 
Example #5
Source File: WrappedMessageContextTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testContainsKey() throws Exception {
    WrappedMessageContext context =
        new WrappedMessageContext(new HashMap<String, Object>(), null, Scope.APPLICATION);

    Map<String, List<String>> headers = new HashMap<>();
    context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

    assertNotNull(context.get(MessageContext.HTTP_REQUEST_HEADERS));

    assertTrue(context.containsKey(MessageContext.HTTP_REQUEST_HEADERS));
}
 
Example #6
Source File: DispatchImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void setupEndpointAddressContext(Endpoint endpoint) {
    //NOTE for jms transport the address would be null
    if (null != endpoint
        && null != endpoint.getEndpointInfo().getAddress()) {
        Map<String, Object> requestContext
            = new WrappedMessageContext(client.getRequestContext(),
                                        null,
                                        Scope.APPLICATION);
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                       endpoint.getEndpointInfo().getAddress());
    }
}
 
Example #7
Source File: AbstractJAXWSMethodInvoker.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Map<String, Object> removeHandlerProperties(WrappedMessageContext ctx) {
    Map<String, Scope> scopes = CastUtils.cast((Map<?, ?>)ctx.get(WrappedMessageContext.SCOPES));
    Map<String, Object> handlerScopedStuff = new HashMap<>();
    if (scopes != null) {
        for (Map.Entry<String, Scope> scope : scopes.entrySet()) {
            if (scope.getValue() == Scope.HANDLER) {
                handlerScopedStuff.put(scope.getKey(), ctx.get(scope.getKey()));
            }
        }
        for (String key : handlerScopedStuff.keySet()) {
            ctx.remove(key);
        }
    }
    return handlerScopedStuff;
}
 
Example #8
Source File: JaxWsClientProxy.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Map<String, Object> getRequestContext() {
    if (client == null) {
        throw new IllegalStateException("The client has been closed.");
    }
    return new WrappedMessageContext(this.getClient().getRequestContext(),
                                     null,
                                     Scope.APPLICATION);
}
 
Example #9
Source File: DispatchImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Map<String, Object> getRequestContext() {
    return new WrappedMessageContext(client.getRequestContext(),
                                     null,
                                     Scope.APPLICATION);
}
 
Example #10
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public Scope getScope(String paramString) {
   throw new UnsupportedOperationException();
}
 
Example #11
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setScope(String paramString, Scope paramScope) {
   throw new UnsupportedOperationException();
}
 
Example #12
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public Scope getScope(String paramString) {
   throw new UnsupportedOperationException();
}
 
Example #13
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setScope(String paramString, Scope paramScope) {
   throw new UnsupportedOperationException();
}
 
Example #14
Source File: WrappedMessageContextTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testPutAndGetJaxwsAttachments() throws Exception {
    WrappedMessageContext context =
        new WrappedMessageContext(new HashMap<String, Object>(), null, Scope.APPLICATION);

    DataHandler dh1 = new DataHandler(new ByteArrayDataSource("Hello world!".getBytes(), "text/plain"));
    DataHandler dh2 = new DataHandler(new ByteArrayDataSource("Hola mundo!".getBytes(), "text/plain"));
    DataHandler dh3 = new DataHandler(new ByteArrayDataSource("Bonjour tout le monde!".getBytes(), "text/plain"));
    Map<String, DataHandler> jattachments = new HashMap<>();
    context.put(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS, jattachments);

    jattachments.put("attachment-1", dh1);

    Set<Attachment> cattachments = CastUtils.cast((Set<?>)context.get(Message.ATTACHMENTS));
    assertNotNull(cattachments);

    assertEquals(1, cattachments.size());

    jattachments.put("attachment-2", dh2);

    assertEquals(2, cattachments.size());

    AttachmentImpl ca = new AttachmentImpl("attachment-3", dh3);
    ca.setHeader("X-test", "true");
    cattachments.add(ca);

    assertEquals(3, jattachments.size());
    assertEquals(3, cattachments.size());
    for (Attachment a : cattachments) {
        if ("attachment-1".equals(a.getId())) {
            assertEquals("Hello world!", a.getDataHandler().getContent());
        } else if ("attachment-2".equals(a.getId())) {
            assertEquals("Hola mundo!", a.getDataHandler().getContent());
        } else if ("attachment-3".equals(a.getId())) {
            assertEquals("Bonjour tout le monde!", a.getDataHandler().getContent());
            assertEquals("true", a.getHeader("X-test"));
        } else {
            fail("unknown attachment");
        }
    }
}
 
Example #15
Source File: JAXWSMethodInvoker.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
protected Object invoke(Exchange exchange,
                        final Object serviceObject, Method m,
                        List<Object> params) {

    // set up the webservice request context
    WrappedMessageContext ctx = new WrappedMessageContext(exchange.getInMessage(), Scope.APPLICATION);

    Map<String, Object> handlerScopedStuff = removeHandlerProperties(ctx);

    final MessageContext oldCtx = WebServiceContextImpl.setMessageContext(ctx);
    List<Object> res = null;
    try {
        if ((params == null || params.isEmpty()) && serviceObject instanceof Provider) {
            params = Collections.singletonList(null);
        }
        res = CastUtils.cast((List<?>)super.invoke(exchange, serviceObject, m, params));

        if ((serviceObject instanceof Provider)
            && MessageUtils.getContextualBoolean(exchange.getInMessage(),
                                                 "jaxws.provider.interpretNullAsOneway",
                                                 true)
            && (res != null && !res.isEmpty() && res.get(0) == null)
            && exchange.getInMessage().getInterceptorChain().getState() == InterceptorChain.State.EXECUTING) {
            // treat the non-oneway call as oneway when a provider returns null
            // and the chain is not suspended due to a continuation suspend
            res = null;
            changeToOneway(exchange);
        }
        //update the webservice response context
        updateWebServiceContext(exchange, ctx);
    } catch (Fault f) {
        //get chance to copy over customer's header
        if (MessageUtils.getContextualBoolean(exchange.getInMessage(), COPY_SOAP_HEADERS_BY_FAULT, true)) {
            updateHeader(exchange, ctx);
        }
        throw f;
    } finally {
        //restore the WebServiceContextImpl's ThreadLocal variable to the previous value
        if (oldCtx == null) {
            WebServiceContextImpl.clear();
        } else {
            WebServiceContextImpl.setMessageContext(oldCtx);
        }

        addHandlerProperties(ctx, handlerScopedStuff);
    }
    return res;
}
 
Example #16
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public Scope getScope(String paramString) {
   throw new UnsupportedOperationException();
}
 
Example #17
Source File: DispatchImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Map<String, Object> getResponseContext() {
    return new WrappedMessageContext(client.getResponseContext(),
                                     null,
                                     Scope.APPLICATION);
}
 
Example #18
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setScope(String paramString, Scope paramScope) {
   throw new UnsupportedOperationException();
}
 
Example #19
Source File: AbstractJAXWSMethodInvoker.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void addHandlerProperties(WrappedMessageContext ctx,
                                    Map<String, Object> handlerScopedStuff) {
    for (Map.Entry<String, Object> key : handlerScopedStuff.entrySet()) {
        ctx.put(key.getKey(), key.getValue(), Scope.HANDLER);
    }
}
 
Example #20
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setScope(String paramString, Scope paramScope) {
   throw new UnsupportedOperationException();
}
 
Example #21
Source File: AbstractProtocolHandlerInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected MessageContext createProtocolMessageContext(T message) {
    return new WrappedMessageContext(message, Scope.HANDLER);
}
 
Example #22
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setScope(String paramString, Scope paramScope) {
   throw new UnsupportedOperationException();
}
 
Example #23
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public Scope getScope(String paramString) {
   throw new UnsupportedOperationException();
}
 
Example #24
Source File: JaxWsClientProxy.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (client == null) {
        throw new IllegalStateException("The client has been closed.");
    }

    Endpoint endpoint = getClient().getEndpoint();
    String address = endpoint.getEndpointInfo().getAddress();
    MethodDispatcher dispatcher = (MethodDispatcher)endpoint.getService().get(
                                                                              MethodDispatcher.class
                                                                                  .getName());
    Object[] params = args;
    if (null == params) {
        params = new Object[0];
    }


    try {
        if (method.getDeclaringClass().equals(BindingProvider.class)
            || method.getDeclaringClass().equals(Object.class)
            || method.getDeclaringClass().equals(Closeable.class)
            || method.getDeclaringClass().equals(AutoCloseable.class)) {
            return method.invoke(this, params);
        } else if (method.getDeclaringClass().isInstance(client)) {
            return method.invoke(client, params);
        }
    } catch (InvocationTargetException e) {
        throw e.getCause();
    }

    BindingOperationInfo oi = dispatcher.getBindingOperation(method, endpoint);
    if (oi == null) {
        Message msg = new Message("NO_BINDING_OPERATION_INFO", LOG, method.getName());
        throw new WebServiceException(msg.toString());
    }

    client.getRequestContext().put(Method.class.getName(), method);
    boolean isAsync = isAsync(method);

    Object result = null;
    try {
        if (isAsync) {
            result = invokeAsync(method, oi, params);
        } else {
            result = invokeSync(method, oi, params);
        }
    } catch (WebServiceException wex) {
        throw wex;
    } catch (Exception ex) {
        throw mapException(method, oi, ex);
    } finally {
        if (addressChanged(address)) {
            setupEndpointAddressContext(getClient().getEndpoint());
        }
    }

    Map<String, Object> respContext = client.getResponseContext();
    Map<String, Scope> scopes = CastUtils.cast((Map<?, ?>)respContext.get(WrappedMessageContext.SCOPES));
    if (scopes != null) {
        for (Map.Entry<String, Scope> scope : scopes.entrySet()) {
            if (scope.getValue() == Scope.HANDLER) {
                respContext.remove(scope.getKey());
            }
        }
    }
    return adjustObject(result);
}
 
Example #25
Source File: SOAPMessageContextImpl.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
public Scope getScope(String paramString) {
   throw new UnsupportedOperationException();
}