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