mirror of https://github.com/apache/jclouds.git
Include the JSESSIONID as part of the LoginResponse
This commit is contained in:
parent
2b8e2a4c01
commit
c34aaa50b5
|
@ -46,6 +46,24 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
||||||
private String timezone;
|
private String timezone;
|
||||||
private String timezoneOffset;
|
private String timezoneOffset;
|
||||||
private String sessionKey;
|
private String sessionKey;
|
||||||
|
private String jSessionId;
|
||||||
|
|
||||||
|
public Builder copyOf(LoginResponse r) {
|
||||||
|
this.userName = r.userName;
|
||||||
|
this.userId = r.userId;
|
||||||
|
this.password = r.password;
|
||||||
|
this.domainId = r.domainId;
|
||||||
|
this.timeout = r.timeout;
|
||||||
|
this.accountName = r.accountName;
|
||||||
|
this.firstName = r.firstName;
|
||||||
|
this.lastName = r.lastName;
|
||||||
|
this.accountType = r.accountType;
|
||||||
|
this.timezone = r.timezone;
|
||||||
|
this.timezoneOffset = r.timezoneOffset;
|
||||||
|
this.sessionKey = r.sessionKey;
|
||||||
|
this.jSessionId = r.jSessionId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Builder userName(String userName) {
|
public Builder userName(String userName) {
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
|
@ -107,9 +125,14 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder jSessionId(String jSessionId) {
|
||||||
|
this.jSessionId = jSessionId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public LoginResponse build() {
|
public LoginResponse build() {
|
||||||
return new LoginResponse(userName, userId, password, domainId, timeout, accountName,
|
return new LoginResponse(userName, userId, password, domainId, timeout, accountName,
|
||||||
firstName, lastName, accountType, timezone, timezoneOffset, sessionKey);
|
firstName, lastName, accountType, timezone, timezoneOffset, sessionKey, jSessionId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,10 +160,11 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
||||||
private String timezoneOffset;
|
private String timezoneOffset;
|
||||||
@SerializedName("sessionkey")
|
@SerializedName("sessionkey")
|
||||||
private String sessionKey;
|
private String sessionKey;
|
||||||
|
private String jSessionId;
|
||||||
|
|
||||||
public LoginResponse(String userName, long userId, String password, long domainId, long timeout,
|
public LoginResponse(String userName, long userId, String password, long domainId, long timeout,
|
||||||
String accountName, String firstName, String lastName, Account.Type accountType,
|
String accountName, String firstName, String lastName, Account.Type accountType,
|
||||||
String timezone, String timezoneOffset, String sessionKey) {
|
String timezone, String timezoneOffset, String sessionKey, String jSessionId) {
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
|
@ -153,6 +177,7 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
||||||
this.timezone = timezone;
|
this.timezone = timezone;
|
||||||
this.timezoneOffset = timezoneOffset;
|
this.timezoneOffset = timezoneOffset;
|
||||||
this.sessionKey = sessionKey;
|
this.sessionKey = sessionKey;
|
||||||
|
this.jSessionId = jSessionId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
|
@ -203,6 +228,10 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
||||||
return sessionKey;
|
return sessionKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getJSessionId() {
|
||||||
|
return jSessionId;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
@ -223,6 +252,7 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
||||||
if (timezoneOffset != null ? !timezoneOffset.equals(loginResponse.timezoneOffset) : loginResponse.timezoneOffset != null)
|
if (timezoneOffset != null ? !timezoneOffset.equals(loginResponse.timezoneOffset) : loginResponse.timezoneOffset != null)
|
||||||
return false;
|
return false;
|
||||||
if (userName != null ? !userName.equals(loginResponse.userName) : loginResponse.userName != null) return false;
|
if (userName != null ? !userName.equals(loginResponse.userName) : loginResponse.userName != null) return false;
|
||||||
|
if (jSessionId != null ? !jSessionId.equals(loginResponse.jSessionId) : loginResponse.jSessionId != null) return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -241,6 +271,7 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
||||||
result = 31 * result + (timezone != null ? timezone.hashCode() : 0);
|
result = 31 * result + (timezone != null ? timezone.hashCode() : 0);
|
||||||
result = 31 * result + (timezoneOffset != null ? timezoneOffset.hashCode() : 0);
|
result = 31 * result + (timezoneOffset != null ? timezoneOffset.hashCode() : 0);
|
||||||
result = 31 * result + (sessionKey != null ? sessionKey.hashCode() : 0);
|
result = 31 * result + (sessionKey != null ? sessionKey.hashCode() : 0);
|
||||||
|
result = 31 * result + (jSessionId != null ? jSessionId.hashCode() : 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,6 +290,7 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
||||||
", timezone='" + timezone + '\'' +
|
", timezone='" + timezone + '\'' +
|
||||||
", timezoneOffset='" + timezoneOffset + '\'' +
|
", timezoneOffset='" + timezoneOffset + '\'' +
|
||||||
", sessionKey='" + sessionKey + '\'' +
|
", sessionKey='" + sessionKey + '\'' +
|
||||||
|
", jSessionId='" + jSessionId + '\'' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,17 +21,18 @@ package org.jclouds.cloudstack.features;
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
import org.jclouds.cloudstack.domain.Account;
|
import org.jclouds.cloudstack.domain.Account;
|
||||||
import org.jclouds.cloudstack.domain.LoginResponse;
|
import org.jclouds.cloudstack.domain.LoginResponse;
|
||||||
|
import org.jclouds.cloudstack.functions.ParseLoginResponseFromHttpResponse;
|
||||||
import org.jclouds.rest.annotations.ExceptionParser;
|
import org.jclouds.rest.annotations.ExceptionParser;
|
||||||
import org.jclouds.rest.annotations.QueryParams;
|
import org.jclouds.rest.annotations.QueryParams;
|
||||||
|
import org.jclouds.rest.annotations.ResponseParser;
|
||||||
import org.jclouds.rest.annotations.SelectJson;
|
import org.jclouds.rest.annotations.SelectJson;
|
||||||
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
|
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
|
||||||
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
|
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
|
||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.CookieParam;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.HeaderParam;
|
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,7 +51,7 @@ public interface SessionAsyncClient {
|
||||||
*/
|
*/
|
||||||
@GET
|
@GET
|
||||||
@QueryParams(keys = "command", values = "login")
|
@QueryParams(keys = "command", values = "login")
|
||||||
@SelectJson("loginresponse")
|
@ResponseParser(ParseLoginResponseFromHttpResponse.class)
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||||
ListenableFuture<LoginResponse> loginUserInDomainWithHashOfPassword(@QueryParam("username") String userName,
|
ListenableFuture<LoginResponse> loginUserInDomainWithHashOfPassword(@QueryParam("username") String userName,
|
||||||
|
@ -64,9 +65,8 @@ public interface SessionAsyncClient {
|
||||||
@SelectJson("account")
|
@SelectJson("account")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||||
@HeaderParam(HttpHeaders.COOKIE)
|
|
||||||
ListenableFuture<Account> getAccountByNameUsingSession(@QueryParam("name") String accountName,
|
ListenableFuture<Account> getAccountByNameUsingSession(@QueryParam("name") String accountName,
|
||||||
@QueryParam("sessionkey") String sessionKey);
|
@QueryParam("sessionkey") String sessionKey, @CookieParam("JSESSIONID") String jSessionId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see SessionClient#logoutUser
|
* @see SessionClient#logoutUser
|
||||||
|
|
|
@ -59,10 +59,12 @@ public interface SessionClient {
|
||||||
* account name
|
* account name
|
||||||
* @param sessionKey
|
* @param sessionKey
|
||||||
* a valid session key
|
* a valid session key
|
||||||
|
* @param jSessionId
|
||||||
|
* the JSESSIONID cookie returned by the server
|
||||||
* @return
|
* @return
|
||||||
* account instance or null
|
* account instance or null
|
||||||
*/
|
*/
|
||||||
Account getAccountByNameUsingSession(String accountName, String sessionKey);
|
Account getAccountByNameUsingSession(String accountName, String sessionKey, String jSessionId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs out the user by invalidating the session key
|
* Logs out the user by invalidating the session key
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package org.jclouds.cloudstack.functions;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Splitter;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
|
import org.jclouds.cloudstack.domain.LoginResponse;
|
||||||
|
import org.jclouds.http.HttpResponse;
|
||||||
|
import org.jclouds.http.functions.ParseFirstJsonValueNamed;
|
||||||
|
import org.jclouds.json.internal.GsonWrapper;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import static com.google.common.collect.Iterables.get;
|
||||||
|
import static com.google.common.collect.Iterables.getOnlyElement;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Andrei Savu
|
||||||
|
*/
|
||||||
|
public class ParseLoginResponseFromHttpResponse implements Function<HttpResponse, LoginResponse> {
|
||||||
|
|
||||||
|
private ParseFirstJsonValueNamed<LoginResponse> parser;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ParseLoginResponseFromHttpResponse(GsonWrapper gson) {
|
||||||
|
this.parser = new ParseFirstJsonValueNamed<LoginResponse>(checkNotNull(gson, "gsonWrapper"),
|
||||||
|
new TypeLiteral<LoginResponse>(){}, "loginresponse");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LoginResponse apply(HttpResponse response) {
|
||||||
|
checkNotNull(response, "response");
|
||||||
|
|
||||||
|
LoginResponse login = parser.apply(response);
|
||||||
|
checkNotNull(login, "loginResponse");
|
||||||
|
|
||||||
|
String jSessionId = get(Splitter.on("=").split(get(Splitter.on(";").trimResults().split(
|
||||||
|
getOnlyElement(response.getHeaders().get("Set-Cookie"))), 0)), 1);
|
||||||
|
|
||||||
|
return LoginResponse.builder().copyOf(login).jSessionId(jSessionId).build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -41,38 +41,39 @@ public class SessionClientLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
private final String PLAIN_TEXT_PASSWORD = "jcl0ud";
|
private final String PLAIN_TEXT_PASSWORD = "jcl0ud";
|
||||||
private final String DOMAIN = "Partners/jCloud";
|
private final String DOMAIN = "Partners/jCloud";
|
||||||
|
|
||||||
private LoginResponse loginResponse;
|
private LoginResponse login;
|
||||||
|
|
||||||
@Test(enabled = true)
|
@Test(enabled = true)
|
||||||
public void testLoginWithHashedPassword() throws Exception {
|
public void testLoginWithHashOfPassword() throws Exception {
|
||||||
loginResponse = client.getSessionClient()
|
login = client.getSessionClient()
|
||||||
.loginUserInDomainWithHashOfPassword(USER, DOMAIN, md5Hex(PLAIN_TEXT_PASSWORD));
|
.loginUserInDomainWithHashOfPassword(USER, DOMAIN, md5Hex(PLAIN_TEXT_PASSWORD));
|
||||||
|
|
||||||
assertNotNull(loginResponse);
|
assertNotNull(login);
|
||||||
assertNotNull(loginResponse.getSessionKey());
|
assertNotNull(login.getSessionKey());
|
||||||
|
assertNotNull(login.getJSessionId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(dependsOnMethods = "testLoginWithHashedPassword")
|
@Test(dependsOnMethods = "testLoginWithHashOfPassword")
|
||||||
public void testRetrieveUserInfoWithSessionKey() throws Exception {
|
public void testRetrieveUserInfoWithSessionKey() throws Exception {
|
||||||
Account account = client.getSessionClient()
|
Account account = client.getSessionClient().getAccountByNameUsingSession(
|
||||||
.getAccountByNameUsingSession(loginResponse.getAccountName(), loginResponse.getSessionKey());
|
login.getAccountName(), login.getSessionKey(), login.getJSessionId());
|
||||||
|
|
||||||
assertNotNull(account);
|
assertNotNull(account);
|
||||||
assertEquals(account.getName(), loginResponse.getAccountName());
|
assertEquals(account.getName(), login.getAccountName());
|
||||||
|
|
||||||
User currentUser = find(account.getUsers(), new Predicate<User>() {
|
User currentUser = find(account.getUsers(), new Predicate<User>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(User user) {
|
public boolean apply(User user) {
|
||||||
return user.getId() == loginResponse.getUserId();
|
return user.getId() == login.getUserId();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
assertNotNull(currentUser);
|
assertNotNull(currentUser);
|
||||||
assertEquals(currentUser.getName(), loginResponse.getUserName());
|
assertEquals(currentUser.getName(), login.getUserName());
|
||||||
assertEquals(currentUser.getDomainId(), loginResponse.getDomainId());
|
assertEquals(currentUser.getDomainId(), login.getDomainId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(dependsOnMethods = "testRetrieveUserInfoWithSessionKey")
|
@Test(dependsOnMethods = "testRetrieveUserInfoWithSessionKey")
|
||||||
public void testLogout() throws Exception {
|
public void testLogout() throws Exception {
|
||||||
client.getSessionClient().logoutUser(loginResponse.getSessionKey());
|
client.getSessionClient().logoutUser(login.getSessionKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue