01: package demo.streams.common;
02:
03: import java.io.IOException;
04: import java.util.Map;
05: import java.util.logging.Logger;
06: import java.util.zip.GZIPInputStream;
07: import java.util.zip.GZIPOutputStream;
08: import javax.xml.ws.ProtocolException;
09: import javax.xml.ws.handler.MessageContext;
10: import org.objectweb.celtix.context.StreamMessageContext;
11: import org.objectweb.celtix.handlers.StreamHandler;
12:
13: public class CompressionStreamHandler implements StreamHandler {
14:
15: private static final Logger LOG = Logger
16: .getLogger(CompressionStreamHandler.class.getName());
17:
18: public final boolean handleMessage(StreamMessageContext ctx) {
19: setUpStream(ctx);
20: return true;
21: }
22:
23: public final boolean handleFault(StreamMessageContext ctx) {
24: setUpStream(ctx);
25: return true;
26: }
27:
28: public final void init(final Map map) {
29: System.out.println("CompressionStreamHandler initialised");
30: }
31:
32: public final void destroy() {
33: }
34:
35: public final void close(MessageContext messageContext) {
36: System.out.println("CompressionStreamHandler closed");
37: }
38:
39: private void setUpStream(StreamMessageContext ctx) {
40: if (isOutbound(ctx)) {
41: LOG.info("encrypting message stream");
42: // compress outbound messages
43: setupCompressionOutputStream(ctx);
44: } else {
45: LOG.info("decrypting message stream");
46: // decompress inbound messages
47: setupDecompressionInputStream(ctx);
48: }
49: }
50:
51: private void setupDecompressionInputStream(StreamMessageContext ctx) {
52: try {
53: ctx
54: .setInputStream(new GZIPInputStream(ctx
55: .getInputStream()));
56: } catch (IOException ex) {
57: throw new ProtocolException(ex);
58: }
59: }
60:
61: private void setupCompressionOutputStream(StreamMessageContext ctx) {
62:
63: try {
64: ctx.setOutputStream(new GZIPOutputStream(ctx
65: .getOutputStream()));
66: } catch (IOException ex) {
67: throw new ProtocolException(ex);
68: }
69: }
70:
71: private boolean isOutbound(MessageContext ctx) {
72: return (Boolean) ctx
73: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
74: }
75:
76: }
|