View Javadoc

1   package nl.geozet.openls.client;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.io.InputStreamReader;
6   import java.io.UnsupportedEncodingException;
7   import java.net.URLEncoder;
8   import java.util.Map;
9   import java.util.Map.Entry;
10  
11  import nl.geozet.common.ProxyConfiguration;
12  import nl.geozet.openls.databinding.openls.GeocodeRequest;
13  import nl.geozet.openls.databinding.openls.GeocodeResponse;
14  import nl.geozet.openls.parser.OpenLSResponseParser;
15  
16  import org.apache.commons.httpclient.HttpClient;
17  import org.apache.commons.httpclient.HttpException;
18  import org.apache.commons.httpclient.HttpStatus;
19  import org.apache.commons.httpclient.methods.GetMethod;
20  import org.apache.commons.httpclient.methods.PostMethod;
21  import org.apache.commons.httpclient.methods.StringRequestEntity;
22  import org.apache.log4j.Logger;
23  
24  /**
25   * OpenLSClient.
26   * 
27   * @author strampel@atlis.nl
28   * @author prinsmc@minlnv.nl
29   */
30  public class OpenLSClient {
31  
32      /** onze LOGGER. */
33      private static final Logger LOGGER = Logger.getLogger(OpenLSClient.class);
34  
35      /** The client. */
36      private final HttpClient client;
37  
38      /** De open ls response parser. */
39      private final OpenLSResponseParser openLSResponseParser;
40  
41      /**
42       * Instantiates a new open ls client.
43       */
44      public OpenLSClient() {
45          this.client = new HttpClient();
46          ProxyConfiguration.updateProxyConfig(this.client);
47          this.openLSResponseParser = new OpenLSResponseParser();
48      }
49  
50      /**
51       * Do get open ls request.
52       * 
53       * @param url
54       *            the url
55       * @param getParams
56       *            the get params
57       * @return the geocode response, will be null if something went wrong in the
58       *         process of getting an openls response and parsing it
59       */
60      public GeocodeResponse doGetOpenLSRequest(String url,
61              Map<String, String> getParams) {
62          final String queryString = url.endsWith("?") ? url : url + "?";
63          final StringBuilder qs = new StringBuilder(queryString);
64          try {
65              for (final Entry<String, String> getParam : getParams.entrySet()) {
66                  qs.append(URLEncoder.encode(getParam.getKey(), "UTF-8"))
67                          .append("=")
68                          .append(URLEncoder.encode(getParam.getValue(), "UTF-8"))
69                          .append("&");
70              }
71          } catch (final UnsupportedEncodingException e) {
72              LOGGER.fatal("De gebruikte Java VM ondersteunt geen UTF-8 encoding: "
73                      + e);
74          }
75          LOGGER.debug("GETting OLS query:\n\t" + qs.toString());
76          final GetMethod getMethod = new GetMethod(qs.toString());
77          BufferedReader br = null;
78          final StringBuilder sb = new StringBuilder();
79          try {
80              final int returnCode = this.client.executeMethod(getMethod);
81              if (returnCode == HttpStatus.SC_OK) {
82                  br = new BufferedReader(new InputStreamReader(
83                          getMethod.getResponseBodyAsStream()));
84                  String readLine;
85                  while (((readLine = br.readLine()) != null)) {
86                      sb.append(readLine);
87                  }
88              } else {
89                  LOGGER.error("OpenLS server get error response: "
90                          + getMethod.getResponseBodyAsString());
91              }
92          } catch (final HttpException e) {
93              LOGGER.fatal(
94                      "Versturen get request naar OpenLS server is mislukt: ", e);
95          } catch (final IOException e) {
96              LOGGER.fatal(
97                      "Ontvangen get response van OpenLS server is mislukt: ", e);
98          } finally {
99              getMethod.releaseConnection();
100             if (br != null) {
101                 try {
102                     br.close();
103                 } catch (final IOException fe) {
104                     LOGGER.debug(
105                             "Fout opgetreden tijdens release van verbinding, "
106                                     + "hier is niks meer aan te doen.", fe);
107                 }
108             }
109         }
110         return this.openLSResponseParser.parseOpenLSResponse(sb.toString());
111     }
112 
113     /**
114      * Do post open ls request.
115      * 
116      * @param url
117      *            the url
118      * @param request
119      *            the request
120      * @return the geocode response, will be null if something went wrong in the
121      *         process of getting an openls response and parsing it
122      */
123     public GeocodeResponse doPostOpenLSRequest(String url,
124             GeocodeRequest request) {
125         final PostMethod postMethod = new PostMethod(url);
126         LOGGER.debug("POSTting OLS query:\n\t" + request.toXML());
127 
128         StringRequestEntity str = null;
129         try {
130             str = new StringRequestEntity(request.toXML(), "text/xml", "UTF-8");
131         } catch (final UnsupportedEncodingException e) {
132             LOGGER.fatal("De gebruikte Java VM ondersteunt geen UTF-8 encoding: "
133                     + e);
134         }
135         postMethod.setRequestEntity(str);
136         final StringBuilder sb = new StringBuilder();
137         BufferedReader br = null;
138         try {
139             final int returnCode = this.client.executeMethod(postMethod);
140             if (returnCode == HttpStatus.SC_OK) {
141                 br = new BufferedReader(new InputStreamReader(
142                         postMethod.getResponseBodyAsStream()));
143                 String readLine;
144                 while (((readLine = br.readLine()) != null)) {
145                     sb.append(readLine);
146                 }
147             } else {
148                 LOGGER.error("OpenLS server post error response: "
149                         + postMethod.getResponseBodyAsString());
150             }
151         } catch (final HttpException e) {
152             LOGGER.fatal(
153                     "Versturen post request naar OpenLS server is mislukt: ", e);
154         } catch (final IOException e) {
155             LOGGER.fatal(
156                     "Ontvangen post response van OpenLS server is mislukt: ", e);
157         } finally {
158             postMethod.releaseConnection();
159             if (br != null) {
160                 try {
161                     br.close();
162                 } catch (final IOException fe) {
163                     LOGGER.debug(
164                             "Fout opgetreden tijden release van verbinding, "
165                                     + "hier is niks meer aan te doen.", fe);
166                 }
167             }
168         }
169         return this.openLSResponseParser.parseOpenLSResponse(sb.toString());
170     }
171 }