]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/impl/AbstractHttpServicesClient.java
@update:79; INtroduce event polling (not working yet)
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / server / client / impl / AbstractHttpServicesClient.java
index 410e8d9afe029d119425a8f0d565bf35750a1013..a7936916ef110b99d8ba84a4e8f53134fbfe4770 100644 (file)
@@ -8,7 +8,6 @@ import java.io.Writer;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLEncoder;
-import java.util.Iterator;
 import java.util.Map;
 
 import javax.xml.transform.Source;
@@ -54,14 +53,11 @@ public abstract class AbstractHttpServicesClient implements HttpServicesClient {
        @SuppressWarnings(value = { "unchecked" })
        public <T> T callServiceSafe(String path, Map<String, String> parameters,
                        Condition<T> condition, Long timeout) {
-               if (timeout == null)
-                       timeout = defaultTimeout;
 
                long begin = System.currentTimeMillis();
                try {
                        Object obj = null;
-                       long duration = System.currentTimeMillis() - begin;
-                       while (duration < timeout) {
+                       while (System.currentTimeMillis() - begin < timeout(timeout)) {
                                try {
                                        obj = callServiceLowLevel(path, parameters, null);
                                } catch (IOException e) {
@@ -93,7 +89,7 @@ public abstract class AbstractHttpServicesClient implements HttpServicesClient {
                                                                + " on "
                                                                + baseUrl
                                                                + " did not return an answer after calling it safely for "
-                                                               + duration + " ms.");
+                                                               + timeout(timeout) + " ms.");
                        return (T) obj;
                } catch (Exception e) {
                        throw new SlcException(
@@ -157,16 +153,19 @@ public abstract class AbstractHttpServicesClient implements HttpServicesClient {
                try {
                        if (parameters != null && parameters.size() != 0) {
                                buf.append('?');
-                               Iterator<String> it = parameters.keySet().iterator();
-                               String key = null;
-                               while (it.hasNext()) {
-                                       if (key != null)
-                                               buf.append('&');
-                                       key = it.next();
-                                       String keyEncoded = URLEncoder.encode(key, urlEncoding);
-                                       String valueEncoded = URLEncoder.encode(
-                                                       parameters.get(key), urlEncoding);
-                                       buf.append(keyEncoded).append('=').append(valueEncoded);
+                               boolean first = true;
+                               for (String key : parameters.keySet()) {
+                                       String value = parameters.get(key);
+                                       if (value != null) {
+                                               if (first)
+                                                       first = false;
+                                               else
+                                                       buf.append('&');
+                                               String keyEncoded = URLEncoder.encode(key, urlEncoding);
+                                               String valueEncoded = URLEncoder.encode(value,
+                                                               urlEncoding);
+                                               buf.append(keyEncoded).append('=').append(valueEncoded);
+                                       }
                                }
                        }
 
@@ -176,6 +175,12 @@ public abstract class AbstractHttpServicesClient implements HttpServicesClient {
                }
        }
 
+       public Long timeout(Long timeout) {
+               if (timeout == null)
+                       timeout = getDefaultTimeout();
+               return timeout;
+       }
+
        public void setUnmarshaller(Unmarshaller unmarshaller) {
                this.unmarshaller = unmarshaller;
        }
@@ -202,9 +207,13 @@ public abstract class AbstractHttpServicesClient implements HttpServicesClient {
                this.encoding = encoding;
        }
 
-       /** Default is 30s*/
+       /** Default is 30s */
        public void setDefaultTimeout(Long defaultTimeout) {
                this.defaultTimeout = defaultTimeout;
        }
 
+       public Long getDefaultTimeout() {
+               return defaultTimeout;
+       }
+
 }