mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 07:55:47 +01:00
implement createSession of ScmClientProvider
This commit is contained in:
@@ -40,6 +40,9 @@ package sonia.scm.client;
|
|||||||
public class ScmUrlProvider
|
public class ScmUrlProvider
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
public static final String API_PATH = "/api/rest/";
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
public static final String URLPART_AUTHENTICATION = "authentication/login";
|
public static final String URLPART_AUTHENTICATION = "authentication/login";
|
||||||
|
|
||||||
@@ -52,9 +55,16 @@ public class ScmUrlProvider
|
|||||||
* @param baseUrl
|
* @param baseUrl
|
||||||
*/
|
*/
|
||||||
public ScmUrlProvider(String baseUrl)
|
public ScmUrlProvider(String baseUrl)
|
||||||
|
{
|
||||||
|
if (!baseUrl.endsWith(API_PATH))
|
||||||
|
{
|
||||||
|
this.baseUrl = baseUrl.concat(API_PATH);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
this.baseUrl = baseUrl;
|
this.baseUrl = baseUrl;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,24 @@
|
|||||||
|
|
||||||
package sonia.scm.client;
|
package sonia.scm.client;
|
||||||
|
|
||||||
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig;
|
||||||
|
|
||||||
|
import sonia.scm.ScmState;
|
||||||
|
import sonia.scm.util.AssertUtil;
|
||||||
|
import sonia.scm.util.Util;
|
||||||
|
|
||||||
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
|
import com.sun.jersey.api.client.Client;
|
||||||
|
import com.sun.jersey.api.client.ClientResponse;
|
||||||
|
import com.sun.jersey.api.client.WebResource;
|
||||||
|
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
||||||
|
import com.sun.servicetag.UnauthorizedAccessException;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
@@ -57,6 +75,40 @@ public class JerseyClientProvider implements ScmClientProvider
|
|||||||
String password)
|
String password)
|
||||||
throws ScmClientException
|
throws ScmClientException
|
||||||
{
|
{
|
||||||
throw new UnsupportedOperationException("Not supported yet.");
|
AssertUtil.assertIsNotEmpty(url);
|
||||||
|
|
||||||
|
ScmUrlProvider urlProvider = new ScmUrlProvider(url);
|
||||||
|
DefaultAhcConfig config = new DefaultAhcConfig();
|
||||||
|
Client client = Client.create(config);
|
||||||
|
WebResource resource = client.resource(urlProvider.getAuthenticationUrl());
|
||||||
|
ClientResponse response = null;
|
||||||
|
|
||||||
|
if (Util.isNotEmpty(username) && Util.isNotEmpty(password))
|
||||||
|
{
|
||||||
|
MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
|
||||||
|
|
||||||
|
formData.add("username", username);
|
||||||
|
formData.add("password", password);
|
||||||
|
response = resource.type("application/x-www-form-urlencoded").post(
|
||||||
|
ClientResponse.class, formData);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
response = resource.get(ClientResponse.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.getStatus() == 401)
|
||||||
|
{
|
||||||
|
throw new UnauthorizedAccessException();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScmState state = response.getEntity(ScmState.class);
|
||||||
|
|
||||||
|
if (!state.isSuccess())
|
||||||
|
{
|
||||||
|
throw new ScmClientException("create ScmClientSession failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new JerseyClientSession(client, urlProvider, state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ import sonia.scm.user.User;
|
|||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
|
import com.sun.jersey.api.client.Client;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,6 +53,24 @@ import java.io.IOException;
|
|||||||
public class JerseyClientSession implements ScmClientSession
|
public class JerseyClientSession implements ScmClientSession
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs ...
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param client
|
||||||
|
* @param urlProvider
|
||||||
|
* @param state
|
||||||
|
*/
|
||||||
|
public JerseyClientSession(Client client, ScmUrlProvider urlProvider,
|
||||||
|
ScmState state)
|
||||||
|
{
|
||||||
|
this.client = client;
|
||||||
|
this.urlProvider = urlProvider;
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -112,4 +132,15 @@ public class JerseyClientSession implements ScmClientSession
|
|||||||
{
|
{
|
||||||
throw new UnsupportedOperationException("Not supported yet.");
|
throw new UnsupportedOperationException("Not supported yet.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private Client client;
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private ScmState state;
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private ScmUrlProvider urlProvider;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user