PolicyUtils { public static Policy fromJson(String policyJson) { JSONObject jsonPolicy = null; JSONParser jsonParser = new JSONParser(); try { jsonPolicy = (JSONObject) jsonParser.parse(policyJson); } catch (ParseException e) { e.printStackTrace(); } JSONObject statement = (JSONObject) jsonPolicy.get(STATEMENT_KEY); String resource = statement.get(RESOURCE_KEY).toString(); JSONObject condition = (JSONObject) statement.get(CONDITION_KEY); final String lessThanString = condition.get(DATE_LESS_THAN_KEY).toString(); final DateTime dateLessThan = new DateTime(Long.parseLong(lessThanString), DateTimeZone.UTC); final DateTime dateGreaterThan; Object greaterThanString = condition.get(DATE_GREATER_THAN_KEY); if (greaterThanString != null) { dateGreaterThan = new DateTime(Long.parseLong(greaterThanString.toString()), DateTimeZone.UTC); } else { dateGreaterThan = null; } return Policy.mkPolicyValidFromWithIP(resource, dateLessThan, dateGreaterThan, (String) condition.get(IP_ADDRESS_KEY)); } 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);  }
@Test public void testFromJson() throws UnsupportedEncodingException { String policyJson = "{\"Statement\": {\"Resource\":\"http: Policy policy = PolicyUtils.fromJson(policyJson); assertEquals("http: assertEquals(EXAMPLE_IP, policy.getClientIpAddress().get().getHostAddress()); DateTime after = new DateTime(2015, 02, 28, 00, 46, 19, 0, DateTimeZone.UTC); after = after.withSecondOfMinute(19); assertEquals(after, policy.getValidFrom().get()); DateTime before = new DateTime(2015, 03, 01, 00, 46, 17, 0, DateTimeZone.UTC); assertEquals(before, policy.getValidUntil()); }