Java Code Examples for org.apache.cxf.message.message#put()

The following examples show how to use org.apache.cxf.message.message#put() . 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: SseEventSinkContextProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
public SseEventSink createContext(Message message) {
    final HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
    if (request == null) {
        throw new IllegalStateException("Unable to retrieve HTTP request from the context");
    }

    final MessageBodyWriter<OutboundSseEvent> writer = new OutboundSseEventBodyWriter(
        ServerProviderFactory.getInstance(message), message.getExchange());

    final AsyncResponse async = new AsyncResponseImpl(message);
    final Integer bufferSize = PropertyUtils.getInteger(message, SseEventSinkImpl.BUFFER_SIZE_PROPERTY);
    
    final SseEventSink sink = createSseEventSink(request, writer, async, bufferSize);
    message.put(SseEventSink.class, sink);
    
    return sink;
}
 
Example 2
Source File: AuthPolicyValidatingInterceptorTest.java    From steady with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidateAuthorizationPolicy() throws Exception {
    AuthPolicyValidatingInterceptor in = new AuthPolicyValidatingInterceptor();
    TestSTSTokenValidator validator = new TestSTSTokenValidator();
    in.setValidator(validator);
    
    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.setUserName("bob");
    policy.setPassword("pswd");
    Message message = new MessageImpl();
    message.put(AuthorizationPolicy.class, policy);
    
    in.handleMessage(message);
    
    assertTrue(validator.isValidated());
}
 
Example 3
Source File: MessageContextImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private Message createMessage() {
    ProviderFactory factory = ServerProviderFactory.getInstance();
    Message m = new MessageImpl();
    m.put("org.apache.cxf.http.case_insensitive_queries", false);
    Exchange e = new ExchangeImpl();
    m.setExchange(e);
    e.setInMessage(m);
    Endpoint endpoint = EasyMock.mock(Endpoint.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(null).anyTimes();
    EasyMock.expect(endpoint.get(Application.class.getName())).andReturn(null);
    EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
    EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
    EasyMock.expect(endpoint.get(ServerProviderFactory.class.getName())).andReturn(factory).anyTimes();
    EasyMock.replay(endpoint);
    e.put(Endpoint.class, endpoint);
    return m;
}
 
Example 4
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryParametersIntegerArray() throws Exception {
    Class<?>[] argType = {Integer[].class};
    Method m = Customer.class.getMethod("testQueryIntegerArray", argType);
    Message messageImpl = createMessage();

    messageImpl.put(Message.QUERY_STRING, "query=1&query=2");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m,
                                                           new ClassResourceInfo(Customer.class)),
                                                       null,
                                                       messageImpl);
    assertEquals(1, params.size());
    Integer[] intValues = (Integer[])params.get(0);
    assertEquals(2, intValues.length);
    assertEquals(1, (int)intValues[0]);
    assertEquals(2, (int)intValues[1]);
}
 
Example 5
Source File: JMSContinuationProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public Continuation getContinuation() {
    Message m = inMessage;
    // Get the real message which is used in the interceptor chain
    if (m != null && m.getExchange() != null && m.getExchange().getInMessage() != null) {
        m = m.getExchange().getInMessage();
    }
    if (m == null || m.getExchange() == null || m.getExchange().isOneWay()) {
        return null;
    }
    JMSContinuation cw = m.get(JMSContinuation.class);
    if (cw == null) {
        cw = new JMSContinuation(bus, m,  incomingObserver, suspendendContinuations);
        m.put(JMSContinuation.class, cw);
    }
    return cw;


}
 
Example 6
Source File: BackChannelConduit.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Send an outbound message, assumed to contain all the name-value mappings of the corresponding input
 * message (if any).
 *
 * @param message the message to be sent.
 */
