/**
*
* Bmob移动后端云服务RestAPI工具类
*
* 提供简单的RestAPI增删改查工具,可直接对表、云函数、支付订单、消息推送进行操作。
* 使用方法:先初始化initBmob,后调用其他方法即可。
* 具体使用方法及传参格式详见Bmob官网RestAPI开发文档。
* http://docs.bmob.cn/restful/developdoc/index.html?menukey=develop_doc&key=develop_restful
*
* @author 金鹰
* @version V1.3.1
* @since 2015-07-07
*
*/
package com.framework.common.sms.bmob.restapi;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.framework.common.sms.bmob.bson.BSONObject;
public class Bmob {
private static boolean IS_INIT = false;
private static int TIME_OUT = 10000;
private static String STRING_EMPTY = "";
private static String APP_ID = STRING_EMPTY;
private static String REST_API_KEY = STRING_EMPTY;
private static String MASTER_KEY = STRING_EMPTY;
private static final String BMOB_APP_ID_TAG = "X-Bmob-Application-Id";
private static final String BMOB_REST_KEY_TAG = "X-Bmob-REST-API-Key";
private static final String BMOB_MASTER_KEY_TAG = "X-Bmob-Master-Key";
private static final String CONTENT_TYPE_TAG = "Content-Type";
private static final String CONTENT_TYPE_JSON = "application/json";
private static final String METHOD_GET = "GET";
private static final String METHOD_POST = "POST";
private static final String METHOD_PUT = "PUT";
private static final String METHOD_DELETE = "DELETE";
private static final String UTF8 = "UTF-8";
private static final String CHAR_RISK = ":";
public static final String MSG_NOT_FOUND = "Not Found";
public static final String MSG_FILE_NOT_FOUND = "file Not Found";
public static final String MSG_ERROR = "Error";
public static final String MSG_UNREGISTERED = "Unregistered";
/**
* 是否初始化Bmob
*
* @return 初始化结果
*/
public static boolean isInit() {
return IS_INIT;
}
/**
* 初始化Bmob
*
* @param appId 填写 Application ID
* @param apiKey 填写 REST API Key
* @return 注册结果
*/
public static boolean initBmob(String appId, String apiKey) {
return initBmob(appId, apiKey, 10000);
}
/**
* 初始化Bmob
*
* @param appId 填写 Application ID
* @param apiKey 填写 REST API Key
* @param timeout 设置超时(1000~20000ms)
* @return 注册结果
*/
public static boolean initBmob(String appId, String apiKey, int timeout) {
APP_ID = appId;
REST_API_KEY = apiKey;
if (!APP_ID.equals(STRING_EMPTY) && !REST_API_KEY.equals(STRING_EMPTY)) {
IS_INIT = true;
}
if (timeout > 1000 && timeout < 20000) {
TIME_OUT = timeout;
}
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
IS_INIT = false;
}
return isInit();
}
/**
* 初始化Bmob Master权限
*
* @param masterKey 填写 Master Key
*/
public static void initMaster(String masterKey) {
MASTER_KEY = masterKey;
}
/**
* 查询表全部记录(最多仅查询1000条记录)
*
* @param tableName 表名
* @return JSON格式结果
*/
public static String findAll(String tableName) {
return find(tableName, STRING_EMPTY);
}
/**
* 条件查询表全部记录(最多仅查询1000条记录)
*
* @param tableName 表名
* @param where 条件JOSN格式
* @return JSON格式结果
*/
public static String findAll(String tableName, String where) {
return find(tableName, where, STRING_EMPTY);
}
/**
* 查询表单条记录
*
* @param tableName 表名
* @param objectId objectId
* @return JSON格式结果
*/
public static String findOne(String tableName, String objectId) {
String result = STRING_EMPTY;
if (isInit()) {
HttpURLConnection conn = null;
String mURL = "https://api.bmob.cn/1/classes/" + tableName + "/" + objectId;
try {
conn = connectionCommonSetting(conn, new URL(mURL), METHOD_GET);
conn.connect();
result = getResultFromConnection(conn);
conn.disconnect();
} catch (FileNotFoundException e) {
result = MSG_NOT_FOUND + CHAR_RISK + "(findOne)" + e.getMessage();
} catch (Exception e) {
result = MSG_ERROR + CHAR_RISK + "(findOne)" + e.getMessage();
}
} else {
result = MSG_UNREGISTERED;
}
return result;
}
/**
* 查询表限定数量记录
*
* @param tableName 表名
* @param limit 查询记录数(1~1000)
* @return JSON格式结果
*/
public static String find(String tableName, int limit) {
return find(tableName, "{}", 0, limit, STRING_EMPTY);
}
/**
* 条件查询表限定数量记录
*
* @param tableName 表名
* @param where 条件JOSN格式
* @param limit 查询记录数(1~1000)
* @return JSON格式结果
*/
public static String find(String tableName, String where, int limit) {
return find(tableName, where, 0, limit, STRING_EMPTY);
}
/**
* 条件查询表限定数量记录,返回指定列
*
* @param tableName 表名
* @param keys 返回列 (例:score,name)
* @param where 条件JOSN格式
* @param limit 查询记录数(1~1000)
* @return JSON格式结果
*/
public static String findColumns(String tableName, String keys, String where, int limit) {
return findColumns(tableName, keys, where, 0, limit, STRING_EMPTY);
}
/**
* 查询表区间记录
*
* @param tableName 表名
* @param skip 跳过记录数
* @param limit 查询记录数(1~1000)
* @return JSON格式结果
*/
public static String find(String tableName, int skip, int limit) {
return find(tableName, "{}", skip, limit, STRING_EMPTY);
}
/**
* 条件查询表区间记录
*
* @param tableName 表名
* @param where 条件JOSN格式
* @param skip 跳过记录数
* @param limit 查询记录数(1~1000)
* @return JSON格式结果
*/
public static String find(String tableName, String where, int skip, int limit) {
return find(tableName, where, skip, limit, STRING_EMPTY);
}
/**
* 条件查询表区间记录,返回指定列
*
* @param tableName 表名
* @param keys 返回列 (例:score,name)
* @param where 条件JOSN格式
* @param skip 跳过记录数
* @param limit 查询记录数(1~1000)
* @return JSON格式结果
*/
public static String findColumns(String tableName, String keys, String where, int skip, int limit) {
return findColumns(tableName, keys, where, skip, limit, STRING_EMPTY);
}
/**
* 排序查询表记录
*
* @para