Search in sources :

Example 1 with PhaseInterceptor

use of org.apache.cxf.phase.PhaseInterceptor in project camel by apache.

the class MessageLossSimulator method handleMessage.

public void handleMessage(Message message) throws Fault {
    Object maps = RMContextUtils.retrieveMAPs(message, false, true);
    // RMContextUtils.ensureExposedVersion(maps);
    String action = getAction(maps);
    if (RMContextUtils.isRMProtocolMessage(action)) {
        return;
    }
    appMessageCount++;
    // do not discard odd-numbered messages
    if (0 != (appMessageCount % 2)) {
        return;
    }
    // discard even-numbered message
    InterceptorChain chain = message.getInterceptorChain();
    ListIterator<Interceptor<? extends Message>> it = chain.getIterator();
    while (it.hasNext()) {
        PhaseInterceptor<?> pi = (PhaseInterceptor<?>) it.next();
        if (MessageSenderInterceptor.class.getName().equals(pi.getId())) {
            chain.remove(pi);
            LOG.debug("Removed MessageSenderInterceptor from interceptor chain.");
            break;
        }
    }
    message.setContent(OutputStream.class, new WrappedOutputStream(message));
    message.getInterceptorChain().add(new AbstractPhaseInterceptor<Message>(Phase.PREPARE_SEND_ENDING) {

        public void handleMessage(Message message) throws Fault {
            try {
                message.getContent(OutputStream.class).close();
            } catch (IOException e) {
                throw new Fault(e);
            }
        }
    });
}
Also used : Message(org.apache.cxf.message.Message) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) MessageSenderInterceptor(org.apache.cxf.interceptor.MessageSenderInterceptor) AbstractWrappedOutputStream(org.apache.cxf.io.AbstractWrappedOutputStream) Interceptor(org.apache.cxf.interceptor.Interceptor) MessageSenderInterceptor(org.apache.cxf.interceptor.MessageSenderInterceptor) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor)

Example 2 with PhaseInterceptor

use of org.apache.cxf.phase.PhaseInterceptor in project tesb-rt-se by Talend.

the class DemoInterceptor method printInterceptorChain.

/**
 * Prints out the interceptor chain in a format that is easy to read. It
 * also filters out instances of the DemoInterceptor so you can see what the
 * chain would look like in a normal invokation.
 *
 * @param chain
 */
public void printInterceptorChain(InterceptorChain chain) {
    Iterator<Interceptor<? extends Message>> it = chain.iterator();
    String phase = "";
    StringBuilder builder = null;
    while (it.hasNext()) {
        Interceptor<? extends Message> interceptor = it.next();
        if (interceptor instanceof DemoInterceptor) {
            continue;
        }
        if (interceptor instanceof PhaseInterceptor) {
            PhaseInterceptor pi = (PhaseInterceptor) interceptor;
            if (!phase.equals(pi.getPhase())) {
                if (builder != null) {
                    System.out.println(builder.toString());
                } else {
                    builder = new StringBuilder(100);
                }
                builder.setLength(0);
                builder.append("             ");
                builder.append(pi.getPhase());
                builder.append(": ");
                phase = pi.getPhase();
            }
            String id = pi.getId();
            int idx = id.lastIndexOf('.');
            if (idx != -1) {
                id = id.substring(idx + 1);
            }
            builder.append(id);
            builder.append(' ');
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor)

Example 3 with PhaseInterceptor

use of org.apache.cxf.phase.PhaseInterceptor in project cxf by apache.

the class MaskSensitiveHelperTest method shouldReplaceSensitiveDataIn.

@Test
public void shouldReplaceSensitiveDataIn() {
    // Arrange
    final LoggingInInterceptor inInterceptor = new LoggingInInterceptor(logEventSender);
    inInterceptor.addSensitiveElementNames(SENSITIVE_ELEMENTS);
    final Message message = prepareInMessage();
    // Act
    Collection<PhaseInterceptor<? extends Message>> interceptors = inInterceptor.getAdditionalInterceptors();
    for (PhaseInterceptor intercept : interceptors) {
        intercept.handleMessage(message);
    }
    inInterceptor.handleMessage(message);
    // Verify
    LogEvent event = logEventSender.getLogEvent();
    assertNotNull(event);
    assertEquals(maskedContent, event.getPayload());
}
Also used : Message(org.apache.cxf.message.Message) LogEvent(org.apache.cxf.ext.logging.event.LogEvent) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) Test(org.junit.Test)

Example 4 with PhaseInterceptor

use of org.apache.cxf.phase.PhaseInterceptor in project cxf by apache.

the class TransformTest method transformInboundInterceptorReader.

@Test
public void transformInboundInterceptorReader() {
    // Arrange
    Message message = new MessageImpl();
    StringReader stringReader = new StringReader(ORIG_LOGGING_CONTENT);
    message.setContent(Reader.class, stringReader);
    Exchange exchange = new ExchangeImpl();
    message.setExchange(exchange);
    LogEventSenderMock logEventSender = new LogEventSenderMock();
    LoggingInInterceptor interceptor = new TransformLoggingInInterceptor(logEventSender);
    // Act
    Collection<PhaseInterceptor<? extends Message>> interceptors = interceptor.getAdditionalInterceptors();
    for (PhaseInterceptor intercept : interceptors) {
        intercept.handleMessage(message);
    }
    interceptor.handleMessage(message);
    // Verify
    LogEvent event = logEventSender.getLogEvent();
    assertNotNull(event);
    // only the first byte is read!
    assertEquals(TRANSFORMED_LOGGING_CONTENT, event.getPayload());
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) LogEvent(org.apache.cxf.ext.logging.event.LogEvent) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) StringReader(java.io.StringReader) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 5 with PhaseInterceptor

