Необходимо написать перехватчик, чтобы изменить ответ. Я написал перехватчик для ответного ответа. Перехватчик добавляется в фазу SEND, поскольку outStream недоступен для изменения до фазы SEND.Apache CXF Interceptors: Невозможно изменить ответ. Поток в перехватчике Out
Я попытался изменить существующий выходной поток в объекте org.apache.cxf.message.Message с новыми байтами данных, а также попытался добавить новый OutputStream в объект Message. Но никто из них не работал.
Пожалуйста, дайте мне знать, если кто-то сталкивается с тем же, и если есть решение для него.
Спасибо.
public class SResponseInterceptor2 extends AbstractPhaseInterceptor<Message> {
private static final Logger LOGGER = LogManager.getLogger(SResponseInterceptor2.class.getName());
public SResponseInterceptor2() {
super(Phase.SEND);
addBefore(MessageSenderInterceptor.class.getName());
LOGGER.info(">>>>>>>>>>>>>>>>>>>>>>>>");
}
public void handleMessage(Message message) throws Fault {
OutputStream outputStream = message.getContent(OutputStream.class);
if(outputStream!=null && outputStream instanceof CacheAndWriteOutputStream){
CacheAndWriteOutputStream cachedOutputStream = (CacheAndWriteOutputStream)outputStream;
try{
String inputMessage = new String(cachedOutputStream.getBytes());
cachedOutputStream.flush();
byte[] bytes = changeResponse(inputMessage).getBytes();
cachedOutputStream.write(bytes);
/* Tried adding a new Stream with the updated data too
OutputStream modifiedOutputStream = new ByteArrayOutputStream();
CacheAndWriteOutputStream cWStream = new CacheAndWriteOutputStream(modifiedOutputStream);
cWStream.write(bytes, 0, bytes.length);
message.setContent(OutputStream.class, cWStream);
*/
message.setContent(OutputStream.class, cachedOutputStream);
} catch (IOException ioe) {
LOGGER.error("Error while changing the Response ." + ioe.getMessage());
ioe.printStackTrace();
}finally{
}
private String changeResponse(String responseMessage) {
responseMessage = responseMessage.replaceAll("sResponse", "sResponse??????");
LOGGER.info("After change message is " + responseMessage);
return responseMessage;
}
}
Проверить этот ответ http://stackoverflow.com/a/12948702/6371459 я думаю, что это то же самое, как ваш – pedrofb
Благодарит так много. Он работал. Я предлагаю для улучшения документации Apache. – vinr
Привет, Pedroft .. Предложение работает, но выходной поток (LoadingByteArrayOutputStream), завернутый в CacheOutputStream, ограничен обработкой только 2046 байтов данных. При появлении нового сообщения из message.getInterceptorChain(). DoIntercept (message); возвращает сообщение без данных в выходном потоке, если у него есть огромные данные для обработки. Потерпел изменение предела размера OutputStream, но это не помогло. Все предложения ?? Спасибо – vinr