Skip to content

Commit

Permalink
More checks on un/follow
Browse files Browse the repository at this point in the history
  • Loading branch information
sfragis committed Jan 20, 2013
1 parent 75891c9 commit a690056
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
31 changes: 29 additions & 2 deletions src/main/java/eu/fabiostrozzi/chirp/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.web.bind.annotation.ResponseStatus;

import eu.fabiostrozzi.chirp.service.ChirpsService;
import eu.fabiostrozzi.chirp.service.InvalidOperationException;

/**
* REST api controller.
Expand All @@ -44,6 +45,10 @@ public class NotFoundException extends Exception {}
@SuppressWarnings("serial")
public class IntSrvErrorException extends Exception {}

@ResponseStatus(HttpStatus.FORBIDDEN)
@SuppressWarnings("serial")
public class ForbiddenException extends Exception {}

/**
* Get the chirps/tweets of a given user.
* <p>
Expand All @@ -53,6 +58,10 @@ public class IntSrvErrorException extends Exception {}
* @param key
* @param token
* @return
* @throws IntSrvErrorException
* If something wrong, unexpectedly happened
* @throws NotFoundException
* If specified user cannot be found
*/
@RequestMapping(value = "/{user}/chirps", method = RequestMethod.GET)
public @ResponseBody
Expand Down Expand Up @@ -83,6 +92,10 @@ List<Chirp> get(@PathVariable String user, @RequestParam(value = "search", requi
* @param user
* @param token
* @return
* @throws IntSrvErrorException
* If something wrong, unexpectedly happened
* @throws NotFoundException
* If specified user cannot be found
*/
@RequestMapping(value = "/{user}/people", method = RequestMethod.GET)
public @ResponseBody
Expand Down Expand Up @@ -114,11 +127,17 @@ Map<String, List<User>> getPeople(@PathVariable String user,
* @param user
* @param token
* @return
* @throws IntSrvErrorException
* If something wrong, unexpectedly happened
* @throws NotFoundException
* If specified user cannot be found
* @throws ForbiddenException
* If authenticated user tries to follow himself
*/
@RequestMapping(value = "/{user}/follow", method = RequestMethod.PUT)
public @ResponseBody
Boolean follow(@PathVariable String user, @RequestHeader(value = TOKEN, required = false) String token)
throws IntSrvErrorException, NotFoundException {
throws IntSrvErrorException, NotFoundException, ForbiddenException {
try {
if (!service.userExists(user))
throw new NotFoundException();
Expand All @@ -129,6 +148,8 @@ Boolean follow(@PathVariable String user, @RequestHeader(value = TOKEN, required

} catch (NotFoundException e) {
throw e;
} catch (InvalidOperationException e) {
throw new ForbiddenException();
} catch (Exception e) {
log.error("Unexpected error occurred while calling '/api/{}/follow'", user, e);
throw new IntSrvErrorException();
Expand All @@ -138,11 +159,17 @@ Boolean follow(@PathVariable String user, @RequestHeader(value = TOKEN, required
/**
* Unfollows a user.
* <p>
* User identified by the input token will stop following the given user.
* User identified by the input token will stop following the given user. happened
*
* @param user
* @param token
* @return
* @throws IntSrvErrorException
* If something wrong, unexpectedly happened
* @throws NotFoundException
* If specified user cannot be found
* @throws ForbiddenException
* If authenticated user tries to unfollow himself
*/
@RequestMapping(value = "/{user}/unfollow", method = RequestMethod.PUT)
public @ResponseBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
@Service
public class ChirpsServiceImpl implements ChirpsService {
@SuppressWarnings("unused")
private static final Logger log = LoggerFactory.getLogger(ChirpsServiceImpl.class);

@Autowired
Expand Down Expand Up @@ -95,6 +94,10 @@ public boolean userExists(String user) {
*/
@Override
public void follow(String actor, String friend) {
if (actor.equals(friend)) {
log.error("Invalid attempt to make user '{}' follow himself", actor);
throw new InvalidOperationException("User cannot follow himself");
}
usersDao.follow(actor, friend);
}

Expand All @@ -104,6 +107,10 @@ public void follow(String actor, String friend) {
*/
@Override
public void unfollow(String actor, String who) {
if (actor.equals(who)) {
log.error("Invalid attempt to make user '{}' unfollow himself", actor);
throw new InvalidOperationException("User cannot unfollow himself");
}
usersDao.unfollow(actor, who);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// InvalidOperationException.java, created on Jan 20, 2013
package eu.fabiostrozzi.chirp.service;

/**
* Describes any attempt to do invalid operations such as following oneself.
*
* @author fabio
*/
public class InvalidOperationException extends RuntimeException {
private static final long serialVersionUID = 3562290360885372281L;

public InvalidOperationException() {}

/**
* @param message
*/
public InvalidOperationException(String message) {
super(message);
}
}

0 comments on commit a690056

Please sign in to comment.