comparison src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java @ 10:e79283dad6f2

Added the option to set the request's locale manually. http-client/trunk@1679 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 14 Apr 2011 06:01:00 +0000
parents 072e8d488f83
children ea8b7e244e61
comparison
equal deleted inserted replaced
9:072e8d488f83 10:e79283dad6f2
8 package de.intevation.artifacts.httpclient.http; 8 package de.intevation.artifacts.httpclient.http;
9 9
10 import java.io.InputStream; 10 import java.io.InputStream;
11 import java.io.IOException; 11 import java.io.IOException;
12 import java.io.OutputStream; 12 import java.io.OutputStream;
13 import java.util.ArrayList;
14 import java.util.List;
13 15
14 import org.apache.log4j.Logger; 16 import org.apache.log4j.Logger;
15 17
16 import org.restlet.Client; 18 import org.restlet.Client;
17 import org.restlet.Request; 19 import org.restlet.Request;
18 import org.restlet.Response; 20 import org.restlet.Response;
21 import org.restlet.data.ClientInfo;
22 import org.restlet.data.Language;
19 import org.restlet.data.MediaType; 23 import org.restlet.data.MediaType;
20 import org.restlet.data.Method; 24 import org.restlet.data.Method;
25 import org.restlet.data.Preference;
21 import org.restlet.data.Protocol; 26 import org.restlet.data.Protocol;
22 import org.restlet.data.Status; 27 import org.restlet.data.Status;
23 import org.restlet.ext.xml.DomRepresentation; 28 import org.restlet.ext.xml.DomRepresentation;
24 import org.restlet.representation.Representation; 29 import org.restlet.representation.Representation;
25 30
61 /** The URL path of the resource to work with an artifact collections.*/ 66 /** The URL path of the resource to work with an artifact collections.*/
62 public static final String PATH_OUT_COLLECTION = "/collection"; 67 public static final String PATH_OUT_COLLECTION = "/collection";
63 68
64 private String serverUrl; 69 private String serverUrl;
65 70
71 private String localeString;
72
66 73
67 public HttpClientImpl(String serverUrl) { 74 public HttpClientImpl(String serverUrl) {
68 this.serverUrl = serverUrl; 75 this.serverUrl = serverUrl;
76 }
77
78
79 /**
80 * This constructor might be used to modify the request's locale manually.
81 * E.g. the localization should not be based on the configured browser
82 * locale, but site specific configuration - than you are able to set the
83 * locale in this constructor.
84 *
85 * @param serverUrl The url that is used for the request.
86 * @param localeString The string representation of the desired locale.
87 */
88 public HttpClientImpl(String serverUrl, String localeString) {
89 this(serverUrl);
90
91 this.localeString = localeString;
69 } 92 }
70 93
71 94
72 @Override 95 @Override
73 public ArtifactFactory[] getArtifactFactories() 96 public ArtifactFactory[] getArtifactFactories()
202 "Connection to server failed: " + ioe.getMessage()); 225 "Connection to server failed: " + ioe.getMessage());
203 } 226 }
204 } 227 }
205 228
206 229
230 //==============================
231 // HTTP specific methods
232 //==============================
233
207 private Response doPost(String url, Document body) throws IOException { 234 private Response doPost(String url, Document body) throws IOException {
208 logger.info("Start HTTP-POST request to: "+ url); 235 logger.info("Start HTTP-POST request to: "+ url);
209 236
210 Client client = new Client(Protocol.HTTP); 237 Client client = new Client(Protocol.HTTP);
211 Request request = new Request(Method.POST, url); 238 Request request = prepareRequest(Method.POST, url);
212 239
213 Representation representation = new DomRepresentation( 240 Representation representation = new DomRepresentation(
214 MediaType.APPLICATION_XML, 241 MediaType.APPLICATION_XML,
215 body); 242 body);
216 243
229 256
230 private Response doGet(String url) throws IOException { 257 private Response doGet(String url) throws IOException {
231 logger.info("Start HTTP-POST request to: "+ url); 258 logger.info("Start HTTP-POST request to: "+ url);
232 259
233 Client client = new Client(Protocol.HTTP); 260 Client client = new Client(Protocol.HTTP);
234 Request request = new Request(Method.GET, url); 261 Request request = prepareRequest(Method.GET, url);
235 262
236 Response response = client.handle(request); 263 Response response = client.handle(request);
237 264
238 Status status = response.getStatus(); 265 Status status = response.getStatus();
239 if (status.getCode() != 200) { 266 if (status.getCode() != 200) {
240 logger.error("Response status: " + status.getCode()); 267 logger.error("Response status: " + status.getCode());
241 throw new IOException(status.getDescription()); 268 throw new IOException(status.getDescription());
242 } 269 }
243 270
244 return response; 271 return response;
272 }
273
274
275 /**
276 * This method prepares the request object.
277 *
278 * @param method The HTTP method (GET,POST).
279 * @param url The URL used for the request.
280 *
281 * @return the request object.
282 */
283 private Request prepareRequest(Method method, String url) {
284 Request request = new Request(method, url);
285
286 ClientInfo info = request.getClientInfo();
287
288 setLocale(info);
289
290 request.setClientInfo(info);
291
292 return request;
293 }
294
295
296 /**
297 * This method is called to set the request's locale.
298 *
299 * @param info The ClientInfo that is used to provide request information.
300 */
301 private void setLocale(ClientInfo info) {
302 if (localeString != null) {
303 return;
304 }
305
306 List<Preference<Language>> accepted =
307 new ArrayList<Preference<Language>>();
308
309 Language lang = Language.valueOf(localeString);
310
311 if (lang != null) {
312 Preference<Language> pref = new Preference<Language>();
313 pref.setMetadata(lang);
314 accepted.add(pref);
315
316 info.setAcceptedLanguages(accepted);
317 }
245 } 318 }
246 319
247 320
248 //============================== 321 //==============================
249 // Collection API 322 // Collection API

http://dive4elements.wald.intevation.org