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  
22      /**
23       * Factory to lazily instantiate the destination.
24       */
25      public static interface DestinationFactory {
26          ServletOutputStream create() throws IOException;
27      }
28  
29      public RoutableServletOutputStream(DestinationFactory factory) {
30          this.factory = factory;
31      }
32  
33      private ServletOutputStream getDestination() throws IOException {
34          if (destination == null) {
35              destination = factory.create();
36          }
37          return destination;
38      }
39  
40      public void updateDestination(DestinationFactory factory) {
41          destination = null;
42          this.factory = factory;
43      }
44  
45      public void close() throws IOException {
46          getDestination().close();
47      }
48  
49      public void write(int b) throws IOException {
50          getDestination().write(b);
51      }
52  
53      public void print(String s) throws IOException {
54          getDestination().print(s);
55      }
56  
57      public void print(boolean b) throws IOException {
58          getDestination().print(b);
59      }
60  
61      public void print(char c) throws IOException {
62          getDestination().print(c);
63      }
64  
65      public void print(int i) throws IOException {
66          getDestination().print(i);
67      }
68  
69      public void print(long l) throws IOException {
70          getDestination().print(l);
71      }
72  
73      public void print(float v) throws IOException {
74          getDestination().print(v);
75      }
76  
77      public void print(double v) throws IOException {
78          getDestination().print(v);
79      }
80  
81      public void println() throws IOException {
82          getDestination().println();
83      }
84  
85      public void println(String s) throws IOException {
86          getDestination().println(s);
87      }
88  
89      public void println(boolean b) throws IOException {
90          getDestination().println(b);
91      }
92  
93      public void println(char c) throws IOException {
94          getDestination().println(c);
95      }
96  
97      public void println(int i) throws IOException {
98          getDestination().println(i);
99      }
100 
101     public void println(long l) throws IOException {
102         getDestination().println(l);
103     }
104 
105     public void println(float v) throws IOException {
106         getDestination().println(v);
107     }
108 
109     public void println(double v) throws IOException {
110         getDestination().println(v);
111     }
112 
113     public void write(byte b[]) throws IOException {
114         getDestination().write(b);
115     }
116 
117     public void write(byte b[], int off, int len) throws IOException {
118         getDestination().write(b, off, len);
119     }
120 
121     public void flush() throws IOException {
122         getDestination().flush();
123     }
124 }