Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
feat(plan): Implementation of Plans
Browse files Browse the repository at this point in the history
  • Loading branch information
brasseld authored and NicolasGeraud committed Oct 19, 2016
1 parent 272120f commit f245c6c
Show file tree
Hide file tree
Showing 40 changed files with 962 additions and 106 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -35,7 +35,7 @@
<jedis.version>2.9.0</jedis.version>
<jackson.version>2.5.3</jackson.version>
<gravitee-repository.version>1.0.0-SNAPSHOT</gravitee-repository.version>
<spring-data-redis.version>1.7.2.RELEASE</spring-data-redis.version>
<spring-data-redis.version>1.7.4.RELEASE</spring-data-redis.version>

<gravitee-repository-test.version>1.0.0-SNAPSHOT</gravitee-repository-test.version>
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
Expand Down
Expand Up @@ -22,7 +22,7 @@
import org.springframework.data.redis.connection.jedis.JedisConnection;

/**
* @author David BRASSELY (brasseld at gmail.com)
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class RedisRepository implements Repository {
Expand Down
Expand Up @@ -19,7 +19,7 @@
import org.springframework.context.annotation.Bean;

/**
* @author David BRASSELY (brasseld at gmail.com)
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public abstract class AbstractRepositoryConfiguration {
Expand Down
Expand Up @@ -24,7 +24,7 @@
import redis.clients.jedis.JedisPoolConfig;

/**
* @author David BRASSELY (brasseld at gmail.com)
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class RedisConnectionFactory implements FactoryBean<org.springframework.data.redis.connection.RedisConnectionFactory> {
Expand Down
Expand Up @@ -27,7 +27,7 @@
import org.springframework.transaction.support.AbstractPlatformTransactionManager;

/**
* @author David BRASSELY (brasseld at gmail.com)
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@Configuration
Expand Down
Expand Up @@ -19,8 +19,6 @@
import io.gravitee.repository.management.api.ApiKeyRepository;
import io.gravitee.repository.management.model.ApiKey;
import io.gravitee.repository.redis.management.internal.ApiKeyRedisRepository;
import io.gravitee.repository.redis.management.internal.ApiRedisRepository;
import io.gravitee.repository.redis.management.internal.ApplicationRedisRepository;
import io.gravitee.repository.redis.management.model.RedisApiKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -31,7 +29,7 @@
import java.util.stream.Collectors;

/**
* @author David BRASSELY (brasseld at gmail.com)
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@Component
Expand All @@ -40,20 +38,14 @@ public class RedisApiKeyRepository implements ApiKeyRepository {
@Autowired
private ApiKeyRedisRepository apiKeyRedisRepository;

@Autowired
private ApplicationRedisRepository applicationRedisRepository;

@Autowired
private ApiRedisRepository apiRedisRepository;

@Override
public Optional<ApiKey> retrieve(String apiKey) throws TechnicalException {
public Optional<ApiKey> findById(String apiKey) throws TechnicalException {
return Optional.ofNullable(convert(apiKeyRedisRepository.find(apiKey)));
}

@Override
public ApiKey create(String applicationId, String apiId, ApiKey key) throws TechnicalException {
RedisApiKey redisApiKey = apiKeyRedisRepository.saveOrUpdate(convert(key));
public ApiKey create(ApiKey apiKey) throws TechnicalException {
RedisApiKey redisApiKey = apiKeyRedisRepository.saveOrUpdate(convert(apiKey));
return convert(redisApiKey);
}

Expand All @@ -64,36 +56,21 @@ public ApiKey update(ApiKey key) throws TechnicalException {
}

@Override
public Set<ApiKey> findByApplicationAndApi(String applicationId, String apiId) throws TechnicalException {
Set<ApiKey> apiKeysByApp = findByApplication(applicationId);
Set<ApiKey> apiKeysByApi = findByApi(apiId);

apiKeysByApp.retainAll(apiKeysByApi);

return apiKeysByApp;
}

@Override
public Set<ApiKey> findByApplication(String applicationId) throws TechnicalException {
return apiKeyRedisRepository.findByApplication(applicationId)
public Set<ApiKey> findBySubscription(String subscription) throws TechnicalException {
return apiKeyRedisRepository.findBySubscription(subscription)
.stream()
.map(this::convert)
.collect(Collectors.toSet());
}

@Override
public Set<ApiKey> findByApi(String apiId) throws TechnicalException {
return apiKeyRedisRepository.findByApi(apiId)
public Set<ApiKey> findByPlan(String plan) throws TechnicalException {
return apiKeyRedisRepository.findByPlan(plan)
.stream()
.map(this::convert)
.collect(Collectors.toSet());
}

@Override
public void delete(String apiKey) throws TechnicalException {
apiKeyRedisRepository.delete(apiKey);
}

private ApiKey convert(RedisApiKey redisApiKey) {
if (redisApiKey == null) {
return null;
Expand All @@ -102,14 +79,20 @@ private ApiKey convert(RedisApiKey redisApiKey) {
ApiKey apiKey = new ApiKey();

apiKey.setKey(redisApiKey.getKey());
apiKey.setApi(redisApiKey.getApi());
apiKey.setApplication(redisApiKey.getApplication());
apiKey.setCreatedAt(new Date(redisApiKey.getCreatedAt()));
if (redisApiKey.getExpiration() != 0) {
apiKey.setExpiration(new Date(redisApiKey.getExpiration()));
apiKey.setSubscription(redisApiKey.getSubscription());
apiKey.setPlan(redisApiKey.getPlan());
if (redisApiKey.getCreatedAt() != 0) {
apiKey.setCreatedAt(new Date(redisApiKey.getCreatedAt()));
}
if (redisApiKey.getUpdatedAt() != 0) {
apiKey.setUpdatedAt(new Date(redisApiKey.getUpdatedAt()));
}
if (redisApiKey.getExpireAt() != 0) {
apiKey.setExpireAt(new Date(redisApiKey.getExpireAt()));
}
if (redisApiKey.getRevokeAt() != 0) {
apiKey.setRevokeAt(new Date(redisApiKey.getRevokeAt()));
apiKey.setRevokedAt(new Date(redisApiKey.getRevokeAt()));
}
apiKey.setRevoked(redisApiKey.isRevoked());

Expand All @@ -120,16 +103,23 @@ private RedisApiKey convert(ApiKey apiKey) {
RedisApiKey redisApiKey = new RedisApiKey();

redisApiKey.setKey(apiKey.getKey());
redisApiKey.setApi(apiKey.getApi());
redisApiKey.setApplication(apiKey.getApplication());
redisApiKey.setCreatedAt(apiKey.getCreatedAt().getTime());
if (apiKey.getExpiration() != null) {
redisApiKey.setExpiration(apiKey.getExpiration().getTime());
redisApiKey.setSubscription(apiKey.getSubscription());
redisApiKey.setPlan(apiKey.getPlan());

if (apiKey.getCreatedAt() != null) {
redisApiKey.setCreatedAt(apiKey.getCreatedAt().getTime());
}
if (apiKey.getUpdatedAt() != null) {
redisApiKey.setUpdatedAt(apiKey.getUpdatedAt().getTime());
}
if (apiKey.getExpireAt() != null) {
redisApiKey.setExpireAt(apiKey.getExpireAt().getTime());
}
if (apiKey.getRevokeAt() != null) {
redisApiKey.setRevokeAt(apiKey.getRevokeAt().getTime());
if (apiKey.getRevokedAt() != null) {
redisApiKey.setRevokeAt(apiKey.getRevokedAt().getTime());
}
redisApiKey.setRevoked(apiKey.isRevoked());
redisApiKey.setRevoked(redisApiKey.isRevoked());

return redisApiKey;
}
Expand Down
Expand Up @@ -33,7 +33,7 @@
import java.util.stream.Collectors;

/**
* @author David BRASSELY (brasseld at gmail.com)
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@Component
Expand Down
Expand Up @@ -32,7 +32,7 @@
import java.util.stream.Collectors;

/**
* @author David BRASSELY (brasseld at gmail.com)
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@Component
Expand Down
@@ -0,0 +1,135 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.repository.redis.management;

import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.api.PlanRepository;
import io.gravitee.repository.management.model.Plan;
import io.gravitee.repository.redis.management.internal.PlanRedisRepository;
import io.gravitee.repository.redis.management.model.RedisPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@Component
public class RedisPlanRepository implements PlanRepository {

@Autowired
private PlanRedisRepository planRedisRepository;

@Override
public Set<Plan> findByApi(String api) throws TechnicalException {
return planRedisRepository.findByApi(api)
.stream()
.map(this::convert)
.collect(Collectors.toSet());
}

@Override
public Optional<Plan> findById(String plan) throws TechnicalException {
RedisPlan redisPlan = planRedisRepository.find(plan);
return Optional.ofNullable(convert(redisPlan));
}

@Override
public Plan create(Plan plan) throws TechnicalException {
RedisPlan redisPlan = planRedisRepository.saveOrUpdate(convert(plan));
return convert(redisPlan);
}

@Override
public Plan update(Plan plan) throws TechnicalException {
RedisPlan redisPlan = planRedisRepository.saveOrUpdate(convert(plan));
return convert(redisPlan);
}

@Override
public void delete(String plan) throws TechnicalException {
planRedisRepository.delete(plan);
}

private Plan convert(RedisPlan redisPlan) {
if (redisPlan == null) {
return null;
}

Plan plan = new Plan();
plan.setId(redisPlan.getId());
plan.setName(redisPlan.getName());
plan.setOrder(redisPlan.getOrder());

if (redisPlan.getType() != null) {
plan.setType(Plan.PlanType.valueOf(redisPlan.getType()));
}

plan.setCharacteristics(redisPlan.getCharacteristics());
plan.setApis(redisPlan.getApis());
plan.setDescription(redisPlan.getDescription());
plan.setDefinition(redisPlan.getDefinition());

if (redisPlan.getValidation() != null) {
plan.setValidation(Plan.PlanValidationType.valueOf(redisPlan.getValidation()));
}

if (redisPlan.getCreatedAt() != 0) {
plan.setCreatedAt(new Date(redisPlan.getCreatedAt()));
}

if (redisPlan.getUpdatedAt() != 0) {
plan.setUpdatedAt(new Date(redisPlan.getUpdatedAt()));
}

return plan;
}

private RedisPlan convert(Plan plan) {
RedisPlan redisPlan = new RedisPlan();
redisPlan.setId(plan.getId());
redisPlan.setName(plan.getName());
redisPlan.setOrder(redisPlan.getOrder());

if (plan.getType() != null) {
redisPlan.setType(plan.getType().name());
}

redisPlan.setCharacteristics(plan.getCharacteristics());
redisPlan.setApis(plan.getApis());
redisPlan.setDescription(plan.getDescription());
redisPlan.setDefinition(plan.getDefinition());

if (plan.getValidation() != null) {
redisPlan.setValidation(plan.getValidation().name());
}

if (plan.getCreatedAt() != null) {
redisPlan.setCreatedAt(plan.getCreatedAt().getTime());
}

if (plan.getUpdatedAt() != null) {
redisPlan.setUpdatedAt(plan.getUpdatedAt().getTime());
}

return redisPlan;
}
}

0 comments on commit f245c6c

Please sign in to comment.