Skip to content

Commit

Permalink
Implement SkillSlideshow Servlet #1293
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham Gupta committed Jul 9, 2019
1 parent 5867618 commit 832e11f
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 66 deletions.
8 changes: 8 additions & 0 deletions src/ai/susi/DAO.java
Expand Up @@ -130,6 +130,7 @@ public class DAO {
public static JsonTray skillSupportedLanguages;
public static JsonTray ratingsOverTime;
public static JsonTray reportedSkills;
public static JsonTray skillSlideshow;

// temporary solution for draft storage
public static Map<String, Map<String, Draft>> drafts = new HashMap<>(); // key is the user's identity, inner map key is draft id
Expand Down Expand Up @@ -415,6 +416,13 @@ public void run() {
OS.protectPath(reportedSkills_per);
OS.protectPath(reportedSkills_vol);

//Skill slideshow
Path skillSlideshow_per = susi_skill_rating_dir.resolve("skillSlideshow.json");
Path skillSlideshow_vol = susi_skill_rating_dir.resolve("skillSlideshow_session.json");
skillSlideshow = new JsonTray(skillSlideshow_per.toFile(), skillSlideshow_vol.toFile(), 1000000);
OS.protectPath(skillSlideshow_per);
OS.protectPath(skillSlideshow_vol);

// open index
Path index_dir = dataPath.resolve("index");
if (index_dir.toFile().exists()) OS.protectPath(index_dir); // no other permissions to this path
Expand Down
4 changes: 4 additions & 0 deletions src/ai/susi/SusiServer.java
Expand Up @@ -579,6 +579,10 @@ private static void setServerHandler(File dataFile){
//Get device wise skill usage data
GetDeviceWiseSkillUsageService.class,

//Skill Slideshow
GetSkillSlideshow.class,
SkillSlideshowService.class,

//Feedback to skill
GetSkillFeedbackService.class,
RemoveFeedbackService.class,
Expand Down
66 changes: 32 additions & 34 deletions src/ai/susi/server/api/aaa/ApiKeysService.java
Expand Up @@ -34,15 +34,16 @@
import javax.ws.rs.core.MediaType;

/**
* This Servlet gives a API Endpoint to add, modify and delete different API keys used by SUSI.
* It requires user role to be ADMIN or above ADMIN
* example:
* This Servlet gives a API Endpoint to add, modify and delete different API
* keys used by SUSI. It requires user role to be ADMIN or above ADMIN example:
* http://localhost:4000/aaa/apiKeys.json?access_token=go2ijgk5ijkmViAac2bifng3uthdZ
* Necessary parameters : access_token, keyName
* Other parameters (one out of two is necessary):
* keyValue -> string http://localhost:4000/aaa/apiKeys.json?keyName=MAP_KEY&keyVale=jfsdf43jss534fsdjgn
* type -> string http://localhost:4000/aaa/apiKeys.json?keyName=MAP_KEY&keyVale=jfsdf43jss534fsdjgn&type=private
* deleteKey -> boolean http://localhost:4000/aaa/apiKeys.json?keyName=MAP_KEY&deleteKey=true
* Necessary parameters : access_token, keyName Other parameters (one out of two
* is necessary): keyValue -> string
* http://localhost:4000/aaa/apiKeys.json?keyName=MAP_KEY&keyVale=jfsdf43jss534fsdjgn
* type -> string
* http://localhost:4000/aaa/apiKeys.json?keyName=MAP_KEY&keyVale=jfsdf43jss534fsdjgn&type=private
* deleteKey -> boolean
* http://localhost:4000/aaa/apiKeys.json?keyName=MAP_KEY&deleteKey=true
*/

@Path("/aaa/apiKeys.json")
Expand All @@ -62,29 +63,26 @@ public JSONObject getDefaultPermissions(UserRole baseUserRole) {

@GET
@ApiOperation(httpMethod = "GET", value = "Endpoint to add, modify and delete diffrent API keys used by SUSI")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Removed API key successfully"),
@ApiResponses(value = { @ApiResponse(code = 200, message = "Removed API key successfully"),
@ApiResponse(code = 201, message = "Added new API key xxxxxxxx successfully"),
@ApiResponse(code = 400, message = "Bad Request. No parameter present"),
@ApiResponse(code = 401, message = "Base user role not sufficient. Your base user role is 'ANONYMOUS', minimum user role required is 'admin'"),
@ApiResponse(code = 500, message = "Failed: Unable to add xxxxxxxx!"),
@ApiResponse(code = 501, message = "Failed: xxxxxxxx doesn't exists")
})
@ApiResponse(code = 501, message = "Failed: xxxxxxxx doesn't exists") })
@ApiImplicitParams({
@ApiImplicitParam(name = "keyName", value = "Name of the API key", required = true, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "keyValue", value = "API key", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "type", value = "Type of API key", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "deleteKey", value = "Parameter to specify to delete API key", dataType = "boolean", paramType = "query")
})
@ApiImplicitParam(name = "deleteKey", value = "Parameter to specify to delete API key", dataType = "boolean", paramType = "query") })
@Override
public String getAPIPath() {
return "/aaa/apiKeys.json";
}

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response, Authorization authorization, final JsonObjectWithDefault permissions) throws APIException {
if (call.get("keyName", null) == null
&& call.get("keyValue", null) == null) {
public ServiceResponse serviceImpl(Query call, HttpServletResponse response, Authorization authorization,
final JsonObjectWithDefault permissions) throws APIException {
if (call.get("keyName", null) == null && call.get("keyValue", null) == null) {
throw new APIException(400, "Bad Request. No parameter present");
}

Expand All @@ -100,28 +98,28 @@ public ServiceResponse serviceImpl(Query call, HttpServletResponse response, Aut
keys = apiKeys.getJSONObject(type);
}

if(!deleteKey){
if (!deleteKey) {
try {
JSONObject api = new JSONObject();
api.put("value", keyValue);
keys.put(keyName, api);
apiKeys.put(type, keys, true);
result.put("accepted", true);
result.put("message", "Added new API key " + call.get("keyName") + " successfully !");
return new ServiceResponse(result);
JSONObject api = new JSONObject();
api.put("value", keyValue);
keys.put(keyName, api);
apiKeys.put(type, keys, true);
result.put("accepted", true);
result.put("message", "Added new API key " + call.get("keyName") + " successfully !");
return new ServiceResponse(result);
} catch (Exception e) {
throw new APIException(500, "Failed : Unable to add" + call.get("keyName") + " !" );
throw new APIException(500, "Failed : Unable to add" + call.get("keyName") + " !");
}
} else {
} else {
try {
keys.remove(keyName);
apiKeys.put(type, keys, true);
result.put("accepted", true);
result.put("message", "Removed API key " + call.get("keyName") + " successfully !");
return new ServiceResponse(result);
keys.remove(keyName);
apiKeys.put(type, keys, true);
result.put("accepted", true);
result.put("message", "Removed API key " + call.get("keyName") + " successfully !");
return new ServiceResponse(result);
} catch (Exception e) {
throw new APIException(501, "Failed : " + call.get("keyName") + " doesn't exists!" );
throw new APIException(501, "Failed : " + call.get("keyName") + " doesn't exists!");
}
}
}
}
}
6 changes: 6 additions & 0 deletions src/ai/susi/server/api/cms/GetImageServlet.java
Expand Up @@ -92,6 +92,12 @@ else if (!post.get("image", "").equals("")) {
}
}

