@Test public void testToJson() { DateTime before = new DateTime(2015, 03, 01, 00, 46, 17, 0, DateTimeZone.UTC); Policy policy = Policy.mkSimplePolicy("http: assertEquals("{\"Statement\":{\"Condition\":{\"DateLessThan\":" + before.getMillis() + "},\"Resource\":\"http:\\/\\/mh-allinone\\/\"}}", PolicyUtils.toJson(policy).toJSONString()); policy = Policy.mkPolicyValidFromWithIP("http: DateTimeZone.UTC), EXAMPLE_IP); assertEquals( "{\"Statement\":{\"Condition\":{\"DateGreaterThan\":1425084379000,\"DateLessThan\":1425170777000,\"IpAddress\":\"10.0.0.1\"},\"Resource\":\"http:\\/\\/mh-allinone\\/\"}}", PolicyUtils.toJson(policy).toJSONString()); }
@SuppressWarnings("unchecked") public static JSONObject toJson(Policy policy) { JSONObject policyJSON = new JSONObject(); Map<String, Object> conditions = new TreeMap<String, Object>(); conditions.put(DATE_LESS_THAN_KEY, new Long(policy.getValidUntil().getMillis())); if (policy.getValidFrom().isPresent()) { conditions.put(DATE_GREATER_THAN_KEY, new Long(policy.getValidFrom().get().getMillis())); } if (policy.getClientIpAddress().isPresent()) { conditions.put(IP_ADDRESS_KEY, policy.getClientIpAddress().get().getHostAddress()); } JSONObject conditionsJSON = new JSONObject(); conditionsJSON.putAll(conditions); JSONObject statement = new JSONObject(); statement.put(RESOURCE_KEY, policy.getResource()); statement.put(CONDITION_KEY, conditions); policyJSON.put(STATEMENT_KEY, statement); return policyJSON; }
PolicyUtils { @SuppressWarnings("unchecked") public static JSONObject toJson(Policy policy) { JSONObject policyJSON = new JSONObject(); Map<String, Object> conditions = new TreeMap<String, Object>(); conditions.put(DATE_LESS_THAN_KEY, new Long(policy.getValidUntil().getMillis())); if (policy.getValidFrom().isPresent()) { conditions.put(DATE_GREATER_THAN_KEY, new Long(policy.getValidFrom().get().getMillis())); } if (policy.getClientIpAddress().isPresent()) { conditions.put(IP_ADDRESS_KEY, policy.getClientIpAddress().get().getHostAddress()); } JSONObject conditionsJSON = new JSONObject(); conditionsJSON.putAll(conditions); JSONObject statement = new JSONObject(); statement.put(RESOURCE_KEY, policy.getResource()); statement.put(CONDITION_KEY, conditions); policyJSON.put(STATEMENT_KEY, statement); return policyJSON; } }
PolicyUtils { @SuppressWarnings("unchecked") public static JSONObject toJson(Policy policy) { JSONObject policyJSON = new JSONObject(); Map<String, Object> conditions = new TreeMap<String, Object>(); conditions.put(DATE_LESS_THAN_KEY, new Long(policy.getValidUntil().getMillis())); if (policy.getValidFrom().isPresent()) { conditions.put(DATE_GREATER_THAN_KEY, new Long(policy.getValidFrom().get().getMillis())); } if (policy.getClientIpAddress().isPresent()) { conditions.put(IP_ADDRESS_KEY, policy.getClientIpAddress().get().getHostAddress()); } JSONObject conditionsJSON = new JSONObject(); conditionsJSON.putAll(conditions); JSONObject statement = new JSONObject(); statement.put(RESOURCE_KEY, policy.getResource()); statement.put(CONDITION_KEY, conditions); policyJSON.put(STATEMENT_KEY, statement); return policyJSON; } private  PolicyUtils(); }
PolicyUtils { @SuppressWarnings("unchecked") public static JSONObject toJson(Policy policy) { JSONObject policyJSON = new JSONObject(); Map<String, Object> conditions = new TreeMap<String, Object>(); conditions.put(DATE_LESS_THAN_KEY, new Long(policy.getValidUntil().getMillis())); if (policy.getValidFrom().isPresent()) { conditions.put(DATE_GREATER_THAN_KEY, new Long(policy.getValidFrom().get().getMillis())); } if (policy.getClientIpAddress().isPresent()) { conditions.put(IP_ADDRESS_KEY, policy.getClientIpAddress().get().getHostAddress()); } JSONObject conditionsJSON = new JSONObject(); conditionsJSON.putAll(conditions); JSONObject statement = new JSONObject(); statement.put(RESOURCE_KEY, policy.getResource()); statement.put(CONDITION_KEY, conditions); policyJSON.put(STATEMENT_KEY, statement); return policyJSON; } private  PolicyUtils(); static String base64Encode(String value); static String base64Decode(String value); static Policy fromJson(String policyJson); @SuppressWarnings("unchecked") static JSONObject toJson(Policy policy); static Policy fromBase64EncodedPolicy(String encodedPolicy); static String toBase64EncodedPolicy(Policy policy); static String getPolicySignature(Policy policy, String encryptionKey); }
PolicyUtils { @SuppressWarnings("unchecked") public static JSONObject toJson(Policy policy) { JSONObject policyJSON = new JSONObject(); Map<String, Object> conditions = new TreeMap<String, Object>(); conditions.put(DATE_LESS_THAN_KEY, new Long(policy.getValidUntil().getMillis())); if (policy.getValidFrom().isPresent()) { conditions.put(DATE_GREATER_THAN_KEY, new Long(policy.getValidFrom().get().getMillis())); } if (policy.getClientIpAddress().isPresent()) { conditions.put(IP_ADDRESS_KEY, policy.getClientIpAddress().get().getHostAddress()); } JSONObject conditionsJSON = new JSONObject(); conditionsJSON.putAll(conditions); JSONObject statement = new JSONObject(); statement.put(RESOURCE_KEY, policy.getResource()); statement.put(CONDITION_KEY, conditions); policyJSON.put(STATEMENT_KEY, statement); return policyJSON; } private  PolicyUtils(); static String base64Encode(String value); static String base64Decode(String value); static Policy fromJson(String policyJson); @SuppressWarnings("unchecked") static JSONObject toJson(Policy policy); static Policy fromBase64EncodedPolicy(String encodedPolicy); static String toBase64EncodedPolicy(Policy policy); static String getPolicySignature(Policy policy, String encryptionKey);  }