Package com.alu.e3.data.model

Examples of com.alu.e3.data.model.Policy


    for (QuotaRLBucket bucket : policy.getAuthIds()) {
      checkIfAuthAlreadyInPolicy(policy.getId(), bucket.getAuthIds(), bucket.getId());
    }

    Policy cachedPolicy = null;

    if(update) {
      cachedPolicy = getPolicyById(policy.getId(), false);

      // Remove existing contexts
      for (Integer contextId : cachedPolicy.getContextIds()) {
        cachingTableContext.remove(contextId);
      }
    }

    policy.getContextIds().clear();

    // TODO: Check value of quota, if changed, then reset the created date, otherwise, keep old value
    //=> Speaker needs to store the created date

    // Store all contexts aside, indexed by an int
    for (Context ctx : policy.getContexts()) {
      boolean ok = false;

      while (!ok) {
        int idx = (int)(Math.random() * Integer.MAX_VALUE);
        Integer contextId = Integer.valueOf(idx);
        if (!cachingTableContext.containsKey(contextId)) {
          ctx.setContextId(contextId);
          ctx.setCreatedDate(new Date());
          cachingTableContext.set(contextId, new ContextWrapper(ctx));
          policy.getContextIds().add(contextId);
          ok = true;
        }
      }
    }

    // Store all contexts aside, indexed by an int
    for (QuotaRLBucket bucket : policy.getAuthIds()) {
      boolean ok = false;

      if ((cachedPolicy != null) && ((bucket.getId() == null) || (bucket.getId().equals("")))) {
        // First, try to find the current bucket in the old policy
        for (QuotaRLBucket cachedBucket : cachedPolicy.getAuthIds()) {
          if (cachedBucket.getId().equals(bucket.getId())) {
            // Bucket was already in old policy
            bucket.setBucketId(cachedBucket.getBucketId());
            cachedPolicy.getAuthIds().remove(cachedBucket);
            ok = true;
            break;
          }
        }
      }

      while (!ok) {
        int idx = (int)(Math.random() * Integer.MAX_VALUE);
        Integer bucketId = Integer.valueOf(idx);
        if (!usedBucketIds.contains(bucketId)) {
          bucket.setBucketId(bucketId);
          usedBucketIds.add(bucketId);

          if ((bucket.getId() == null) || (bucket.getId().equals(""))) {
            bucket.setId(bucketId.toString());
          }

          ok = true;
        }
      }
    }

    if (cachedPolicy != null) {
      // Now remove all buckets which are not in the new policy
      for (QuotaRLBucket cachedBucket : cachedPolicy.getAuthIds()) {
        usedBucketIds.remove(cachedBucket.getBucketId());
        for (IDataManagerUsedBucketIdsListener listner : this.usedBucketIdslisteners)
        {
          listner.usedBucketIdsRemoved(cachedBucket.getBucketId());
        }
View Full Code Here


  }

  @Override
  public void removePolicy(String id) throws InvalidIDException {

    Policy policy = cachingTablePolicy.remove(id);
    if (policy == null)
      throw new InvalidIDException("A Policy with that ID [" + id + "] doesn't exist");

    // Now, iterate through all APIs and Auth in this policy and update them
    for (String apiId : policy.getApiIds()) {
      Api api = getApiById(apiId);
      if (api != null) {
        api.getPolicyIds().remove(policy.getId());
        cachingTableApi.set(api.getId(), api);
      }
    }

    for (QuotaRLBucket bucket : policy.getAuthIds()) {
      for(String authId : bucket.getAuthIds()) {
        String authToken = cachingTableAuthIdToAuthToken.get(authId);
        if (authToken == null)
          continue;

        Auth auth = cachingTableAuth.get(authToken);
        if (auth == null)
          continue;

        for (AuthIds authCtx : auth.getPolicyContexts()) {
          if (authCtx.getPolicyId().equals(policy.getId())) {
            auth.getPolicyContexts().remove(authCtx);
            break;
          }
        }
        cachingTableAuth.set(authToken, auth);
      }
      usedBucketIds.remove(bucket.getBucketId());
      for (IDataManagerUsedBucketIdsListener listner : this.usedBucketIdslisteners)
      {
        listner.usedBucketIdsRemoved(bucket.getBucketId());
      }
    }

    // Remove the contexts
    for (Integer contextId : policy.getContextIds()) {
      cachingTableContext.remove(contextId);
    }
  }
View Full Code Here

  }

  @Override
  public Policy getPolicyById(String id, boolean getFullDetails) throws InvalidIDException {

    Policy policy = cachingTablePolicy.get(id);
    if (policy == null)
      throw new InvalidIDException("A Policy with that ID [" + id + "] doesn't exist");

    if (getFullDetails) {
      // It's not because List<Context> is transient on Policy
      // that it is not stored in hazelcast memory _locally_.
      // For sure, it will not be replicated
      policy.getContexts().clear();
      // We can't be sure here to reuse existing List<Context>
      // it may be not up to date.
      for (Integer contextId : policy.getContextIds()) {
        policy.getContexts().add(cachingTableContext.get(contextId).getPolicyContext());
      }
    }

    return policy;
  }
