1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class JavaScriptPortlet extends BaseBSFPortlet {
52  
53      protected void declareBeans(
54              String code, PortletRequest portletRequest,
55              PortletResponse portletResponse)
56          throws IOException {
57  
58          StringBuilder sb = new StringBuilder();
59  
60          sb.append(getGlobalScript());
61          sb.append(code);
62  
63          String script = sb.toString();
64  
65          PortletConfig portletConfig = getPortletConfig();
66          PortletContext portletContext = getPortletContext();
67          PortletPreferences preferences = portletRequest.getPreferences();
68          Map<String, String> userInfo =
69              (Map<String, String>)portletRequest.getAttribute(
70                  PortletRequest.USER_INFO);
71  
72          Context context = Context.enter();
73  
74          try {
75              Scriptable scope = context.initStandardObjects();
76  
77              Object out = Context.javaToJS(System.out, scope);
78  
79              ScriptableObject.putProperty(scope, "out", out);
80  
81              ScriptableObject.putProperty(
82                  scope, "portletConfig", Context.javaToJS(portletConfig, scope));
83              ScriptableObject.putProperty(
84                  scope, "portletContext",
85                  Context.javaToJS(portletContext, scope));
86              ScriptableObject.putProperty(
87                  scope, "preferences", Context.javaToJS(preferences, scope));
88              ScriptableObject.putProperty(
89                  scope, "userInfo", Context.javaToJS(userInfo, scope));
90  
91              if (portletRequest instanceof ActionRequest) {
92                  ScriptableObject.putProperty(
93                      scope, "actionRequest",
94                      Context.javaToJS(portletRequest, scope));
95              }
96              else if (portletRequest instanceof RenderRequest) {
97                  ScriptableObject.putProperty(
98                      scope, "renderRequest",
99                      Context.javaToJS(portletRequest, scope));
100             }
101             else if (portletRequest instanceof ResourceRequest) {
102                 ScriptableObject.putProperty(
103                     scope, "resourceRequest",
104                     Context.javaToJS(portletRequest, scope));
105             }
106 
107             if (portletResponse instanceof ActionResponse) {
108                 ScriptableObject.putProperty(
109                     scope, "actionResponse",
110                     Context.javaToJS(portletResponse, scope));
111             }
112             else if (portletResponse instanceof RenderResponse) {
113                 ScriptableObject.putProperty(
114                     scope, "renderResponse",
115                     Context.javaToJS(portletResponse, scope));
116             }
117             else if (portletResponse instanceof ResourceResponse) {
118                 ScriptableObject.putProperty(
119                     scope, "resourceResponse",
120                     Context.javaToJS(portletResponse, scope));
121             }
122 
123             context.evaluateString(scope, script, "script", 1, null);
124         }
125         finally {
126             Context.exit();
127         }
128     }
129 
130     protected String getFileParam() {
131         return _FILE_PARAM;
132     }
133 
134     protected String getScriptingEngineClassName() {
135         return _SCRIPTING_ENGINE_CLASS_NAME;
136     }
137 
138     protected String getScriptingEngineExtension() {
139         return _SCRIPTING_ENGINE_EXTENSION;
140     }
141 
142     protected String getScriptingEngineLanguage() {
143         return _SCRIPTING_ENGINE_LANGUAGE;
144     }
145 
146     private static final String _FILE_PARAM = "javaScriptFile";
147 
148     private static final String _SCRIPTING_ENGINE_CLASS_NAME =
149         "org.apache.bsf.engines.javascript.JavaScriptEngine";
150 
151     private static final String _SCRIPTING_ENGINE_EXTENSION = "js";
152 
153     private static final String _SCRIPTING_ENGINE_LANGUAGE = "javascript";
154 
155 }