public void prepare(final Message message) throws IOException {
    // setup the message to be sent back
    javax.jms.Message jmsMessage = (javax.jms.Message)inMessage
        .get(JMSConstants.JMS_REQUEST_MESSAGE);
    message.put(JMSConstants.JMS_REQUEST_MESSAGE, jmsMessage);

    if (!message.containsKey(JMSConstants.JMS_SERVER_RESPONSE_HEADERS)
        && inMessage.containsKey(JMSConstants.JMS_SERVER_RESPONSE_HEADERS)) {
        message.put(JMSConstants.JMS_SERVER_RESPONSE_HEADERS, inMessage
            .get(JMSConstants.JMS_SERVER_RESPONSE_HEADERS));
    }

    Exchange exchange = inMessage.getExchange();
    exchange.setOutMessage(message);

    boolean isTextMessage = (jmsMessage instanceof TextMessage) && !JMSMessageUtils.isMtomEnabled(message);
    MessageStreamUtil.prepareStream(message, isTextMessage, this);
}
 
Example 7
Source File: SearchContextImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleEquals() {
    Message m = new MessageImpl();
    m.put(Message.QUERY_STRING, "_s=name=CXF");
    m.put("fiql.support.single.equals.operator", "true");
    SearchContext context = new SearchContextImpl(m);
    SearchCondition<SearchBean> sc = context.getCondition(SearchBean.class);
    assertNotNull(sc);

    PrimitiveStatement ps = sc.getStatement();
    assertNotNull(ps);

    assertEquals("name", ps.getProperty());
    assertEquals("CXF", ps.getValue());
    assertEquals(ConditionType.EQUALS, ps.getCondition());
    assertEquals(String.class, ps.getValueType());
}
 
Example 8
Source File: HttpUtilsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doTestReplaceAnyIPAddressWithPort(boolean anyIp) {
    Message m = new MessageImpl();
    HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
    m.put(AbstractHTTPDestination.HTTP_REQUEST, req);
    req.getScheme();
    EasyMock.expectLastCall().andReturn("http");
    req.getServerName();
    EasyMock.expectLastCall().andReturn("localhost");
    req.getServerPort();
    EasyMock.expectLastCall().andReturn(8080);
    EasyMock.replay(req);
    String host = anyIp ? "0.0.0.0" : "127.0.0.1";
    URI u = HttpUtils.toAbsoluteUri(URI.create("http://" + host + ":8080/bar/foo"), m);
    assertEquals("http://localhost:8080/bar/foo", u.toString());
}
 
Example 9
Source File: HttpsTokenInterceptorProvider.java    From steady with Apache License 2.0 5 votes vote down vote up
private static Map<String, List<String>> getSetProtocolHeaders(Message message) {
    Map<String, List<String>> headers =
        CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));        
    if (null == headers) {
        headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
        message.put(Message.PROTOCOL_HEADERS, headers);
    }
    return headers;
}
 
Example 10
Source File: SelectMethodCandidatesTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doTestProducesResource(Class<?> resourceClass,
                                    String path,
                                    String acceptContentTypes,
                                    String expectedResponseType,
                                    String expectedMethodName) throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(resourceClass);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl)sf.getService()).getClassResourceInfos();
    String contentType = "*/*";

    Message m = new MessageImpl();
    m.put(Message.CONTENT_TYPE, contentType);
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(m);
    m.setExchange(ex);
    Endpoint e = mockEndpoint();
    ex.put(Endpoint.class, e);

    MetadataMap<String, String> values = new MetadataMap<>();
    Map<ClassResourceInfo, MultivaluedMap<String, String>> mResources
        = JAXRSUtils.selectResourceClass(resources, path, m);

    OperationResourceInfo ori = JAXRSUtils.findTargetMethod(mResources, m, "GET",
                                            values, contentType,
                                            sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals(expectedMethodName,  ori.getMethodToInvoke().getName());
    assertEquals(expectedResponseType, m.getExchange().get(Message.CONTENT_TYPE));
}
 
