1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.UnsupportedEncodingException;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.extensions.config.RemoteConfigElement.EndpointDescriptor;
29 import org.springframework.extensions.surf.exception.WebScriptsPlatformException;
30 import org.springframework.extensions.surf.util.I18NUtil;
31 import org.springframework.extensions.webscripts.connector.Connector;
32 import org.springframework.extensions.webscripts.connector.ConnectorContext;
33 import org.springframework.extensions.webscripts.connector.HttpMethod;
34 import org.springframework.extensions.webscripts.connector.Response;
35
36
37
38
39
40
41
42
43
44
45
46 public final class ScriptRemoteConnector
47 {
48 private static final Log logger = LogFactory.getLog(ScriptRemote.class);
49
50 final private Connector connector;
51 final private EndpointDescriptor descriptor;
52
53
54
55
56
57
58
59
60 public ScriptRemoteConnector(Connector connector, EndpointDescriptor descriptor)
61 {
62 this.connector = connector;
63 this.descriptor = descriptor;
64 }
65
66
67
68
69
70
71
72
73
74 public Response call(String uri)
75 {
76 ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
77 return this.connector.call(uri, context);
78 }
79
80
81
82
83
84
85
86
87 public Response get(String uri)
88 {
89 return call(uri);
90 }
91
92
93
94
95
96
97
98
99
100 public Response post(String uri, String body)
101 {
102 ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
103 context.setMethod(HttpMethod.POST);
104 try
105 {
106 return this.connector.call(uri, context, new ByteArrayInputStream(body.getBytes("UTF-8")));
107 }
108 catch (UnsupportedEncodingException err)
109 {
110 throw new WebScriptsPlatformException("Unsupported encoding.", err);
111 }
112 }
113
114
115
116
117
118
119
120
121
122
123 public Response post(String uri, String body, String contentType)
124 {
125 ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
126 context.setMethod(HttpMethod.POST);
127 context.setContentType(contentType);
128 try
129 {
130 return this.connector.call(uri, context, new ByteArrayInputStream(body.getBytes("UTF-8")));
131 }
132 catch (UnsupportedEncodingException err)
133 {
134 throw new WebScriptsPlatformException("Unsupported encoding.", err);
135 }
136 }
137
138
139
140
141
142
143
144
145
146 public Response put(String uri, String body)
147 {
148 ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
149 context.setMethod(HttpMethod.PUT);
150 try
151 {
152 return this.connector.call(uri, context, new ByteArrayInputStream(body.getBytes("UTF-8")));
153 }
154 catch (UnsupportedEncodingException err)
155 {
156 throw new WebScriptsPlatformException("Unsupported encoding.", err);
157 }
158 }
159
160
161
162
163
164
165
166
167
168
169 public Response put(String uri, String body, String contentType)
170 {
171 ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
172 context.setMethod(HttpMethod.PUT);
173 context.setContentType(contentType);
174 try
175 {
176 return this.connector.call(uri, context, new ByteArrayInputStream(body.getBytes("UTF-8")));
177 }
178 catch (UnsupportedEncodingException err)
179 {
180 throw new WebScriptsPlatformException("Unsupported encoding.", err);
181 }
182 }
183
184
185
186
187
188
189
190
191
192
193
194 public Response del(String uri)
195 {
196 ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
197 context.setMethod(HttpMethod.DELETE);
198 return this.connector.call(uri, context);
199 }
200
201
202
203
204
205
206 public String getEndpoint()
207 {
208 return this.connector.getEndpoint();
209 }
210
211
212
213
214 public EndpointDescriptor getDescriptor()
215 {
216 return this.descriptor;
217 }
218
219
220
221
222
223
224
225
226 private static Map<String, String> buildDefaultHeaders()
227 {
228 Map<String, String> headers = new HashMap<String, String>(1, 1.0f);
229 headers.put("Accept-Language", I18NUtil.getLocale().toString().replace('_', '-'));
230 return headers;
231 }
232 }