X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.launcher%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fserver%2Fclient%2Fimpl%2FAbstractHttpServicesClient.java;h=94f9c68fa757f288b2bc35548ff7df5f76cdb5d0;hb=74904a755b5b344238eafa798419b80c5e74f7ed;hp=43f8fe16a9a82bbdc127b42ab447aa17d07c786b;hpb=bf343dd62ca1c1610b8b2cc5a1af2879e57e6ff3;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/server/client/impl/AbstractHttpServicesClient.java b/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/server/client/impl/AbstractHttpServicesClient.java index 43f8fe16a..94f9c68fa 100644 --- a/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/server/client/impl/AbstractHttpServicesClient.java +++ b/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/server/client/impl/AbstractHttpServicesClient.java @@ -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 callService(String path, Map 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 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; + } + }