Mercurial > dive4elements > http-client
changeset 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 |
files | ChangeLog src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java |
diffstat | 2 files changed, 84 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- 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 <ingo@intevation.de> + + * 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 <ingo@intevation.de> * 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<Preference<Language>> accepted = + new ArrayList<Preference<Language>>(); + + Language lang = Language.valueOf(localeString); + + if (lang != null) { + Preference<Language> pref = new Preference<Language>(); + pref.setMetadata(lang); + accepted.add(pref); + + info.setAcceptedLanguages(accepted); + } + } + + //============================== // Collection API //==============================