Java Code Examples for com.almworks.sqlite4java.SQLiteStatement#columnBlob()

The following examples show how to use com.almworks.sqlite4java.SQLiteStatement#columnBlob() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DbUtils.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T extract(SQLiteStatement stmt, Class<T> type, int idx) throws Exception {
   if (type == String.class) {
      return (T) stmt.columnString(idx);
   } else if (type == int.class || type == Integer.class) {
      return (T) (Integer) stmt.columnInt(idx);
   } else if (type == long.class || type == Long.class) {
      return (T) (Long) stmt.columnLong(idx);
   } else if (type == double.class || type == Double.class) {
      return (T) (Double) stmt.columnDouble(idx);
   } else if (type == byte[].class) {
      return (T) stmt.columnBlob(idx);
   } else if (type == Object.class) {
      return (T) stmt.columnValue(idx);
   } else {
      throw new DbException("cannot get column type: " + type);
   }
}
 
Example 2
Source File: SQLite3StatementBindInvoker.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Nullable
public static Object bind(SQLiteStatement stmt, Class<?> type, int index) {
   try {
      if (type.equals(String.class)) {
         return stmt.columnString(index);
      } else if (type.equals(Integer.class) || type.equals(int.class) ) {
         return stmt.columnInt(index);
      } else if (type.equals(Long.class) || type.equals(long.class)) {
         return stmt.columnLong(index);
      } else if (type.equals(Byte.class) || type.equals(byte.class) ) {
         return (byte)stmt.columnInt(index);
      } else if ( type.equals(Boolean.class) || type.equals(boolean.class) ) {
         return stmt.columnInt(index) != 0;
      } else if (type.equals(Double.class) || type.equals(double.class)) {
         return stmt.columnDouble(index);
      } else if (type.equals(float.class) || type.equals(Float.class)) {
         return (float)stmt.columnDouble(index);
      } else if (type.equals(Serializable.class)) {
         return stmt.columnBlob(index);
      }
   } catch (SQLiteException e) {
      log.error("sql bind invoker could not invoke:" , e);
   }

   return null;
}
 
Example 3
Source File: TestDbService.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public TestPojo extract(SQLiteConnection conn, SQLiteStatement stmt) throws Exception {
   TestPojo result = new TestPojo();
   result.key = stmt.columnString(0);
   result.value1 = stmt.columnLong(1);
   result.value2 = stmt.columnDouble(2);

   byte[] value3 = stmt.columnBlob(3);
   if (value3 == null) {
      value3 = new byte[0];
   }

   result.value3 = value3;
   return result;
}
 
Example 4
Source File: AttestationServer.java    From AttestationServer with MIT License 5 votes vote down vote up
private static void changePassword(final long userId, final String currentPassword, final String newPassword)
        throws GeneralSecurityException, SQLiteException {
    validatePassword(currentPassword);
    validatePassword(newPassword);

    final SQLiteConnection conn = new SQLiteConnection(AttestationProtocol.ATTESTATION_DATABASE);
    try {
        open(conn, false);

        conn.exec("BEGIN TRANSACTION");

        final SQLiteStatement select = conn.prepare("SELECT passwordHash, passwordSalt " +
                "FROM Accounts WHERE userId = ?");
        select.bind(1, userId);
        select.step();
        final byte[] currentPasswordHash = select.columnBlob(0);
        final byte[] currentPasswordSalt = select.columnBlob(1);
        select.dispose();
        if (!MessageDigest.isEqual(hash(currentPassword.getBytes(), currentPasswordSalt), currentPasswordHash)) {
            throw new GeneralSecurityException("invalid password");
        }

        final byte[] newPasswordSalt = generateRandomToken();
        final byte[] newPasswordHash = hash(newPassword.getBytes(), newPasswordSalt);

        final SQLiteStatement update = conn.prepare("UPDATE Accounts " +
                "SET passwordHash = ?, passwordSalt = ? WHERE userId = ?");
        update.bind(1, newPasswordHash);
        update.bind(2, newPasswordSalt);
        update.bind(3, userId);
        update.step();
        update.dispose();

        conn.exec("COMMIT TRANSACTION");
    } finally {
        conn.dispose();
    }
}
 
