1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts.servlet.mvc;
20
21 import java.util.Map;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.springframework.extensions.surf.util.URLEncoder;
27 import org.springframework.extensions.webscripts.servlet.HTTPProxy;
28 import org.springframework.web.servlet.ModelAndView;
29 import org.springframework.web.servlet.mvc.AbstractController;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class HTTPProxyController extends AbstractController
55 {
56 private static final String PARAM_ENDPOINT = "endpoint";
57
58
59
60
61 public ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse res) throws Exception
62 {
63 ModelAndView mav = null;
64
65 String endpoint = null;
66 StringBuilder args = new StringBuilder(32);
67
68 Map<String, String[]> parameters = req.getParameterMap();
69 for (Map.Entry<String, String[]> parameter : parameters.entrySet())
70 {
71 String[] values = parameter.getValue();
72 int startIdx = 0;
73
74 if (parameter.getKey().equals(PARAM_ENDPOINT) && values.length != 0)
75 {
76 endpoint = values[0];
77 startIdx++;
78 }
79
80 for (int i = startIdx; i < values.length; i++)
81 {
82 if (args.length() != 0)
83 {
84 args.append("&");
85 }
86 args.append(parameter.getKey()).append('=').append(URLEncoder.encode(values[i]));
87 }
88 }
89
90 if (endpoint == null || endpoint.length() == 0)
91 {
92 throw new IllegalArgumentException("endpoint argument not specified");
93 }
94
95 String url = endpoint + ((args.length() == 0) ? "" : "?" + args.toString());
96 HTTPProxy proxy = new HTTPProxy(url, res);
97 proxy.service();
98
99 return mav;
100 }
101
102
103
104
105
106
107
108
109
110 public static String createProxyUrl(String url)
111 {
112 String proxy = "/proxy";
113 if (url != null && url.length() > 0)
114 {
115 int argIndex = url.lastIndexOf("?");
116 if (argIndex == -1)
117 {
118 proxy += "?endpoint=" + url;
119 }
120 else
121 {
122 proxy += "?endpoint=" + url.substring(0, argIndex) + "&" + url.substring(argIndex + 1);
123 }
124 }
125
126 return proxy;
127 }
128
129 }
130
131