Search in sources :

Example 6 with PhaseInterceptor

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

the class TruncatedTest method truncatedInboundInterceptorReader.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void truncatedInboundInterceptorReader() throws IOException {
    Message message = new MessageImpl();
    StringReader stringReader = new StringReader("TestMessage");
    message.setContent(Reader.class, stringReader);
    Exchange exchange = new ExchangeImpl();
    message.setExchange(exchange);
    LogEventSenderMock logEventSender = new LogEventSenderMock();
    LoggingInInterceptor interceptor = new LoggingInInterceptor(logEventSender);
    // set limit to 1 byte in order to get a truncated message!
    interceptor.setLimit(1);
    Collection<PhaseInterceptor<? extends Message>> interceptors = interceptor.getAdditionalInterceptors();
    for (PhaseInterceptor intercept : interceptors) {
        intercept.handleMessage(message);
    }
    interceptor.handleMessage(message);
    LogEvent event = logEventSender.getLogEvent();
    assertNotNull(event);
    // only the first byte is read!
    assertEquals("T", event.getPayload());
    assertTrue(event.isTruncated());
}
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 7 with PhaseInterceptor

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

the class MessageLossSimulator method handleMessage.

public void handleMessage(Message message) throws Fault {
    AddressingProperties maps = RMContextUtils.retrieveMAPs(message, false, true);
    String action = null;
    if (maps != null && null != maps.getAction()) {
        action = maps.getAction().getValue();
    }
    if (RMContextUtils.isRMProtocolMessage(action)) {
        return;
    }
    if (MessageUtils.isPartialResponse(message)) {
        return;
    }
    if (Boolean.TRUE.equals(message.get(RMMessageConstants.RM_RETRANSMISSION))) {
        return;
    }
    if (mode == 1) {
        // never lose
        return;
    } else if (mode == -1) {
    // always lose
    } else {
        // alternatively lose
        synchronized (this) {
            appMessageCount++;
            if (0 != (appMessageCount % 2)) {
                return;
            }
        }
    }
    InterceptorChain chain = message.getInterceptorChain();
    ListIterator<Interceptor<? extends Message>> it = chain.getIterator();
    while (it.hasNext()) {
        PhaseInterceptor<?> pi = (PhaseInterceptor<? extends Message>) it.next();
        if (MessageSenderInterceptor.class.getName().equals(pi.getId())) {
            chain.remove(pi);
            LOG.fine("Removed MessageSenderInterceptor from interceptor chain.");
            break;
        }
    }
    message.setContent(OutputStream.class, new WrappedOutputStream(message));
    message.getInterceptorChain().add(new MessageLossEndingInterceptor(throwsException));
}
Also used : InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) Message(org.apache.cxf.message.Message) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) MessageSenderInterceptor(org.apache.cxf.interceptor.MessageSenderInterceptor) AbstractWrappedOutputStream(org.apache.cxf.io.AbstractWrappedOutputStream) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) 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 8 with PhaseInterceptor

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

the class RMSoapInInterceptor method updateServiceModelInfo.

/**
 * When invoked inbound, check if the action indicates that this is one of the
 * RM protocol messages (CreateSequence, CreateSequenceResponse, TerminateSequence)
 * and if so, replace references to the application service model with references to
 * the RM service model.
 * The addressing protocol handler must have extracted the action beforehand.
 * @see org.apache.cxf.transport.ChainInitiationObserver
 *
 * @param message the message
 */
