1   /**
2    * Copyright (c) 2000-2005 Liferay, LLC. 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.util.Constants;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.util.GetterUtil;
28  import com.liferay.util.Validator;
29  
30  import java.io.IOException;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletException;
35  import javax.portlet.PortletRequestDispatcher;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  /**
43   * <a href="JSPPortlet.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author  Brian Wing Shun Chan
46   * @version $Revision: 1.21 $
47   *
48   */
49  public class JSPPortlet extends LiferayPortlet {
50  
51      public void init() throws PortletException {
52          _aboutJSP = getInitParameter("about-jsp");
53          _configJSP = getInitParameter("config-jsp");
54          _editJSP = getInitParameter("edit-jsp");
55          _editDefaultsJSP = getInitParameter("edit-defaults-jsp");
56          _helpJSP = getInitParameter("help-jsp");
57          _previewJSP = getInitParameter("preview-jsp");
58          _printJSP = getInitParameter("print-jsp");
59          _viewJSP = getInitParameter("view-jsp");
60  
61          _copyRequestParameters = GetterUtil.get(
62              getInitParameter("copy-request-parameters"), true);
63      }
64  
65      public void doDispatch(RenderRequest req, RenderResponse res)
66          throws IOException, PortletException {
67  
68          String jspPage = req.getParameter("jsp_page");
69  
70          if (Validator.isNotNull(jspPage)) {
71              include(jspPage, req, res);
72          }
73          else {
74              super.doDispatch(req, res);
75          }
76      }
77  
78      public void doAbout(RenderRequest req, RenderResponse res)
79          throws IOException, PortletException {
80  
81          include(_aboutJSP, req, res);
82      }
83  
84      public void doConfig(RenderRequest req, RenderResponse res)
85          throws IOException, PortletException {
86  
87          include(_configJSP, req, res);
88      }
89  
90      public void doEdit(RenderRequest req, RenderResponse res)
91          throws IOException, PortletException {
92  
93          if (req.getPreferences() == null) {
94              super.doEdit(req, res);
95          }
96          else {
97              include(_editJSP, req, res);
98          }
99      }
100 
101     public void doEditDefaults(RenderRequest req, RenderResponse res)
102         throws IOException, PortletException {
103 
104         if (req.getPreferences() == null) {
105             super.doEdit(req, res);
106         }
107         else {
108             include(_editDefaultsJSP, req, res);
109         }
110     }
111 
112     public void doHelp(RenderRequest req, RenderResponse res)
113         throws IOException, PortletException {
114 
115         include(_helpJSP, req, res);
116     }
117 
118     public void doPreview(RenderRequest req, RenderResponse res)
119         throws IOException, PortletException {
120 
121         include(_previewJSP, req, res);
122     }
123 
124     public void doPrint(RenderRequest req, RenderResponse res)
125         throws IOException, PortletException {
126 
127         include(_printJSP, req, res);
128     }
129 
130     public void doView(RenderRequest req, RenderResponse res)
131         throws IOException, PortletException {
132 
133         include(_viewJSP, req, res);
134     }
135 
136     public void processAction(ActionRequest req, ActionResponse res)
137         throws IOException, PortletException {
138 
139         if (_copyRequestParameters) {
140             PortalUtil.copyRequestParameters(req, res);
141         }
142     }
143 
144     protected void include(String path, RenderRequest req, RenderResponse res)
145         throws IOException, PortletException {
146 
147         PortletRequestDispatcher prd =
148             getPortletContext().getRequestDispatcher(
149                 Constants.TEXT_HTML_DIR + path);
150 
151         if (prd == null) {
152             _log.error(path + " is not a valid include");
153         }
154 
155         prd.include(req, res);
156 
157         if (_copyRequestParameters) {
158             PortalUtil.clearRequestParameters(req);
159         }
160     }
161 
162     private static final Log _log = LogFactory.getLog(JSPPortlet.class);
163 
164     private String _aboutJSP;
165     private String _configJSP;
166     private String _editJSP;
167     private String _editDefaultsJSP;
168     private String _helpJSP;
169     private String _previewJSP;
170     private String _printJSP;
171     private String _viewJSP;
172     private boolean _copyRequestParameters;
173 
174 }