Example 11
Source File: JAXRSInInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void setExchangeProperties(Message message,
                                   Exchange exchange,
                                   OperationResourceInfo ori,
                                   MultivaluedMap<String, String> values,
                                   int numberOfResources) {
    final ClassResourceInfo cri = ori.getClassResourceInfo();
    exchange.put(OperationResourceInfo.class, ori);
    exchange.put(JAXRSUtils.ROOT_RESOURCE_CLASS, cri);
    message.put(RESOURCE_METHOD, ori.getMethodToInvoke());
    message.put(URITemplate.TEMPLATE_PARAMETERS, values);

    String plainOperationName = ori.getMethodToInvoke().getName();
    if (numberOfResources > 1) {
        plainOperationName = cri.getServiceClass().getSimpleName() + "#" + plainOperationName;
    }
    exchange.put(RESOURCE_OPERATION_NAME, plainOperationName);

    if (ori.isOneway()
        || PropertyUtils.isTrue(HttpUtils.getProtocolHeader(message, Message.ONE_WAY_REQUEST, null))) {
        exchange.setOneWay(true);
    }
    ResourceProvider rp = cri.getResourceProvider();
    if (rp instanceof SingletonResourceProvider) {
        //cri.isSingleton is not guaranteed to indicate we have a 'pure' singleton
        exchange.put(Message.SERVICE_OBJECT, rp.getInstance(message));
    }
}
 
Example 12
Source File: MessageContextImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void put(Object key, Object value) {
    if (MultipartBody.OUTBOUND_MESSAGE_ATTACHMENTS.equals(key.toString())) {
        convertToAttachments(value);
    }
    Message currentMessage = getCurrentMessage();
    currentMessage.put(key.toString(), value);
    currentMessage.getExchange().put(key.toString(), value);

}
 
Example 13
Source File: JAXRSOutInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void setResponseStatus(Message message, int status) {
    message.put(Message.RESPONSE_CODE, status);
    boolean responseHeadersCopied = isResponseHeadersCopied(message);
    if (responseHeadersCopied) {
        HttpServletResponse response =
            (HttpServletResponse)message.get(AbstractHTTPDestination.HTTP_RESPONSE);
        response.setStatus(status);
    }
}
 
Example 14
Source File: HTTPConduitURLEasyMockTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendHttpGetConnectionAutoRedirect() throws Exception {
    control = EasyMock.createNiceControl();
    HTTPConduit conduit = setUpConduit(true, true, "GET");
    Message message = new MessageImpl();
    message.put(HTTPConduit.SET_HTTP_RESPONSE_MESSAGE, Boolean.TRUE);
    message.put(Message.HTTP_REQUEST_METHOD, "GET");
    conduit.prepare(message);
    verifySentMessage(conduit, message, "GET");
    assertEquals(HTTP_RESPONSE_MESSAGE, inMessage.get(HTTPConduit.HTTP_RESPONSE_MESSAGE));
    conduit.close(message);
    finalVerify();
}
 
Example 15
Source File: BinaryDataProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void copyUsingNio(InputStream is, OutputStream os, Continuation cont) {
    NioWriteListenerImpl listener =
        new NioWriteListenerImpl(cont,
                                 new NioWriteEntity(getNioHandler(is), null),
                                 new NioOutputStream(os));
    Message m = JAXRSUtils.getCurrentMessage();
    m.put(WriteListener.class, listener);
    cont.suspend(0);
}
 
