com.chaosinmotion.securechat.server.commands.RemoveDevice.java Source code

Java tutorial

Introduction

Here is the source code for com.chaosinmotion.securechat.server.commands.RemoveDevice.java

Source

/*   SecureChat: A secure chat system which permits secure communications 
 *  between iOS devices and a back-end server.
 *
 *   Copyright  2016 by William Edward Woody
 *
 *   This program is free software: you can redistribute it and/or modify it 
 *   under the terms of the GNU General Public License as published by the 
 *   Free Software Foundation, either version 3 of the License, or (at your 
 *   option) any later version.
 *
 *   This program is distributed in the hope that it will be useful, but 
 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *   for more details.
 *
 *   You should have received a copy of the GNU General Public License along 
 *   with this program. If not, see <http://www.gnu.org/licenses/>
 */

package com.chaosinmotion.securechat.server.commands;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.json.JSONObject;
import com.chaosinmotion.securechat.server.database.Database;

/**
 * Remove device.
 * @author woody
 *
 */
public class RemoveDevice {
    public static boolean processRequest(Login.UserInfo userinfo, JSONObject requestParams)
            throws ClassNotFoundException, SQLException, IOException {
        String deviceid = requestParams.optString("deviceid");

        /*
         * Delete device. We only delete if it is also ours.
         */
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            c = Database.get();
            ps = c.prepareStatement("DELETE FROM Devices " + "WHERE userid = ? AND deviceuuid = ?");
            ps.setInt(1, userinfo.getUserID());
            ps.setString(2, deviceid);
            ps.execute();

            return true;
        } finally {
            if (rs != null)
                rs.close();
            if (ps != null)
                ps.close();
            if (c != null)
                c.close();
        }
    }
}