else if(!post.get("sliderImage", "").equals("")) {
//For slider image
String image_path = post.get("sliderImage", "");
imageFile = new File(DAO.data_dir + File.separator + "slider_uploads" + File.separator + image_path);
}

if (imageFile == null || !imageFile.exists()) {
imageFile = new File(DAO.html_dir + File.separator + "images" + File.separator + "default.jpg");
}
Expand Down
78 changes: 78 additions & 0 deletions src/ai/susi/server/api/cms/GetSkillSlideshow.java
@@ -0,0 +1,78 @@
/**
* GetApiKeys
* Copyright by Shubham Gupta @fragm3
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/

package ai.susi.server.api.cms;

import ai.susi.DAO;
import ai.susi.json.JsonObjectWithDefault;
import ai.susi.json.JsonTray;
import ai.susi.server.*;
import org.json.JSONObject;
import javax.servlet.http.HttpServletResponse;

import ai.susi.server.APIException;
import ai.susi.server.APIHandler;
import ai.susi.server.AbstractAPIHandler;
import ai.susi.server.Query;
import ai.susi.server.ServiceResponse;
import ai.susi.server.UserRole;

/**
* This Servlet gives a API Endpoint to fetch Skill Slideshow used by SUSI
* homepage. It requires user role to be ANONYMOUS or above ANONYMOUS example:
* http://localhost:4000/cms/getSkillSlideshow.json
*/

