PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/openid-connect-server/src/main/java/org/mitre/openid/connect/web/WhitelistAPI.java

https://gitlab.com/jslee1/OpenID-Connect-Java-Spring-Server
Java | 211 lines | 124 code | 38 blank | 49 comment | 9 complexity | b734d109c90fdc53e8161bd4c7f0dde0 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright 2016 The MITRE Corporation
  3. * and the MIT Internet Trust Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *******************************************************************************/
  17. /**
  18. *
  19. */
  20. package org.mitre.openid.connect.web;
  21. import java.security.Principal;
  22. import java.util.Collection;
  23. import org.mitre.openid.connect.model.WhitelistedSite;
  24. import org.mitre.openid.connect.service.WhitelistedSiteService;
  25. import org.mitre.openid.connect.view.HttpCodeView;
  26. import org.mitre.openid.connect.view.JsonEntityView;
  27. import org.mitre.openid.connect.view.JsonErrorView;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.http.HttpStatus;
  32. import org.springframework.http.MediaType;
  33. import org.springframework.security.access.prepost.PreAuthorize;
  34. import org.springframework.stereotype.Controller;
  35. import org.springframework.ui.ModelMap;
  36. import org.springframework.web.bind.annotation.PathVariable;
  37. import org.springframework.web.bind.annotation.RequestBody;
  38. import org.springframework.web.bind.annotation.RequestMapping;
  39. import org.springframework.web.bind.annotation.RequestMethod;
  40. import com.google.gson.Gson;
  41. import com.google.gson.JsonObject;
  42. import com.google.gson.JsonParseException;
  43. import com.google.gson.JsonParser;
  44. /**
  45. * @author jricher
  46. *
  47. */
  48. @Controller
  49. @RequestMapping("/" + WhitelistAPI.URL)
  50. @PreAuthorize("hasRole('ROLE_USER')")
  51. public class WhitelistAPI {
  52. public static final String URL = RootController.API_URL + "/whitelist";
  53. @Autowired
  54. private WhitelistedSiteService whitelistService;
  55. /**
  56. * Logger for this class
  57. */
  58. private static final Logger logger = LoggerFactory.getLogger(WhitelistAPI.class);
  59. private Gson gson = new Gson();
  60. private JsonParser parser = new JsonParser();
  61. /**
  62. * Get a list of all whitelisted sites
  63. * @param m
  64. * @return
  65. */
  66. @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  67. public String getAllWhitelistedSites(ModelMap m) {
  68. Collection<WhitelistedSite> all = whitelistService.getAll();
  69. m.put(JsonEntityView.ENTITY, all);
  70. return JsonEntityView.VIEWNAME;
  71. }
  72. /**
  73. * Create a new whitelisted site
  74. * @param jsonString
  75. * @param m
  76. * @param p
  77. * @return
  78. */
  79. @PreAuthorize("hasRole('ROLE_ADMIN')")
  80. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  81. public String addNewWhitelistedSite(@RequestBody String jsonString, ModelMap m, Principal p) {
  82. JsonObject json;
  83. WhitelistedSite whitelist = null;
  84. try {
  85. json = parser.parse(jsonString).getAsJsonObject();
  86. whitelist = gson.fromJson(json, WhitelistedSite.class);
  87. } catch (JsonParseException e) {
  88. logger.error("addNewWhitelistedSite failed due to JsonParseException", e);
  89. m.addAttribute(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
  90. m.addAttribute(JsonErrorView.ERROR_MESSAGE, "Could not save new whitelisted site. The server encountered a JSON syntax exception. Contact a system administrator for assistance.");
  91. return JsonErrorView.VIEWNAME;
  92. } catch (IllegalStateException e) {
  93. logger.error("addNewWhitelistedSite failed due to IllegalStateException", e);
  94. m.addAttribute(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
  95. m.addAttribute(JsonErrorView.ERROR_MESSAGE, "Could not save new whitelisted site. The server encountered an IllegalStateException. Refresh and try again - if the problem persists, contact a system administrator for assistance.");
  96. return JsonErrorView.VIEWNAME;
  97. }
  98. // save the id of the person who created this
  99. whitelist.setCreatorUserId(p.getName());
  100. WhitelistedSite newWhitelist = whitelistService.saveNew(whitelist);
  101. m.put(JsonEntityView.ENTITY, newWhitelist);
  102. return JsonEntityView.VIEWNAME;
  103. }
  104. /**
  105. * Update an existing whitelisted site
  106. */
  107. @PreAuthorize("hasRole('ROLE_ADMIN')")
  108. @RequestMapping(value="/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  109. public String updateWhitelistedSite(@PathVariable("id") Long id, @RequestBody String jsonString, ModelMap m, Principal p) {
  110. JsonObject json;
  111. WhitelistedSite whitelist = null;
  112. try {
  113. json = parser.parse(jsonString).getAsJsonObject();
  114. whitelist = gson.fromJson(json, WhitelistedSite.class);
  115. } catch (JsonParseException e) {
  116. logger.error("updateWhitelistedSite failed due to JsonParseException", e);
  117. m.put(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
  118. m.put(JsonErrorView.ERROR_MESSAGE, "Could not update whitelisted site. The server encountered a JSON syntax exception. Contact a system administrator for assistance.");
  119. return JsonErrorView.VIEWNAME;
  120. } catch (IllegalStateException e) {
  121. logger.error("updateWhitelistedSite failed due to IllegalStateException", e);
  122. m.put(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
  123. m.put(JsonErrorView.ERROR_MESSAGE, "Could not update whitelisted site. The server encountered an IllegalStateException. Refresh and try again - if the problem persists, contact a system administrator for assistance.");
  124. return JsonErrorView.VIEWNAME;
  125. }
  126. WhitelistedSite oldWhitelist = whitelistService.getById(id);
  127. if (oldWhitelist == null) {
  128. logger.error("updateWhitelistedSite failed; whitelist with id " + id + " could not be found.");
  129. m.put(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
  130. m.put(JsonErrorView.ERROR_MESSAGE, "Could not update whitelisted site. The requested whitelisted site with id " + id + "could not be found.");
  131. return JsonErrorView.VIEWNAME;
  132. } else {
  133. WhitelistedSite newWhitelist = whitelistService.update(oldWhitelist, whitelist);
  134. m.put(JsonEntityView.ENTITY, newWhitelist);
  135. return JsonEntityView.VIEWNAME;
  136. }
  137. }
  138. /**
  139. * Delete a whitelisted site
  140. *
  141. */
  142. @PreAuthorize("hasRole('ROLE_ADMIN')")
  143. @RequestMapping(value="/{id}", method = RequestMethod.DELETE)
  144. public String deleteWhitelistedSite(@PathVariable("id") Long id, ModelMap m) {
  145. WhitelistedSite whitelist = whitelistService.getById(id);
  146. if (whitelist == null) {
  147. logger.error("deleteWhitelistedSite failed; whitelist with id " + id + " could not be found.");
  148. m.put(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
  149. m.put(JsonErrorView.ERROR_MESSAGE, "Could not delete whitelisted site. The requested whitelisted site with id " + id + "could not be found.");
  150. return JsonErrorView.VIEWNAME;
  151. } else {
  152. m.put(HttpCodeView.CODE, HttpStatus.OK);
  153. whitelistService.remove(whitelist);
  154. }
  155. return HttpCodeView.VIEWNAME;
  156. }
  157. /**
  158. * Get a single whitelisted site
  159. */
  160. @RequestMapping(value="/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  161. public String getWhitelistedSite(@PathVariable("id") Long id, ModelMap m) {
  162. WhitelistedSite whitelist = whitelistService.getById(id);
  163. if (whitelist == null) {
  164. logger.error("getWhitelistedSite failed; whitelist with id " + id + " could not be found.");
  165. m.put(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
  166. m.put(JsonErrorView.ERROR_MESSAGE, "The requested whitelisted site with id " + id + "could not be found.");
  167. return JsonErrorView.VIEWNAME;
  168. } else {
  169. m.put(JsonEntityView.ENTITY, whitelist);
  170. return JsonEntityView.VIEWNAME;
  171. }
  172. }
  173. }