1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.portlet.LiferayWindowState;
23  import com.liferay.portal.kernel.util.Validator;
24  
25  import java.io.IOException;
26  import java.io.OutputStream;
27  import java.io.PrintWriter;
28  
29  import java.util.Enumeration;
30  import java.util.Locale;
31  
32  import javax.portlet.CacheControl;
33  import javax.portlet.MimeResponse;
34  import javax.portlet.PortletRequest;
35  
36  import javax.servlet.http.HttpServletResponse;
37  
38  /**
39   * <a href="MimeResponseImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public abstract class MimeResponseImpl
45      extends PortletResponseImpl implements MimeResponse {
46  
47      public boolean isCalledFlushBuffer() {
48          return _calledFlushBuffer;
49      }
50  
51      public void flushBuffer() throws IOException {
52          _response.flushBuffer();
53  
54          _calledFlushBuffer = true;
55      }
56  
57      public int getBufferSize() {
58          return _response.getBufferSize();
59      }
60  
61      public CacheControl getCacheControl() {
62          return new CacheControlImpl(null, 0, false, false);
63      }
64  
65      public String getCharacterEncoding() {
66          return _response.getCharacterEncoding();
67      }
68  
69      public String getContentType() {
70          return _contentType;
71      }
72  
73      public Locale getLocale() {
74          return _portletRequestImpl.getLocale();
75      }
76  
77      public OutputStream getPortletOutputStream() throws IOException {
78          if (_calledGetWriter) {
79              throw new IllegalStateException();
80          }
81  
82          if (_contentType == null) {
83              setContentType(_portletRequestImpl.getResponseContentType());
84          }
85  
86          _calledGetPortletOutputStream = true;
87  
88          return _response.getOutputStream();
89      }
90  
91      public PrintWriter getWriter() throws IOException {
92          if (_calledGetPortletOutputStream) {
93              throw new IllegalStateException();
94          }
95  
96          if (_contentType == null) {
97              setContentType(_portletRequestImpl.getResponseContentType());
98          }
99  
100         _calledGetWriter = true;
101 
102         return _response.getWriter();
103     }
104 
105     public boolean isCalledGetPortletOutputStream() {
106         return _calledGetPortletOutputStream;
107     }
108 
109     public boolean isCalledGetWriter() {
110         return _calledGetWriter;
111     }
112 
113     public boolean isCommitted() {
114         return false;
115     }
116 
117     public void reset() {
118         if (_calledFlushBuffer) {
119             throw new IllegalStateException();
120         }
121     }
122 
123     public void resetBuffer() {
124         _response.resetBuffer();
125     }
126 
127     public void setBufferSize(int size) {
128         _response.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 = _portletRequestImpl.getResponseContentTypes();
137 
138         boolean valid = false;
139 
140         if (getLifecycle().equals(PortletRequest.RESOURCE_PHASE) ||
141             _portletRequestImpl.getWindowState().equals(
142                 LiferayWindowState.EXCLUSIVE)) {
143 
144             valid = true;
145         }
146         else {
147             while (enu.hasMoreElements()) {
148                 String resContentType = enu.nextElement();
149 
150                 if (contentType.startsWith(resContentType)) {
151                     valid = true;
152 
153                     break;
154                 }
155             }
156         }
157 
158         if (!valid) {
159             throw new IllegalArgumentException();
160         }
161 
162         _contentType = contentType;
163 
164         _response.setContentType(contentType);
165     }
166 
167     protected void init(
168         PortletRequestImpl portletRequestImpl, HttpServletResponse response,
169         String portletName, long companyId, long plid) {
170 
171         super.init(portletRequestImpl, response, portletName, companyId, plid);
172 
173         _portletRequestImpl = portletRequestImpl;
174         _response = response;
175     }
176 
177     protected void recycle() {
178         super.recycle();
179 
180         _portletRequestImpl = null;
181         _response = null;
182         _contentType = null;
183         _calledGetPortletOutputStream = false;
184         _calledGetWriter = false;
185         _calledFlushBuffer = true;
186     }
187 
188     private PortletRequestImpl _portletRequestImpl;
189     private HttpServletResponse _response;
190     private String _contentType;
191     private boolean _calledGetPortletOutputStream;
192     private boolean _calledGetWriter;
193     private boolean _calledFlushBuffer;
194 
195 }