001: /*
002: * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.widget;
030:
031: import com.caucho.util.L10N;
032:
033: import javax.portlet.*;
034: import java.io.IOException;
035: import java.util.Enumeration;
036: import java.util.Locale;
037: import java.util.Map;
038: import java.util.logging.Logger;
039:
040: public class PortletWidgetConnection extends WidgetConnection {
041: private static L10N L = new L10N(PortletWidgetConnection.class);
042:
043: static protected final Logger log = Logger
044: .getLogger(PortletWidgetConnection.class.getName());
045:
046: private PortletRequest _portletRequest;
047: private PortletResponse _portletResponse;
048: private ActionRequest _actionRequest;
049: private ActionResponse _actionResponse;
050: private RenderRequest _renderRequest;
051: private RenderResponse _renderResponse;
052:
053: private WidgetWriter _widgetWriter;
054:
055: static public <S extends WidgetState> S prepare(
056: PortletRequest request, PortletResponse response,
057: Widget<S> top) throws PortletException {
058: WidgetConnection widgetConnection = PortletWidgetConnection
059: .create(request, response);
060:
061: try {
062: return widgetConnection.prepare(top);
063: } catch (WidgetException ex) {
064: throw new PortletException(ex);
065: }
066: }
067:
068: static public <S extends WidgetState> S render(
069: RenderRequest request, RenderResponse response,
070: Widget<S> top) throws PortletException, IOException {
071: WidgetConnection widgetConnection = PortletWidgetConnection
072: .create(request, response);
073:
074: try {
075: return widgetConnection.render(top);
076: } catch (WidgetException ex) {
077: throw new PortletException(ex);
078: }
079: }
080:
081: public static PortletWidgetConnection create(
082: PortletRequest request, PortletResponse response) {
083: PortletWidgetConnection connection = new PortletWidgetConnection();
084:
085: connection.start(request, response);
086:
087: return connection;
088: }
089:
090: public static PortletWidgetConnection create(ActionRequest request,
091: ActionResponse response) {
092: PortletWidgetConnection connection = new PortletWidgetConnection();
093:
094: connection.start(request, response);
095:
096: return connection;
097: }
098:
099: public static PortletWidgetConnection create(RenderRequest request,
100: RenderResponse response) {
101: PortletWidgetConnection connection = new PortletWidgetConnection();
102:
103: connection.start(request, response);
104:
105: return connection;
106: }
107:
108: PortletWidgetConnection() {
109: }
110:
111: void start(PortletRequest request, PortletResponse response) {
112: finish();
113: _portletRequest = request;
114: _portletResponse = response;
115: }
116:
117: void start(ActionRequest request, ActionResponse response) {
118: finish();
119: _portletRequest = request;
120: _portletResponse = response;
121: _actionRequest = request;
122: _actionResponse = response;
123: }
124:
125: void start(RenderRequest request, RenderResponse response) {
126: finish();
127: _portletRequest = request;
128: _portletResponse = response;
129: _renderRequest = request;
130: _renderResponse = response;
131: }
132:
133: public void finish() {
134: _widgetWriter = null;
135: _portletRequest = null;
136: _portletResponse = null;
137: _actionRequest = null;
138: _actionResponse = null;
139: _renderRequest = null;
140: _renderResponse = null;
141: }
142:
143: RenderRequest getRenderRequest() {
144: return _renderRequest;
145: }
146:
147: RenderResponse getRenderResponse() {
148: return _renderResponse;
149: }
150:
151: public <S extends WidgetState> WidgetURL createURL()
152: throws WidgetException {
153: PortletWidgetURL url = new PortletWidgetURL(this );
154:
155: return url;
156: }
157:
158: public String[] getPreferenceValues(String name, String[] defaults) {
159: return _portletRequest.getPreferences().getValues(name,
160: defaults);
161: }
162:
163: public Object getAttribute(String name) {
164: return _portletRequest.getAttribute(name);
165: }
166:
167: public void setAttribute(String name, Object object) {
168: _portletRequest.setAttribute(name, object);
169: }
170:
171: public void removeAttribute(String name) {
172: _portletRequest.removeAttribute(name);
173: }
174:
175: public Enumeration getAttributeNames() {
176: return _portletRequest.getAttributeNames();
177: }
178:
179: public String getParameter(String name) {
180: return _portletRequest.getParameter(name);
181: }
182:
183: public String[] getParameterValues(String name) {
184: return _portletRequest.getParameterValues(name);
185: }
186:
187: public Map getParameterMap() {
188: return _portletRequest.getParameterMap();
189: }
190:
191: public Enumeration getParameterNames() {
192: return _portletRequest.getParameterNames();
193: }
194:
195: public Locale getLocale() {
196: if (_renderResponse == null)
197: return _portletRequest.getLocale();
198: else
199: return _renderResponse.getLocale();
200: }
201:
202: public String getContentType() {
203: String contentType;
204:
205: if (_renderResponse == null) {
206: contentType = _portletRequest.getResponseContentType();
207:
208: if (contentType == null)
209: contentType = "text/html";
210: } else {
211: contentType = _renderResponse.getContentType();
212:
213: if (contentType == null) {
214: contentType = "text/html";
215: _renderResponse.setContentType(contentType);
216: }
217: }
218:
219: return contentType;
220: }
221:
222: public String getRemoteUser() {
223: return _portletRequest.getRemoteUser();
224: }
225:
226: public java.security.Principal getUserPrincipal() {
227: return _portletRequest.getUserPrincipal();
228: }
229:
230: public boolean isUserInRole(String role) {
231: return _portletRequest.isUserInRole(role);
232: }
233:
234: public boolean isSecure() {
235: return _portletRequest.isSecure();
236: }
237:
238: public String resolveURL(String path) {
239: return _portletResponse.encodeURL(path);
240: }
241:
242: public WidgetWriter getWriter() throws IOException {
243: if (_widgetWriter == null) {
244:
245: if (_renderResponse == null)
246: throw new IllegalStateException(
247: L
248: .l("Writer for portlets can only be obtained during render phase"));
249:
250: _widgetWriter = new WidgetWriter(_renderResponse
251: .getWriter());
252: }
253:
254: return _widgetWriter;
255: }
256: }
|