1   /**
2    * Copyright (c) 2000-2009 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          _response.flushBuffer();
56  
57          _calledFlushBuffer = true;
58      }
59  
60      public int getBufferSize() {
61          return _response.getBufferSize();
62      }
63  
64      public CacheControl getCacheControl() {
65          return new CacheControlImpl(null, 0, false, false);
66      }
67  
68      public String getCharacterEncoding() {
69          return _response.getCharacterEncoding();
70      }
71  
72      public String getContentType() {
73          return _contentType;
74      }
75  
76      public Locale getLocale() {
77          return _portletRequestImpl.getLocale();
78      }
79  
80      public OutputStream getPortletOutputStream() throws IOException {
81          if (_calledGetWriter) {
82              throw new IllegalStateException();
83          }
84  
85          if (_contentType == null) {
86              setContentType(_portletRequestImpl.getResponseContentType());
87          }
88  
89          _calledGetPortletOutputStream = true;
90  
91          return _response.getOutputStream();
92      }
93  
94      public PrintWriter getWriter() throws IOException {
95          if (_calledGetPortletOutputStream) {
96              throw new IllegalStateException();
97          }
98  
99          if (_contentType == null) {
100             setContentType(_portletRequestImpl.getResponseContentType());
101         }
102 
103         _calledGetWriter = true;
104 
105         return _response.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         if (_calledFlushBuffer) {
122             throw new IllegalStateException();
123         }
124     }
125 
126     public void resetBuffer() {
127         _response.resetBuffer();
128     }
129 
130     public void setBufferSize(int size) {
131         _response.setBufferSize(size);
132     }
133 
134     public void setContentType(String contentType) {
135         if (Validator.isNull(contentType)) {
136             throw new IllegalArgumentException();
137         }
138 
139         Enumeration<String> enu = _portletRequestImpl.getResponseContentTypes();
140 
141         boolean valid = false;
142 
143         if (getLifecycle().equals(PortletRequest.RESOURCE_PHASE) ||
144             _portletRequestImpl.getWindowState().equals(
145                 LiferayWindowState.EXCLUSIVE)) {
146 
147             valid = true;
148         }
149         else {
150             while (enu.hasMoreElements()) {
151                 String resContentType = enu.nextElement();
152 
153                 if (contentType.startsWith(resContentType)) {
154                     valid = true;
155 
156                     break;
157                 }
158             }
159         }
160 
161         if (!valid) {
162             throw new IllegalArgumentException();
163         }
164 
165         _contentType = contentType;
166 
167         _response.setContentType(contentType);
168     }
169 
170     protected void init(
171         PortletRequestImpl portletRequestImpl, HttpServletResponse response,
172         String portletName, long companyId, long plid) {
173 
174         super.init(portletRequestImpl, response, portletName, companyId, plid);
175 
176         _portletRequestImpl = portletRequestImpl;
177         _response = response;
178     }
179 
180     private PortletRequestImpl _portletRequestImpl;
181     private HttpServletResponse _response;
182     private String _contentType;
183     private boolean _calledGetPortletOutputStream;
184     private boolean _calledGetWriter;
185     private boolean _calledFlushBuffer;
186 
187 }