View Full Code Here


  @Override
  public void createBucket(String policyId, QuotaRLBucket bucket) throws IllegalArgumentException {

    Policy policy = getPolicyById(policyId);

    if(getBucketWithIdForPolicy(policy, bucket.getId()) != null) {
      throw new IllegalArgumentException("A Bucket with this ID already exists for this policy");
    }

    policy.getAuthIds().add(bucket);
    cachingTablePolicy.set(policy.getId(), policy);

    checkIfAuthAlreadyInPolicy(policyId, bucket.getAuthIds(), bucket.getId());

    for(String authId : bucket.getAuthIds()) {
      String authToken = cachingTableAuthIdToAuthToken.get(authId);
      if (authToken == null)
        throw new IllegalArgumentException("Auth ID [" + authId + "] not found");

      Auth auth = cachingTableAuth.get(authToken);
      if (auth == null)
        throw new IllegalArgumentException("Auth token not found");

      // Add auth to policy
      if (bucket.getAuthIds().indexOf(auth.getId()) < 0) {
        bucket.getAuthIds().add(auth.getId());
        cachingTablePolicy.set(policy.getId(), policy);
      }

      // Add policy to auth
      boolean found = false;
      for (AuthIds authCtx : auth.getPolicyContexts()) {
        if (authCtx.getPolicyId().equals(policy.getId())) {
          //update Auth with good Policy context and bucket
          authCtx.setPolicyContextId(getPolicyContextId(auth, policy));
          authCtx.setPolicyBucketId(bucket.getBucketId());
          cachingTableAuth.set(authToken, auth);//updating auth
          found = true;
View Full Code Here

  }

  @Override
  public void addAuthsToBucket(String policyId, String bucketId, QuotaRLBucket bucket) throws IllegalArgumentException {

    Policy policy = getPolicyById(policyId);

    // Find the correct bucket in this policy
    QuotaRLBucket pAuthIds = getBucketWithIdForPolicy(policy, bucketId);
    if(pAuthIds == null) {
      throw new IllegalArgumentException("A Bucket with that ID [" + bucketId + "] doesn't exist for this Policy [" + policyId + "]");
    }

    checkIfAuthAlreadyInPolicy(policyId, bucket.getAuthIds(), bucketId);

    for(String authId : bucket.getAuthIds()) {

      String authToken = cachingTableAuthIdToAuthToken.get(authId);
      if (authToken == null)
        throw new IllegalArgumentException("Auth ID [" + authId + "] not found");

      Auth auth = cachingTableAuth.get(authToken);
      if (auth == null)
        throw new IllegalArgumentException("Auth token not found");

      // Add auth to the bucket
      if (pAuthIds.getAuthIds().indexOf(auth.getId()) < 0)
      {
        pAuthIds.getAuthIds().add(auth.getId());
        cachingTablePolicy.set(policy.getId(), policy);
      }

      // Add policy to auth
      boolean found = false;
      for (AuthIds authCtx : auth.getPolicyContexts()) {
        if (authCtx.getPolicyId().equals(policy.getId())) {
          //update Auth with good Policy context and bucket
          authCtx.setPolicyContextId(getPolicyContextId(auth, policy));
          authCtx.setPolicyBucketId(bucket.getBucketId());
          cachingTableAuth.set(authToken, auth);//updating auth
          found = true;
View Full Code Here

  }

  @Override
  public void removeAuthFromBucket(String policyId, String bucketId, String authId) throws IllegalArgumentException {

    Policy policy = getPolicyById(policyId);

    String authToken = cachingTableAuthIdToAuthToken.get(authId);
    if (authToken == null)
      throw new InvalidIDException("An Authorization with that ID [" + authId + "] doesn't exist");

    Auth auth = cachingTableAuth.get(authToken);
    if (auth == null)
      throw new InvalidIDException("An Authorization with that token doesn't exist");

    QuotaRLBucket pAuthIds = getBucketWithIdForPolicy(policy, bucketId);
    if(pAuthIds == null) {
      throw new InvalidIDException("A Bucket with that ID [" + bucketId + "] doesn't exist for this Policy [" + policyId + "]");
    }

    // Remove auth from the bucket
    pAuthIds.getAuthIds().remove(auth.getId());
    cachingTablePolicy.set(policy.getId(), policy);

    // Remove policy from auth
    for (AuthIds authCtx : auth.getPolicyContexts()) {
      if (authCtx.getPolicyId().equals(policy.getId())) {
        auth.getPolicyContexts().remove(authCtx);
        cachingTableAuth.set(authToken, auth);
        break;
      }
    }
View Full Code Here

  }

  @Override
  public void removeBucket(String policyId, String bucketId) throws IllegalArgumentException {

    Policy policy = getPolicyById(policyId);

    QuotaRLBucket pAuthIds = getBucketWithIdForPolicy(policy, bucketId);
    if(pAuthIds == null) {
      throw new InvalidIDException("A Bucket with that ID [" + bucketId + "] doesn't exist for this Policy [" + policyId + "]");
    }

    // Iterating over Auth Ids in the bucket to remove policy from the auth
    for(String authId : pAuthIds.getAuthIds()) {

      String authToken = cachingTableAuthIdToAuthToken.get(authId);
      if (authToken == null)
        continue;

      Auth auth = cachingTableAuth.get(authToken);
      if (auth == null)
        continue;

      // Remove policy from auth
      for (AuthIds authCtx : auth.getPolicyContexts()) {
        if (authCtx.getPolicyId().equals(policy.getId())) {
          auth.getPolicyContexts().remove(authCtx);
          cachingTableAuth.set(authToken, auth);
          break;
        }
      }
    }

    policy.getAuthIds().remove(pAuthIds);
    cachingTablePolicy.set(policy.getId(), policy);
  }
View Full Code Here

       
        // No Auth, but an API: return all Policies with no Auth associated
        // => API doesn't have any authentification method
        for (String policyIdInApi : api.getPolicyIds()) {

          Policy policy = getPolicyById(policyIdInApi);

          // Add only if no auth in policy's buckets
          boolean add = true;
          for(QuotaRLBucket authIds : policy.getAuthIds()) {
            if (!authIds.getAuthIds().isEmpty()) {
              add = false;
            }
          }
          if(add) {
            if (logger.isDebugEnabled()) {
              logger.debug("Adding policy {} as it does not have any auth in it's contexts", policy.getId());
            }
            if (result == null)
              result = new ArrayList<CallDescriptor>();

            int policyIdx = -1;
            if (policy != null && policy.getContextIds().size() > 0) {
              policyIdx = policy.getContextIds().get(0);
            }
           
            result.add(new CallDescriptor(policy, policyIdx , -1));

          }
        }

        // Now, add the CallDescriptor of the "default" context
        if (api.getContextIds().isEmpty() == false) {
          if (result == null)
            result = new ArrayList<CallDescriptor>();

          // Looking for default context, placed at index 0 by addApi
          ApiIds ctx = api.getContextIds().get(0);
          if (ctx.isStatusActive()) {
            if (logger.isDebugEnabled()) {
              logger.debug("Adding CallDescriptor({}, {}, {})", new String[] {
                  null,
                  ""+ctx.getApiContextId(),
                  ""+ctx.getApiBucketId()
                  });
            }
            result.add(new CallDescriptor(null, ctx.getApiContextId(), ctx.getApiBucketId()));
          }
        }
      }
    } else {

      // We have an Auth and an API, check matching policies
      for (AuthIds authCtx : auth.getPolicyContexts()) {

        String policyIdInAuth = authCtx.getPolicyId();

        boolean policyAdded = false;

        // API may be null if Auth is for "Company"
        if (api != null) {
          for (String policyIdInApi : api.getPolicyIds()) {

            // Matching means that the policy is in Auth and API
            if (policyIdInApi.equals(policyIdInAuth)) {
              Policy policy = getPolicyById(policyIdInAuth);
              if (result == null)
                result = new ArrayList<CallDescriptor>();
              if (authCtx.isStatusActive()) {
                if (logger.isDebugEnabled()) {
                  logger.debug("Adding CallDescriptor({}, {}, {})", new String[] {
                      policy.getId(),
                      ""+authCtx.getPolicyContextId(),
                      ""+authCtx.getPolicyBucketId()
                      });
                }
                result.add(new CallDescriptor(policy, authCtx.getPolicyContextId(), authCtx.getPolicyBucketId()));
              }
              policyAdded = true;
            }
          }
        }

        // If the policy wasn't added...
        if (!policyAdded) {
          // Slow, but need to check if that policy has some API attached
          Policy policy = getPolicyById(policyIdInAuth, false);
          if ((policy.getApiIds() == null) || policy.getApiIds().isEmpty()) {
            if (result == null)
              result = new ArrayList<CallDescriptor>();
            if (authCtx.isStatusActive()) {
              result.add(new CallDescriptor(policy, authCtx.getPolicyContextId(), authCtx.getPolicyBucketId()));
            }
View Full Code Here

  @Test
  public void testAddGetRemovePolicy() {

    // Add a new Policy
    Policy policy = new Policy();
    policy.setId("id3");
    dataManager.addPolicy(policy);

    // Now, get the API and check that it's OK
    Policy policy2 = dataManager.getPolicyById("id3");
    assertNotNull("Policy was added/returned", policy2);

    dataManager.removePolicy("id3");

    boolean isInvalidID = false;
View Full Code Here

  @Test
  public void testAddTwicePolicy() {

    // Add a new API
    Policy policy = new Policy();
    policy.setId("id3");
    dataManager.addPolicy(policy);

    boolean exceptionRaised = false;
    try
    {
      dataManager.addPolicy(policy);
    }
    catch(Exception e)
    {
      exceptionRaised = true;
    }

    assertTrue("Exception raised", exceptionRaised);

    // cleanup
    dataManager.removePolicy(policy.getId());
  }
View Full Code Here

TOP

Related Classes of com.alu.e3.data.model.Policy

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.