@Test public void shouldSerialiseACollectionOfTimestamps() { OAuthAdapter adapter = generateOAuthAdapter(); String timestamp = "1370425721197"; OAuthTokenField field = OAuthTokenField.EXPIRY_TIME; Map<String, Object> values = new HashMap<String, Object>(); values.put(field.getOAuthField(), Arrays.asList(new String[]{timestamp})); JsonValue jsonValue = makeDefaultJsonValue(values); Calendar result = adapter.toToken(jsonValue).getExpiryTimestamp(); Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Europe/London")); c.setTimeInMillis(result.getTimeInMillis()); assertEquals(c.get(Calendar.MONTH), 5); assertEquals(c.get(Calendar.YEAR), 2013); assertEquals(c.get(Calendar.HOUR_OF_DAY), 10); assertEquals(c.get(Calendar.MINUTE), 48); }
public Token toToken(JsonValue request) { assertObjectIsAMap(request); Set<String> idSet = (Set<String>) request.get(TokenIdFactory.ID).getObject(); String id = null; if (idSet != null && !idSet.isEmpty()){ id = tokenIdFactory.getOAuthTokenId(idSet.iterator().next()); } else { id = tokenIdFactory.getOAuthTokenId(null); } request.get(TokenIdFactory.ID).setObject(id); Token token = new Token(id, TokenType.OAUTH); Map<String,Object> values = request.asMap(); if (values != null) { for (OAuthTokenField field : OAuthTokenField.values()) { String key = field.getOAuthField(); if (values.containsKey(key)) { Object value = values.get(key); if (OAuthTokenField.ID.getOAuthField().equals(key)) { continue; } if (OAuthTokenField.EXPIRY_TIME.getOAuthField().equals(key)) { if (!Collection.class.isAssignableFrom(value.getClass())) { throw new IllegalStateException("Date must be in a collection"); } value = oAuthValues.getDateValue((Collection<String>) value); } else if (value instanceof Collection) { value = oAuthValues.getSingleValue((Collection<String>) value); } token.setAttribute(field.getField(), value); } } } Object objectToStore = request.getObject(); String serialisedObject = serialisation.serialise(objectToStore); blobUtils.setBlobFromString(token, serialisedObject); return token; }
OAuthAdapter implements TokenAdapter<JsonValue> { public Token toToken(JsonValue request) { assertObjectIsAMap(request); Set<String> idSet = (Set<String>) request.get(TokenIdFactory.ID).getObject(); String id = null; if (idSet != null && !idSet.isEmpty()){ id = tokenIdFactory.getOAuthTokenId(idSet.iterator().next()); } else { id = tokenIdFactory.getOAuthTokenId(null); } request.get(TokenIdFactory.ID).setObject(id); Token token = new Token(id, TokenType.OAUTH); Map<String,Object> values = request.asMap(); if (values != null) { for (OAuthTokenField field : OAuthTokenField.values()) { String key = field.getOAuthField(); if (values.containsKey(key)) { Object value = values.get(key); if (OAuthTokenField.ID.getOAuthField().equals(key)) { continue; } if (OAuthTokenField.EXPIRY_TIME.getOAuthField().equals(key)) { if (!Collection.class.isAssignableFrom(value.getClass())) { throw new IllegalStateException("Date must be in a collection"); } value = oAuthValues.getDateValue((Collection<String>) value); } else if (value instanceof Collection) { value = oAuthValues.getSingleValue((Collection<String>) value); } token.setAttribute(field.getField(), value); } } } Object objectToStore = request.getObject(); String serialisedObject = serialisation.serialise(objectToStore); blobUtils.setBlobFromString(token, serialisedObject); return token; } }
OAuthAdapter implements TokenAdapter<JsonValue> { public Token toToken(JsonValue request) { assertObjectIsAMap(request); Set<String> idSet = (Set<String>) request.get(TokenIdFactory.ID).getObject(); String id = null; if (idSet != null && !idSet.isEmpty()){ id = tokenIdFactory.getOAuthTokenId(idSet.iterator().next()); } else { id = tokenIdFactory.getOAuthTokenId(null); } request.get(TokenIdFactory.ID).setObject(id); Token token = new Token(id, TokenType.OAUTH); Map<String,Object> values = request.asMap(); if (values != null) { for (OAuthTokenField field : OAuthTokenField.values()) { String key = field.getOAuthField(); if (values.containsKey(key)) { Object value = values.get(key); if (OAuthTokenField.ID.getOAuthField().equals(key)) { continue; } if (OAuthTokenField.EXPIRY_TIME.getOAuthField().equals(key)) { if (!Collection.class.isAssignableFrom(value.getClass())) { throw new IllegalStateException("Date must be in a collection"); } value = oAuthValues.getDateValue((Collection<String>) value); } else if (value instanceof Collection) { value = oAuthValues.getSingleValue((Collection<String>) value); } token.setAttribute(field.getField(), value); } } } Object objectToStore = request.getObject(); String serialisedObject = serialisation.serialise(objectToStore); blobUtils.setBlobFromString(token, serialisedObject); return token; } @Inject  OAuthAdapter(TokenIdFactory tokenIdFactory, JSONSerialisation serialisation,
                        OAuthValues oAuthValues, TokenBlobUtils blobUtils); }
OAuthAdapter implements TokenAdapter<JsonValue> { public Token toToken(JsonValue request) { assertObjectIsAMap(request); Set<String> idSet = (Set<String>) request.get(TokenIdFactory.ID).getObject(); String id = null; if (idSet != null && !idSet.isEmpty()){ id = tokenIdFactory.getOAuthTokenId(idSet.iterator().next()); } else { id = tokenIdFactory.getOAuthTokenId(null); } request.get(TokenIdFactory.ID).setObject(id); Token token = new Token(id, TokenType.OAUTH); Map<String,Object> values = request.asMap(); if (values != null) { for (OAuthTokenField field : OAuthTokenField.values()) { String key = field.getOAuthField(); if (values.containsKey(key)) { Object value = values.get(key); if (OAuthTokenField.ID.getOAuthField().equals(key)) { continue; } if (OAuthTokenField.EXPIRY_TIME.getOAuthField().equals(key)) { if (!Collection.class.isAssignableFrom(value.getClass())) { throw new IllegalStateException("Date must be in a collection"); } value = oAuthValues.getDateValue((Collection<String>) value); } else if (value instanceof Collection) { value = oAuthValues.getSingleValue((Collection<String>) value); } token.setAttribute(field.getField(), value); } } } Object objectToStore = request.getObject(); String serialisedObject = serialisation.serialise(objectToStore); blobUtils.setBlobFromString(token, serialisedObject); return token; } @Inject  OAuthAdapter(TokenIdFactory tokenIdFactory, JSONSerialisation serialisation,
                        OAuthValues oAuthValues, TokenBlobUtils blobUtils); Token toToken(JsonValue request); JsonValue fromToken(Token token); }
OAuthAdapter implements TokenAdapter<JsonValue> { public Token toToken(JsonValue request) { assertObjectIsAMap(request); Set<String> idSet = (Set<String>) request.get(TokenIdFactory.ID).getObject(); String id = null; if (idSet != null && !idSet.isEmpty()){ id = tokenIdFactory.getOAuthTokenId(idSet.iterator().next()); } else { id = tokenIdFactory.getOAuthTokenId(null); } request.get(TokenIdFactory.ID).setObject(id); Token token = new Token(id, TokenType.OAUTH); Map<String,Object> values = request.asMap(); if (values != null) { for (OAuthTokenField field : OAuthTokenField.values()) { String key = field.getOAuthField(); if (values.containsKey(key)) { Object value = values.get(key); if (OAuthTokenField.ID.getOAuthField().equals(key)) { continue; } if (OAuthTokenField.EXPIRY_TIME.getOAuthField().equals(key)) { if (!Collection.class.isAssignableFrom(value.getClass())) { throw new IllegalStateException("Date must be in a collection"); } value = oAuthValues.getDateValue((Collection<String>) value); } else if (value instanceof Collection) { value = oAuthValues.getSingleValue((Collection<String>) value); } token.setAttribute(field.getField(), value); } } } Object objectToStore = request.getObject(); String serialisedObject = serialisation.serialise(objectToStore); blobUtils.setBlobFromString(token, serialisedObject); return token; } @Inject  OAuthAdapter(TokenIdFactory tokenIdFactory, JSONSerialisation serialisation,
                        OAuthValues oAuthValues, TokenBlobUtils blobUtils); Token toToken(JsonValue request); JsonValue fromToken(Token token); static final String VALUE; }