# HG changeset patch # User Ingo Weinzierl # Date 1302760860 0 # Node ID e79283dad6f2a0b42eb5d566a6feb2121ad4abf5 # Parent 072e8d488f831c30c1017e0d5ff4eb5509e51e72 Added the option to set the request's locale manually. http-client/trunk@1679 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 072e8d488f83 -r e79283dad6f2 ChangeLog --- a/ChangeLog Thu Apr 07 11:28:15 2011 +0000 +++ b/ChangeLog Thu Apr 14 06:01:00 2011 +0000 @@ -1,3 +1,12 @@ +2011-04-14 Ingo Weinzierl + + * src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java: + Added a new constructor and methods to set the locale of the requests + manually. This is useful, if there is an application which language + depends on site specific user settings. E.g. if the user has the option + to choose the language in the browser window by button click - which + differs from the browser settings. + 2011-04-07 Ingo Weinzierl * src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java, diff -r 072e8d488f83 -r e79283dad6f2 src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java --- a/src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java Thu Apr 07 11:28:15 2011 +0000 +++ b/src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java Thu Apr 14 06:01:00 2011 +0000 @@ -10,14 +10,19 @@ import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; import org.apache.log4j.Logger; import org.restlet.Client; import org.restlet.Request; import org.restlet.Response; +import org.restlet.data.ClientInfo; +import org.restlet.data.Language; import org.restlet.data.MediaType; import org.restlet.data.Method; +import org.restlet.data.Preference; import org.restlet.data.Protocol; import org.restlet.data.Status; import org.restlet.ext.xml.DomRepresentation; @@ -63,12 +68,30 @@ private String serverUrl; + private String localeString; + public HttpClientImpl(String serverUrl) { this.serverUrl = serverUrl; } + /** + * This constructor might be used to modify the request's locale manually. + * E.g. the localization should not be based on the configured browser + * locale, but site specific configuration - than you are able to set the + * locale in this constructor. + * + * @param serverUrl The url that is used for the request. + * @param localeString The string representation of the desired locale. + */ + public HttpClientImpl(String serverUrl, String localeString) { + this(serverUrl); + + this.localeString = localeString; + } + + @Override public ArtifactFactory[] getArtifactFactories() throws ConnectionException @@ -204,11 +227,15 @@ } + //============================== + // HTTP specific methods + //============================== + private Response doPost(String url, Document body) throws IOException { logger.info("Start HTTP-POST request to: "+ url); Client client = new Client(Protocol.HTTP); - Request request = new Request(Method.POST, url); + Request request = prepareRequest(Method.POST, url); Representation representation = new DomRepresentation( MediaType.APPLICATION_XML, @@ -231,7 +258,7 @@ logger.info("Start HTTP-POST request to: "+ url); Client client = new Client(Protocol.HTTP); - Request request = new Request(Method.GET, url); + Request request = prepareRequest(Method.GET, url); Response response = client.handle(request); @@ -245,6 +272,52 @@ } + /** + * This method prepares the request object. + * + * @param method The HTTP method (GET,POST). + * @param url The URL used for the request. + * + * @return the request object. + */ + private Request prepareRequest(Method method, String url) { + Request request = new Request(method, url); + + ClientInfo info = request.getClientInfo(); + + setLocale(info); + + request.setClientInfo(info); + + return request; + } + + + /** + * This method is called to set the request's locale. + * + * @param info The ClientInfo that is used to provide request information. + */ + private void setLocale(ClientInfo info) { + if (localeString != null) { + return; + } + + List> accepted = + new ArrayList>(); + + Language lang = Language.valueOf(localeString); + + if (lang != null) { + Preference pref = new Preference(); + pref.setMetadata(lang); + accepted.add(pref); + + info.setAcceptedLanguages(accepted); + } + } + + //============================== // Collection API //==============================