From 45d9d852ef28e301221ea8bf7323b9a444b719b8 Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Tue, 2 Oct 2012 11:33:37 -0400 Subject: [PATCH] polish whitespace and warnings --- .gitignore | 6 +- .../samples/montyhall/config/AppConfig.java | 5 - .../montyhall/controller/GameController.java | 44 ++++----- .../samples/montyhall/domain/Door.java | 7 +- .../samples/montyhall/domain/Game.java | 94 ++++++++++--------- .../repository/InMemoryGameRepository.java | 21 +++-- .../resource/ClickStreamResource.java | 6 +- .../montyhall/resource/DoorResource.java | 6 +- .../resource/DoorResourceAssembler.java | 7 +- .../resource/DoorStatusResource.java | 5 +- .../montyhall/resource/DoorsResource.java | 4 +- .../resource/DoorsResourceAssembler.java | 8 +- .../montyhall/resource/GameResource.java | 2 +- .../resource/GameResourceAssembler.java | 7 +- .../montyhall/resource/HistoryResource.java | 2 +- .../resource/HistoryResourceAssembler.java | 6 +- .../montyhall/service/ClickStreamService.java | 3 +- .../montyhall/web/CorsInterceptor.java | 20 ++-- .../web/MontyHallWebAppInitializer.java | 30 +++--- 19 files changed, 141 insertions(+), 142 deletions(-) diff --git a/.gitignore b/.gitignore index 0e615c9..9d5773f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ +bin build *~ gradle.properties -.gradle/ +.gradle +.classpath +.project +.settings \ No newline at end of file diff --git a/src/main/java/org/springsource/samples/montyhall/config/AppConfig.java b/src/main/java/org/springsource/samples/montyhall/config/AppConfig.java index 395a9e4..e7e05d9 100644 --- a/src/main/java/org/springsource/samples/montyhall/config/AppConfig.java +++ b/src/main/java/org/springsource/samples/montyhall/config/AppConfig.java @@ -1,18 +1,13 @@ package org.springsource.samples.montyhall.config; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.stereotype.Controller; import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @ComponentScan(basePackages = "org.springsource.samples.montyhall", excludeFilters = {@Filter(Controller.class)}) public class AppConfig { - } \ No newline at end of file diff --git a/src/main/java/org/springsource/samples/montyhall/controller/GameController.java b/src/main/java/org/springsource/samples/montyhall/controller/GameController.java index 25b028e..3177035 100644 --- a/src/main/java/org/springsource/samples/montyhall/controller/GameController.java +++ b/src/main/java/org/springsource/samples/montyhall/controller/GameController.java @@ -44,11 +44,11 @@ public class GameController { @Autowired public GameController(GameRepository repo, - GameResourceAssembler assembler, - DoorResourceAssembler doorAssembler, - DoorsResourceAssembler doorsAssembler, - HistoryResourceAssembler historyAssembler, - ClickStreamService clicks) { + GameResourceAssembler assembler, + DoorResourceAssembler doorAssembler, + DoorsResourceAssembler doorsAssembler, + HistoryResourceAssembler historyAssembler, + ClickStreamService clicks) { this.repository = repo; this.gameResourceAssembler = assembler; this.doorResourceAssembler = doorAssembler; @@ -58,7 +58,7 @@ public GameController(GameRepository repo, } @RequestMapping(method=RequestMethod.POST) - public HttpEntity createGame() { + public HttpEntity createGame() { Game game = new Game(); this.repository.storeGame(game); HttpHeaders headers = new HttpHeaders(); @@ -73,7 +73,7 @@ public HttpEntity show(@PathVariable Long id) { HttpHeaders headers = new HttpHeaders(); return new ResponseEntity(headers, HttpStatus.NOT_FOUND); } - + GameResource resource = this.gameResourceAssembler.toResource(game); return new ResponseEntity(resource, HttpStatus.OK); } @@ -85,7 +85,7 @@ public HttpEntity showDoors(@PathVariable Long id) { HttpHeaders headers = new HttpHeaders(); return new ResponseEntity(headers, HttpStatus.NOT_FOUND); } - + DoorsResource resource = this.doorsResourceAssembler.toResource(game); return new ResponseEntity(resource, HttpStatus.OK); } @@ -97,7 +97,7 @@ public HttpEntity showDoor(@PathVariable Long gameId, @PathVariabl HttpHeaders headers = new HttpHeaders(); return new ResponseEntity(headers, HttpStatus.NOT_FOUND); } - + Door door = game.getDoors().get((int)(doorId - 1)); DoorResource resource = this.doorResourceAssembler.toResource(game,door); return new ResponseEntity(resource, HttpStatus.OK); @@ -105,16 +105,16 @@ public HttpEntity showDoor(@PathVariable Long gameId, @PathVariabl @RequestMapping(value="/{gameId}/doors/{doorId}", method=RequestMethod.PUT) public HttpEntity updateDoor(@PathVariable Long gameId, - @PathVariable Long doorId, - @RequestBody DoorStatusResource doorResource) { + @PathVariable Long doorId, + @RequestBody DoorStatusResource doorResource) { Game game = this.repository.findById(gameId); if (game == null || doorId < 1 || doorId > 3) { HttpHeaders headers = new HttpHeaders(); return new ResponseEntity(headers, HttpStatus.NOT_FOUND); } - + Door door = game.getDoors().get((int)(doorId - 1)); - + try { if (doorResource.status == DoorStatus.SELECTED) { game.select(door); @@ -126,7 +126,7 @@ public HttpEntity updateDoor(@PathVariable Long gameId, HttpHeaders headers = new HttpHeaders(); return new ResponseEntity(headers, HttpStatus.CONFLICT); } - + DoorResource resource = this.doorResourceAssembler.toResource(game,door); return new ResponseEntity(resource, HttpStatus.OK); } @@ -138,35 +138,35 @@ public HttpEntity showHistory(@PathVariable Long id) { HttpHeaders headers = new HttpHeaders(); return new ResponseEntity(headers, HttpStatus.NOT_FOUND); } - + HistoryResource resource = this.historyResourceAssembler.toResource(game); return new ResponseEntity(resource, HttpStatus.OK); } - /** and a handler method for the click stream data */ @RequestMapping(value="/{id}/clicks", method=RequestMethod.POST) - public HttpEntity recordClickStreamData(@PathVariable Long id, - @RequestBody ClickStreamResource clicks) { + public HttpEntity recordClickStreamData(@PathVariable Long id, + @RequestBody ClickStreamResource clicks) { Game game = this.repository.findById(id); if (game == null) { HttpHeaders headers = new HttpHeaders(); return new ResponseEntity(headers, HttpStatus.NOT_FOUND); } - + this.clickStreamService.recordClickStream(game,clicks); HttpHeaders headers = new HttpHeaders(); return new ResponseEntity(headers, HttpStatus.CREATED); } - + /** OPTIONS processing for CORS */ + @SuppressWarnings({ "rawtypes", "unchecked" }) @RequestMapping(value="/**", method=RequestMethod.OPTIONS) public HttpEntity handleOptionsRequest() { // a CORS preflight request will be handled by our interceptor HttpHeaders headers = new HttpHeaders(); headers.add("Allow","GET, HEAD, POST, PUT, OPTIONS"); - return new ResponseEntity(headers,HttpStatus.OK); + return new ResponseEntity(headers, HttpStatus.OK); } - + } diff --git a/src/main/java/org/springsource/samples/montyhall/domain/Door.java b/src/main/java/org/springsource/samples/montyhall/domain/Door.java index c945d4a..aafa8be 100644 --- a/src/main/java/org/springsource/samples/montyhall/domain/Door.java +++ b/src/main/java/org/springsource/samples/montyhall/domain/Door.java @@ -3,6 +3,7 @@ import org.springframework.hateoas.Identifiable; public class Door implements Identifiable { + private final Prize prize; private final int id; private DoorStatus status = DoorStatus.CLOSED; @@ -34,16 +35,16 @@ public void setStatus(DoorStatus status) { break; case SELECTED : if (this.status == DoorStatus.OPENED) { - illegalTransitionAttempted = true; + illegalTransitionAttempted = true; } - break; + break; case OPENED : break; } if (illegalTransitionAttempted) { throw new IllegalStateException("Cannot transition to " + status + " from " + this.status); } - this.status = status; + this.status = status; } public void reveal() { diff --git a/src/main/java/org/springsource/samples/montyhall/domain/Game.java b/src/main/java/org/springsource/samples/montyhall/domain/Game.java index 1f99c2a..a95f310 100644 --- a/src/main/java/org/springsource/samples/montyhall/domain/Game.java +++ b/src/main/java/org/springsource/samples/montyhall/domain/Game.java @@ -13,7 +13,6 @@ public class Game implements Identifiable { private Door[] doors = new Door[3]; private List history = new ArrayList(); - public Game() { int doorWithJuergen = new Random().nextInt(3); doors[0] = (doorWithJuergen == 0) ? new Door(1,Prize.JUERGEN) : new Door(1,Prize.SMALL_FURRY_ANIMAL); @@ -45,55 +44,58 @@ public List getHistory() { return this.history; } - /** Select a door, and return the door opened by the host */ public Door select(Door door) { - if (this.status != GameStatus.AWAITING_INITIAL_SELECTION) { - throw new IllegalStateException("Can't select a door from state " + this.status); - } - - Door toSelect = this.doors[door.getId() - 1]; - toSelect.setStatus(DoorStatus.SELECTED); - GameEvent selectEvent = door.getId() == 1 ? GameEvent.SELECTED_DOOR_ONE - : (door.getId() == 2 ? GameEvent.SELECTED_DOOR_TWO : GameEvent.SELECTED_DOOR_THREE); - this.history.add(selectEvent); - - int doorToReveal = new Random().nextInt(3); - while ( (this.doors[doorToReveal] == toSelect) || (this.doors[doorToReveal].getPrize() == Prize.JUERGEN) ) { - doorToReveal = new Random().nextInt(3); - } - Door toReveal = this.doors[doorToReveal]; - toReveal.reveal(); - GameEvent revealEvent = doorToReveal == 0 ? GameEvent.REVEALED_DOOR_ONE - : (doorToReveal == 1 ? GameEvent.REVEALED_DOOR_TWO : GameEvent.REVEALED_DOOR_THREE); - this.history.add(revealEvent); - this.status = GameStatus.AWAITING_FINAL_SELECTION; - return toReveal; + if (this.status != GameStatus.AWAITING_INITIAL_SELECTION) { + throw new IllegalStateException("Can't select a door from state " + this.status); + } + + Door toSelect = this.doors[door.getId() - 1]; + toSelect.setStatus(DoorStatus.SELECTED); + GameEvent selectEvent = door.getId() == 1 + ? GameEvent.SELECTED_DOOR_ONE + : (door.getId() == 2 ? GameEvent.SELECTED_DOOR_TWO : GameEvent.SELECTED_DOOR_THREE); + this.history.add(selectEvent); + + int doorToReveal = new Random().nextInt(3); + while ( (this.doors[doorToReveal] == toSelect) || (this.doors[doorToReveal].getPrize() == Prize.JUERGEN) ) { + doorToReveal = new Random().nextInt(3); + } + Door toReveal = this.doors[doorToReveal]; + toReveal.reveal(); + GameEvent revealEvent = doorToReveal == 0 + ? GameEvent.REVEALED_DOOR_ONE + : (doorToReveal == 1 ? GameEvent.REVEALED_DOOR_TWO : GameEvent.REVEALED_DOOR_THREE); + this.history.add(revealEvent); + this.status = GameStatus.AWAITING_FINAL_SELECTION; + return toReveal; } public GameStatus open(Door door) { - if (this.status != GameStatus.AWAITING_FINAL_SELECTION) { - throw new IllegalStateException("Can't open a door from state " + this.status); - } - Door toOpen = this.doors[door.getId() -1]; - toOpen.setStatus(DoorStatus.OPENED); - Prize prize = toOpen.getPrize(); - if (prize == Prize.JUERGEN) { - this.status = GameStatus.WON; - } - else { - this.status = GameStatus.LOST; - } - - GameEvent openEvent = (door.getId() == 1 ? GameEvent.OPENED_DOOR_ONE - : (door.getId() == 2 ? GameEvent.OPENED_DOOR_TWO : GameEvent.OPENED_DOOR_THREE)); - this.history.add(openEvent); - if (this.status == GameStatus.WON) { - this.history.add(GameEvent.WON); - } else { - this.history.add(GameEvent.LOST); - } - - return this.status; + if (this.status != GameStatus.AWAITING_FINAL_SELECTION) { + throw new IllegalStateException("Can't open a door from state " + this.status); + } + Door toOpen = this.doors[door.getId() -1]; + toOpen.setStatus(DoorStatus.OPENED); + Prize prize = toOpen.getPrize(); + if (prize == Prize.JUERGEN) { + this.status = GameStatus.WON; + } + else { + this.status = GameStatus.LOST; + } + + GameEvent openEvent = (door.getId() == 1 + ? GameEvent.OPENED_DOOR_ONE + : (door.getId() == 2 ? GameEvent.OPENED_DOOR_TWO : GameEvent.OPENED_DOOR_THREE)); + this.history.add(openEvent); + if (this.status == GameStatus.WON) { + this.history.add(GameEvent.WON); + } else { + this.history.add(GameEvent.LOST); + } + + return this.status; } + } diff --git a/src/main/java/org/springsource/samples/montyhall/repository/InMemoryGameRepository.java b/src/main/java/org/springsource/samples/montyhall/repository/InMemoryGameRepository.java index 1c0995c..82e0c29 100644 --- a/src/main/java/org/springsource/samples/montyhall/repository/InMemoryGameRepository.java +++ b/src/main/java/org/springsource/samples/montyhall/repository/InMemoryGameRepository.java @@ -13,20 +13,21 @@ public class InMemoryGameRepository implements GameRepository { private Map games = Collections.synchronizedMap(new HashMap()); public Game findById(Long id) { - return this.games.get(id); + return this.games.get(id); } public void storeGame(Game game) { - if (game.getId() == null) { - synchronized(games) { - Long newId = new Random().nextLong(); - while ((newId <= 0) || this.games.containsKey(newId)) { - newId = new Random().nextLong(); + if (game.getId() == null) { + synchronized(games) { + Long newId = new Random().nextLong(); + while ((newId <= 0) || this.games.containsKey(newId)) { + newId = new Random().nextLong(); + } + game.setId(newId); + games.put(newId,game); + } } - game.setId(newId); - games.put(newId,game); - } - } } + } diff --git a/src/main/java/org/springsource/samples/montyhall/resource/ClickStreamResource.java b/src/main/java/org/springsource/samples/montyhall/resource/ClickStreamResource.java index 1b48a6d..34eae4c 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/ClickStreamResource.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/ClickStreamResource.java @@ -1,14 +1,12 @@ package org.springsource.samples.montyhall.resource; -import org.springframework.hateoas.ResourceSupport; import org.codehaus.jackson.annotate.JsonProperty; -import org.springsource.samples.montyhall.domain.DoorStatus; /** Used when mapping incoming POST requests with Click Stream data */ public class ClickStreamResource { - + @JsonProperty("data") public String data; - + } diff --git a/src/main/java/org/springsource/samples/montyhall/resource/DoorResource.java b/src/main/java/org/springsource/samples/montyhall/resource/DoorResource.java index 4e16696..5d8d3c1 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/DoorResource.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/DoorResource.java @@ -7,11 +7,11 @@ public class DoorResource extends ResourceSupport { - + @JsonProperty("status") DoorStatus status; - + @JsonProperty("content") String content; - + } diff --git a/src/main/java/org/springsource/samples/montyhall/resource/DoorResourceAssembler.java b/src/main/java/org/springsource/samples/montyhall/resource/DoorResourceAssembler.java index 221c7a8..e931a98 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/DoorResourceAssembler.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/DoorResourceAssembler.java @@ -2,20 +2,18 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; -import org.springframework.hateoas.mvc.ResourceAssemblerSupport; import org.springframework.stereotype.Component; import org.springsource.samples.montyhall.controller.GameController; import org.springsource.samples.montyhall.domain.Game; import org.springsource.samples.montyhall.domain.Door; import org.springsource.samples.montyhall.domain.DoorStatus; -import org.springsource.samples.montyhall.domain.Prize; @Component public class DoorResourceAssembler { - public DoorResourceAssembler() { + public DoorResourceAssembler() { } - + public DoorResource toResource(Game game, Door door) { DoorResource resource = new DoorResource(); resource.status = door.getStatus(); @@ -27,4 +25,5 @@ public DoorResource toResource(Game game, Door door) { resource.add(linkTo(GameController.class).slash(game).slash("doors").slash(door).withSelfRel()); return resource; } + } \ No newline at end of file diff --git a/src/main/java/org/springsource/samples/montyhall/resource/DoorStatusResource.java b/src/main/java/org/springsource/samples/montyhall/resource/DoorStatusResource.java index 5e52a29..f528a59 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/DoorStatusResource.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/DoorStatusResource.java @@ -1,14 +1,13 @@ package org.springsource.samples.montyhall.resource; -import org.springframework.hateoas.ResourceSupport; import org.codehaus.jackson.annotate.JsonProperty; import org.springsource.samples.montyhall.domain.DoorStatus; /** Used when mapping incoming PUT / PATCH requests with partial info */ public class DoorStatusResource { - + @JsonProperty("status") public DoorStatus status; - + } diff --git a/src/main/java/org/springsource/samples/montyhall/resource/DoorsResource.java b/src/main/java/org/springsource/samples/montyhall/resource/DoorsResource.java index d8432b4..9652453 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/DoorsResource.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/DoorsResource.java @@ -4,8 +4,8 @@ import org.codehaus.jackson.annotate.JsonProperty; public class DoorsResource extends ResourceSupport { - + @JsonProperty("doors") DoorResource[] doors = new DoorResource[3]; - + } diff --git a/src/main/java/org/springsource/samples/montyhall/resource/DoorsResourceAssembler.java b/src/main/java/org/springsource/samples/montyhall/resource/DoorsResourceAssembler.java index c2b8bf5..d66c136 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/DoorsResourceAssembler.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/DoorsResourceAssembler.java @@ -2,14 +2,11 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; -import org.springframework.hateoas.mvc.ResourceAssemblerSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springsource.samples.montyhall.controller.GameController; import org.springsource.samples.montyhall.domain.Game; import org.springsource.samples.montyhall.domain.Door; -import org.springsource.samples.montyhall.domain.DoorStatus; -import org.springsource.samples.montyhall.domain.Prize; import java.util.List; @@ -19,10 +16,10 @@ public class DoorsResourceAssembler { private final DoorResourceAssembler doorAssembler; @Autowired - public DoorsResourceAssembler(DoorResourceAssembler assembler) { + public DoorsResourceAssembler(DoorResourceAssembler assembler) { this.doorAssembler = assembler; } - + public DoorsResource toResource(Game game) { DoorsResource resource = new DoorsResource(); List doors = game.getDoors(); @@ -32,4 +29,5 @@ public DoorsResource toResource(Game game) { resource.add(linkTo(GameController.class).slash(game).slash("doors").withSelfRel()); return resource; } + } \ No newline at end of file diff --git a/src/main/java/org/springsource/samples/montyhall/resource/GameResource.java b/src/main/java/org/springsource/samples/montyhall/resource/GameResource.java index 9222262..b98266a 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/GameResource.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/GameResource.java @@ -5,7 +5,7 @@ import org.springsource.samples.montyhall.domain.GameStatus; public class GameResource extends ResourceSupport { - + @JsonProperty("status") GameStatus status; diff --git a/src/main/java/org/springsource/samples/montyhall/resource/GameResourceAssembler.java b/src/main/java/org/springsource/samples/montyhall/resource/GameResourceAssembler.java index 9c66531..59ec7a3 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/GameResourceAssembler.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/GameResourceAssembler.java @@ -10,10 +10,10 @@ @Component public class GameResourceAssembler extends ResourceAssemblerSupport { - public GameResourceAssembler() { + public GameResourceAssembler() { super(GameController.class, GameResource.class); } - + @Override public GameResource toResource(Game game) { GameResource resource = createResource(game); @@ -22,5 +22,6 @@ public GameResource toResource(Game game) { resource.add(linkTo(GameController.class).slash(game).slash("history").withRel("history")); return resource; } + } - + diff --git a/src/main/java/org/springsource/samples/montyhall/resource/HistoryResource.java b/src/main/java/org/springsource/samples/montyhall/resource/HistoryResource.java index f5669a2..90e9216 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/HistoryResource.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/HistoryResource.java @@ -10,5 +10,5 @@ public class HistoryResource extends ResourceSupport { @JsonProperty("events") GameEvent[] events; - + } diff --git a/src/main/java/org/springsource/samples/montyhall/resource/HistoryResourceAssembler.java b/src/main/java/org/springsource/samples/montyhall/resource/HistoryResourceAssembler.java index 4856e2f..59fb224 100644 --- a/src/main/java/org/springsource/samples/montyhall/resource/HistoryResourceAssembler.java +++ b/src/main/java/org/springsource/samples/montyhall/resource/HistoryResourceAssembler.java @@ -2,7 +2,6 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; -import org.springframework.hateoas.mvc.ResourceAssemblerSupport; import org.springframework.stereotype.Component; import org.springsource.samples.montyhall.controller.GameController; import org.springsource.samples.montyhall.domain.Game; @@ -12,9 +11,9 @@ @Component public class HistoryResourceAssembler { - public HistoryResourceAssembler() { + public HistoryResourceAssembler() { } - + public HistoryResource toResource(Game game) { HistoryResource resource = new HistoryResource(); List history = game.getHistory(); @@ -22,4 +21,5 @@ public HistoryResource toResource(Game game) { resource.add(linkTo(GameController.class).slash(game).slash("history").withSelfRel()); return resource; } + } \ No newline at end of file diff --git a/src/main/java/org/springsource/samples/montyhall/service/ClickStreamService.java b/src/main/java/org/springsource/samples/montyhall/service/ClickStreamService.java index e07017a..96a84c2 100644 --- a/src/main/java/org/springsource/samples/montyhall/service/ClickStreamService.java +++ b/src/main/java/org/springsource/samples/montyhall/service/ClickStreamService.java @@ -10,8 +10,9 @@ public class ClickStreamService { public ClickStreamService() { // TODO - inject SI outbound channel } - + public void recordClickStream(Game game, ClickStreamResource clicks) { // TODO, post as message on outbound channel } + } diff --git a/src/main/java/org/springsource/samples/montyhall/web/CorsInterceptor.java b/src/main/java/org/springsource/samples/montyhall/web/CorsInterceptor.java index 8598fdb..b4d1ed8 100644 --- a/src/main/java/org/springsource/samples/montyhall/web/CorsInterceptor.java +++ b/src/main/java/org/springsource/samples/montyhall/web/CorsInterceptor.java @@ -6,45 +6,45 @@ import javax.servlet.http.HttpServletResponse; public class CorsInterceptor extends HandlerInterceptorAdapter { - + private static final String ORIGIN = "Origin"; private static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin"; private static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods"; private static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers"; private static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method"; private static final String ACCESS_CONTROL_REQUEST_HEADERS = "Access-Control-Request-Headers"; - + @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, - ModelAndView modelAndView) { + ModelAndView modelAndView) { // Our REST API is accessible from anywhere response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); } - + @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { // For PUT requests we need an extra round-trip // See e.g. http://www.html5rocks.com/en/tutorials/cors/ - + String origin = request.getHeader(ORIGIN); String acRequestMethod = request.getHeader(ACCESS_CONTROL_REQUEST_METHOD); String acRequestHeaders = request.getHeader(ACCESS_CONTROL_REQUEST_HEADERS); - + if (hasValue(origin) && hasValue(acRequestMethod)) { // this is a preflight check // our API only needs this for PUT requests, anything we can PUT we can also GET response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN,origin); response.setHeader(ACCESS_CONTROL_ALLOW_METHODS,"GET, PUT"); response.setHeader(ACCESS_CONTROL_ALLOW_HEADERS,acRequestHeaders); - + return false; // Don't continue processing, return to browser immediately } - + return true; // Not a preflight check, continue as normal } - + private boolean hasValue(String s) { return ((s != null) && !s.isEmpty()); } - + } \ No newline at end of file diff --git a/src/main/java/org/springsource/samples/montyhall/web/MontyHallWebAppInitializer.java b/src/main/java/org/springsource/samples/montyhall/web/MontyHallWebAppInitializer.java index 52e314b..758cd00 100644 --- a/src/main/java/org/springsource/samples/montyhall/web/MontyHallWebAppInitializer.java +++ b/src/main/java/org/springsource/samples/montyhall/web/MontyHallWebAppInitializer.java @@ -18,22 +18,22 @@ */ public class MontyHallWebAppInitializer implements WebApplicationInitializer { - @java.lang.Override - public void onStartup(ServletContext servletContext) throws ServletException { - // Spring annotation based configuration from the 'config' package - AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.scan("org.springsource.samples.montyhall.config"); + @java.lang.Override + public void onStartup(ServletContext servletContext) throws ServletException { + // Spring annotation based configuration from the 'config' package + AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); + root.scan("org.springsource.samples.montyhall.config"); - // Servlet setup... - servletContext.addListener(new ContextLoaderListener(root)); + // Servlet setup... + servletContext.addListener(new ContextLoaderListener(root)); - ServletRegistration.Dynamic appServlet = - servletContext.addServlet("appServlet", new DispatcherServlet(root)); - appServlet.setLoadOnStartup(1); - Set mappingConflicts = appServlet.addMapping("/"); - if ( !mappingConflicts.isEmpty() ) { - throw new IllegalStateException ("Could not bind to '/', ensure Tomcat version > 7.0.14 "); - } - } + ServletRegistration.Dynamic appServlet = + servletContext.addServlet("appServlet", new DispatcherServlet(root)); + appServlet.setLoadOnStartup(1); + Set mappingConflicts = appServlet.addMapping("/"); + if ( !mappingConflicts.isEmpty() ) { + throw new IllegalStateException ("Could not bind to '/', ensure Tomcat version > 7.0.14 "); + } + } }