1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.portlet.LiferayWindowState;
18  import com.liferay.portal.kernel.util.Validator;
19  
20  import java.io.IOException;
21  import java.io.OutputStream;
22  import java.io.PrintWriter;
23  
24  import java.util.Enumeration;
25  import java.util.Locale;
26  
27  import javax.portlet.CacheControl;
28  import javax.portlet.MimeResponse;
29  import javax.portlet.PortletRequest;
30  
31  import javax.servlet.http.HttpServletResponse;
32  
33  /**
34   * <a href="MimeResponseImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public abstract class MimeResponseImpl
39      extends PortletResponseImpl implements MimeResponse {
40  
41      public boolean isCalledFlushBuffer() {
42          return _calledFlushBuffer;
43      }
44  
45      public void flushBuffer() throws IOException {
46          _response.flushBuffer();
47  
48          _calledFlushBuffer = true;
49      }
50  
51      public int getBufferSize() {
52          return _response.getBufferSize();
53      }
54  
55      public CacheControl getCacheControl() {
56          return new CacheControlImpl(null, 0, false, false, this);
57      }
58  
59      public String getCharacterEncoding() {
60          return _response.getCharacterEncoding();
61      }
62  
63      public String getContentType() {
64          return _contentType;
65      }
66  
67      public Locale getLocale() {
68          return _portletRequestImpl.getLocale();
69      }
70  
71      public OutputStream getPortletOutputStream() throws IOException {
72          if (_calledGetWriter) {
73              throw new IllegalStateException(
74                  "Cannot obtain OutputStream because Writer is already in use");
75          }
76  
77          if (_contentType == null) {
78              setContentType(_portletRequestImpl.getResponseContentType());
79          }
80  
81          _calledGetPortletOutputStream = true;
82  
83          return _response.getOutputStream();
84      }
85  
86      public PrintWriter getWriter() throws IOException {
87          if (_calledGetPortletOutputStream) {
88              throw new IllegalStateException(
89                  "Cannot obtain Writer because OutputStream is already in use");
90          }
91  
92          if (_contentType == null) {
93              setContentType(_portletRequestImpl.getResponseContentType());
94          }
95  
96          _calledGetWriter = true;
97  
98          return _response.getWriter();
99      }
100 
101     public boolean isCalledGetPortletOutputStream() {
102         return _calledGetPortletOutputStream;
103     }
104 
105     public boolean isCalledGetWriter() {
106         return _calledGetWriter;
107     }
108 
109     public boolean isCommitted() {
110         return false;
111     }
112 
113     public void reset() {
114         if (_calledFlushBuffer) {
115             throw new IllegalStateException(
116                 "Cannot reset a buffer that has been flushed");
117         }
118     }
119 
120     public void resetBuffer() {
121         if (_calledFlushBuffer) {
122             throw new IllegalStateException(
123                 "Cannot reset a buffer that has been flushed");
124         }
125 
126         _response.resetBuffer();
127     }
128 
129     public void setBufferSize(int bufferSize) {
130         _response.setBufferSize(bufferSize);
131     }
132 
133     public void setContentType(String contentType) {
134         if (Validator.isNull(contentType)) {
135             throw new IllegalArgumentException("Content type cannot be null");
136         }
137 
138         Enumeration<String> enu = _portletRequestImpl.getResponseContentTypes();
139 
140         boolean valid = false;
141 
142         if (getLifecycle().equals(PortletRequest.RESOURCE_PHASE) ||
143             _portletRequestImpl.getWindowState().equals(
144                 LiferayWindowState.EXCLUSIVE)) {
145 
146             valid = true;
147         }
148         else {
149             while (enu.hasMoreElements()) {
150                 String resContentType = enu.nextElement();
151 
152                 if (contentType.startsWith(resContentType)) {
153                     valid = true;
154 
155                     break;
156                 }
157             }
158         }
159 
160         if (!valid) {
161             throw new IllegalArgumentException(
162                 contentType + " is not a supported mime type");
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 }