2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.accountserver.db;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.Date;
29 import org.bson.Document;
30 import org.iotivity.cloud.accountserver.Constants;
31 import org.iotivity.cloud.util.Logger;
33 import com.mongodb.MongoClient;
34 import com.mongodb.client.MongoCollection;
35 import com.mongodb.client.MongoCursor;
36 import com.mongodb.client.MongoDatabase;
37 import com.mongodb.client.model.Filters;
41 * This class provides a set of APIs to use MongoDB APIs.
44 public class MongoDB {
46 private MongoClient mongoClient = null;
47 private MongoDatabase db = null;
50 * API creating MongoClient and initializing MongoDatabase
53 * database name to create MongoDatabase
56 public MongoDB(String dbname) throws Exception {
58 mongoClient = new MongoClient();
59 mongoClient.dropDatabase(dbname);
60 db = mongoClient.getDatabase(dbname);
64 * API creating collection
69 public void createTable(String tableName) {
71 db.createCollection(tableName);
75 * API deleting collection
80 public void deleteTable(String tableName) {
82 db.getCollection(tableName).drop();
86 * API getting database object
89 public MongoDatabase getMongoDatabase() {
94 public void createResource(UserToken userToken) {
95 Document doc = createDocument(userToken);
96 MongoCollection<Document> collection = db
97 .getCollection(Constants.TOKEN_TABLE);
99 if (collection.findOneAndReplace(
101 Filters.eq(Constants.KEY_USER_ID,
102 doc.get(Constants.KEY_USER_ID)),
103 Filters.eq(Constants.KEY_ACCESS_TOKEN,
104 doc.get(Constants.KEY_ACCESS_TOKEN)),
105 Filters.eq(Constants.KEY_REFRESH_TOKEN,
106 doc.get(Constants.KEY_REFRESH_TOKEN))),
108 collection.insertOne(doc);
115 * API for inserting device information of user
118 * device information of user
120 public void createResource(UserDevice userDevice) {
122 Document doc = createDocument(userDevice);
123 MongoCollection<Document> collection = db
124 .getCollection(Constants.DEVICE_TABLE);
126 if (collection.findOneAndReplace(
128 Filters.eq(Constants.KEY_USER_ID,
129 doc.get(Constants.KEY_USER_ID)),
130 Filters.eq(Constants.KEY_DEVICE_ID,
131 doc.get(Constants.KEY_DEVICE_ID))),
134 collection.insertOne(doc);
141 * API for getting user identifier corresponding with session code from
146 * @return String - user identifier
148 public String getUserIdByAccessToken(String token) {
150 String userId = null;
152 MongoCollection<Document> collection = db
153 .getCollection(Constants.TOKEN_TABLE);
155 MongoCursor<Document> cursor = collection
156 .find(Filters.eq(Constants.KEY_ACCESS_TOKEN, token)).iterator();
160 while (cursor.hasNext()) {
162 Document doc = cursor.next();
163 UserToken userToken = convertTokenDocToResource(doc);
165 userId = userToken.getUserId();
177 public String getUserIdByRefreshToken(String token) {
179 String userId = null;
181 MongoCollection<Document> collection = db
182 .getCollection(Constants.TOKEN_TABLE);
184 MongoCursor<Document> cursor = collection
185 .find(Filters.eq(Constants.KEY_REFRESH_TOKEN, token))
190 while (cursor.hasNext()) {
192 Document doc = cursor.next();
193 UserToken userToken = convertTokenDocToResource(doc);
195 userId = userToken.getUserId();
208 * API for getting devices corresponding with user identifier from database
213 public ArrayList<String> getDevices(String userId) {
215 ArrayList<String> deviceList = new ArrayList<>();
217 MongoCollection<Document> collection = db
218 .getCollection(Constants.DEVICE_TABLE);
220 MongoCursor<Document> cursor = collection
221 .find(Filters.eq(Constants.KEY_USER_ID, userId)).iterator();
225 while (cursor.hasNext()) {
227 Document doc = cursor.next();
228 UserDevice userDeivce = convertDeviceDocToResource(doc);
230 deviceList.add(userDeivce.getDeviceId());
241 public Boolean hasAccessToken(String token) {
243 Boolean hasAccessToken = false;
245 MongoCollection<Document> collection = db
246 .getCollection(Constants.TOKEN_TABLE);
248 MongoCursor<Document> cursor = collection
249 .find(Filters.eq(Constants.KEY_ACCESS_TOKEN, token)).iterator();
251 if (cursor.hasNext())
252 hasAccessToken = true;
256 return hasAccessToken;
259 public Boolean hasRefreshToken(String token) {
261 Boolean hasRefreshToken = false;
263 MongoCollection<Document> collection = db
264 .getCollection(Constants.TOKEN_TABLE);
266 MongoCursor<Document> cursor = collection
267 .find(Filters.eq(Constants.KEY_REFRESH_TOKEN, token))
270 if (cursor.hasNext())
271 hasRefreshToken = true;
275 return hasRefreshToken;
278 public Boolean updateResource(UserToken oldUserToken,
279 UserToken newUserToken) {
281 Boolean updateResource = false;
282 String userId = oldUserToken.getUserId();
283 String oldRefreshToken = oldUserToken.getRefreshToken();
285 Document doc = createDocument(newUserToken);
287 MongoCollection<Document> collection = db
288 .getCollection(Constants.TOKEN_TABLE);
291 if (collection.findOneAndReplace(
292 Filters.and(Filters.eq(Constants.KEY_USER_ID, userId), Filters
293 .eq(Constants.KEY_REFRESH_TOKEN, oldRefreshToken)),
296 // collection.insertOne(doc);
297 updateResource = true;
300 Logger.e("UpdateResource failed!");
303 return updateResource;
306 public String getIssuedTime(String accessToken) {
308 MongoCollection<Document> collection = db
309 .getCollection(Constants.TOKEN_TABLE);
311 MongoCursor<Document> cursor = collection
312 .find(Filters.eq(Constants.KEY_ACCESS_TOKEN, accessToken))
315 String issuedTime = null;
319 while (cursor.hasNext()) {
321 Document doc = cursor.next();
322 UserToken userToken = convertTokenDocToResource(doc);
324 issuedTime = userToken.getIssuedTime();
337 public void printResources() {
339 ArrayList<UserDevice> dlist = readDeviceResources();
340 int size = dlist.size();
342 Logger.i("[" + Constants.DEVICE_TABLE + "]Table");
343 for (int i = 0; i < size; i++) {
345 UserDevice item = dlist.get(i);
347 Logger.i("[" + i + "]" + item.getUserId() + ", "
348 + item.getDeviceId());
352 * ArrayList<UserSession> slist = readSessionResources(); size =
355 * Logger.i("*Table: " + Constants.SESSION_TABLE);
357 * for (int i = 0; i < size; i++) {
359 * UserSession item = slist.get(i);
361 * Logger.i("[" + i + "]" + item.getUserId() + ", " +
362 * item.getSessionCode());
367 ArrayList<UserToken> tlist = readUserTokenResources();
370 Logger.i("[" + Constants.TOKEN_TABLE + "]Table");
372 for (int i = 0; i < size; i++) {
374 UserToken item = tlist.get(i);
376 Logger.i("[" + i + "]" + item.getUserId() + "/"
377 + item.getAccessToken() + "/" + item.getRefreshToken() + "/"
378 + item.getIssuedTime());
383 private Document createDocument(UserToken userToken) {
385 String userId = userToken.getUserId();
386 String accessToken = userToken.getAccessToken();
387 String refreshToken = userToken.getRefreshToken();
389 DateFormat f = new SimpleDateFormat("yyyyMMddkkmm");
390 Date currentDate = new Date();
392 String issuedTime = f.format(currentDate);
394 Document doc = new Document(Constants.KEY_USER_ID, userId)
395 .append(Constants.KEY_ACCESS_TOKEN, accessToken)
396 .append(Constants.KEY_REFRESH_TOKEN, refreshToken)
397 .append(Constants.KEY_ISSUED_TIME, issuedTime);
402 private Document createDocument(UserDevice userDevice) {
404 Document doc = new Document(Constants.KEY_USER_ID,
405 userDevice.getUserId()).append(Constants.KEY_DEVICE_ID,
406 userDevice.getDeviceId());
411 private UserToken convertTokenDocToResource(Document doc) {
413 UserToken userToken = new UserToken();
415 String userId = doc.getString(Constants.KEY_USER_ID);
416 String accessToken = doc.getString(Constants.KEY_ACCESS_TOKEN);
417 String refreshToken = doc.getString(Constants.KEY_REFRESH_TOKEN);
418 String issuedTime = doc.getString(Constants.KEY_ISSUED_TIME);
420 // Logger.d("issuedTime: " + issuedTime);
421 userToken.setUserToken(userId, accessToken, refreshToken);
422 userToken.setIssuedTime(issuedTime);
427 private UserDevice convertDeviceDocToResource(Document doc) {
429 UserDevice userDevice = new UserDevice();
431 userDevice.setUserId(doc.getString(Constants.KEY_USER_ID));
432 userDevice.setDeviceId(doc.getString(Constants.KEY_DEVICE_ID));
437 private ArrayList<UserToken> readUserTokenResources() {
439 ArrayList<UserToken> userTokenList = new ArrayList<>();
441 MongoCollection<Document> collection = db
442 .getCollection(Constants.TOKEN_TABLE);
443 MongoCursor<Document> cursor = collection.find().iterator();
445 while (cursor.hasNext()) {
447 Document doc = cursor.next();
448 userTokenList.add(convertTokenDocToResource(doc));
453 return userTokenList;
456 private ArrayList<UserDevice> readDeviceResources() {
458 ArrayList<UserDevice> userDeviceList = new ArrayList<>();
460 MongoCollection<Document> collection = db
461 .getCollection(Constants.DEVICE_TABLE);
462 MongoCursor<Document> cursor = collection.find().iterator();
464 while (cursor.hasNext()) {
466 Document doc = cursor.next();
467 userDeviceList.add(convertDeviceDocToResource(doc));
472 return userDeviceList;