Example 16
Source File: AttachmentDeserializerTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testCXF3582c() throws Exception {
    String contentType = "multipart/related; type=\"application/xop+xml\"; "
        + "boundary=\"uuid:906fa67b-85f9-4ef5-8e3d-52416022d463\"; "
        + "start=\"<[email protected]>\"; start-info=\"text/xml\"";


    Message message = new MessageImpl();
    message.put(Message.CONTENT_TYPE, contentType);
    message.setContent(InputStream.class, getClass().getResourceAsStream("cxf3582.data"));
    message.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, System
            .getProperty("java.io.tmpdir"));
    message.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String
            .valueOf(AttachmentDeserializer.THRESHOLD));


    AttachmentDeserializer ad
        = new AttachmentDeserializer(message,
                                     Collections.singletonList("multipart/related"));

    ad.initializeAttachments();

    String cid = "[email protected]";
    DataSource ds = AttachmentUtil.getAttachmentDataSource(cid, message.getAttachments());
    byte[] bts = new byte[1024];
    InputStream ins = ds.getInputStream();
    int count = 0;
    int x = ins.read(bts, 100, 600);
    while (x != -1) {
        count += x;
        x = ins.read(bts, 100, 600);
    }
    assertEquals(500, count);
    assertEquals(-1, ins.read(new byte[1000], 100, 600));
    ins.close();

    cid = "[email protected]";
    ds = AttachmentUtil.getAttachmentDataSource(cid, message.getAttachments());
    bts = new byte[1024];
    ins = ds.getInputStream();
    count = 0;
    x = ins.read(bts, 100, 600);
    while (x != -1) {
        count += x;
        x = ins.read(bts, 100, 600);
    }
    assertEquals(1249, count);
    assertEquals(-1, ins.read(new byte[1000], 100, 600));
    ins.close();
}
 
Example 17
Source File: HttpHeadersImplTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static Message createMessage(MetadataMap<String, String> headers) {
    Message m = new MessageImpl();
    m.put(Message.PROTOCOL_HEADERS, headers);
    return m;
}
 
Example 18
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testIntersectMimeTypesCompositeSubtype10() throws Exception {
    Message m = new MessageImpl();
    m.put(JAXRSUtils.PARTIAL_HIERARCHICAL_MEDIA_SUBTYPE_CHECK, true);
    assertFalse(JAXRSUtils.compareCompositeSubtypes("application/v1+xml", "application/v2+xml", m));
}
 
Example 19
Source File: RMContextUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static void setProtocolVariation(Message message, ProtocolVariation protocol) {
    message.put(RMMessageConstants.RM_PROTOCOL_VARIATION, protocol);
}
 
Example 20
Source File: MessageContextImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void convertToAttachments(Object value) {
    List<?> handlers = (List<?>)value;
    List<org.apache.cxf.message.Attachment> atts =
        new ArrayList<>();

    for (int i = 1; i < handlers.size(); i++) {
        Attachment handler = (Attachment)handlers.get(i);
        AttachmentImpl att = new AttachmentImpl(handler.getContentId(), handler.getDataHandler());
        for (String key : handler.getHeaders().keySet()) {
            att.setHeader(key, handler.getHeader(key));
        }
        att.setXOP(false);
        atts.add(att);
    }
    Message outMessage = getOutMessage();
    outMessage.setAttachments(atts);
    outMessage.put(AttachmentOutInterceptor.WRITE_ATTACHMENTS, "true");
    Attachment root = (Attachment)handlers.get(0);

    String rootContentType = root.getContentType().toString();
    MultivaluedMap<String, String> rootHeaders = new MetadataMap<>(root.getHeaders());
    if (!AttachmentUtil.isMtomEnabled(outMessage)) {
        rootHeaders.putSingle(Message.CONTENT_TYPE, rootContentType);
    }

    String messageContentType = outMessage.get(Message.CONTENT_TYPE).toString();
    int index = messageContentType.indexOf(";type");
    if (index > 0) {
        messageContentType = messageContentType.substring(0, index).trim();
    }
    AttachmentOutputInterceptor attInterceptor =
        new AttachmentOutputInterceptor(messageContentType, rootHeaders);

    outMessage.put(Message.CONTENT_TYPE, rootContentType);
    Map<String, List<String>> allHeaders =
        CastUtils.cast((Map<?, ?>)outMessage.get(Message.PROTOCOL_HEADERS));
    if (allHeaders != null) {
        allHeaders.remove(Message.CONTENT_TYPE);
    }
    attInterceptor.handleMessage(outMessage);
}