Permalink
Browse files

Corrected exception handling to separate out concerns between service…

… and http.
  • Loading branch information...
1 parent e750f68 commit 765a32fc35b6ed7c1a24ea4f7c8c2f5c89b880cf Christopher Hunt committed Oct 5, 2011
@@ -19,16 +19,13 @@
package org.springframework.samples.springtz;
-import org.springframework.http.HttpStatus;
-import org.springframework.web.bind.annotation.ResponseStatus;
/**
* Thrown when a time zone object cannot be found.
*
* @author huntc
*
*/
-@ResponseStatus(HttpStatus.NOT_FOUND)
public class TimeZoneNotFound extends RuntimeException {
/**
*
@@ -19,12 +19,14 @@
package org.springframework.samples.springtz;
+import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.integration.Message;
@@ -66,22 +68,30 @@ public TimeZoneServiceController() {
* Get the available ids.
*
* @return a list of ides.
+ * @throws IOException
+ * something went wrong.
*/
@RequestMapping(value = "ids", method = RequestMethod.GET)
@ResponseBody
- public List<?> getAvailableIDs() {
- List<?> payloadList;
+ public List<?> getAvailableIDs(HttpServletResponse httpResponse)
+ throws IOException {
+ List<?> result = null;
Message<String> request = MessageBuilder.withPayload("").build();
Message<?> reply = template.sendAndReceive(idsRequestChannel, request);
if (reply != null) {
- payloadList = (List<?>) reply.getPayload();
- } else {
- payloadList = null;
+ Object payload = reply.getPayload();
+ if (payload instanceof List<?>) {
+ result = (List<?>) reply.getPayload();
+ }
}
- return payloadList;
+ if (result == null) {
+ httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
+ }
+
+ return result;
}
/**
@@ -94,25 +104,33 @@ public TimeZoneServiceController() {
* @param when
* the time to get the response at.
* @return the model object key to use in the response.
+ * @throws IOException
+ * something went wrong.
*/
@RequestMapping(value = "offset/{country}/{locality}", method = RequestMethod.GET)
@ResponseBody
public Integer getOffset(@PathVariable("country") String country,
@PathVariable("locality") String locality,
- @RequestParam("when") Date when) {
+ @RequestParam("when") Date when, HttpServletResponse httpResponse)
+ throws IOException {
- Integer result;
+ Integer result = null;
- String payload = country + "/" + locality;
- Message<String> request = MessageBuilder.withPayload(payload)
+ String id = country + "/" + locality;
+ Message<String> request = MessageBuilder.withPayload(id)
.setHeader("when", when).build();
Message<?> reply = template.sendAndReceive(offsetRequestChannel,
request);
if (reply != null) {
- result = (Integer) reply.getPayload();
- } else {
- result = null;
+ Object payload = reply.getPayload();
+ if (payload instanceof Integer) {
+ result = (Integer) payload;
+ }
+ }
+
+ if (result == null) {
+ httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
}
return result;

0 comments on commit 765a32f

Please sign in to comment.