Package org.openmhealth.reference.exception

Examples of org.openmhealth.reference.exception.OmhException


    final AuthorizationToken token)
    throws OmhException {
   
    // Validate the parameter.
    if(token == null) {
      throw new OmhException("The token is null.");
    }
   
    // Get the DAO.
    SqlDao dao = SqlDao.getInstance();

    // Get the transaction manager.
    PlatformTransactionManager transactionManager =
      dao.getTransactionManager();
   
    // Create a new transaction definition and name it.
    DefaultTransactionDefinition transactionDefinition =
      new DefaultTransactionDefinition();
    transactionDefinition.setName("Adding an authorization token.");
   
    // Create the new transaction.
    TransactionStatus transactionStatus =
      transactionManager.getTransaction(transactionDefinition);
   
    // Get the JDBC template.
    JdbcTemplate jdbcTemplate = dao.getJdbcTemplate();
   
    // Add the authorization token.
    try {
      jdbcTemplate
        .update(
          "INSERT INTO " + AuthorizationTokenBin.DB_NAME +
            " (" +
              AuthorizationCodeBin.DB_NAME + "_id" + ", " +
              AuthorizationToken.JSON_KEY_ACCESS_TOKEN + ", " +
              AuthorizationToken.JSON_KEY_REFRESH_TOKEN + ", " +
              AuthorizationToken.JSON_KEY_CREATION_TIME + ", " +
              AuthorizationToken.JSON_KEY_EXPIRATION_TIME + " " +
            ") " +
            "VALUES" +
            " (" +
              "(" +
                "SELECT " + SqlDao.KEY_DATABASE_ID + " " +
                "FROM " + AuthorizationCodeBin.DB_NAME + " " +
                "WHERE " +
                  AuthorizationCode.JSON_KEY_CODE +
                  " = ?" +
              "), " +
              "?, " +
              "?, " +
              "?, " +
              "?" +
            ")",
          new Object[] {
            token.getAuthorizationCodeString(),
            token.getAccessToken(),
            token.getRefreshToken(),
            token.getCreationTime(),
            token.getExpirationTime()
            }
          );
     
      // Commit the transaction.
      transactionManager.commit(transactionStatus);
    }
    catch(DataAccessException e) {
      transactionManager.rollback(transactionStatus);
      throw
        new OmhException(
          "There was a problem storing the authorization token.",
          e);
    }
  }
View Full Code Here


    final String accessToken)
    throws OmhException {
   
    // Validate the parameter.
    if(accessToken == null) {
      throw new OmhException("The access token is null.");
    }
   
    try {
      return
        SqlDao
          .getInstance()
          .getJdbcTemplate()
          .queryForObject(
            "SELECT " +
                AuthorizationCode.JSON_KEY_CODE + ", " +
                AuthorizationToken.JSON_KEY_ACCESS_TOKEN +
                  ", " +
                AuthorizationToken.JSON_KEY_REFRESH_TOKEN +
                  ", " +
                AuthorizationTokenBin.DB_NAME + "." +
                  AuthorizationToken.JSON_KEY_CREATION_TIME +
                  ", " +
                AuthorizationTokenBin.DB_NAME + "." +
                  AuthorizationToken
                    .JSON_KEY_EXPIRATION_TIME +
                  " " +
              "FROM " +
                AuthorizationCodeBin.DB_NAME + ", " +
                AuthorizationTokenBin.DB_NAME + " " +
              "WHERE " +
                  AuthorizationCodeBin.DB_NAME +
                  "." +
                  SqlDao.KEY_DATABASE_ID +
                " = " +
                  AuthorizationTokenBin.DB_NAME +
                  "." +
                  AuthorizationCodeBin.DB_NAME + "_id " +
              "AND " +
                AuthorizationToken.JSON_KEY_ACCESS_TOKEN +
                  " = ?",
            new Object[] { accessToken },
            new RowMapper<AuthorizationToken>() {
              /**
               * Maps the row to an {@link AuthorizationToken}
               * object.
               */
              @Override
              public AuthorizationToken mapRow(
                final ResultSet resultSet,
                final int rowNum)
                throws SQLException {
               
                return
                  new AuthorizationToken(
                    resultSet
                      .getString(
                        AuthorizationCode
                          .JSON_KEY_CODE),
                    resultSet
                      .getString(
                        AuthorizationToken
                          .JSON_KEY_ACCESS_TOKEN),
                    resultSet
                      .getString(
                        AuthorizationToken
                          .JSON_KEY_REFRESH_TOKEN),
                    resultSet
                      .getLong(
                        AuthorizationToken
                          .JSON_KEY_CREATION_TIME),
                    resultSet
                      .getLong(
                        AuthorizationToken
                          .JSON_KEY_EXPIRATION_TIME));
              }
            });
    }
    // 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 tokens have the same access " +
            "token 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 token.",
          e);
    }
  }
