Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2010-2015, Inc. or its affiliates. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
  * or in the "license" file accompanying this file. This file is distributed
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
 package com.amazonaws.auth.policy.internal;
 import java.util.List;
 import java.util.Map;
Generate an AWS policy object by parsing the given JSON string.
 public class JsonPolicyReader {
     private static final String PRINCIPAL_SCHEMA_USER = "AWS";
     private static final String PRINCIPAL_SCHEMA_SERVICE = "Service";
     private static final String PRINICIPAL_SCHEMA_FEDERATED = "Federated";
Converts the specified JSON string to an AWS policy object. For more information see,

jsonString the specified JSON string representation of this AWS access control policy.
An AWS policy object.
java.lang.IllegalArgumentException If the specified JSON string is null or invalid and cannot be converted to an AWS policy object.
See also: /DeveloperGuide/java-dg-access-control.html
     public Policy createPolicyFromJsonString(String jsonString) {
         if (jsonString == null) {
             throw new IllegalArgumentException("JSON string cannot be null");
         JsonNode policyNode;
         JsonNode idNode;
         JsonNode statementNodes;
         Policy policy = new Policy();
         List<Statementstatements = new LinkedList<Statement>();
         try {
             policyNode = Jackson.jsonNodeOf(jsonString);
             idNode = policyNode.get(.);
             if (isNotNull(idNode)) {
             statementNodes = policyNode.get(.);
             if (isNotNull(statementNodes)) {
                 for (JsonNode node : statementNodes) {
         } catch (Exception e) {
             String message = "Unable to generate policy object fron JSON string "
                     + e.getMessage();
             throw new IllegalArgumentException(messagee);
         return policy;

Creates a Statement instance from the statement node. A statement consists of an Effect, id (optional), principal, action, resource, and conditions.

principal is the AWS account that is making a request to access or modify one of your AWS resources.

action is the way in which your AWS resource is being accessed or modified, such as sending a message to an Amazon SQS queue, or storing an object in an Amazon S3 bucket.

resource is the AWS entity that the principal wants to access, such as an Amazon SQS queue, or an object stored in Amazon S3.

conditions are the optional constraints that specify when to allow or deny access for the principal to access your resource. Many expressive conditions are available, some specific to each service. For example, you can use date conditions to allow access to your resources only after or before a specific time.

jStatement JsonNode representing the statement.
a reference to the statement instance created.
    private Statement statementOf(JsonNode jStatement) {
        JsonNode effectNode = jStatement.get(.);
        final Effect effect = isNotNull(effectNode)
                                   ? Effect.valueOf(effectNode.asText())
                                   : . ;
        Statement statement = new Statement(effect);
        JsonNode id = jStatement.get(.);
        if (isNotNull(id)) {
        JsonNode actionNodes = jStatement.get(.);
        if (isNotNull(actionNodes))
        JsonNode resourceNodes = jStatement.get(.);
        if (isNotNull(resourceNodes))
        JsonNode conditionNodes = jStatement.get(.);
        if (isNotNull(conditionNodes))
        JsonNode principalNodes = jStatement.get(.);
        if (isNotNull(principalNodes))
        return statement;

Generates a list of actions from the Action Json Node.

actionNodes the action Json node to be parsed.
the list of actions.
    private List<ActionactionsOf(JsonNode actionNodes) {
        List<Actionactions = new LinkedList<Action>();
        if (actionNodes.isArray()) {
            for (JsonNode action : actionNodes) {
                actions.add(new NamedAction(action.asText()));
        } else {
            actions.add(new NamedAction(actionNodes.asText()));
        return actions;

Generates a list of resources from the Resource Json Node.

resourceNodes the resource Json node to be parsed.
the list of resources.
    private List<ResourceresourcesOf(JsonNode resourceNodes) {
        List<Resourceresources = new LinkedList<Resource>();
        if (resourceNodes.isArray()) {
            for (JsonNode resource : resourceNodes) {
                resources.add(new Resource(resource.asText()));
        } else {
            resources.add(new Resource(resourceNodes.asText()));
        return resources;

Generates a list of principals from the Principal Json Node

principalNodes the principal Json to be parsed
a list of principals
    private List<PrincipalprincipalOf(JsonNode principalNodes) {
        List<Principalprincipals = new LinkedList<Principal>();
        if (principalNodes.asText().equals("*")) {
            return principals;
        Iterator<Map.Entry<StringJsonNode>> mapOfPrincipals = principalNodes
        String schema;
        JsonNode principalNode;
        while (mapOfPrincipals.hasNext()) {
            principal =;
            schema = principal.getKey();
            principalNode = principal.getValue();
            if (principalNode.isArray()) {
                elements = principalNode.elements();
                while (elements.hasNext()) {
            } else {
        return principals;

Creates a new principal instance for the given schema and the Json node.

schema the schema for the principal instance being created.
principalNode the node indicating the AWS account that is making the request.
a principal instance.
    private Principal createPrincipal(String schemaJsonNode principalNode) {
        if (schema.equalsIgnoreCase()) {
            return new Principal(principalNode.asText());
        } else if (schema.equalsIgnoreCase()) {
            return new Principal(schema,principalNode.asText());
        } else if (schema.equalsIgnoreCase()) {
            if (WebIdentityProviders.fromString(principalNode.asText()) != null) {
                return new Principal(
            } else {
                return new Principal(principalNode.asText());
        throw new AmazonClientException("Schema " + schema + " is not a valid value for the principal.");

Generates a list of condition from the Json node.

conditionNodes the condition Json node to be parsed.
the list of conditions.
    private List<ConditionconditionsOf(JsonNode conditionNodes) {
        List<ConditionconditionList = new LinkedList<Condition>();
        Iterator<Map.Entry<StringJsonNode>> mapOfConditions = conditionNodes
        while (mapOfConditions.hasNext()) {
            condition =;
        return conditionList;

Generates a condition instance for each condition type under the Condition Json node.

conditions the complete list of conditions
conditionType the condition type for the condition being created.
conditionNode each condition node to be parsed.
    private void convertConditionRecord(List<Conditionconditions,
            String conditionTypeJsonNode conditionNode) {
        Iterator<Map.Entry<StringJsonNode>> mapOfFields = conditionNode
        JsonNode fieldValue;
        while (mapOfFields.hasNext()) {
            values = new LinkedList<String>();
            field =;
            fieldValue = field.getValue();
            if (fieldValue.isArray()) {
                elements = fieldValue.elements();
                while (elements.hasNext()) {
            } else {
            conditions.add(new Condition().withType(conditionType)

An auxiliary class to help instantiate the action object.
    private static class NamedAction implements Action {
        private String actionName;
        public NamedAction(String actionName) {
            this. = actionName;
        public String getActionName() {
            return ;

Checks if the given object is not null.

object the object compared to null.
true if the object is not null else false
    private boolean isNotNull(Object object) {
        return null != object;
New to GrepCode? Check out our FAQ X