View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts.portlet;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.Writer;
24  
25  import javax.portlet.PortletURL;
26  import javax.portlet.RenderResponse;
27  
28  import org.springframework.extensions.webscripts.Cache;
29  import org.springframework.extensions.webscripts.Runtime;
30  import org.springframework.extensions.webscripts.WebScriptRequest;
31  import org.springframework.extensions.webscripts.WebScriptResponseImpl;
32  import org.springframework.extensions.webscripts.ui.common.StringUtils;
33  
34  
35  /**
36   * JSR-168 Web Script Response
37   * 
38   * @author davidc
39   */
40  public class WebScriptPortletResponse extends WebScriptResponseImpl
41  {
42      /** Portlet response */
43      private RenderResponse res;
44      
45      
46      /**
47       * Construct
48       * 
49       * @param res
50       */
51      WebScriptPortletResponse(Runtime container, RenderResponse res)
52      {
53          super(container);
54          this.res = res;
55      }
56  
57      /**
58       * Gets the Portlet Render Response
59       * 
60       * @return  render response
61       */
62      public RenderResponse getRenderResponse()
63      {
64          return res;
65      }
66      
67      /* (non-Javadoc)
68       * @see org.alfresco.web.scripts.WebScriptResponse#setStatus(int)
69       */
70      public void setStatus(int status)
71      {
72      }
73           
74      /* (non-Javadoc)
75       * @see org.alfresco.web.scripts.WebScriptResponse#setHeader(java.lang.String, java.lang.String)
76       */
77      public void setHeader(String name, String value)
78      {
79          // NOTE: not applicable
80      }
81  
82      /* (non-Javadoc)
83       * @see org.alfresco.web.scripts.WebScriptResponse#addHeader(java.lang.String, java.lang.String)
84       */
85      public void addHeader(String name, String value)
86      {
87          // NOTE: not applicable
88      }
89  
90      /* (non-Javadoc)
91       * @see org.alfresco.web.scripts.WebScriptResponse#setContentType(java.lang.String)
92       */
93      public void setContentType(String contentType)
94      {
95          res.setContentType(contentType);
96      }
97  
98      /*
99       * (non-Javadoc)
100      * @see org.alfresco.web.scripts.WebScriptResponse#setContentEncoding(java.lang.String)
101      */
102     public void setContentEncoding(String contentEncoding)
103     {
104         // NOTE: not applicable
105     }
106 
107     /* (non-Javadoc)
108      * @see org.alfresco.web.scripts.WebScriptResponse#getCache()
109      */
110     public void setCache(Cache cache)
111     {
112         // NOTE: Not applicable
113     }
114     
115     /* (non-Javadoc)
116      * @see org.alfresco.web.scripts.WebScriptResponse#reset()
117      */
118     public void reset()
119     {
120         try
121         {
122             res.reset();
123         }
124         catch(IllegalStateException e)
125         {
126         }
127     }
128 
129     /* (non-Javadoc)
130      * @see org.alfresco.web.scripts.WebScriptResponse#getWriter()
131      */
132     public Writer getWriter() throws IOException
133     {
134         return res.getWriter();
135     }
136 
137     /* (non-Javadoc)
138      * @see org.alfresco.web.scripts.WebScriptResponse#getOutputStream()
139      */
140     public OutputStream getOutputStream() throws IOException
141     {
142         return res.getPortletOutputStream();
143     }
144     
145     /* (non-Javadoc)
146      * @see org.alfresco.web.scripts.WebScriptResponse#encodeScriptUrl(java.lang.String)
147      */
148     public String encodeScriptUrl(String url)
149     {
150         WebScriptRequest req = new WebScriptPortletRequest(getRuntime(), null, url, null);
151         PortletURL portletUrl = res.createActionURL();
152         portletUrl.setParameter("scriptUrl", req.getServicePath());
153         String[] parameterNames = req.getParameterNames();
154         for (String parameterName : parameterNames)
155         {
156             portletUrl.setParameter("arg." + parameterName, req.getParameter(parameterName));
157         }
158         return portletUrl.toString();
159     }
160 
161     /* (non-Javadoc)
162      * @see org.alfresco.web.scripts.WebScriptResponse#getEncodeScriptUrlFunction(java.lang.String)
163      */
164     public String getEncodeScriptUrlFunction(String name)
165     {
166         PortletURL portletUrl = res.createActionURL();
167         
168         String func = ENCODE_FUNCTION.replace("$name$", name);
169         func = func.replace("$actionUrl$", portletUrl.toString());
170         return StringUtils.encodeJavascript(func);
171     }
172     
173     private static final String ENCODE_FUNCTION = 
174             "{ $name$: function(url) {" + 
175             " var out = \"$actionUrl$\";" + 
176             " var argsIndex = url.indexOf(\"?\");" + 
177             " if (argsIndex == -1)" + 
178             " {" + 
179             "    out += \"&scriptUrl=\" + escape(url);" + 
180             " }" + 
181             " else" + 
182             " {" + 
183             "    out += \"&scriptUrl=\" + escape(url.substring(0, argsIndex));" + 
184             "    var args = url.substring(argsIndex + 1).split(\"&\");" + 
185             "    for (var i=0; i<args.length; i++)" + 
186             "    {" + 
187             "       out += \"&arg.\" + args[i];" + 
188             "    }" + 
189             " }" + 
190             " return out; } }"; 
191 }