View Full Code Here

    final String refreshToken)
    throws OmhException {
   
    // Validate the parameter.
    if(refreshToken == null) {
      throw new OmhException("The refresh token is null.");
    }
   
    try {
      return
        SqlDao
          .getInstance()
          .getJdbcTemplate()
          .queryForObject(
            "SELECT " +
                AuthorizationCode.JSON_KEY_CODE + ", " +
                AuthorizationToken.JSON_KEY_ACCESS_TOKEN +
                  ", " +
                AuthorizationToken.JSON_KEY_REFRESH_TOKEN +
                  ", " +
                AuthorizationTokenBin.DB_NAME + "." +
                  AuthorizationToken.JSON_KEY_CREATION_TIME +
                  ", " +
                AuthorizationTokenBin.DB_NAME + "." +
                  AuthorizationToken
                    .JSON_KEY_EXPIRATION_TIME +
                  " " +
              "FROM " +
                AuthorizationCodeBin.DB_NAME + ", " +
                AuthorizationTokenBin.DB_NAME + " " +
              "WHERE " +
                  AuthorizationCodeBin.DB_NAME +
                  "." +
                  SqlDao.KEY_DATABASE_ID +
                " = " +
                  AuthorizationTokenBin.DB_NAME +
                  "." +
                  AuthorizationCodeBin.DB_NAME + "_id " +
              "AND " +
                AuthorizationToken.JSON_KEY_REFRESH_TOKEN +
                  " = ?",
            new Object[] { refreshToken },
            new RowMapper<AuthorizationToken>() {
              /**
               * Maps the row to an {@link AuthorizationToken}
               * object.
               */
              @Override
              public AuthorizationToken mapRow(
                final ResultSet resultSet,
                final int rowNum)
                throws SQLException {
               
                return
                  new AuthorizationToken(
                    resultSet
                      .getString(
                        AuthorizationCode
                          .JSON_KEY_CODE),
                    resultSet
                      .getString(
                        AuthorizationToken
                          .JSON_KEY_ACCESS_TOKEN),
                    resultSet
                      .getString(
                        AuthorizationToken
                          .JSON_KEY_REFRESH_TOKEN),
                    resultSet
                      .getLong(
                        AuthorizationToken
                          .JSON_KEY_CREATION_TIME),
                    resultSet
                      .getLong(
                        AuthorizationToken
                          .JSON_KEY_EXPIRATION_TIME));
              }
            });
    }
    // 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 tokens have the same refresh " +
            "token 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 token.",
          e);
    }
  }
View Full Code Here

    final AuthorizationCodeResponse response)
    throws OmhException {

    // Validate the parameter.
    if(response == null) {
      throw new OmhException("The response is null.");
    }
   
    // Get the authentication token collection.
    JacksonDBCollection<AuthorizationCodeResponse, Object> collection =
      JacksonDBCollection
        .wrap(
          MongoDao
            .getInstance()
            .getDb()
            .getCollection(DB_NAME),
          AuthorizationCodeResponse.class);
   
    // Make sure the token doesn't already exist.
    if(collection
      .count(
        new BasicDBObject(
          AuthorizationCodeResponse.JSON_KEY_AUTHORIZATION_CODE,
          response.getAuthorizationCode())) > 0) {
     
      throw
        new OmhException(
          "A response already exists for the given authorizaion " +
            "code.");
    }
   
    // Save it.
View Full Code Here

   
    // If multiple responses of the same authorization code were returned,
    // that is a violation of the system.
    if(result.count() > 1) {
      throw
        new OmhException(
          "Multiple responses of the same authorization code " +
            "exist: " +
            code);
    }
   
View Full Code Here

    final AuthorizationToken token)
    throws OmhException {
   
    // Validate the parameter.
    if(token == null) {
      throw new OmhException("The token is null.");
    }
   
    // Get the authentication token collection.
    JacksonDBCollection<AuthorizationToken, Object> collection =
      JacksonDBCollection
        .wrap(
          MongoDao
            .getInstance()
            .getDb()
            .getCollection(DB_NAME),
          AuthorizationToken.class);
   
    // Make sure the access token doesn't already exist.
    if(collection
      .count(
        new BasicDBObject(
          AuthorizationToken.JSON_KEY_ACCESS_TOKEN,
          token.getAccessToken())) > 0) {
     
      throw new OmhException("The access token already exists.");
    }
   
    // Also, make sure the refresh token doesn't already exist.
    if(collection
      .count(
        new BasicDBObject(
          AuthorizationToken.JSON_KEY_REFRESH_TOKEN,
          token.getRefreshToken())) > 0) {
     
      throw new OmhException("The refresh token already exists.");
    }
   
    // Save it.
    collection.insert(token);
  }
