]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/server/client/impl/AbstractHttpServicesClient.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / runtime / org.argeo.slc.launcher / src / main / java / org / argeo / slc / server / client / impl / AbstractHttpServicesClient.java
index 43f8fe16a9a82bbdc127b42ab447aa17d07c786b..94f9c68fa757f288b2bc35548ff7df5f76cdb5d0 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.argeo.slc.server.client.impl;
 
 import java.io.IOException;
@@ -5,7 +20,9 @@ import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.io.Writer;
+import java.net.Authenticator;
 import java.net.HttpURLConnection;
+import java.net.PasswordAuthentication;
 import java.net.URL;
 import java.net.URLEncoder;
 import java.util.Map;
@@ -27,6 +44,10 @@ import org.springframework.util.Assert;
 public abstract class AbstractHttpServicesClient implements HttpServicesClient {
        private final static Log log = LogFactory
                        .getLog(AbstractHttpServicesClient.class);
+
+       private String user;
+       private String password;
+
        private Unmarshaller unmarshaller;
        private Marshaller marshaller;
        private String baseUrl;
@@ -35,9 +56,19 @@ public abstract class AbstractHttpServicesClient implements HttpServicesClient {
        private Long retryPeriod = 1000l;
        private Long defaultTimeout = 30 * 1000l;
 
+       public void init() {
+               if (user != null && password != null)
+                       Authenticator.setDefault(new Authenticator() {
+                               protected PasswordAuthentication getPasswordAuthentication() {
+                                       return new PasswordAuthentication(user, password
+                                                       .toCharArray());
+                               }
+                       });
+       }
+
        @SuppressWarnings(value = { "unchecked" })
        public <T> T callService(String path, Map<String, String> parameters) {
-               return (T)callService(path, parameters, null);
+               return (T) callService(path, parameters, null);
        }
 
        @SuppressWarnings(value = { "unchecked" })
@@ -101,6 +132,7 @@ public abstract class AbstractHttpServicesClient implements HttpServicesClient {
 
        protected Object callServiceLowLevel(String path,
                        Map<String, String> parameters, Object body) throws IOException {
+
                Assert.notNull(baseUrl, "base url");
                HttpURLConnection connection = null;
                Writer writer = null;
@@ -217,4 +249,12 @@ public abstract class AbstractHttpServicesClient implements HttpServicesClient {
                return defaultTimeout;
        }
 
+       public void setUser(String user) {
+               this.user = user;
+       }
+
+       public void setPassword(String password) {
+               this.password = password;
+       }
+
 }