1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.bridges.javascript;
16  
17  import com.liferay.mozilla.javascript.Context;
18  import com.liferay.mozilla.javascript.Scriptable;
19  import com.liferay.mozilla.javascript.ScriptableObject;
20  import com.liferay.util.bridges.bsf.BaseBSFPortlet;
21  
22  import java.io.IOException;
23  
24  import java.util.Map;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletContext;
30  import javax.portlet.PortletPreferences;
31  import javax.portlet.PortletRequest;
32  import javax.portlet.PortletResponse;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  import javax.portlet.ResourceRequest;
36  import javax.portlet.ResourceResponse;
37  
38  /**
39   * <a href="JavaScriptPortlet.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Alberto Montero
42   */
43  public class JavaScriptPortlet extends BaseBSFPortlet {
44  
45      protected void declareBeans(
46              String code, PortletRequest portletRequest,
47              PortletResponse portletResponse)
48          throws IOException {
49  
50          StringBuilder sb = new StringBuilder();
51  
52          sb.append(getGlobalScript());
53          sb.append(code);
54  
55          String script = sb.toString();
56  
57          PortletConfig portletConfig = getPortletConfig();
58          PortletContext portletContext = getPortletContext();
59          PortletPreferences preferences = portletRequest.getPreferences();
60          Map<String, String> userInfo =
61              (Map<String, String>)portletRequest.getAttribute(
62                  PortletRequest.USER_INFO);
63  
64          Context context = Context.enter();
65  
66          try {
67              Scriptable scope = context.initStandardObjects();
68  
69              Object out = Context.javaToJS(System.out, scope);
70  
71              ScriptableObject.putProperty(scope, "out", out);
72  
73              ScriptableObject.putProperty(
74                  scope, "portletConfig", Context.javaToJS(portletConfig, scope));
75              ScriptableObject.putProperty(
76                  scope, "portletContext",
77                  Context.javaToJS(portletContext, scope));
78              ScriptableObject.putProperty(
79                  scope, "preferences", Context.javaToJS(preferences, scope));
80              ScriptableObject.putProperty(
81                  scope, "userInfo", Context.javaToJS(userInfo, scope));
82  
83              if (portletRequest instanceof ActionRequest) {
84                  ScriptableObject.putProperty(
85                      scope, "actionRequest",
86                      Context.javaToJS(portletRequest, scope));
87              }
88              else if (portletRequest instanceof RenderRequest) {
89                  ScriptableObject.putProperty(
90                      scope, "renderRequest",
91                      Context.javaToJS(portletRequest, scope));
92              }
93              else if (portletRequest instanceof ResourceRequest) {
94                  ScriptableObject.putProperty(
95                      scope, "resourceRequest",
96                      Context.javaToJS(portletRequest, scope));
97              }
98  
99              if (portletResponse instanceof ActionResponse) {
100                 ScriptableObject.putProperty(
101                     scope, "actionResponse",
102                     Context.javaToJS(portletResponse, scope));
103             }
104             else if (portletResponse instanceof RenderResponse) {
105                 ScriptableObject.putProperty(
106                     scope, "renderResponse",
107                     Context.javaToJS(portletResponse, scope));
108             }
109             else if (portletResponse instanceof ResourceResponse) {
110                 ScriptableObject.putProperty(
111                     scope, "resourceResponse",
112                     Context.javaToJS(portletResponse, scope));
113             }
114 
115             context.evaluateString(scope, script, "script", 1, null);
116         }
117         finally {
118             Context.exit();
119         }
120     }
121 
122     protected String getFileParam() {
123         return _FILE_PARAM;
124     }
125 
126     protected String getScriptingEngineClassName() {
127         return _SCRIPTING_ENGINE_CLASS_NAME;
128     }
129 
130     protected String getScriptingEngineExtension() {
131         return _SCRIPTING_ENGINE_EXTENSION;
132     }
133 
134     protected String getScriptingEngineLanguage() {
135         return _SCRIPTING_ENGINE_LANGUAGE;
136     }
137 
138     private static final String _FILE_PARAM = "javaScriptFile";
139 
140     private static final String _SCRIPTING_ENGINE_CLASS_NAME =
141         "org.apache.bsf.engines.javascript.JavaScriptEngine";
142 
143     private static final String _SCRIPTING_ENGINE_EXTENSION = "js";
144 
145     private static final String _SCRIPTING_ENGINE_LANGUAGE = "javascript";
146 
147 }