public class GetSkillSlideshow extends AbstractAPIHandler implements APIHandler {

private static final long serialVersionUID = 5000658108778105134L;

@Override
public String getAPIPath() {
return "/cms/getSkillSlideshow.json";
}

@Override
public UserRole getMinimalUserRole() {
return UserRole.ANONYMOUS;
}

@Override
public JSONObject getDefaultPermissions(UserRole baseUserRole) {
return null;
}

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response, Authorization rights,
final JsonObjectWithDefault permissions) throws APIException {

JsonTray skillSlideshow = DAO.skillSlideshow;
JSONObject skillSlideshowObj = skillSlideshow.getJSONObject("slideshow");
JSONObject result = new JSONObject();

try {
result.put("accepted", true);
result.put("slideshow", skillSlideshowObj);
result.put("message", "Success : Fetched all Skills Slides!");
return new ServiceResponse(result);
} catch (Exception e) {
throw new APIException(500, "Failed : Unable to fetch Skills Slides!");
}
}
}
104 changes: 104 additions & 0 deletions src/ai/susi/server/api/cms/SkillSlideshowService.java
@@ -0,0 +1,104 @@
/**
* APIKeysService
* Copyright by Shubham Gupta @fragm3
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/

package ai.susi.server.api.cms;

import ai.susi.DAO;
import ai.susi.json.JsonObjectWithDefault;
import ai.susi.json.JsonTray;
import ai.susi.server.*;
import ai.susi.server.Authorization;
import org.json.JSONObject;

import javax.servlet.http.HttpServletResponse;

/**
* This Servlet gives a API Endpoint to add, modify and delete Skill Slider
* details used by SUSI. It requires user role to be ADMIN or above ADMIN
* example: For adding/uploading new image: Necessary parameters : access_token,
* redirect_link(for redirect on click), image_name, Optional parameter: info
* http://localhost:4000/cms/skillSlideshow.json?access_token=go2ijgk5ijkmViAac2bifng3uthdZ&redirect_link=https://susi.ai&info=Metadata&image_name=NAME_OF_IMAGE
* For deleting, Necessary parameters: redirect_link -> string, deleteSlide=true
* http://localhost:4000/aaa/apiKeys.json?redirect_link=https://susi.ai&deleteSlide=true
*/

public class SkillSlideshowService extends AbstractAPIHandler implements APIHandler {

private static final long serialVersionUID = 5000658108778105134L;

@Override
public UserRole getMinimalUserRole() {
return UserRole.ADMIN;
}

@Override
public JSONObject getDefaultPermissions(UserRole baseUserRole) {
return null;
}

@Override
public String getAPIPath() {
return "/cms/skillSlideshow.json";
}

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response, Authorization authorization,
final JsonObjectWithDefault permissions) throws APIException {
if (call.get("redirect_link", null) == null || call.get("image_name", null) == null) {
throw new APIException(400, "Bad Request. No enough parameter present");
}

String redirectLink = call.get("redirect_link", null);
String imageName = call.get("image_name", null);
String info = call.get("info", null);
boolean deleteSlide = call.get("deleteSlide", false);
JsonTray skillSlideshow = DAO.skillSlideshow;
JSONObject result = new JSONObject();
JSONObject skillSlideshowObj = new JSONObject();
if (skillSlideshow.has("slideshow")) {
skillSlideshowObj = skillSlideshow.getJSONObject("slideshow");
}
if (!deleteSlide) {
try {
JSONObject slideObj = new JSONObject();
slideObj.put("image_name", imageName);
slideObj.put("info", info);
skillSlideshowObj.put(redirectLink, slideObj);
skillSlideshow.put("slideshow", skillSlideshowObj, true);
result.put("accepted", true);
result.put("message", "Added new Slide " + call.get("redirect_link") + " successfully !");
return new ServiceResponse(result);
} catch (Exception e) {
throw new APIException(500,
"Failed : Unable to add slide with path " + call.get("redirect_link") + " !");
}
} else {
try {
skillSlideshowObj.remove(redirectLink);
skillSlideshow.put("slideshow", skillSlideshowObj, true);
result.put("accepted", true);
result.put("message", "Removed Slide with path " + call.get("redirect_link") + " successfully !");
return new ServiceResponse(result);
} catch (Exception e) {
throw new APIException(501,
"Failed to remove Slide: " + call.get("redirect_link") + " doesn't exists!");
}
}
}
}

0 comments on commit 832e11f

Please sign in to comment.