001
014
015 package com.liferay.util.bridges.scripting;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.portlet.PortletResponseUtil;
020 import com.liferay.portal.kernel.scripting.ScriptingException;
021 import com.liferay.portal.kernel.scripting.ScriptingUtil;
022 import com.liferay.portal.kernel.servlet.SessionErrors;
023 import com.liferay.portal.kernel.util.ContentTypes;
024 import com.liferay.portal.kernel.util.FileUtil;
025 import com.liferay.portal.kernel.util.HtmlUtil;
026 import com.liferay.portal.kernel.util.StringBundler;
027 import com.liferay.portal.kernel.util.StringPool;
028 import com.liferay.portal.kernel.util.StringUtil;
029 import com.liferay.portal.kernel.util.WebKeys;
030 import com.liferay.portal.security.permission.ActionKeys;
031 import com.liferay.portal.security.permission.PermissionChecker;
032 import com.liferay.portal.service.permission.PortalPermissionUtil;
033 import com.liferay.portal.theme.ThemeDisplay;
034
035 import java.io.IOException;
036 import java.io.InputStream;
037
038 import java.util.Map;
039
040 import javax.portlet.ActionRequest;
041 import javax.portlet.ActionResponse;
042 import javax.portlet.GenericPortlet;
043 import javax.portlet.PortletConfig;
044 import javax.portlet.PortletContext;
045 import javax.portlet.PortletException;
046 import javax.portlet.PortletRequest;
047 import javax.portlet.PortletResponse;
048 import javax.portlet.RenderRequest;
049 import javax.portlet.RenderResponse;
050 import javax.portlet.ResourceRequest;
051 import javax.portlet.ResourceResponse;
052
053
058 public class ScriptingPortlet extends GenericPortlet {
059
060 @Override
061 public void doDispatch(
062 RenderRequest renderRequest, RenderResponse renderResponse)
063 throws IOException, PortletException {
064
065 String fileName = getFileName(renderRequest);
066
067 if (fileName != null) {
068 include(fileName, renderRequest, renderResponse);
069 }
070 else {
071 super.doDispatch(renderRequest, renderResponse);
072 }
073 }
074
075 @Override
076 public void doEdit(
077 RenderRequest renderRequest, RenderResponse renderResponse)
078 throws IOException, PortletException {
079
080 if (renderRequest.getPreferences() == null) {
081 super.doEdit(renderRequest, renderResponse);
082 }
083 else {
084 include(editFile, renderRequest, renderResponse);
085 }
086 }
087
088 @Override
089 public void doHelp(
090 RenderRequest renderRequest, RenderResponse renderResponse)
091 throws IOException {
092
093 include(helpFile, renderRequest, renderResponse);
094 }
095
096 @Override
097 public void doView(
098 RenderRequest renderRequest, RenderResponse renderResponse)
099 throws IOException {
100
101 include(viewFile, renderRequest, renderResponse);
102 }
103
104 @Override
105 public void init() {
106 actionFile = getInitParameter("action-file");
107 editFile = getInitParameter("edit-file");
108 helpFile = getInitParameter("help-file");
109 resourceFile = getInitParameter("resource-file");
110 viewFile = getInitParameter("view-file");
111
112 language = getInitParameter("scripting-language");
113 globalFiles = StringUtil.split(getInitParameter("global-files"));
114 }
115
116 @Override
117 public void processAction(
118 ActionRequest actionRequest, ActionResponse actionResponse)
119 throws IOException {
120
121 include(actionFile, actionRequest, actionResponse);
122 }
123
124 @Override
125 public void render(
126 RenderRequest renderRequest, RenderResponse renderResponse)
127 throws IOException, PortletException {
128
129 try {
130 doRender(renderRequest, renderResponse);
131 }
132 catch (IOException ioe) {
133 throw ioe;
134 }
135 catch (PortletException pe) {
136 throw pe;
137 }
138 catch (Exception e) {
139 throw new PortletException(e);
140 }
141 }
142
143 @Override
144 public void serveResource(
145 ResourceRequest resourceRequest, ResourceResponse resourceResponse)
146 throws IOException {
147
148 include(resourceFile, resourceRequest, resourceResponse);
149 }
150
151 protected void declareBeans(
152 InputStream is, PortletRequest portletRequest,
153 PortletResponse portletResponse)
154 throws IOException, ScriptingException {
155
156 String script = new String(FileUtil.getBytes(is));
157
158 declareBeans(script, portletRequest, portletResponse);
159 }
160
161 protected void declareBeans(
162 String script, PortletRequest portletRequest,
163 PortletResponse portletResponse)
164 throws IOException, ScriptingException {
165
166 script = getGlobalScript() + script;
167
168 PortletConfig portletConfig = getPortletConfig();
169 PortletContext portletContext = getPortletContext();
170
171 Map<String, Object> portletObjects = ScriptingUtil.getPortletObjects(
172 portletConfig, portletContext, portletRequest, portletResponse);
173
174 ScriptingUtil.exec(null, portletObjects, language, script);
175 }
176
177 protected void doRender(
178 RenderRequest renderRequest, RenderResponse renderResponse)
179 throws Exception {
180
181 Object error = SessionErrors.get(renderRequest, _ERROR);
182
183 if (error != null) {
184 Exception e = (Exception)error;
185
186 writeErrorMessage(renderRequest, renderResponse, e.getMessage());
187
188 return;
189 }
190
191 super.render(renderRequest, renderResponse);
192
193 error = SessionErrors.get(renderRequest, _ERROR);
194
195 if (error != null) {
196 Exception e = (Exception)error;
197
198 writeErrorMessage(renderRequest, renderResponse, e.getMessage());
199 }
200 }
201
202 protected String getFileName(RenderRequest renderRequest) {
203 return renderRequest.getParameter("file");
204 }
205
206 protected String getGlobalScript() throws IOException {
207 if (globalScript != null) {
208 return globalScript;
209 }
210
211 if (globalFiles.length == 0) {
212 globalScript = StringPool.BLANK;
213
214 return globalScript;
215 }
216
217 StringBundler sb = new StringBundler();
218
219 for (String globalFile : globalFiles) {
220 InputStream is = getPortletContext().getResourceAsStream(
221 globalFile);
222
223 if (is == null) {
224 if (_log.isWarnEnabled()) {
225 _log.warn("Global file " + globalFile + " does not exist");
226 }
227 }
228
229 if (is != null) {
230 String script = new String(FileUtil.getBytes(is));
231
232 sb.append(script);
233 sb.append(StringPool.NEW_LINE);
234 }
235 }
236
237 globalScript = sb.toString();
238
239 return globalScript;
240 }
241
242 protected void include(
243 String path, PortletRequest portletRequest,
244 PortletResponse portletResponse)
245 throws IOException {
246
247 InputStream is = getPortletContext().getResourceAsStream(path);
248
249 if (is == null) {
250 _log.error(path + " is not a valid " + language + " file");
251
252 return;
253 }
254
255 try {
256 declareBeans(is, portletRequest, portletResponse);
257 }
258 catch (ScriptingException se) {
259 SessionErrors.add(portletRequest, _ERROR, se);
260 }
261 finally {
262 is.close();
263 }
264 }
265
266 protected void writeErrorMessage(
267 RenderRequest renderRequest, RenderResponse renderResponse,
268 String errorMessage)
269 throws Exception {
270
271 ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
272 WebKeys.THEME_DISPLAY);
273
274 PermissionChecker permissionChecker =
275 themeDisplay.getPermissionChecker();
276
277 StringBundler sb = new StringBundler(6);
278
279 sb.append("<div class=\"portlet-msg-error\">");
280 sb.append(themeDisplay.translate("an-unexpected-error-occurred"));
281 sb.append("</div>");
282
283 if (PortalPermissionUtil.contains(
284 permissionChecker, ActionKeys.CONFIGURATION)) {
285
286 sb.append("<pre>");
287 sb.append(HtmlUtil.escape(errorMessage));
288 sb.append("</pre>");
289 }
290
291 renderResponse.setContentType(ContentTypes.TEXT_HTML);
292
293 PortletResponseUtil.write(renderResponse, sb.toString());
294 }
295
296 protected String actionFile;
297 protected String editFile;
298 protected String[] globalFiles;
299 protected String globalScript;
300 protected String helpFile;
301 protected String language;
302 protected String resourceFile;
303 protected String viewFile;
304
305 private static final String _ERROR = ScriptingPortlet.class + ".ERROR";
306
307 private static Log _log = LogFactoryUtil.getLog(ScriptingPortlet.class);
308
309 }