1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.bridges.javascript;
21  
22  import com.liferay.mozilla.javascript.Context;
23  import com.liferay.mozilla.javascript.Scriptable;
24  import com.liferay.mozilla.javascript.ScriptableObject;
25  import com.liferay.util.bridges.bsf.BaseBSFPortlet;
26  
27  import java.io.IOException;
28  
29  import java.util.Map;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.PortletContext;
35  import javax.portlet.PortletPreferences;
36  import javax.portlet.PortletRequest;
37  import javax.portlet.PortletResponse;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  import javax.portlet.ResourceRequest;
41  import javax.portlet.ResourceResponse;
42  
43  /**
44   * <a href="JavaScriptPortlet.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Alberto Montero
47   *
48   */
49  public class JavaScriptPortlet extends BaseBSFPortlet {
50  
51      protected void declareBeans(
52              String code, PortletRequest portletRequest,
53              PortletResponse portletResponse)
54          throws IOException {
55  
56          StringBuilder sb = new StringBuilder();
57  
58          sb.append(getGlobalScript());
59          sb.append(code);
60  
61          String script = sb.toString();
62  
63          PortletConfig portletConfig = getPortletConfig();
64          PortletContext portletContext = getPortletContext();
65          PortletPreferences preferences = portletRequest.getPreferences();
66          Map<String, String> userInfo =
67              (Map<String, String>)portletRequest.getAttribute(
68                  PortletRequest.USER_INFO);
69  
70          Context context = Context.enter();
71  
72          try {
73              Scriptable scope = context.initStandardObjects();
74  
75              Object out = Context.javaToJS(System.out, scope);
76  
77              ScriptableObject.putProperty(scope, "out", out);
78  
79              ScriptableObject.putProperty(
80                  scope, "portletConfig", Context.javaToJS(portletConfig, scope));
81              ScriptableObject.putProperty(
82                  scope, "portletContext",
83                  Context.javaToJS(portletContext, scope));
84              ScriptableObject.putProperty(
85                  scope, "preferences", Context.javaToJS(preferences, scope));
86              ScriptableObject.putProperty(
87                  scope, "userInfo", Context.javaToJS(userInfo, scope));
88  
89              if (portletRequest instanceof ActionRequest) {
90                  ScriptableObject.putProperty(
91                      scope, "actionRequest",
92                      Context.javaToJS(portletRequest, scope));
93              }
94              else if (portletRequest instanceof RenderRequest) {
95                  ScriptableObject.putProperty(
96                      scope, "renderRequest",
97                      Context.javaToJS(portletRequest, scope));
98              }
99              else if (portletRequest instanceof ResourceRequest) {
100                 ScriptableObject.putProperty(
101                     scope, "resourceRequest",
102                     Context.javaToJS(portletRequest, scope));
103             }
104 
105             if (portletResponse instanceof ActionResponse) {
106                 ScriptableObject.putProperty(
107                     scope, "actionResponse",
108                     Context.javaToJS(portletResponse, scope));
109             }
110             else if (portletResponse instanceof RenderResponse) {
111                 ScriptableObject.putProperty(
112                     scope, "renderResponse",
113                     Context.javaToJS(portletResponse, scope));
114             }
115             else if (portletResponse instanceof ResourceResponse) {
116                 ScriptableObject.putProperty(
117                     scope, "resourceResponse",
118                     Context.javaToJS(portletResponse, scope));
119             }
120 
121             context.evaluateString(scope, script, "script", 1, null);
122         }
123         finally {
124             Context.exit();
125         }
126     }
127 
128     protected String getFileParam() {
129         return _FILE_PARAM;
130     }
131 
132     protected String getScriptingEngineClassName() {
133         return _SCRIPTING_ENGINE_CLASS_NAME;
134     }
135 
136     protected String getScriptingEngineExtension() {
137         return _SCRIPTING_ENGINE_EXTENSION;
138     }
139 
140     protected String getScriptingEngineLanguage() {
141         return _SCRIPTING_ENGINE_LANGUAGE;
142     }
143 
144     private static final String _FILE_PARAM = "javaScriptFile";
145 
146     private static final String _SCRIPTING_ENGINE_CLASS_NAME =
147         "org.apache.bsf.engines.javascript.JavaScriptEngine";
148 
149     private static final String _SCRIPTING_ENGINE_EXTENSION = "js";
150 
151     private static final String _SCRIPTING_ENGINE_LANGUAGE = "javascript";
152 
153 }