comparison gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java @ 40:23a4f196d7d6

Adding Languagetransfer to the ArtifactDatabase to the GNV-Client gnv/trunk@180 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 02 Oct 2009 14:55:18 +0000
parents fccf90761825
children f35b7d7a04d6
comparison
equal deleted inserted replaced
39:6c46f9b16166 40:23a4f196d7d6
9 import java.util.ArrayList; 9 import java.util.ArrayList;
10 import java.util.Collection; 10 import java.util.Collection;
11 import java.util.HashMap; 11 import java.util.HashMap;
12 import java.util.Iterator; 12 import java.util.Iterator;
13 import java.util.Map; 13 import java.util.Map;
14 import java.util.List;
15 import java.util.Locale;
14 16
15 import org.apache.log4j.Logger; 17 import org.apache.log4j.Logger;
18
16 import org.restlet.Client; 19 import org.restlet.Client;
20
17 import org.restlet.data.Method; 21 import org.restlet.data.Method;
18 import org.restlet.data.Protocol; 22 import org.restlet.data.Protocol;
19 import org.restlet.data.Request; 23 import org.restlet.data.Request;
20 import org.restlet.data.Response; 24 import org.restlet.data.Response;
25 import org.restlet.data.Preference;
26 import org.restlet.data.Language;
27 import org.restlet.data.ClientInfo;
28
21 import org.restlet.representation.Representation; 29 import org.restlet.representation.Representation;
22 import org.restlet.representation.StringRepresentation; 30 import org.restlet.representation.StringRepresentation;
23 import org.w3c.dom.Document; 31 import org.w3c.dom.Document;
24 import org.w3c.dom.Element; 32 import org.w3c.dom.Element;
25 import org.w3c.dom.Node; 33 import org.w3c.dom.Node;
71 * Is the Class initialized? 79 * Is the Class initialized?
72 */ 80 */
73 private static boolean initialized = false; 81 private static boolean initialized = false;
74 82
75 /** 83 /**
84 * Request locale
85 */
86 private Locale locale;
87
88
89 /**
76 * Constructor 90 * Constructor
77 */ 91 */
78 public DefaultArtifactDatabaseClient() { 92 public DefaultArtifactDatabaseClient() {
79 super(); 93
80 } 94 }
81 95
82 /** 96 /**
83 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getArtifactFactories() 97 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getArtifactFactories()
84 */ 98 */
149 Document document = xmlUtils.readDocument(output.getStream()); 163 Document document = xmlUtils.readDocument(output.getStream());
150 this.check4ExceptionReport(document); 164 this.check4ExceptionReport(document);
151 return document; 165 return document;
152 } 166 }
153 167
168 protected Request initialize(Request request) {
169 return initialize(request, locale);
170 }
171
172 protected Request initialize(Request request, Locale locale) {
173 if (locale != null) {
174 ClientInfo clientInfo = request.getClientInfo();
175 String lang = locale.getLanguage();
176 Language language = new Language(lang);
177 List<Preference<Language>> acceptedLanguages =
178 new ArrayList<Preference<Language>>(2);
179 acceptedLanguages.add(new Preference(language, 1.0f));
180 acceptedLanguages.add(new Preference(Language.ALL, 0.5f));
181 clientInfo.setAcceptedLanguages(acceptedLanguages);
182 }
183 return request;
184 }
185
154 /** 186 /**
155 * @param requestUrl 187 * @param requestUrl
156 * @param requestBody 188 * @param requestBody
157 * @return 189 * @return
158 */ 190 */
159 private Representation doGetRequestInternal(String requestUrl, 191 private Representation doGetRequestInternal(String requestUrl,
160 Document requestBody) { 192 Document requestBody) {
161 Client client = new Client(Protocol.HTTP); 193 Client client = new Client(Protocol.HTTP);
162 Request request = new Request(Method.GET, requestUrl); 194 Request request = initialize(new Request(Method.GET, requestUrl));
163 if (requestBody != null) { 195 if (requestBody != null) {
164 String documentBody = new XMLUtils() 196 String documentBody = new XMLUtils()
165 .writeDocument2String(requestBody); 197 .writeDocument2String(requestBody);
166 Representation representation = new StringRepresentation( 198 Representation representation = new StringRepresentation(
167 documentBody); 199 documentBody);
180 throws IOException { 212 throws IOException {
181 log.debug("##################################################"); 213 log.debug("##################################################");
182 log.debug(new XMLUtils().writeDocument2String(requestBody)); 214 log.debug(new XMLUtils().writeDocument2String(requestBody));
183 log.debug("##################################################"); 215 log.debug("##################################################");
184 Client client = new Client(Protocol.HTTP); 216 Client client = new Client(Protocol.HTTP);
185 Request request = new Request(Method.POST, requestUrl); 217 Request request = initialize(new Request(Method.POST, requestUrl));
186 String documentBody = new XMLUtils().writeDocument2String(requestBody); 218 String documentBody = new XMLUtils().writeDocument2String(requestBody);
187 Representation representation = new StringRepresentation(documentBody); 219 Representation representation = new StringRepresentation(documentBody);
188 request.setEntity(representation); 220 request.setEntity(representation);
189 Response response = client.handle(request); 221 Response response = client.handle(request);
190 // TODO RESPONSESTATUS AUSWERTEN. 222 // TODO RESPONSESTATUS AUSWERTEN.
653 throw new ArtifactDatabaseClientException(e); 685 throw new ArtifactDatabaseClientException(e);
654 } 686 }
655 687
656 return result; 688 return result;
657 } 689 }
690
691 public void setLocale(Locale locale) {
692 this.locale = locale;
693 }
658 } 694 }

http://dive4elements.wald.intevation.org