1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. 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.util.bridges.javascript;
24  
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  import org.mozilla.javascript.Context;
44  import org.mozilla.javascript.Scriptable;
45  import org.mozilla.javascript.ScriptableObject;
46  
47  /**
48   * <a href="JavaScriptPortlet.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Alberto Montero
51   *
52   */
53  public class JavaScriptPortlet extends BaseBSFPortlet {
54  
55      protected void declareBeans(
56              String code, PortletRequest portletRequest,
57              PortletResponse portletResponse)
58          throws IOException {
59  
60          StringBuilder sb = new StringBuilder();
61  
62          sb.append(getGlobalScript());
63          sb.append(code);
64  
65          String script = sb.toString();
66  
67          PortletConfig portletConfig = getPortletConfig();
68          PortletContext portletContext = getPortletContext();
69          PortletPreferences preferences = portletRequest.getPreferences();
70          Map<String, String> userInfo =
71              (Map<String, String>)portletRequest.getAttribute(
72                  PortletRequest.USER_INFO);
73  
74          Context context = Context.enter();
75  
76          try {
77              Scriptable scope = context.initStandardObjects();
78  
79              Object out = Context.javaToJS(System.out, scope);
80  
81              ScriptableObject.putProperty(scope, "out", out);
82  
83              ScriptableObject.putProperty(
84                  scope, "portletConfig", Context.javaToJS(portletConfig, scope));
85              ScriptableObject.putProperty(
86                  scope, "portletContext",
87                  Context.javaToJS(portletContext, scope));
88              ScriptableObject.putProperty(
89                  scope, "preferences", Context.javaToJS(preferences, scope));
90              ScriptableObject.putProperty(
91                  scope, "userInfo", Context.javaToJS(userInfo, scope));
92  
93              if (portletRequest instanceof ActionRequest) {
94                  ScriptableObject.putProperty(
95                      scope, "actionRequest",
96                      Context.javaToJS(portletRequest, scope));
97              }
98              else if (portletRequest instanceof RenderRequest) {
99                  ScriptableObject.putProperty(
100                     scope, "renderRequest",
101                     Context.javaToJS(portletRequest, scope));
102             }
103             else if (portletRequest instanceof ResourceRequest) {
104                 ScriptableObject.putProperty(
105                     scope, "resourceRequest",
106                     Context.javaToJS(portletRequest, scope));
107             }
108 
109             if (portletResponse instanceof ActionResponse) {
110                 ScriptableObject.putProperty(
111                     scope, "actionResponse",
112                     Context.javaToJS(portletResponse, scope));
113             }
114             else if (portletResponse instanceof RenderResponse) {
115                 ScriptableObject.putProperty(
116                     scope, "renderResponse",
117                     Context.javaToJS(portletResponse, scope));
118             }
119             else if (portletResponse instanceof ResourceResponse) {
120                 ScriptableObject.putProperty(
121                     scope, "resourceResponse",
122                     Context.javaToJS(portletResponse, scope));
123             }
124 
125             context.evaluateString(scope, script, "script", 1, null);
126         }
127         finally {
128             Context.exit();
129         }
130     }
131 
132     protected String getFileParam() {
133         return _FILE_PARAM;
134     }
135 
136     protected String getScriptingEngineClassName() {
137         return _SCRIPTING_ENGINE_CLASS_NAME;
138     }
139 
140     protected String getScriptingEngineExtension() {
141         return _SCRIPTING_ENGINE_EXTENSION;
142     }
143 
144     protected String getScriptingEngineLanguage() {
145         return _SCRIPTING_ENGINE_LANGUAGE;
146     }
147 
148     private static final String _FILE_PARAM = "javaScriptFile";
149 
150     private static final String _SCRIPTING_ENGINE_CLASS_NAME =
151         "org.apache.bsf.engines.javascript.JavaScriptEngine";
152 
153     private static final String _SCRIPTING_ENGINE_EXTENSION = "js";
154 
155     private static final String _SCRIPTING_ENGINE_LANGUAGE = "javascript";
156 
157 }