private void updateServiceModelInfo(SoapMessage message) throws Fault {
    AddressingProperties maps = ContextUtils.retrieveMAPs(message, false, false, false);
    AttributedURIType actionURI = null == maps ? null : maps.getAction();
    String action = null == actionURI ? null : actionURI.getValue().trim();
    LOG.fine("action: " + action);
    RMConstants consts;
    if (RM10Constants.ACTIONS.contains(action)) {
        consts = RM10Constants.INSTANCE;
    } else if (RM11Constants.ACTIONS.contains(action)) {
        consts = RM11Constants.INSTANCE;
    } else {
        return;
    }
    RMProperties rmps = RMContextUtils.retrieveRMProperties(message, false);
    rmps.exposeAs(consts.getWSRMNamespace());
    ProtocolVariation protocol = ProtocolVariation.findVariant(consts.getWSRMNamespace(), maps.getNamespaceURI());
    LOG.info("Updating service model info in exchange");
    RMManager manager = getManager(message);
    assert manager != null;
    final RMEndpoint rme;
    try {
        rme = manager.getReliableEndpoint(message);
    } catch (RMException e) {
        throw new SoapFault(new org.apache.cxf.common.i18n.Message("CANNOT_PROCESS", LOG), e, message.getVersion().getSender());
    }
    Exchange exchange = message.getExchange();
    Endpoint ep = rme.getEndpoint(protocol);
    exchange.put(Endpoint.class, ep);
    exchange.put(Service.class, ep.getService());
    exchange.put(Binding.class, ep.getBinding());
    // Also set BindingOperationInfo as some operations (SequenceAcknowledgment) have
    // neither in nor out messages, and thus the WrappedInInterceptor cannot
    // determine the operation name.
    BindingInfo bi = ep.getEndpointInfo().getBinding();
    BindingOperationInfo boi = null;
    boolean isOneway = true;
    if (consts.getCreateSequenceAction().equals(action)) {
        if (RMContextUtils.isServerSide(message)) {
            boi = bi.getOperation(consts.getCreateSequenceOperationName());
            isOneway = false;
        } else {
            boi = bi.getOperation(consts.getCreateSequenceOnewayOperationName());
        }
    } else if (consts.getCreateSequenceResponseAction().equals(action)) {
        if (RMContextUtils.isServerSide(message)) {
            boi = bi.getOperation(consts.getCreateSequenceResponseOnewayOperationName());
        } else {
            boi = bi.getOperation(consts.getCreateSequenceOperationName());
            isOneway = false;
        }
    } else if (consts.getSequenceAckAction().equals(action)) {
        boi = bi.getOperation(consts.getSequenceAckOperationName());
    } else if (consts.getAckRequestedAction().equals(action)) {
        boi = bi.getOperation(consts.getAckRequestedOperationName());
    } else if (consts.getTerminateSequenceAction().equals(action)) {
        boi = bi.getOperation(consts.getTerminateSequenceOperationName());
    } else if (RM11Constants.INSTANCE.getTerminateSequenceResponseAction().equals(action)) {
        // TODO add server-side TSR handling
        boi = bi.getOperation(RM11Constants.INSTANCE.getTerminateSequenceOperationName());
        isOneway = false;
    } else if (consts.getCloseSequenceAction().equals(action)) {
        boi = bi.getOperation(consts.getCloseSequenceOperationName());
    } else if (RM11Constants.INSTANCE.getCloseSequenceResponseAction().equals(action)) {
        boi = bi.getOperation(RM11Constants.INSTANCE.getCloseSequenceOperationName());
        isOneway = false;
    }
    // make sure the binding information has been set
    if (boi == null) {
        LOG.fine("No BindingInfo for action " + action);
    } else {
        exchange.put(BindingOperationInfo.class, boi);
        exchange.setOneWay(isOneway);
    }
    if (!consts.getCreateSequenceResponseAction().equals(action) && !consts.getSequenceAckAction().equals(action) && !RM11Constants.INSTANCE.getTerminateSequenceResponseAction().equals(action) && !RM11Constants.INSTANCE.getCloseSequenceResponseAction().equals(action)) {
        LOG.fine("Changing requestor role from " + message.get(Message.REQUESTOR_ROLE) + " to false");
        Object originalRequestorRole = message.get(Message.REQUESTOR_ROLE);
        if (null != originalRequestorRole) {
            message.put(RMMessageConstants.ORIGINAL_REQUESTOR_ROLE, originalRequestorRole);
        }
        message.put(Message.REQUESTOR_ROLE, Boolean.FALSE);
    }
    // replace WrappedInInterceptor with BareInInterceptor if necessary
    // as RM protocol messages use parameter style BARE
    InterceptorChain chain = message.getInterceptorChain();
    ListIterator<Interceptor<? extends Message>> it = chain.getIterator();
    boolean bareIn = false;
    boolean wrappedIn = false;
    while (it.hasNext() && !wrappedIn && !bareIn) {
        PhaseInterceptor<? extends Message> pi = (PhaseInterceptor<? extends Message>) it.next();
        if (BareInInterceptor.class.getName().equals(pi.getId())) {
            bareIn = true;
        }
    }
    if (!bareIn) {
        chain.add(new BareInInterceptor());
        LOG.fine("Added BareInInterceptor to chain.");
    }
}
Also used : SoapFault(org.apache.cxf.binding.soap.SoapFault) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Message(org.apache.cxf.message.Message) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) RMConstants(org.apache.cxf.ws.rm.RMConstants) RMEndpoint(org.apache.cxf.ws.rm.RMEndpoint) RMException(org.apache.cxf.ws.rm.RMException) RMEndpoint(org.apache.cxf.ws.rm.RMEndpoint) Endpoint(org.apache.cxf.endpoint.Endpoint) RMManager(org.apache.cxf.ws.rm.RMManager) BindingInfo(org.apache.cxf.service.model.BindingInfo) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) AbstractRMInterceptor(org.apache.cxf.ws.rm.AbstractRMInterceptor) AbstractSoapInterceptor(org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor) BareInInterceptor(org.apache.cxf.wsdl.interceptors.BareInInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) RMProperties(org.apache.cxf.ws.rm.RMProperties) BareInInterceptor(org.apache.cxf.wsdl.interceptors.BareInInterceptor) ProtocolVariation(org.apache.cxf.ws.rm.ProtocolVariation) Exchange(org.apache.cxf.message.Exchange) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain)

