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());
}
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));
}
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.");
}
}
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
}
}
}
}
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);
}
}
});
}
Aggregations