View Full Code Here

   
    // If multiple authorization tokens were returned, that is a violation
    // of the system.
    if(result.count() > 1) {
      throw
        new OmhException(
          "Multiple copies of the same authorization access token " +
            "exist: " +
            accessToken);
    }
   
View Full Code Here

   
    // If multiple authorization tokens were returned, that is a violation
    // of the system.
    if(result.count() > 1) {
      throw
        new OmhException(
          "Multiple copies of the same authorization refresh " +
            "token exist: " +
            refreshToken);
    }
   
View Full Code Here

   */
  @Override
  public void storeCode(final AuthorizationCode code) throws OmhException {
    // Validate the parameter.
    if(code == null) {
      throw new OmhException("The code is null.");
    }
   
    // Get the DAO.
    SqlDao dao = SqlDao.getInstance();

    // Get the transaction manager.
    PlatformTransactionManager transactionManager =
      dao.getTransactionManager();
   
    // Create a new transaction definition and name it.
    DefaultTransactionDefinition transactionDefinition =
      new DefaultTransactionDefinition();
    transactionDefinition
      .setName("Adding an authorization code.");
   
    // Create the new transaction.
    TransactionStatus transactionStatus =
      transactionManager.getTransaction(transactionDefinition);
   
    // Get the JDBC template.
    JdbcTemplate jdbcTemplate = dao.getJdbcTemplate();
   
    // Create the binary representation of the data.
    byte[] scopes;
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos);
      oos.writeObject(code.getScopes());
      scopes = baos.toByteArray();
    }
    catch(IOException e) {
      throw
        new OmhException(
          "There was an error creating the output stream.",
          e);
    }
   
    // Add the authorization code.
    try {
      jdbcTemplate
        .update(
          "INSERT INTO " + AuthorizationCodeBin.DB_NAME +
            " (" +
              ThirdPartyBin.DB_NAME + "_id" + ", " +
              AuthorizationCode.JSON_KEY_CODE + ", " +
              AuthorizationCode.JSON_KEY_CREATION_TIME + ", " +
              AuthorizationCode.JSON_KEY_EXPIRATION_TIME + ", " +
              AuthorizationCode.JSON_KEY_SCOPES + ", " +
              AuthorizationCode.JSON_KEY_STATE + " " +
            ") " +
            "VALUES" +
            " (" +
              "(" +
                "SELECT " + SqlDao.KEY_DATABASE_ID + " " +
                "FROM " + ThirdPartyBin.DB_NAME + " " +
                "WHERE " + ThirdParty.JSON_KEY_ID + " = ?" +
              "), " +
              "?, " +
              "?, " +
              "?, " +
              "?, " +
              "?" +
            ")",
          new Object[] {
            code.getThirdPartyId(),
            code.getCode(),
            code.getCreationTime(),
            code.getExpirationTime(),
            scopes,
            code.getState()
            }
          );
     
      // Commit the transaction
      transactionManager.commit(transactionStatus);
    }
    catch(DataAccessException e) {
      transactionManager.rollback(transactionStatus);
      throw
        new OmhException(
          "There was a problem storing the authorization code.",
          e);
    }
  }
View Full Code Here

   */
  @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);
    }
  }
View Full Code Here

TOP

Related Classes of org.openmhealth.reference.exception.OmhException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.