@RequestMapping(value = "/Codes/{code}", method = RequestMethod.GET) @ResponseBody public ExpiringCode retrieveCode(@PathVariable String code) { ExpiringCode result = null; try { result = expiringCodeStore.retrieveCode(code, IdentityZoneHolder.get().getId()); } catch (NullPointerException e) { throw new CodeStoreException("code is required.", HttpStatus.BAD_REQUEST); } if (result == null) { throw new CodeStoreException("Code not found: " + code, HttpStatus.NOT_FOUND); } return result; }
@RequestMapping(value = { "/Codes" }, method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) @ResponseBody public ExpiringCode generateCode(@RequestBody ExpiringCode expiringCode) { try { return expiringCodeStore.generateCode(expiringCode.getData(), expiringCode.getExpiresAt(), null, IdentityZoneHolder.get().getId()); } catch (NullPointerException e) { throw new CodeStoreException("data and expiresAt are required.", HttpStatus.BAD_REQUEST); } catch (IllegalArgumentException e) { throw new CodeStoreException("expiresAt must be in the future.", HttpStatus.BAD_REQUEST); } catch (DataIntegrityViolationException e) { throw new CodeStoreException("Duplicate code generated.", HttpStatus.INTERNAL_SERVER_ERROR); } }
@ExceptionHandler public View handleException(Exception t, HttpServletRequest request) throws CodeStoreException { CodeStoreException e = new CodeStoreException("Unexpected error", t, HttpStatus.INTERNAL_SERVER_ERROR); if (t instanceof CodeStoreException) { e = (CodeStoreException) t; } // User can supply trace=true or just trace (unspecified) to get stack // traces boolean trace = request.getParameter("trace") != null && !request.getParameter("trace").equals("false"); return new ConvertingExceptionView(new ResponseEntity<ExceptionReport>(new ExceptionReport(e, trace), e.getStatus()), messageConverters); } }