View Javadoc

1   /* This software is published under the terms of the OpenSymphony Software
2    * License version 1.1, of which a copy has been included with this
3    * distribution in the LICENSE.txt file. */
4   package com.atlassian.gzipfilter;
5   
6   import javax.servlet.ServletOutputStream;
7   import java.io.IOException;
8   
9   /**
10   * Provides a ServletOutputStream that routes through to another ServletOutputStream, however the destination
11   * can be changed at any point. The destination can be passed in using a factory, so it will not be created
12   * until it's actually needed.
13   *
14   * @author Joe Walnes
15   * @version $Revision: 1.1 $
16   */
17  public class RoutableServletOutputStream extends ServletOutputStream {
18  
19      private ServletOutputStream destination;
20      private DestinationFactory factory;
21      private Runnable callBeforeUse;
22      private static final Runnable doNothing = new Runnable()
23      {
24          public void run()
25          {
26          }
27      };
28  
29      /**
30       * Factory to lazily instantiate the destination.
31       */
32      public static interface DestinationFactory {
33          ServletOutputStream create() throws IOException;
34      }
35  
36      public RoutableServletOutputStream(DestinationFactory factory, final Runnable callBeforeUse) {
37          this.factory = factory;
38          this.callBeforeUse = callBeforeUse;
39      }
40  
41      public RoutableServletOutputStream(final DestinationFactory factory)
42      {
43          this(factory, doNothing);
44      }
45  
46      private ServletOutputStream getDestination() throws IOException {
47          if (destination == null) {
48              destination = factory.create();
49          }
50  
51          callBeforeUse.run();
52  
53          return destination;
54      }
55  
56      public void updateDestination(DestinationFactory factory) {
57          destination = null;
58          this.factory = factory;
59      }
60  
61      public void close() throws IOException {
62          getDestination().close();
63      }
64  
65      public void write(int b) throws IOException {
66          getDestination().write(b);
67      }
68  
69      public void print(String s) throws IOException {
70          getDestination().print(s);
71      }
72  
73      public void print(boolean b) throws IOException {
74          getDestination().print(b);
75      }
76  
77      public void print(char c) throws IOException {
78          getDestination().print(c);
79      }
80  
81      public void print(int i) throws IOException {
82          getDestination().print(i);
83      }
84  
85      public void print(long l) throws IOException {
86          getDestination().print(l);
87      }
88  
89      public void print(float v) throws IOException {
90          getDestination().print(v);
91      }
92  
93      public void print(double v) throws IOException {
94          getDestination().print(v);
95      }
96  
97      public void println() throws IOException {
98          getDestination().println();
99      }
100 
101     public void println(String s) throws IOException {
102         getDestination().println(s);
103     }
104 
105     public void println(boolean b) throws IOException {
106         getDestination().println(b);
107     }
108 
109     public void println(char c) throws IOException {
110         getDestination().println(c);
111     }
112 
113     public void println(int i) throws IOException {
114         getDestination().println(i);
115     }
116 
117     public void println(long l) throws IOException {
118         getDestination().println(l);
119     }
120 
121     public void println(float v) throws IOException {
122         getDestination().println(v);
123     }
124 
125     public void println(double v) throws IOException {
126         getDestination().println(v);
127     }
128 
129     public void write(byte b[]) throws IOException {
130         getDestination().write(b);
131     }
132 
133     public void write(byte b[], int off, int len) throws IOException {
134         getDestination().write(b, off, len);
135     }
136 
137     public void flush() throws IOException {
138         getDestination().flush();
139     }
140 }