class AccountController {

	@PreAuthorize("hasRole('ROLE_ADMIN')")
	@RequestMapping(value = "/user", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<InputStreamResource> createUser(@RequestBody OadrUserCreateDto dto,
			HttpServletResponse response) {

		OadrUser findOneByUsername = oadrUserService.findByUsername(dto.getUsername());

		if (findOneByUsername != null) {
			LOGGER.warn("User: " + dto.getUsername() + " already exists");
			return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE_406)
					.contentType(OCTET_STREAM_MEDIA_TYPE).body(null);
		}
		OadrUser prepare = oadrUserService.prepare(dto);
		ResponseEntity<InputStreamResource> body = null;
		try {
			Optional<File> generateCertificateIfRequired = oadrUserService.generateCertificateIfRequired(dto, prepare);

			if (generateCertificateIfRequired.isPresent()) {
				InputStreamResource resource = new InputStreamResource(
						new FileInputStream(generateCertificateIfRequired.get()));
				body = ResponseEntity.status(HttpStatus.CREATED_201)
						.header("Content-Disposition", "attachment; filename=\"archive.tar\"")
						.header("x-Username", prepare.getUsername())
						.contentLength(generateCertificateIfRequired.get().length())
						.contentType(OCTET_STREAM_MEDIA_TYPE).body(resource);
			} else {
				body = ResponseEntity.status(HttpStatus.CREATED_201).header("x-username", prepare.getUsername())
						.body(null);
			}

		} catch (GenerateX509VenException e) {
			LOGGER.error("", e);
			response.setStatus(HttpStatus.NOT_ACCEPTABLE_406);
		} catch (FileNotFoundException e) {
			LOGGER.error("", e);
			response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
		}

		oadrUserService.save(prepare);
		LOGGER.info("Create User: " + prepare.getUsername());

		return body;
	}

}