Example 9 with PhaseInterceptor

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

the class RetransmissionQueueImpl method doResend.

private void doResend(SoapMessage message) {
    InputStream is = null;
    try {
        // NOPMD
        // initialize copied interceptor chain for message
        PhaseInterceptorChain retransmitChain = manager.getRetransmitChain(message);
        ProtocolVariation protocol = RMContextUtils.getProtocolVariation(message);
        Endpoint endpoint = manager.getReliableEndpoint(message).getEndpoint(protocol);
        PhaseChainCache cache = new PhaseChainCache();
        boolean after = true;
        if (retransmitChain == null) {
            // no saved retransmit chain, so construct one from scratch (won't work for WS-Security on server, so
            // need to fix)
            retransmitChain = buildRetransmitChain(endpoint, cache);
            after = false;
        }
        message.setInterceptorChain(retransmitChain);
        // clear flag for SOAP out interceptor so envelope will be written
        message.remove(SoapOutInterceptor.WROTE_ENVELOPE_START);
        // discard all saved content
        Set<Class<?>> formats = message.getContentFormats();
        List<CachedOutputStreamCallback> callbacks = null;
        for (Class<?> clas : formats) {
            Object content = message.getContent(clas);
            if (content != null) {
                LOG.info("Removing " + clas.getName() + " content of actual type " + content.getClass().getName());
                message.removeContent(clas);
                if (clas == OutputStream.class && content instanceof WriteOnCloseOutputStream) {
                    callbacks = ((WriteOnCloseOutputStream) content).getCallbacks();
                }
            }
        }
        // read SOAP headers from saved input stream
        CachedOutputStream cos = (CachedOutputStream) message.get(RMMessageConstants.SAVED_CONTENT);
        // CachedOutputStream is hold until delivering was successful
        cos.holdTempFile();
        // instance is needed to close input stream later on
        is = cos.getInputStream();
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(is, StandardCharsets.UTF_8.name());
        message.getHeaders().clear();
        if (reader.getEventType() != XMLStreamConstants.START_ELEMENT && reader.nextTag() != XMLStreamConstants.START_ELEMENT) {
            throw new IllegalStateException("No document found");
        }
        readHeaders(reader, message);
        int event;
        while ((event = reader.nextTag()) != XMLStreamConstants.START_ELEMENT) {
            if (event == XMLStreamConstants.END_ELEMENT) {
                throw new IllegalStateException("No body content present");
            }
        }
        // set message addressing properties
        AddressingProperties maps = MAPCodec.getInstance(message.getExchange().getBus()).unmarshalMAPs(message);
        RMContextUtils.storeMAPs(maps, message, true, MessageUtils.isRequestor(message));
        AttributedURIType to = null;
        if (null != maps) {
            to = maps.getTo();
        }
        if (null == to) {
            LOG.log(Level.SEVERE, "NO_ADDRESS_FOR_RESEND_MSG");
            return;
        }
        if (RMUtils.getAddressingConstants().getAnonymousURI().equals(to.getValue())) {
            LOG.log(Level.FINE, "Cannot resend to anonymous target");
            return;
        }
        // initialize conduit for new message
        Conduit c = message.getExchange().getConduit(message);
        if (c == null) {
            c = buildConduit(message, endpoint, to);
        }
        c.prepare(message);
        // replace standard message marshaling with copy from saved stream
        ListIterator<Interceptor<? extends Message>> iterator = retransmitChain.getIterator();
        while (iterator.hasNext()) {
            Interceptor<? extends Message> incept = iterator.next();
            // remove JAX-WS interceptors which handle message modes and such
            if (incept.getClass().getName().startsWith("org.apache.cxf.jaxws.interceptors")) {
                retransmitChain.remove(incept);
            } else if (incept instanceof PhaseInterceptor && Phase.MARSHAL.equals(((PhaseInterceptor<?>) incept).getPhase())) {
                // remove any interceptors from the marshal phase
                retransmitChain.remove(incept);
            }
        }
        retransmitChain.add(new CopyOutInterceptor(reader));
        // restore callbacks on output stream
        if (callbacks != null) {
            OutputStream os = message.getContent(OutputStream.class);
            if (os != null) {
                WriteOnCloseOutputStream woc;
                if (os instanceof WriteOnCloseOutputStream) {
                    woc = (WriteOnCloseOutputStream) os;
                } else {
                    woc = new WriteOnCloseOutputStream(os);
                    message.setContent(OutputStream.class, woc);
                }
                for (CachedOutputStreamCallback cb : callbacks) {
                    woc.registerCallback(cb);
                }
            }
        }
        // send the message
        message.put(RMMessageConstants.RM_RETRANSMISSION, Boolean.TRUE);
        if (after) {
            retransmitChain.doInterceptStartingAfter(message, RMCaptureOutInterceptor.class.getName());
        } else {
            retransmitChain.doIntercept(message);
        }
        if (LOG.isLoggable(Level.INFO)) {
            RMProperties rmps = RMContextUtils.retrieveRMProperties(message, true);
            SequenceType seq = rmps.getSequence();
            LOG.log(Level.INFO, "Retransmitted message " + seq.getMessageNumber() + " in sequence " + seq.getIdentifier().getValue());
        }
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "RESEND_FAILED_MSG", ex);
    } finally {
        // make sure to always close InputStreams of the CachedOutputStream to avoid leaving temp files undeleted
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
            // Ignore
            }
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Message(org.apache.cxf.message.Message) WriteOnCloseOutputStream(org.apache.cxf.io.WriteOnCloseOutputStream) CachedOutputStreamCallback(org.apache.cxf.io.CachedOutputStreamCallback) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) WriteOnCloseOutputStream(org.apache.cxf.io.WriteOnCloseOutputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) SequenceType(org.apache.cxf.ws.rm.v200702.SequenceType) RMCaptureOutInterceptor(org.apache.cxf.ws.rm.RMCaptureOutInterceptor) PhaseChainCache(org.apache.cxf.phase.PhaseChainCache) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) RMEndpoint(org.apache.cxf.ws.rm.RMEndpoint) Endpoint(org.apache.cxf.endpoint.Endpoint) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) RMCaptureOutInterceptor(org.apache.cxf.ws.rm.RMCaptureOutInterceptor) AbstractOutDatabindingInterceptor(org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) SoapOutInterceptor(org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor) RMProperties(org.apache.cxf.ws.rm.RMProperties) ProtocolVariation(org.apache.cxf.ws.rm.ProtocolVariation) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) InputStream(java.io.InputStream) IOException(java.io.IOException) RMEndpoint(org.apache.cxf.ws.rm.RMEndpoint) Endpoint(org.apache.cxf.endpoint.Endpoint) XMLStreamException(javax.xml.stream.XMLStreamException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) RMException(org.apache.cxf.ws.rm.RMException) Conduit(org.apache.cxf.transport.Conduit)

Example 10 with PhaseInterceptor

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

the class MessageLossSimulator method handleMessage.

// CHECKSTYLE:OFF: ReturnCount
public void handleMessage(Message message) throws Fault {
    AddressingProperties maps = RMContextUtils.retrieveMAPs(message, false, true);
    // Fixed the build error of ws_rm, now there is no ensureExposedVersion anymore
    // RMContextUtils.ensureExposedVersion(maps);
    String action = null;
    if (maps != null && null != maps.getAction()) {
        action = maps.getAction().getValue();
    }
    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<? extends Message> pi = (PhaseInterceptor<? extends Message>) it.next();
        if (MessageSenderInterceptor.class.getName().equals(pi.getId())) {
            chain.remove(pi);
            LOG.info("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) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) Interceptor(org.apache.cxf.interceptor.Interceptor) MessageSenderInterceptor(org.apache.cxf.interceptor.MessageSenderInterceptor) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor)

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