---------- Пересланное сообщение ----------
От кого: Максим Мартынов <[email protected]>
Дата: 21 сентября 2011 г. 10:11
Тема: Re: Process outgoing message before sending.
Кому: Daniel Kulp <[email protected]>
Thanks.
I wrote this code, and it's work for me.
public class SmevStreamOutIntercepter extends
AbstractPhaseInterceptor<Message> {
private static final Logger LOG =
LogUtils.getLogger(SmevStreamOutIntercepter.class);
public SmevStreamOutIntercepter() {
super(Phase.PRE_STREAM);
addBefore(StaxOutInterceptor.class.getName());
addAfter(LoggingOutInterceptor.class.getName());
}
@Override
public void handleMessage(Message message) throws Fault {
OutputStream originOutputStream = message.getContent(OutputStream.class);
CachedOutputStream newOutputStream = new CachedOutputStream();
message.setContent(OutputStream.class, newOutputStream);
message.getInterceptorChain().add(new
SmevStreamOutEndingIntercepter(originOutputStream));
}
public class SmevStreamOutEndingIntercepter extends
AbstractPhaseInterceptor<Message> {
OutputStream originOutputStream;
public SmevStreamOutEndingIntercepter(OutputStream originOutputStream) {
super(Phase.PRE_STREAM_ENDING);
this.originOutputStream = originOutputStream;
}
@Override
public void handleMessage(Message message) throws Fault {
try {
CachedOutputStream newOutputStream = (CachedOutputStream)
message.getContent(OutputStream.class);
newOutputStream.flush();
byte[] security = modifyEnvelop(newOutputStream.getBytes());
ByteArrayInputStream bis = new ByteArrayInputStream(security);
CachedOutputStream.copyStream(bis, originOutputStream, 1024);
newOutputStream.close();
originOutputStream.flush();
message.setContent(OutputStream.class, originOutputStream);
} catch (IOException e) {
LOG.severe(e.getMessage());
throw new Fault(e);
}
}
}
private byte[] modifyEnvelop(byte[] bytes) {
//modifyEnvelop
return bytes;
}
}
2011/9/20 Daniel Kulp <[email protected]>
> On Monday, September 19, 2011 4:15:40 PM Максим Мартынов wrote:
> > Error "Cannot Replace the To Soap Header" was because 2 Soap Envelop
> write
> > in request.
> > But i can't to replace outgoing stream.
> > Can anybody help?
>
> With the way CXF streams data onto the wire, replacing the contents of the
> stream is a bit more difficult. It's really a two part thing:
>
> 1) You would need one interceptor that lives very early in the chain that
> would grab the current OutputStream and saves it (likely on a unique key on
> the Message) for retrieval later. Then replace it with a new
> ByteArrayOutputStream or similar (FileOutputStream, CachedOutputStream,
> etc...). CXF would then use that for writing anything it produces.
>
>
> 2) A SECOND interceptor on the chain (late in the chain) that would grab
> the
> bytes from that replaces stream, manipulate it, produce new bytes, and then
> write that to the original output stream and then put the output stream
> back
> on the message.
>
> Dan
>
>
>
> > 19 сентября 2011 г. 9:49 пользователь Максим Мартынов
> >
> > <[email protected]>написал:
> > > I try to process OutputStream at Phase.PRE_STREAM_ENDING phase but now
> i
> > > can get error "javax.xml.ws.soap.SOAPFaultException: Cannot Replace the
> > > To Soap Header"
> > >
> > > My code is:
> > > OutputStream os = message.getContent(OutputStream.class);
> > >
> > > CacheAndWriteOutputStream cos = (CacheAndWriteOutputStream) os;
> > >
> > > byte[] security =
> cryptoService.createSmevHeaderSecurity(cos.getBytes(),
> > > fingerprint);
> > > ByteArrayInputStream bis = new ByteArrayInputStream(security);
> > >
> > > CachedOutputStream.copyStream(bis, os, 1024);
> > >
> > > os.flush();
> > > message.setContent(OutputStream.class, os);
> > >
> > > 2011/9/16 Максим Мартынов <[email protected]>
> > >
> > >> Hi. Please help.
> > >>
> > >> How i can get outgoing message in outIntercepter as *byte[]*?
> > >> Change it, and set it to original outgoing message?
> > >> In which phase a can do that?
> > >>
> > >> My task is to sign envelope, but service which can do this whant as
> > >> incoming parameters soap envelop as byte[] and result is signing soap
> > >> envelop, as byte[] too.
> > >>
> > >> Please give me an example?
> > >> *
> > >> *
> --
> Daniel Kulp
> [email protected]
> http://dankulp.com/blog
> Talend - http://www.talend.com
>