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,7 +46,25 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
|||
private String timezone;
|
||||
private String timezoneOffset;
|
||||
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) {
|
||||
this.userName = userName;
|
||||
return this;
|
||||
|
@ -106,10 +124,15 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
|||
this.sessionKey = sessionKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder jSessionId(String jSessionId) {
|
||||
this.jSessionId = jSessionId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LoginResponse build() {
|
||||
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;
|
||||
@SerializedName("sessionkey")
|
||||
private String sessionKey;
|
||||
private String jSessionId;
|
||||
|
||||
public LoginResponse(String userName, long userId, String password, long domainId, long timeout,
|
||||
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.userId = userId;
|
||||
this.password = password;
|
||||
|
@ -153,6 +177,7 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
|||
this.timezone = timezone;
|
||||
this.timezoneOffset = timezoneOffset;
|
||||
this.sessionKey = sessionKey;
|
||||
this.jSessionId = jSessionId;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
|
@ -202,6 +227,10 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
|||
public String getSessionKey() {
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
public String getJSessionId() {
|
||||
return jSessionId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
@ -223,6 +252,7 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
|||
if (timezoneOffset != null ? !timezoneOffset.equals(loginResponse.timezoneOffset) : loginResponse.timezoneOffset != 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;
|
||||
}
|
||||
|
@ -241,6 +271,7 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
|||
result = 31 * result + (timezone != null ? timezone.hashCode() : 0);
|
||||
result = 31 * result + (timezoneOffset != null ? timezoneOffset.hashCode() : 0);
|
||||
result = 31 * result + (sessionKey != null ? sessionKey.hashCode() : 0);
|
||||
result = 31 * result + (jSessionId != null ? jSessionId.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -259,6 +290,7 @@ public class LoginResponse implements Comparable<LoginResponse> {
|
|||
", timezone='" + timezone + '\'' +
|
||||
", timezoneOffset='" + timezoneOffset + '\'' +
|
||||
", sessionKey='" + sessionKey + '\'' +
|
||||
", jSessionId='" + jSessionId + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
|
|
@ -21,17 +21,18 @@ package org.jclouds.cloudstack.features;
|
|||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import org.jclouds.cloudstack.domain.Account;
|
||||
import org.jclouds.cloudstack.domain.LoginResponse;
|
||||
import org.jclouds.cloudstack.functions.ParseLoginResponseFromHttpResponse;
|
||||
import org.jclouds.rest.annotations.ExceptionParser;
|
||||
import org.jclouds.rest.annotations.QueryParams;
|
||||
import org.jclouds.rest.annotations.ResponseParser;
|
||||
import org.jclouds.rest.annotations.SelectJson;
|
||||
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
|
||||
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.CookieParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
/**
|
||||
|
@ -50,7 +51,7 @@ public interface SessionAsyncClient {
|
|||
*/
|
||||
@GET
|
||||
@QueryParams(keys = "command", values = "login")
|
||||
@SelectJson("loginresponse")
|
||||
@ResponseParser(ParseLoginResponseFromHttpResponse.class)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||
ListenableFuture<LoginResponse> loginUserInDomainWithHashOfPassword(@QueryParam("username") String userName,
|
||||
|
@ -64,9 +65,8 @@ public interface SessionAsyncClient {
|
|||
@SelectJson("account")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
|
||||
@HeaderParam(HttpHeaders.COOKIE)
|
||||
ListenableFuture<Account> getAccountByNameUsingSession(@QueryParam("name") String accountName,
|
||||
@QueryParam("sessionkey") String sessionKey);
|
||||
@QueryParam("sessionkey") String sessionKey, @CookieParam("JSESSIONID") String jSessionId);
|
||||
|
||||
/**
|
||||
* @see SessionClient#logoutUser
|
||||
|
|
|
@ -59,10 +59,12 @@ public interface SessionClient {
|
|||
* account name
|
||||
* @param sessionKey
|
||||
* a valid session key
|
||||
* @param jSessionId
|
||||
* the JSESSIONID cookie returned by the server
|
||||
* @return
|
||||
* 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
|
||||
|
|
|
@ -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 DOMAIN = "Partners/jCloud";
|
||||
|
||||
private LoginResponse loginResponse;
|
||||
private LoginResponse login;
|
||||
|
||||
@Test(enabled = true)
|
||||
public void testLoginWithHashedPassword() throws Exception {
|
||||
loginResponse = client.getSessionClient()
|
||||
public void testLoginWithHashOfPassword() throws Exception {
|
||||
login = client.getSessionClient()
|
||||
.loginUserInDomainWithHashOfPassword(USER, DOMAIN, md5Hex(PLAIN_TEXT_PASSWORD));
|
||||
|
||||
assertNotNull(loginResponse);
|
||||
assertNotNull(loginResponse.getSessionKey());
|
||||
assertNotNull(login);
|
||||
assertNotNull(login.getSessionKey());
|
||||
assertNotNull(login.getJSessionId());
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testLoginWithHashedPassword")
|
||||
@Test(dependsOnMethods = "testLoginWithHashOfPassword")
|
||||
public void testRetrieveUserInfoWithSessionKey() throws Exception {
|
||||
Account account = client.getSessionClient()
|
||||
.getAccountByNameUsingSession(loginResponse.getAccountName(), loginResponse.getSessionKey());
|
||||
Account account = client.getSessionClient().getAccountByNameUsingSession(
|
||||
login.getAccountName(), login.getSessionKey(), login.getJSessionId());
|
||||
|
||||
assertNotNull(account);
|
||||
assertEquals(account.getName(), loginResponse.getAccountName());
|
||||
assertEquals(account.getName(), login.getAccountName());
|
||||
|
||||
User currentUser = find(account.getUsers(), new Predicate<User>() {
|
||||
@Override
|
||||
public boolean apply(User user) {
|
||||
return user.getId() == loginResponse.getUserId();
|
||||
return user.getId() == login.getUserId();
|
||||
}
|
||||
});
|
||||
assertNotNull(currentUser);
|
||||
assertEquals(currentUser.getName(), loginResponse.getUserName());
|
||||
assertEquals(currentUser.getDomainId(), loginResponse.getDomainId());
|
||||
assertEquals(currentUser.getName(), login.getUserName());
|
||||
assertEquals(currentUser.getDomainId(), login.getDomainId());
|
||||
}
|
||||
|
||||
|
||||
@Test(dependsOnMethods = "testRetrieveUserInfoWithSessionKey")
|
||||
public void testLogout() throws Exception {
|
||||
client.getSessionClient().logoutUser(loginResponse.getSessionKey());
|
||||
client.getSessionClient().logoutUser(login.getSessionKey());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue