moved services and api over to using new client Id field (instead of client_id)

pull/165/merge
Justin Richer 2012-08-10 16:53:31 -04:00
parent eb5a24690f
commit 155974d8e3
5 changed files with 34 additions and 35 deletions

View File

@ -29,8 +29,9 @@ public interface OAuth2ClientRepository {
public void deleteClient(ClientDetailsEntity client);
public ClientDetailsEntity updateClient(String clientId, ClientDetailsEntity client);
public ClientDetailsEntity updateClient(Long id, ClientDetailsEntity client);
public Collection<ClientDetailsEntity> getAllClients();
}

View File

@ -25,6 +25,8 @@ import org.springframework.security.oauth2.provider.ClientDetailsService;
public interface ClientDetailsEntityService extends ClientDetailsService {
public ClientDetailsEntity saveNewClient(ClientDetailsEntity client);
public ClientDetailsEntity getClientById(Long id);
public ClientDetailsEntity loadClientByClientId(String clientId) throws OAuth2Exception;
@ -33,7 +35,6 @@ public interface ClientDetailsEntityService extends ClientDetailsService {
public ClientDetailsEntity updateClient(ClientDetailsEntity oldClient, ClientDetailsEntity newClient);
public ClientDetailsEntity saveClient(ClientDetailsEntity client);
public Collection<ClientDetailsEntity> getAllClients();
}

View File

@ -73,7 +73,7 @@ public class JpaOAuth2ClientRepository implements OAuth2ClientRepository {
*/
@Override
public void deleteClient(ClientDetailsEntity client) {
ClientDetailsEntity found = getClientByClientId(client.getClientId());
ClientDetailsEntity found = getById(client.getId());
if (found != null) {
manager.remove(found);
} else {
@ -82,8 +82,8 @@ public class JpaOAuth2ClientRepository implements OAuth2ClientRepository {
}
@Override
public ClientDetailsEntity updateClient(String clientId, ClientDetailsEntity client) {
return JpaUtil.saveOrUpdate(clientId, manager, client);
public ClientDetailsEntity updateClient(Long id, ClientDetailsEntity client) {
return JpaUtil.saveOrUpdate(id, manager, client);
}
@Override

View File

@ -51,6 +51,24 @@ public class DefaultOAuth2ClientDetailsEntityService implements ClientDetailsEnt
this.tokenRepository = tokenRepository;
}
@Override
public ClientDetailsEntity saveNewClient(ClientDetailsEntity client) {
if (client.getId() != null) { // if it's not null, it's already been saved, this is an error
return null; // TODO: throw exception?
}
// assign a random clientid and secret if they're empty
if (client.getClientId() == null || client.getClientId().equals("")) {
client.setClientId(UUID.randomUUID().toString());
}
if (client.getClientSecret().equals("")) {
client.setClientSecret(UUID.randomUUID().toString());
}
return clientRepository.saveClient(client);
}
/**
* Get the client by its internal ID
*/
@ -84,7 +102,7 @@ public class DefaultOAuth2ClientDetailsEntityService implements ClientDetailsEnt
@Override
public void deleteClient(ClientDetailsEntity client) throws InvalidClientException {
if (clientRepository.getClientByClientId(client.getClientId()) == null) {
if (clientRepository.getById(client.getId()) == null) {
throw new InvalidClientException("Client with id " + client.getClientId() + " was not found");
}
@ -103,34 +121,11 @@ public class DefaultOAuth2ClientDetailsEntityService implements ClientDetailsEnt
@Override
public ClientDetailsEntity updateClient(ClientDetailsEntity oldClient, ClientDetailsEntity newClient) throws IllegalArgumentException {
if (oldClient != null && newClient != null) {
return clientRepository.updateClient(oldClient.getClientId(), newClient);
return clientRepository.updateClient(oldClient.getId(), newClient);
}
throw new IllegalArgumentException("Neither old client or new client can be null!");
}
/**
*
* @param client object to be saved
* @return ClientDetailsEntity the saved object
*/
@Override
public ClientDetailsEntity saveClient(ClientDetailsEntity client) {
if (client.getClientSecret().equals("")) {
client.setClientSecret(UUID.randomUUID().toString());
}
// this must be a new client if we don't have a clientID
// assign it a new ID
if (client.getClientId() == null || client.getClientId().equals("") || this.loadClientByClientId(client.getClientId()) == null) {
client.setClientId(UUID.randomUUID().toString());
return clientRepository.saveClient(client);
} else {
return clientRepository.updateClient(client.getClientId(), client);
}
}
/**
* Get all clients in the system
*/

View File

@ -68,21 +68,23 @@ public class ClientAPI {
// set owners as current logged in user
//client.setOwner(principal.getName());
//TODO: owner has been replaced by a list of contacts, which should be styled as email addresses.
m.addAttribute("entity", clientService.saveClient(client));
m.addAttribute("entity", clientService.saveNewClient(client));
return "jsonClientView";
}
@RequestMapping(value="/{id}", method = RequestMethod.PUT, headers = "Accept=application/json")
public String apiUpdateClient(@PathVariable("id") String id, @RequestBody String json, Model m, Principal principal) {
public String apiUpdateClient(@PathVariable("id") Long id, @RequestBody String json, Model m, Principal principal) {
ClientDetailsEntity client = new Gson().fromJson(json, ClientDetailsEntity.class);
client.setClientId(id);
ClientDetailsEntity oldClient = clientService.getClientById(id);
// set owners as current logged in user
// client.setOwner(principal.getName());
//TODO: owner has been replaced by a list of contacts, which should be styled as email addresses.
m.addAttribute("entity", clientService.saveClient(client));
m.addAttribute("entity", clientService.updateClient(oldClient, client));
return "jsonClientView";
}