]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/SlcServerHttpClient.java
Make tree test results serializable so that it can be used with ObjectList.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / server / client / SlcServerHttpClient.java
1 package org.argeo.slc.server.client;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.HttpURLConnection;
6 import java.net.URL;
7 import java.util.Map;
8
9 import javax.xml.transform.stream.StreamSource;
10
11 import org.apache.commons.io.IOUtils;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.slc.SlcException;
15 import org.springframework.oxm.Unmarshaller;
16 import org.springframework.util.Assert;
17
18 public class SlcServerHttpClient {
19 private final static Log log = LogFactory.getLog(SlcServerHttpClient.class);
20
21 private Unmarshaller unmarshaller;
22 private String baseUrl;
23
24 private Long retryPeriod = 1000l;
25
26 @SuppressWarnings(value = { "unchecked" })
27 public <T> T callService(String path, Map<String, String> parameters) {
28 try {
29 return (T) callServiceLowLevel(path, parameters);
30 } catch (Exception e) {
31 throw new SlcException("Cannot call service " + path + " on "
32 + baseUrl, e);
33 }
34 }
35
36 @SuppressWarnings(value = { "unchecked" })
37 public <T> T callServiceSafe(String path, Map<String, String> parameters,
38 long timeout) {
39 long begin = System.currentTimeMillis();
40 try {
41 Object obj = null;
42 long duration = System.currentTimeMillis() - begin;
43 while (duration < timeout) {
44 try {
45 obj = callServiceLowLevel(path, parameters);
46 } catch (IOException e) {
47 if (log.isTraceEnabled())
48 log.trace("Exception when calling service " + path
49 + " on " + baseUrl, e);
50 }
51
52 if (obj != null)
53 break;
54
55 // wait a bit
56 try {
57 Thread.sleep(retryPeriod);
58 } catch (InterruptedException e) {
59 // silent
60 }
61 }
62
63 if (obj == null)
64 throw new SlcException(
65 "Service "
66 + path
67 + " on "
68 + baseUrl
69 + " did not return an answer after calling it safely for "
70 + duration + " ms.");
71 return (T) obj;
72 } catch (Exception e) {
73 throw new SlcException(
74 "Unexpected exception when safely calling service " + path
75 + " on " + baseUrl, e);
76 }
77 }
78
79 protected Object callServiceLowLevel(String path,
80 Map<String, String> parameters) throws IOException {
81 Assert.notNull(baseUrl, "base url");
82 InputStream in = null;
83 try {
84 URL url = new URL(baseUrl + path);
85 HttpURLConnection connection = (HttpURLConnection) url
86 .openConnection();
87 if (parameters != null) {
88 for (String key : parameters.keySet()) {
89 connection.addRequestProperty(key, parameters.get(key));
90 }
91 }
92
93 connection.connect();
94
95 in = connection.getInputStream();
96 StreamSource source = new StreamSource(in);
97 Object obj = unmarshaller.unmarshal(source);
98 return obj;
99 } finally {
100 IOUtils.closeQuietly(in);
101 }
102 }
103
104 public void setUnmarshaller(Unmarshaller unmarshaller) {
105 this.unmarshaller = unmarshaller;
106 }
107
108 public void setBaseUrl(String baseUrl) {
109 this.baseUrl = baseUrl;
110 }
111
112 public Long getRetryPeriod() {
113 return retryPeriod;
114 }
115
116 /** Retry period in ms when accessing service safely. Default is 1000 ms. */
117 public void setRetryPeriod(Long retryPeriod) {
118 this.retryPeriod = retryPeriod;
119 }
120
121 }