added preliminary whitelist api

pull/210/head
Justin Richer 12 years ago
parent 4af3dd89be
commit 6a180acf3c

@ -35,7 +35,7 @@ import org.springframework.transaction.annotation.Transactional;
public class WhitelistedSiteServiceImpl implements WhitelistedSiteService {
@Autowired
private WhitelistedSiteRepository whitelistedSiteRepository;
private WhitelistedSiteRepository repository;
/**
* Default constructor
@ -49,43 +49,46 @@ public class WhitelistedSiteServiceImpl implements WhitelistedSiteService {
*
* @param repository
*/
public WhitelistedSiteServiceImpl(
WhitelistedSiteRepository whitelistedSiteRepository) {
this.whitelistedSiteRepository = whitelistedSiteRepository;
public WhitelistedSiteServiceImpl(WhitelistedSiteRepository whitelistedSiteRepository) {
this.repository = whitelistedSiteRepository;
}
@Override
public WhitelistedSite getById(Long id) {
return whitelistedSiteRepository.getById(id);
return repository.getById(id);
}
@Override
public void remove(WhitelistedSite whitelistedSite) {
whitelistedSiteRepository.remove(whitelistedSite);
repository.remove(whitelistedSite);
}
@Override
public void removeById(Long id) {
WhitelistedSite w = repository.getById(id);
if (w != null) {
repository.remove(w);
}
}
@Override
public WhitelistedSite save(WhitelistedSite whitelistedSite) {
return whitelistedSiteRepository.save(whitelistedSite);
return repository.save(whitelistedSite);
}
@Override
public Collection<WhitelistedSite> getAll() {
return whitelistedSiteRepository.getAll();
return repository.getAll();
}
@Override
public WhitelistedSite getByClientId(String clientId) {
return whitelistedSiteRepository.getByClientId(clientId);
return repository.getByClientId(clientId);
}
@Override
public Collection<WhitelistedSite> getByCreator(String creatorId) {
return whitelistedSiteRepository.getByCreator(creatorId);
return repository.getByCreator(creatorId);
}
}

@ -0,0 +1,137 @@
/**
*
*/
package org.mitre.openid.connect.web;
import java.security.Principal;
import java.util.Collection;
import org.mitre.oauth2.model.ClientDetailsEntity;
import org.mitre.openid.connect.model.WhitelistedSite;
import org.mitre.openid.connect.service.WhitelistedSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* @author jricher
*
*/
@Controller
@RequestMapping("/api/whitelist")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class WhitelistApi {
@Autowired
private WhitelistedSiteService whitelistService;
private Gson gson = new Gson();
private JsonParser parser = new JsonParser();
/**
* Get a list of all whitelisted sites
* @param m
* @return
*/
@RequestMapping(method = RequestMethod.GET, headers = "Accept=application/json")
public String getAllWhitelistedSites(ModelMap m) {
Collection<WhitelistedSite> all = whitelistService.getAll();
m.put("entity", all);
return "jsonEntityView";
}
/**
* Create a new whitelisted site
* @param jsonString
* @param m
* @param p
* @return
*/
@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
public String addNewWhitelistedSite(@RequestBody String jsonString, ModelMap m, Principal p) {
JsonObject json = parser.parse(jsonString).getAsJsonObject();
WhitelistedSite whitelist = gson.fromJson(json, WhitelistedSite.class);
// save the id of the person who created this
whitelist.setCreatorUserId(p.getName());
WhitelistedSite newWhitelist = whitelistService.save(whitelist);
m.put("entity", newWhitelist);
return "jsonEntityView";
}
/**
* Update an existing whitelisted site
*/
@RequestMapping(value="/{id}", method = RequestMethod.PUT, headers = "Accept=application/json")
public String updateWhitelistedSite(@PathVariable("id") Long id, @RequestBody String jsonString, ModelMap m, Principal p) {
JsonObject json = parser.parse(jsonString).getAsJsonObject();
WhitelistedSite whitelist = gson.fromJson(json, WhitelistedSite.class);
WhitelistedSite oldWhitelist = whitelistService.getById(id);
if (oldWhitelist == null) {
// TODO: throw new "entity not found"
}
WhitelistedSite newWhitelist = whitelistService.save(whitelist);
m.put("entity", newWhitelist);
return "jsonEntityView";
}
/**
* Delete a whitelisted site
*
*/
@RequestMapping(value="/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
public String deleteWhitelistedSite(@PathVariable("id") Long id, ModelMap m) {
WhitelistedSite whitelist = whitelistService.getById(id);
if (whitelist == null) {
// TODO: throw new "entity not found"
}
whitelistService.remove(whitelist);
// TODO: not really an entity view, more of an empty view w/code
return "jsonEntityView";
}
/**
* Get a single whitelisted site
*/
@RequestMapping(value="/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public String getWhitelistedSite(@PathVariable("id") Long id, ModelMap m) {
WhitelistedSite whitelist = whitelistService.getById(id);
if (whitelist == null) {
// TODO: throw new "entity not found"
}
m.put("entity", whitelist);
return "jsonEntityView";
}
}
Loading…
Cancel
Save