class VenController {

	@RequestMapping(value = "/", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<InputStreamResource> createVen(@Valid @RequestBody VenCreateDto dto,
			HttpServletResponse response) {

		Ven findOneByUsername = venService.findOneByUsername(dto.getUsername());

		if (findOneByUsername != null) {
			LOGGER.warn("Ven: " + dto.getUsername() + " already exists");
			return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE_406)
					.contentType(MediaType.parseMediaType("application/octet-stream")).body(null);
		}
		Ven prepare = venService.prepare(dto);
		ResponseEntity<InputStreamResource> body = null;
		try {
			Optional<File> generateCertificateIfRequired = venService.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-VenID", prepare.getUsername())
						.contentLength(generateCertificateIfRequired.get().length())
						.contentType(MediaType.parseMediaType("application/octet-stream")).body(resource);
			} else {
				body = ResponseEntity.status(HttpStatus.CREATED_201).header("x-venID", 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);
		}

		venService.save(prepare);
		LOGGER.info("Create Ven: " + prepare.getUsername());

		return body;
	}

}