From 9903f1ad7fbd91b8ff3a9eac67c6d1e68f9d75b1 Mon Sep 17 00:00:00 2001 From: skavanagh Date: Sun, 21 Sep 2014 11:54:56 -0400 Subject: [PATCH] [OSJC-127] Added operations to support all active authorizations Added operations to support multiple authorizations (removeAuthorization, getAuthorization, getAuthorizations). --- src/main/java/com/openshift/client/IUser.java | 29 ++++++++++ .../internal/client/APIResource.java | 56 +++++++++++++++++-- .../internal/client/UserResource.java | 29 +++++++++- .../client/response/EnumDataType.java | 1 + .../response/OpenShiftJsonDTOFactory.java | 24 ++++++++ .../client/AuthorizationIntegrationTest.java | 11 ++-- 6 files changed, 137 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/openshift/client/IUser.java b/src/main/java/com/openshift/client/IUser.java index 88ca1f3d..dc27eaf2 100755 --- a/src/main/java/com/openshift/client/IUser.java +++ b/src/main/java/com/openshift/client/IUser.java @@ -10,6 +10,7 @@ ******************************************************************************/ package com.openshift.client; +import java.util.Collection; import java.util.List; /** @@ -52,6 +53,24 @@ public interface IUser extends IOpenShiftResource { */ public IAuthorization getAuthorization() throws OpenShiftException; + /** + * Returns authorization using a token or id + * + * @param id + * authorization token or id + * @return authorization + * @throws OpenShiftException + */ + public IAuthorization getAuthorization(String id) throws OpenShiftException; + + /** + * Returns all current authorizations for a user + * + * @return all authorizations + * @throws OpenShiftException + */ + public Collection getAuthorizations() throws OpenShiftException; + /** * Creates and returns new authorization set for user * @@ -78,6 +97,16 @@ public interface IUser extends IOpenShiftResource { */ public IAuthorization createAuthorization(String note, String scopes, int expiresIn) throws OpenShiftException; + /** + * Removes authorization using a token or id + * + * @param id + * authorization token or id + * @return success or fail + * @throws OpenShiftException + */ + public boolean removeAuthorization(String id) throws OpenShiftException; + /** * Deprecated, use {@link #addSSHKey(String, ISSHPublicKey)} * diff --git a/src/main/java/com/openshift/internal/client/APIResource.java b/src/main/java/com/openshift/internal/client/APIResource.java index 7ce6f5c9..f17c9e5a 100755 --- a/src/main/java/com/openshift/internal/client/APIResource.java +++ b/src/main/java/com/openshift/internal/client/APIResource.java @@ -66,6 +66,7 @@ public class APIResource extends AbstractOpenShiftResource implements IOpenShift private List domains; private List standaloneCartridges; private List embeddableCartridges; + private List authorizations; private Map quickstartsByName; private final ExecutorService executorService; @@ -134,21 +135,26 @@ public IUser getUser() throws OpenShiftException { } public IAuthorization createAuthorization(String note, String scopes) throws OpenShiftException { - if (authorization != null) { - authorization.destroy(); - } - return this.authorization = createAuthorization(note, scopes, null); + return createAuthorization(note, scopes, null); } protected AuthorizationResource createAuthorization(String note, String scopes, Integer expiresIn) throws OpenShiftException { + if (authorizations == null){ + authorizations=loadAuthorizations(); + } Parameters parameters = new Parameters() .add(IOpenShiftJsonConstants.PROPERTY_NOTE, note) .add(IOpenShiftJsonConstants.PROPERTY_SCOPES, scopes) .add(IOpenShiftJsonConstants.PROPERTY_EXPIRES_IN, expiresIn == null ? null : Integer.toString(expiresIn)); - return new AuthorizationResource(this, + authorization = new AuthorizationResource(this, new AddAuthorizationRequest().execute(parameters.toArray())); + + //add to cached authorizations + authorizations.add(authorization); + + return authorization; } protected void removeAuthorization() { @@ -165,13 +171,39 @@ protected AuthorizationResource getOrCreateAuthorization(String token) { } public IAuthorization getAuthorization() throws OpenShiftException { - if (authorization == null) { + if (authorization == null || authorization.getId() == null) { // TODO: if the given token is expired we get an exception here this.authorization = getOrCreateAuthorization(token); } return this.authorization; } + public IAuthorization getAuthorization(String id) throws OpenShiftException { + + for (IAuthorization authorization: getAuthorizations()) { + if (authorization.getId().equals(id) || authorization.getToken().equals(id)) { + return authorization; + } + } + return null; + + } + + public List getAuthorizations() throws OpenShiftException { + if (authorizations == null) { + this.authorizations = loadAuthorizations(); + } + return CollectionUtils.toUnmodifiableCopy(this.authorizations); + } + + private List loadAuthorizations() throws OpenShiftException { + List authorizations = new ArrayList(); + for (AuthorizationResourceDTO authorizationDTO : new ListAuthorizationsRequest().execute()) { + authorizations.add(new AuthorizationResource(this, authorizationDTO)); + } + return authorizations; + } + @Override public List getDomains() throws OpenShiftException { if (domains == null) { @@ -323,6 +355,7 @@ private void addCartridge(CartridgeResourceDTO dto, List s @Override public void refresh() throws OpenShiftException { this.domains = null; + this.authorizations = null; } /** @@ -459,4 +492,15 @@ protected AuthorizationResourceDTO execute(String id) throws OpenShiftException Collections. emptyList()); } } + + private class ListAuthorizationsRequest extends ServiceRequest { + + private ListAuthorizationsRequest() throws OpenShiftException { + super("LIST_AUTHORIZATIONS"); + } + + protected List execute() throws OpenShiftException { + return super.execute(); + } + } } diff --git a/src/main/java/com/openshift/internal/client/UserResource.java b/src/main/java/com/openshift/internal/client/UserResource.java index 845536a6..8228ba94 100755 --- a/src/main/java/com/openshift/internal/client/UserResource.java +++ b/src/main/java/com/openshift/internal/client/UserResource.java @@ -10,6 +10,7 @@ ******************************************************************************/ package com.openshift.internal.client; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -49,7 +50,7 @@ public class UserResource extends AbstractOpenShiftResource implements IUser { public UserResource(final APIResource api, final UserResourceDTO dto, final String password) { super(api.getService(), dto.getLinks(), dto.getMessages()); this.api = api; - this.id = dto.getId(); + this.id = dto.getId(); this.rhLogin = dto.getRhLogin(); this.maxGears = dto.getMaxGears(); this.consumedGears = dto.getConsumedGears(); @@ -141,6 +142,32 @@ public IAuthorization getAuthorization() throws OpenShiftException { return api.getAuthorization(); } + @Override + public IAuthorization getAuthorization(String id) throws OpenShiftException { + Assert.notNull(id); + return api.getAuthorization(id); + } + + @Override + public Collection getAuthorizations() throws OpenShiftException { + return api.getAuthorizations(); + } + + @Override + public boolean removeAuthorization(String id) { + Assert.notNull(id); + IAuthorization auth = getAuthorization(id); + if (auth == null) { + return false; + } + auth.destroy(); + api.removeAuthorization(); + //sets authorization list to null so next get/create will reload + api.refresh(); + return true; + } + + @Override public void refresh() throws OpenShiftException { this.sshKeys = loadKeys(); diff --git a/src/main/java/com/openshift/internal/client/response/EnumDataType.java b/src/main/java/com/openshift/internal/client/response/EnumDataType.java index d40c9d9e..abe2807f 100755 --- a/src/main/java/com/openshift/internal/client/response/EnumDataType.java +++ b/src/main/java/com/openshift/internal/client/response/EnumDataType.java @@ -33,6 +33,7 @@ public enum EnumDataType { applications, application, authorization, + authorizations, /** The embedded cartridge type. */ embedded, gear_groups, diff --git a/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java b/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java index da2322b0..620af692 100755 --- a/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java +++ b/src/main/java/com/openshift/internal/client/response/OpenShiftJsonDTOFactory.java @@ -110,6 +110,8 @@ protected Object createData(EnumDataType dataType, Messages messages, ModelNode return createApplication(dataNode, messages); case authorization: return createAuthorization(dataNode, messages); + case authorizations: + return createAuthorizations(dataNode); case gear_groups: return createGearGroups(dataNode); case cartridges: @@ -165,6 +167,28 @@ private AuthorizationResourceDTO createAuthorization(ModelNode dataNode, Message final Map links = createLinks(dataNode.get(PROPERTY_LINKS)); return new AuthorizationResourceDTO(id, note, scopes, token, expiresIn, links, messages); } + /** + * Creates a list of authorization DTO objects. + * + * @param dataNode + * the root node + * @return the list< authorization dto> + * @throws OpenShiftException + * the open shift exception + */ + private List createAuthorizations(final ModelNode dataNode) throws OpenShiftException { + final List authorizationDtos = new ArrayList(); + for (ModelNode authorizationNode : dataNode.asList()) { + if (authorizationNode.getType() == ModelType.OBJECT) { + AuthorizationResourceDTO dto = createAuthorization(authorizationNode, null); + if (dto != null) { + authorizationDtos.add(dto); + } + } + } + + return authorizationDtos; + } /** * Creates a new ResourceDTO object. diff --git a/src/test/java/com/openshift/internal/client/AuthorizationIntegrationTest.java b/src/test/java/com/openshift/internal/client/AuthorizationIntegrationTest.java index 3ca4e71c..0af63bd5 100644 --- a/src/test/java/com/openshift/internal/client/AuthorizationIntegrationTest.java +++ b/src/test/java/com/openshift/internal/client/AuthorizationIntegrationTest.java @@ -13,22 +13,22 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertThat; -import org.junit.internal.matchers.StringContains; +import static org.junit.Assert.assertTrue; import java.io.IOException; +import java.util.List; import org.junit.Before; import org.junit.Test; -import java.util.List; +import org.junit.internal.matchers.StringContains; import com.openshift.client.IAuthorization; import com.openshift.client.IOpenShiftConnection; +import com.openshift.client.IOpenShiftSSHKey; import com.openshift.client.IUser; -import com.openshift.client.OpenShiftException; import com.openshift.client.OpenShiftEndpointException; -import com.openshift.client.IOpenShiftSSHKey; +import com.openshift.client.OpenShiftException; import com.openshift.client.utils.TestConnectionFactory; import com.openshift.internal.client.httpclient.HttpClientException; @@ -170,7 +170,6 @@ public void shouldCheckUserInfoPermissions() throws Exception { } //clean up authorization.destroy(); - } @Test