View Javadoc

1   /*
2    * $Id: $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.portlet.servlet;
22  
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  import java.util.Locale;
26  
27  import javax.portlet.PortletResponse;
28  import javax.portlet.RenderResponse;
29  import javax.servlet.ServletOutputStream;
30  import javax.servlet.http.Cookie;
31  import javax.servlet.http.HttpServletResponse;
32  
33  public class PortletServletResponse implements HttpServletResponse {
34  
35  	private PortletResponse portletResponse;
36  	
37  	public PortletServletResponse(PortletResponse portletResponse) {
38  		this.portletResponse = portletResponse;
39  	}
40  	
41  	public void addCookie(Cookie cookie) {
42  		throw new IllegalStateException("Not allowed in a portlet");
43  	}
44  
45  	public void addDateHeader(String name, long date) {
46  		throw new IllegalStateException("Not allowed in a portlet");
47  	}
48  
49  	public void addHeader(String name, String value) {
50  		throw new IllegalStateException("Not allowed in a portlet");
51  	}
52  
53  	public void addIntHeader(String name, int value) {
54  		throw new IllegalStateException("Not allowed in a portlet");
55  	}
56  
57  	public boolean containsHeader(String name) {
58  		throw new IllegalStateException("Not allowed in a portlet");
59  	}
60  
61  	public String encodeRedirectURL(String url) {
62  		throw new IllegalStateException("Not allowed in a portlet");
63  	}
64  
65  	public String encodeRedirectUrl(String url) {
66  		throw new IllegalStateException("Not allowed in a portlet");
67  	}
68  
69  	public String encodeURL(String url) {
70  		throw new IllegalStateException("Not allowed in a portlet");
71  	}
72  
73  	public String encodeUrl(String url) {
74  		throw new IllegalStateException("Not allowed in a portlet");
75  	}
76  
77  	public void sendError(int sc) throws IOException {
78  		throw new IllegalStateException("Not allowed in a portlet");
79  	}
80  
81  	public void sendError(int sc, String msg) throws IOException {
82  		throw new IllegalStateException("Not allowed in a portlet");
83  	}
84  
85  	public void sendRedirect(String location) throws IOException {
86  		throw new IllegalStateException("Not allowed in a portlet");
87  	}
88  
89  	public void setDateHeader(String name, long date) {
90  		throw new IllegalStateException("Not allowed in a portlet");
91  	}
92  
93  	public void setHeader(String name, String value) {
94  		throw new IllegalStateException("Not allowed in a portlet");
95  	}
96  
97  	public void setIntHeader(String name, int value) {
98  		throw new IllegalStateException("Not allowed in a portlet");
99  	}
100 
101 	public void setStatus(int sc) {
102 		throw new IllegalStateException("Not allowed in a portlet");
103 	}
104 
105 	public void setStatus(int sc, String sm) {
106 		throw new IllegalStateException("Not allowed in a portlet");
107 	}
108 
109 	public void flushBuffer() throws IOException {
110 		if(portletResponse instanceof RenderResponse) {
111 			((RenderResponse)portletResponse).flushBuffer();
112 		}
113 		else {
114 			throw new IllegalStateException("Not allowed in event phase");
115 		}
116 	}
117 
118 	public int getBufferSize() {
119 		if(portletResponse instanceof RenderResponse) {
120 			return ((RenderResponse)portletResponse).getBufferSize();
121 		}
122 		else {
123 			throw new IllegalStateException("Not allowed in event phase");
124 		}
125 	}
126 
127 	public String getCharacterEncoding() {
128 		if(portletResponse instanceof RenderResponse) {
129 			return ((RenderResponse)portletResponse).getCharacterEncoding();
130 		}
131 		else {
132 			throw new IllegalStateException("Not allowed in event phase");
133 		}
134 	}
135 
136 	public String getContentType() {
137 		if(portletResponse instanceof RenderResponse) {
138 			return ((RenderResponse)portletResponse).getContentType();
139 		}
140 		else {
141 			throw new IllegalStateException("Not allowed in event phase");
142 		}
143 	}
144 
145 	public Locale getLocale() {
146 		if(portletResponse instanceof RenderResponse) {
147 			return ((RenderResponse)portletResponse).getLocale();
148 		}
149 		else {
150 			throw new IllegalStateException("Not allowed in event phase");
151 		}
152 	}
153 
154 	public ServletOutputStream getOutputStream() throws IOException {
155 		if(portletResponse instanceof RenderResponse) {
156 			return new PortletServletOutputStream(((RenderResponse)portletResponse).getPortletOutputStream());
157 		}
158 		else {
159 			throw new IllegalStateException("Not allowed in event phase");
160 		}
161 	}
162 
163 	public PrintWriter getWriter() throws IOException {
164 		if(portletResponse instanceof RenderResponse) {
165 			return ((RenderResponse)portletResponse).getWriter();
166 		}
167 		else {
168 			throw new IllegalStateException("Not allowed in event phase");
169 		}
170 	}
171 
172 	public boolean isCommitted() {
173 		if(portletResponse instanceof RenderResponse) {
174 			return ((RenderResponse)portletResponse).isCommitted();
175 		}
176 		else {
177 			throw new IllegalStateException("Not allowed in event phase");
178 		}
179 	}
180 
181 	public void reset() {
182 		if(portletResponse instanceof RenderResponse) {
183 			((RenderResponse)portletResponse).reset();
184 		}
185 		else {
186 			throw new IllegalStateException("Not allowed in event phase");
187 		}
188 	}
189 
190 	public void resetBuffer() {
191 		if(portletResponse instanceof RenderResponse) {
192 			((RenderResponse)portletResponse).resetBuffer();
193 		}
194 		else {
195 			throw new IllegalStateException("Not allowed in event phase");
196 		}
197 	}
198 
199 	public void setBufferSize(int size) {
200 		if(portletResponse instanceof RenderResponse) {
201 			((RenderResponse)portletResponse).setBufferSize(size);
202 		}
203 		else {
204 			throw new IllegalStateException("Not allowed in event phase");
205 		}
206 	}
207 
208 	public void setCharacterEncoding(String charset) {
209 		throw new IllegalStateException("Not allowed in a portlet");
210 	}
211 
212 	public void setContentLength(int len) {
213 		throw new IllegalStateException("Not allowed in a portlet");
214 	}
215 
216 	public void setContentType(String type) {
217 		if(portletResponse instanceof RenderResponse) {
218 			((RenderResponse)portletResponse).setContentType(type);
219 		}
220 		else {
221 			throw new IllegalStateException("Not allowed in event phase");
222 		}
223 	}
224 
225 	public void setLocale(Locale loc) {
226 		throw new IllegalStateException("Not allowed in a portlet");
227 	}
228 
229 	public PortletResponse getPortletResponse() {
230 		return portletResponse;
231 	}
232 
233 }