Search in sources :

Example 51 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class FlowIdProducerTest method flowIdProducerOut2Test.

@Test
public void flowIdProducerOut2Test() {
    FlowIdProducerOut<Message> flowIdProducerOut = new FlowIdProducerOut<Message>();
    Message message = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    Message inMessage = new MessageImpl();
    exchange.setInMessage(inMessage);
    message.setExchange(exchange);
    WeakReference<Message> wrPreviousMessage = EasyMock.createMock(WeakReference.class);
    EasyMock.expect(wrPreviousMessage.get()).andReturn(message);
    EasyMock.replay(wrPreviousMessage);
    message.put(PhaseInterceptorChain.PREVIOUS_MESSAGE, wrPreviousMessage);
    message.put(Message.REQUESTOR_ROLE, true);
    String flowId = FlowIdHelper.getFlowId(message);
    Assert.assertNull("FlowId should be null before FlowIdProducerOut handleMessage()", flowId);
    flowIdProducerOut.handleMessage(message);
    flowId = FlowIdHelper.getFlowId(message);
    Assert.assertNotNull("FlowId should not be null after FlowIdProducerOut handleMessage()", flowId);
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 52 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class FlowIdProducerTest method flowIdProducerOutTest.

@Test
public void flowIdProducerOutTest() {
    FlowIdProducerOut<Message> flowIdProducerOut = new FlowIdProducerOut<Message>();
    Message message = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    Message inMessage = new MessageImpl();
    exchange.setInMessage(inMessage);
    message.setExchange(exchange);
    String flowId = FlowIdHelper.getFlowId(message);
    Assert.assertNull("FlowId should be null before FlowIdProducerOut handleMessage()", flowId);
    flowIdProducerOut.handleMessage(message);
    flowId = FlowIdHelper.getFlowId(message);
    Assert.assertNotNull("FlowId should not be null after FlowIdProducerOut handleMessage()", flowId);
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 53 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class WireTapIn method handleMessage.

/* (non-Javadoc)
     * @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)
     */
@Override
public void handleMessage(final Message message) throws Fault {
    final InputStream is = message.getContent(InputStream.class);
    if (logMessageContent) {
        if (null == is) {
            Reader reader = message.getContent(Reader.class);
            if (null != reader) {
                String encoding = (String) message.get(Message.ENCODING);
                if (encoding == null) {
                    encoding = "UTF-8";
                }
                try {
                    final CachedOutputStream cos = new CachedOutputStream();
                    final Writer writer = new OutputStreamWriter(cos, encoding);
                    IOUtils.copy(reader, writer, 1024);
                    reader.reset();
                    writer.flush();
                    message.setContent(InputStream.class, cos.getInputStream());
                    message.setContent(Reader.class, null);
                    message.setContent(CachedOutputStream.class, cos);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } finally {
                    try {
                        reader.reset();
                    } catch (IOException e) {
                    }
                }
            }
        } else {
            try {
                final CachedOutputStream cos = new CachedOutputStream();
                // TODO: We should try to make this streaming
                // WireTapInputStream wtis = new WireTapInputStream(is, cos);
                // message.setContent(InputStream.class, wtis);
                IOUtils.copyAndCloseInput(is, cos);
                message.setContent(InputStream.class, cos.getInputStream());
                message.setContent(CachedOutputStream.class, cos);
                message.getInterceptorChain().add(new AbstractPhaseInterceptor<Message>(Phase.POST_INVOKE) {

                    @Override
                    public void handleMessage(Message message) throws Fault {
                        if (cos != null) {
                            try {
                                cos.close();
                            } catch (IOException e) {
                            }
                        }
                    }
                });
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) InputStream(java.io.InputStream) Reader(java.io.Reader) OutputStreamWriter(java.io.OutputStreamWriter) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 54 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class SecurityContextFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    Message message = JAXRSUtils.getCurrentMessage();
    SecurityContext sc = message.get(SecurityContext.class);
    if (sc != null) {
        Principal principal = sc.getUserPrincipal();
        if (principal != null) {
            String accountName = principal.getName();
            UserAccount account = accounts.getAccount(accountName);
            if (account == null) {
                account = accounts.getAccountWithAlias(accountName);
            }
            if (account == null) {
                requestContext.abortWith(createFaultResponse());
            } else {
                setNewSecurityContext(message, account.getName());
            }
            return;
        }
    }
    List<String> authValues = headers.getRequestHeader("Authorization");
    if (authValues == null || authValues.size() != 1) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String[] values = authValues.get(0).split(" ");
    if (values.length != 2 || !"Basic".equals(values[0])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String decodedValue = null;
    try {
        decodedValue = new String(Base64Utility.decode(values[1]));
    } catch (Base64Exception ex) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String[] namePassword = decodedValue.split(":");
    if (namePassword.length != 2) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    final UserAccount account = accounts.getAccount(namePassword[0]);
    if (account == null || !account.getPassword().equals(namePassword[1])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    setNewSecurityContext(message, account.getName());
}
Also used : Message(org.apache.cxf.message.Message) Base64Exception(org.apache.cxf.common.util.Base64Exception) SecurityContext(org.apache.cxf.security.SecurityContext) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) Principal(java.security.Principal)

Example 55 with Message

use of org.apache.cxf.message.Message in project tesb-rt-se by Talend.

the class SecurityContextFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    Message message = JAXRSUtils.getCurrentMessage();
    if (ui.getAbsolutePath().toString().endsWith(userRegistrationPath)) {
        return;
    }
    List<String> authValues = headers.getRequestHeader("Authorization");
    if (authValues.size() != 1) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String[] values = authValues.get(0).split(" ");
    if (values.length != 2 || !"Basic".equals(values[0])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String decodedValue = null;
    try {
        decodedValue = new String(Base64Utility.decode(values[1]));
    } catch (Base64Exception ex) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String[] namePassword = decodedValue.split(":");
    if (namePassword.length != 2) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    final UserAccount account = accounts.getAccount(namePassword[0]);
    if (account == null || !account.getPassword().equals(namePassword[1])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    final SecurityContext sc = new SecurityContext() {

        public Principal getUserPrincipal() {
            return new SimplePrincipal(account.getName());
        }

        public boolean isUserInRole(String arg0) {
            return false;
        }
    };
    message.put(SecurityContext.class, sc);
}
Also used : Message(org.apache.cxf.message.Message) Base64Exception(org.apache.cxf.common.util.Base64Exception) SecurityContext(org.apache.cxf.security.SecurityContext) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal)

Aggregations

Message (org.apache.cxf.message.Message)1002 Test (org.junit.Test)507 MessageImpl (org.apache.cxf.message.MessageImpl)291 Exchange (org.apache.cxf.message.Exchange)199 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)169 Endpoint (org.apache.cxf.endpoint.Endpoint)91 Interceptor (org.apache.cxf.interceptor.Interceptor)87 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)85 ArrayList (java.util.ArrayList)83 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)76 List (java.util.List)75 IOException (java.io.IOException)73 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)73 Method (java.lang.reflect.Method)69 Bus (org.apache.cxf.Bus)69 QName (javax.xml.namespace.QName)62 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)55 HashMap (java.util.HashMap)53 Fault (org.apache.cxf.interceptor.Fault)51 ByteArrayInputStream (java.io.ByteArrayInputStream)49