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