1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
37
38
39
40 public class WebScriptPortletResponse extends WebScriptResponseImpl
41 {
42
43 private RenderResponse res;
44
45
46
47
48
49
50
51 WebScriptPortletResponse(Runtime container, RenderResponse res)
52 {
53 super(container);
54 this.res = res;
55 }
56
57
58
59
60
61
62 public RenderResponse getRenderResponse()
63 {
64 return res;
65 }
66
67
68
69
70 public void setStatus(int status)
71 {
72 }
73
74
75
76
77 public void setHeader(String name, String value)
78 {
79
80 }
81
82
83
84
85 public void addHeader(String name, String value)
86 {
87
88 }
89
90
91
92
93 public void setContentType(String contentType)
94 {
95 res.setContentType(contentType);
96 }
97
98
99
100
101
102 public void setContentEncoding(String contentEncoding)
103 {
104
105 }
106
107
108
109
110 public void setCache(Cache cache)
111 {
112
113 }
114
115
116
117
118 public void reset()
119 {
120 try
121 {
122 res.reset();
123 }
124 catch(IllegalStateException e)
125 {
126 }
127 }
128
129
130
131
132 public Writer getWriter() throws IOException
133 {
134 return res.getWriter();
135 }
136
137
138
139
140 public OutputStream getOutputStream() throws IOException
141 {
142 return res.getPortletOutputStream();
143 }
144
145
146
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
162
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 }