1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.portlet.LiferayWindowState;
26  import com.liferay.portal.kernel.util.Validator;
27  
28  import java.io.IOException;
29  import java.io.OutputStream;
30  import java.io.PrintWriter;
31  
32  import java.util.Enumeration;
33  import java.util.Locale;
34  
35  import javax.portlet.CacheControl;
36  import javax.portlet.MimeResponse;
37  import javax.portlet.PortletRequest;
38  
39  import javax.servlet.http.HttpServletResponse;
40  
41  /**
42   * <a href="MimeResponseImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public abstract class MimeResponseImpl
48      extends PortletResponseImpl implements MimeResponse {
49  
50      public boolean isCalledFlushBuffer() {
51          return _calledFlushBuffer;
52      }
53  
54      public void flushBuffer() throws IOException {
55          _res.flushBuffer();
56  
57          _calledFlushBuffer = true;
58      }
59  
60      public int getBufferSize() {
61          return _res.getBufferSize();
62      }
63  
64      public CacheControl getCacheControl() {
65          return new CacheControlImpl(null, 0, false, false);
66      }
67  
68      public String getCharacterEncoding() {
69          return _res.getCharacterEncoding();
70      }
71  
72      public String getContentType() {
73          return _contentType;
74      }
75  
76      public Locale getLocale() {
77          return _req.getLocale();
78      }
79  
80      public OutputStream getPortletOutputStream() throws IOException {
81          if (_calledGetWriter) {
82              throw new IllegalStateException();
83          }
84  
85          if (_contentType == null) {
86              throw new IllegalStateException();
87          }
88  
89          _calledGetPortletOutputStream = true;
90  
91          return _res.getOutputStream();
92      }
93  
94      public PrintWriter getWriter() throws IOException {
95          if (_calledGetPortletOutputStream) {
96              throw new IllegalStateException();
97          }
98  
99          if (_contentType == null) {
100             throw new IllegalStateException();
101         }
102 
103         _calledGetWriter = true;
104 
105         return _res.getWriter();
106     }
107 
108     public boolean isCalledGetPortletOutputStream() {
109         return _calledGetPortletOutputStream;
110     }
111 
112     public boolean isCalledGetWriter() {
113         return _calledGetWriter;
114     }
115 
116     public boolean isCommitted() {
117         return false;
118     }
119 
120     public void reset() {
121     }
122 
123     public void resetBuffer() {
124         _res.resetBuffer();
125     }
126 
127     public void setBufferSize(int size) {
128         _res.setBufferSize(size);
129     }
130 
131     public void setContentType(String contentType) {
132         if (Validator.isNull(contentType)) {
133             throw new IllegalArgumentException();
134         }
135 
136         Enumeration<String> enu = _req.getResponseContentTypes();
137 
138         boolean valid = false;
139 
140         if (getLifecycle().equals(PortletRequest.RESOURCE_PHASE) ||
141             _req.getWindowState().equals(LiferayWindowState.EXCLUSIVE)) {
142 
143             valid = true;
144         }
145         else {
146             while (enu.hasMoreElements()) {
147                 String resContentType = enu.nextElement();
148 
149                 if (contentType.startsWith(resContentType)) {
150                     valid = true;
151 
152                     break;
153                 }
154             }
155         }
156 
157         if (!valid) {
158             throw new IllegalArgumentException();
159         }
160 
161         _contentType = contentType;
162     }
163 
164     protected void init(
165         PortletRequestImpl req, HttpServletResponse res, String portletName,
166         long companyId, long plid) {
167 
168         super.init(req, res, portletName, companyId, plid);
169 
170         _req = req;
171         _res = res;
172     }
173 
174     protected void recycle() {
175         super.recycle();
176 
177         _req = null;
178         _res = null;
179         _contentType = null;
180         _calledGetPortletOutputStream = false;
181         _calledGetWriter = false;
182         _calledFlushBuffer = true;
183     }
184 
185     private PortletRequestImpl _req;
186     private HttpServletResponse _res;
187     private String _contentType;
188     private boolean _calledGetPortletOutputStream;
189     private boolean _calledGetWriter;
190     private boolean _calledFlushBuffer;
191 
192 }