use of org.apache.cxf.phase.PhaseInterceptor in project cxf by apache.

the class TransformTest method transformInboundInterceptorInputStream.

@Test
public void transformInboundInterceptorInputStream() {
    // Arrange
    Message message = new MessageImpl();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(ORIG_LOGGING_CONTENT.getBytes(StandardCharsets.UTF_8));
    message.setContent(InputStream.class, inputStream);
    Exchange exchange = new ExchangeImpl();
    message.setExchange(exchange);
    LogEventSenderMock logEventSender = new LogEventSenderMock();
    LoggingInInterceptor interceptor = new TransformLoggingInInterceptor(logEventSender);
    // Act
    Collection<PhaseInterceptor<? extends Message>> interceptors = interceptor.getAdditionalInterceptors();
    for (PhaseInterceptor intercept : interceptors) {
        intercept.handleMessage(message);
    }
    interceptor.handleMessage(message);
    // Verify
    LogEvent event = logEventSender.getLogEvent();
    assertNotNull(event);
    // only the first byte is read!
    assertEquals(TRANSFORMED_LOGGING_CONTENT, event.getPayload());
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) LogEvent(org.apache.cxf.ext.logging.event.LogEvent) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Aggregations

PhaseInterceptor (org.apache.cxf.phase.PhaseInterceptor)12 Message (org.apache.cxf.message.Message)11 Interceptor (org.apache.cxf.interceptor.Interceptor)6 LogEvent (org.apache.cxf.ext.logging.event.LogEvent)5 Exchange (org.apache.cxf.message.Exchange)5 Test (org.junit.Test)5 InterceptorChain (org.apache.cxf.interceptor.InterceptorChain)4 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)4 MessageImpl (org.apache.cxf.message.MessageImpl)4 AddressingProperties (org.apache.cxf.ws.addressing.AddressingProperties)4 IOException (java.io.IOException)3 MessageSenderInterceptor (org.apache.cxf.interceptor.MessageSenderInterceptor)3 AbstractWrappedOutputStream (org.apache.cxf.io.AbstractWrappedOutputStream)3 AbstractPhaseInterceptor (org.apache.cxf.phase.AbstractPhaseInterceptor)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringReader (java.io.StringReader)2 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)2 Endpoint (org.apache.cxf.endpoint.Endpoint)2 Fault (org.apache.cxf.interceptor.Fault)2 AttributedURIType (org.apache.cxf.ws.addressing.AttributedURIType)2