*/
@Override
public AuthorizationCode getCode(final String code) throws OmhException {
// Validate the parameter.
if(code == null) {
throw new OmhException("The code is null.");
}
try {
return
SqlDao
.getInstance()
.getJdbcTemplate()
.queryForObject(
"SELECT " +
ThirdParty.JSON_KEY_ID + ", " +
AuthorizationCode.JSON_KEY_CODE + ", " +
AuthorizationCode.JSON_KEY_CREATION_TIME +
", " +
AuthorizationCode.JSON_KEY_EXPIRATION_TIME +
", " +
AuthorizationCode.JSON_KEY_SCOPES + ", " +
AuthorizationCode.JSON_KEY_STATE + " " +
"FROM " +
ThirdPartyBin.DB_NAME + ", " +
AuthorizationCodeBin.DB_NAME + " " +
"WHERE " +
ThirdPartyBin.DB_NAME +
"." +
SqlDao.KEY_DATABASE_ID +
" = " +
AuthorizationCodeBin.DB_NAME +
"." +
ThirdPartyBin.DB_NAME + "_id " +
"AND " +
AuthorizationCode.JSON_KEY_CODE + " = ?",
new Object[] { code },
new RowMapper<AuthorizationCode>() {
/**
* Maps the row to an {@link AuthorizationCode}
* object.
*/
@SuppressWarnings("unchecked")
@Override
public AuthorizationCode mapRow(
final ResultSet resultSet,
final int rowNum)
throws SQLException {
// Decode the scopes byte array.
Set<String> scopes;
try {
byte[] scopesArray =
resultSet
.getBytes(
AuthorizationCode
.JSON_KEY_SCOPES);
ByteArrayInputStream bais =
new ByteArrayInputStream(scopesArray);
ObjectInputStream ois =
new ObjectInputStream(bais);
scopes = (Set<String>) ois.readObject();
}
catch(IOException e) {
throw
new SQLException(
"The scopes object could not be " +
"read or decoded.",
e);
}
catch(ClassNotFoundException e) {
throw
new SQLException(
"The Set class is unknown.",
e);
}
return
new AuthorizationCode(
resultSet
.getString(ThirdParty.JSON_KEY_ID),
resultSet
.getString(
AuthorizationCode
.JSON_KEY_CODE),
resultSet
.getLong(
AuthorizationCode
.JSON_KEY_CREATION_TIME),
resultSet
.getLong(
AuthorizationCode
.JSON_KEY_EXPIRATION_TIME),
scopes,
resultSet
.getString(
AuthorizationCode
.JSON_KEY_STATE));
}
});
}
// If the problem is that the number of results isn't what we expected,
// we may still be alright.
catch(IncorrectResultSizeDataAccessException e) {
// If there weren't any tokens with the given token value, then we
// simply return null.
if(e.getActualSize() == 0) {
return null;
}
// Otherwise, we throw an exception.
throw
new OmhException(
"Multiple authorization codes have the same value.",
e);
}
// For all other issues, we simply propagate the exception.
catch(DataAccessException e) {
throw
new OmhException(
"There was an error querying for an authorization codes.",
e);
}
}