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; }  @RequestMapping(value = "/", method = RequestMethod.GET) @ResponseBody List<VenDto> listVen(); @RequestMapping(value = "/search", method = RequestMethod.POST) @ResponseBody List<VenDto> searchVen(@RequestBody List<VenFilter> filters, @RequestParam("page") int page,
			@RequestParam("size") int size, HttpServletResponse response); @RequestMapping(value = "/", method = RequestMethod.POST) @ResponseBody ResponseEntity<InputStreamResource> createVen(@Valid @RequestBody VenCreateDto dto,
			HttpServletResponse response); @RequestMapping(value = "/{venID}", method = RequestMethod.PUT) @ResponseBody VenCreateDto updateVen(@PathVariable("venID") String venUsername, @Valid @RequestBody VenUpdateDto dto,
			HttpServletResponse response); @RequestMapping(value = "/{venID}", method = RequestMethod.GET) @ResponseBody VenDto findVenByUsername(@PathVariable("venID") String venUsername, HttpServletResponse response); @RequestMapping(value = "/{venID}", method = RequestMethod.DELETE) @ResponseBody void deleteVenByUsername(@PathVariable("venID") String venUsername, HttpServletResponse response); @RequestMapping(value = "/{venID}/resource", method = RequestMethod.POST) @ResponseBody VenResourceDto createVenResource(@PathVariable("venID") String venUsername, @RequestBody VenResourceDto dto,
			HttpServletResponse response); @RequestMapping(value = "/{venID}/resource", method = RequestMethod.GET) @ResponseBody List<VenResourceDto> listVenResource(@PathVariable("venID") String venUsername,
			HttpServletResponse response); @RequestMapping(value = "/{venID}/resource/{resourceName}", method = RequestMethod.DELETE) @ResponseBody void deleteVenResource(@PathVariable("venID") String venUsername,
			@PathVariable("resourceName") String resourceName, HttpServletResponse response); @RequestMapping(value = "/{venID}/group", method = RequestMethod.POST) @ResponseBody VenCreateDto addGroupToVen(@PathVariable("venID") String venUsername, @RequestParam("groupId") Long groupId,
			HttpServletResponse response); @RequestMapping(value = "/{venID}/group", method = RequestMethod.GET) @ResponseBody List<VenGroupDto> listVenGroup(@PathVariable("venID") String venUsername, HttpServletResponse response); @RequestMapping(value = "/{venID}/group/remove", method = RequestMethod.POST) @ResponseBody void deleteVenGroup(@PathVariable("venID") String venUsername, @RequestParam("groupId") Long groupId,
			HttpServletResponse response); @RequestMapping(value = "/{venID}/marketContext", method = RequestMethod.POST) @ResponseBody VenCreateDto addMarketContextToVen(@PathVariable("venID") String venUsername,
			@RequestParam("marketContextId") Long marketContextId, HttpServletResponse response); @RequestMapping(value = "/{venID}/marketContext", method = RequestMethod.GET) @ResponseBody List<VenMarketContextDto> listVenMarketContext(@PathVariable("venID") String venUsername,
			HttpServletResponse response); @RequestMapping(value = "/{venID}/marketContext/remove", method = RequestMethod.POST) @ResponseBody void deleteVenMarketContext(@PathVariable("venID") String venUsername,
			@RequestParam("marketContextId") Long marketContextId, HttpServletResponse response); @RequestMapping(value = "/{venID}/cleanRegistration", method = RequestMethod.POST) @ResponseBody void cleanRegistration(@PathVariable("venID") String venUsername, HttpServletResponse response);  }
@Test public void createVenTest() throws Exception { VenCreateDto dto = new VenCreateDto(); dto.setCommonName("myapp"); dto.setUsername("myapp"); dto.setAuthenticationType("login"); dto.setPassword("pass"); byte[] content = mapper.writeValueAsBytes(dto); MvcResult andReturn = this.mockMvc .perform(MockMvcRequestBuilders.post(VEN_URL).content(content) .header("Content-Type", "application/json").with(adminSession)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn(); assertEquals(0, andReturn.getResponse().getContentLength()); Ven findByUsername = venService.findOneByUsername("myapp"); assertNotNull(findByUsername); venService.delete(findByUsername); dto = new VenCreateDto(); dto.setCommonName("myapp"); dto.setUsername("myapp"); dto.setAuthenticationType("x509"); content = mapper.writeValueAsBytes(dto); andReturn = this.mockMvc .perform(MockMvcRequestBuilders.post(VEN_URL).content(content) .header("Content-Type", "application/json").with(adminSession)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn(); assertEquals(0, andReturn.getResponse().getContentLength()); findByUsername = venService.findOneByUsername("myapp"); assertNotNull(findByUsername); venService.delete(findByUsername); dto = new VenCreateDto(); dto.setCommonName("myapp"); dto.setAuthenticationType("x509"); dto.setNeedCertificateGeneration("rsa"); content = mapper.writeValueAsBytes(dto); andReturn = this.mockMvc .perform(MockMvcRequestBuilders.post(VEN_URL).content(content) .header("Content-Type", "application/json").with(adminSession)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn(); assertNotEquals(0, andReturn.getResponse().getContentLength()); assertNotNull(andReturn.getResponse().getHeader("x-VenID")); findByUsername = venService.findOneByUsername(andReturn.getResponse().getHeader("x-VenID")); assertNotNull(findByUsername); assertFalse(findByUsername.getReportOnly()); assertFalse(findByUsername.getXmlSignature()); venService.delete(findByUsername); dto = new VenCreateDto(); dto.setCommonName("myapp"); dto.setAuthenticationType("x509"); dto.setNeedCertificateGeneration("ecc"); dto.setReportOnly(true); dto.setXmlSignature(true); content = mapper.writeValueAsBytes(dto); andReturn = this.mockMvc .perform(MockMvcRequestBuilders.post(VEN_URL).content(content) .header("Content-Type", "application/json").with(adminSession)) .andExpect(MockMvcResultMatchers.status().is(HttpStatus.CREATED_201)).andReturn(); assertNotEquals(0, andReturn.getResponse().getContentLength()); assertNotNull(andReturn.getResponse().getHeader("x-VenID")); findByUsername = venService.findOneByUsername(andReturn.getResponse().getHeader("x-VenID")); assertNotNull(findByUsername); assertTrue(findByUsername.getReportOnly()); assertTrue(findByUsername.getXmlSignature()); venService.delete(findByUsername); }