Example 5
Source File: AttestationServer.java    From AttestationServer with MIT License 4 votes vote down vote up
private static Session login(final String username, final String password)
        throws GeneralSecurityException, SQLiteException {
    validatePassword(password);

    final SQLiteConnection conn = new SQLiteConnection(AttestationProtocol.ATTESTATION_DATABASE);
    try {
        open(conn, false);
        final SQLiteStatement select = conn.prepare("SELECT userId, passwordHash, " +
                "passwordSalt FROM Accounts WHERE username = ?");
        select.bind(1, username);
        if (!select.step()) {
            throw new UsernameUnavailableException();
        }
        final long userId = select.columnLong(0);
        final byte[] passwordHash = select.columnBlob(1);
        final byte[] passwordSalt = select.columnBlob(2);
        select.dispose();
        if (!MessageDigest.isEqual(hash(password.getBytes(), passwordSalt), passwordHash)) {
            throw new GeneralSecurityException("invalid password");
        }

        final long now = System.currentTimeMillis();
        final SQLiteStatement delete = conn.prepare("DELETE FROM Sessions WHERE expiryTime < ?");
        delete.bind(1, now);
        delete.step();
        delete.dispose();

        final byte[] cookieToken = generateRandomToken();
        final byte[] requestToken = generateRandomToken();

        final SQLiteStatement insert = conn.prepare("INSERT INTO Sessions " +
                "(userId, cookieToken, requestToken, expiryTime) VALUES (?, ?, ?, ?)");
        insert.bind(1, userId);
        insert.bind(2, cookieToken);
        insert.bind(3, requestToken);
        insert.bind(4, now + SESSION_LENGTH);
        insert.step();
        insert.dispose();

        return new Session(conn.getLastInsertId(), cookieToken, requestToken);
    } finally {
        conn.dispose();
    }
}
 
Example 6
Source File: AttestationServer.java    From AttestationServer with MIT License 4 votes vote down vote up
private static Account verifySession(final HttpExchange exchange, final boolean end, byte[] requestTokenEncoded)
        throws IOException, SQLiteException {
    final String cookie = getCookie(exchange, "__Host-session");
    if (cookie == null) {
        exchange.sendResponseHeaders(403, -1);
        return null;
    }
    final String[] session = cookie.split("\\|", 2);
    if (session.length != 2) {
        clearCookie(exchange);
        exchange.sendResponseHeaders(403, -1);
        return null;
    }
    final long sessionId = Long.parseLong(session[0]);
    final byte[] cookieToken = Base64.getDecoder().decode(session[1]);

    if (requestTokenEncoded == null) {
        requestTokenEncoded = new byte[session[1].length()];
        final DataInputStream input = new DataInputStream(exchange.getRequestBody());
        try {
            input.readFully(requestTokenEncoded);
        } catch (final EOFException e) {
            clearCookie(exchange);
            exchange.sendResponseHeaders(403, -1);
            return null;
        }
    }
    final byte[] requestToken = Base64.getDecoder().decode(requestTokenEncoded);

    final SQLiteConnection conn = new SQLiteConnection(AttestationProtocol.ATTESTATION_DATABASE);
    try {
        open(conn, !end);

        final SQLiteStatement select = conn.prepare("SELECT cookieToken, requestToken, " +
                "expiryTime, username, subscribeKey, Accounts.userId, verifyInterval, alertDelay " +
                "FROM Sessions " +
                "INNER JOIN Accounts on Accounts.userId = Sessions.userId " +
                "WHERE sessionId = ?");
        select.bind(1, sessionId);
        if (!select.step() || !MessageDigest.isEqual(cookieToken, select.columnBlob(0)) ||
                !MessageDigest.isEqual(requestToken, select.columnBlob(1))) {
            clearCookie(exchange);
            exchange.sendResponseHeaders(403, -1);
            return null;
        }

        if (select.columnLong(2) < System.currentTimeMillis()) {
            clearCookie(exchange);
            exchange.sendResponseHeaders(403, -1);
            return null;
        }

        if (end) {
            final SQLiteStatement delete = conn.prepare("DELETE FROM Sessions " +
                    "WHERE sessionId = ?");
            delete.bind(1, sessionId);
            delete.step();
            delete.dispose();
        }

        return new Account(select.columnLong(5), select.columnString(3), select.columnBlob(4),
                select.columnInt(6), select.columnInt(7));
    } finally {
        conn.dispose();
    }
}