@Test public void uploadReturnsErrorWhenPackageIsDuplicate() throws IOException { File file = _tempFolder.newFile(_testPackageName); MultipartFile mockFile = mock(MultipartFile.class); when(mockFile.getOriginalFilename()) .thenReturn(file.getName()); ResponseEntity<?> result = _controller.uploadComponentRest(mockFile); assertEquals(HttpStatus.CONFLICT, result.getStatusCode()); verifyNotSaved(mockFile); }
@RequestMapping(value = {"/components", "/rest/components"}, method = RequestMethod.POST) @ResponseBody public ResponseEntity<?> uploadComponentRest(@RequestParam("file") MultipartFile file) { return withWriteLock(() -> { String componentPackageName = file.getOriginalFilename(); Path uploadFileDestinationPath = Paths.get( _propertiesUtil.getUploadedComponentsDirectory().getAbsolutePath(), componentPackageName); if (Files.exists(uploadFileDestinationPath)) { String errorMsg = "a component with the same file name has already been uploaded"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.CONFLICT); } String contentType = file.getContentType(); if (contentType == null) { String errorMsg = "Content type was not provided for uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!acceptableComponentContentTypes.contains(contentType.toLowerCase())) { String errorMsg = "Uploaded file content type is not application/gzip"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!componentPackageName.toLowerCase().endsWith(".tar.gz")) { String errorMsg = "Uploaded file does not have the .tar.gz extension"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } try { file.transferTo(uploadFileDestinationPath.toFile()); _componentState.addEntryForUploadedPackage(uploadFileDestinationPath); return ResponseEntity.ok(Collections.singletonMap("success", componentPackageName + " uploaded")); } catch (IOException ex) { String errorMsg = "An error occurred while saving uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.INTERNAL_SERVER_ERROR, ex); } }); }
AdminComponentRegistrationController extends BasicAdminComponentRegistrationController { @RequestMapping(value = {"/components", "/rest/components"}, method = RequestMethod.POST) @ResponseBody public ResponseEntity<?> uploadComponentRest(@RequestParam("file") MultipartFile file) { return withWriteLock(() -> { String componentPackageName = file.getOriginalFilename(); Path uploadFileDestinationPath = Paths.get( _propertiesUtil.getUploadedComponentsDirectory().getAbsolutePath(), componentPackageName); if (Files.exists(uploadFileDestinationPath)) { String errorMsg = "a component with the same file name has already been uploaded"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.CONFLICT); } String contentType = file.getContentType(); if (contentType == null) { String errorMsg = "Content type was not provided for uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!acceptableComponentContentTypes.contains(contentType.toLowerCase())) { String errorMsg = "Uploaded file content type is not application/gzip"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!componentPackageName.toLowerCase().endsWith(".tar.gz")) { String errorMsg = "Uploaded file does not have the .tar.gz extension"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } try { file.transferTo(uploadFileDestinationPath.toFile()); _componentState.addEntryForUploadedPackage(uploadFileDestinationPath); return ResponseEntity.ok(Collections.singletonMap("success", componentPackageName + " uploaded")); } catch (IOException ex) { String errorMsg = "An error occurred while saving uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.INTERNAL_SERVER_ERROR, ex); } }); } }
AdminComponentRegistrationController extends BasicAdminComponentRegistrationController { @RequestMapping(value = {"/components", "/rest/components"}, method = RequestMethod.POST) @ResponseBody public ResponseEntity<?> uploadComponentRest(@RequestParam("file") MultipartFile file) { return withWriteLock(() -> { String componentPackageName = file.getOriginalFilename(); Path uploadFileDestinationPath = Paths.get( _propertiesUtil.getUploadedComponentsDirectory().getAbsolutePath(), componentPackageName); if (Files.exists(uploadFileDestinationPath)) { String errorMsg = "a component with the same file name has already been uploaded"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.CONFLICT); } String contentType = file.getContentType(); if (contentType == null) { String errorMsg = "Content type was not provided for uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!acceptableComponentContentTypes.contains(contentType.toLowerCase())) { String errorMsg = "Uploaded file content type is not application/gzip"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!componentPackageName.toLowerCase().endsWith(".tar.gz")) { String errorMsg = "Uploaded file does not have the .tar.gz extension"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } try { file.transferTo(uploadFileDestinationPath.toFile()); _componentState.addEntryForUploadedPackage(uploadFileDestinationPath); return ResponseEntity.ok(Collections.singletonMap("success", componentPackageName + " uploaded")); } catch (IOException ex) { String errorMsg = "An error occurred while saving uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.INTERNAL_SERVER_ERROR, ex); } }); } @Inject  AdminComponentRegistrationController(
            PropertiesUtil propertiesUtil,
            AddComponentService addComponentService,
            RemoveComponentService removeComponentService,
            ComponentStateService componentState,
            ComponentReRegisterService reRegisterService); }
AdminComponentRegistrationController extends BasicAdminComponentRegistrationController { @RequestMapping(value = {"/components", "/rest/components"}, method = RequestMethod.POST) @ResponseBody public ResponseEntity<?> uploadComponentRest(@RequestParam("file") MultipartFile file) { return withWriteLock(() -> { String componentPackageName = file.getOriginalFilename(); Path uploadFileDestinationPath = Paths.get( _propertiesUtil.getUploadedComponentsDirectory().getAbsolutePath(), componentPackageName); if (Files.exists(uploadFileDestinationPath)) { String errorMsg = "a component with the same file name has already been uploaded"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.CONFLICT); } String contentType = file.getContentType(); if (contentType == null) { String errorMsg = "Content type was not provided for uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!acceptableComponentContentTypes.contains(contentType.toLowerCase())) { String errorMsg = "Uploaded file content type is not application/gzip"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!componentPackageName.toLowerCase().endsWith(".tar.gz")) { String errorMsg = "Uploaded file does not have the .tar.gz extension"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } try { file.transferTo(uploadFileDestinationPath.toFile()); _componentState.addEntryForUploadedPackage(uploadFileDestinationPath); return ResponseEntity.ok(Collections.singletonMap("success", componentPackageName + " uploaded")); } catch (IOException ex) { String errorMsg = "An error occurred while saving uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.INTERNAL_SERVER_ERROR, ex); } }); } @Inject  AdminComponentRegistrationController(
            PropertiesUtil propertiesUtil,
            AddComponentService addComponentService,
            RemoveComponentService removeComponentService,
            ComponentStateService componentState,
            ComponentReRegisterService reRegisterService); @RequestMapping(value = {"/components/{componentPackageFileName:.+}", "/rest/components/{componentPackageFileName:.+}"}, method = RequestMethod.GET) @ResponseBody ResponseEntity<RegisterComponentModel> getComponentRest(
            @PathVariable("componentPackageFileName") String componentPackageFileName); @RequestMapping(value = {"/components/{componentPackageFileName:.+}/register", "/rest/components/{componentPackageFileName:.+}/register"}, method = RequestMethod.POST) @ResponseBody ResponseEntity<?> registerComponentRest(
            @PathVariable("componentPackageFileName") String componentPackageFileName); @RequestMapping(value = {"/components", "/rest/components"}, method = RequestMethod.POST) @ResponseBody ResponseEntity<?> uploadComponentRest(@RequestParam("file") MultipartFile file); @RequestMapping(value = {"/component/registerViaFile", "/rest/component/registerViaFile"}, method = RequestMethod.GET) @ResponseBody ResponseMessage registerViaFileRest(
            /*@ApiParam(required = true, defaultValue = "",
                      value = "The path to the JSON component descriptor file")*/
            @RequestParam String filePath
    ); @RequestMapping(value = {"/component/unregisterViaFile", "/rest/component/unregisterViaFile"}, method = RequestMethod.GET) @ResponseBody ResponseMessage unregisterViaFileRest(
            /*@ApiParam(required = true, value = "The path to the JSON component descriptor file")*/
            @RequestParam String filePath,
            @RequestParam(required = false, defaultValue = "true") boolean deletePackage); @RequestMapping(value = {"/components/packages/{componentPackageFileName:.+}/", "/rest/components/packages/{componentPackageFileName:.+}/"}, method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.NO_CONTENT) @ResponseBody void removeComponentPackageRest(
            @PathVariable("componentPackageFileName") String componentPackageFileName); @RequestMapping(value = {"/components/{componentPackageFileName:.+}/reRegister", "/rest/components/{componentPackageFileName:.+}/reRegister"}, method = RequestMethod.POST) @ResponseBody ResponseEntity<?> reRegisterRest(@PathVariable("componentPackageFileName") String componentPackageFileName); }
AdminComponentRegistrationController extends BasicAdminComponentRegistrationController { @RequestMapping(value = {"/components", "/rest/components"}, method = RequestMethod.POST) @ResponseBody public ResponseEntity<?> uploadComponentRest(@RequestParam("file") MultipartFile file) { return withWriteLock(() -> { String componentPackageName = file.getOriginalFilename(); Path uploadFileDestinationPath = Paths.get( _propertiesUtil.getUploadedComponentsDirectory().getAbsolutePath(), componentPackageName); if (Files.exists(uploadFileDestinationPath)) { String errorMsg = "a component with the same file name has already been uploaded"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.CONFLICT); } String contentType = file.getContentType(); if (contentType == null) { String errorMsg = "Content type was not provided for uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!acceptableComponentContentTypes.contains(contentType.toLowerCase())) { String errorMsg = "Uploaded file content type is not application/gzip"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } if (!componentPackageName.toLowerCase().endsWith(".tar.gz")) { String errorMsg = "Uploaded file does not have the .tar.gz extension"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.BAD_REQUEST); } try { file.transferTo(uploadFileDestinationPath.toFile()); _componentState.addEntryForUploadedPackage(uploadFileDestinationPath); return ResponseEntity.ok(Collections.singletonMap("success", componentPackageName + " uploaded")); } catch (IOException ex) { String errorMsg = "An error occurred while saving uploaded file"; return handleRegistrationErrorResponse(componentPackageName, errorMsg, HttpStatus.INTERNAL_SERVER_ERROR, ex); } }); } @Inject  AdminComponentRegistrationController(
            PropertiesUtil propertiesUtil,
            AddComponentService addComponentService,
            RemoveComponentService removeComponentService,
            ComponentStateService componentState,
            ComponentReRegisterService reRegisterService); @RequestMapping(value = {"/components/{componentPackageFileName:.+}", "/rest/components/{componentPackageFileName:.+}"}, method = RequestMethod.GET) @ResponseBody ResponseEntity<RegisterComponentModel> getComponentRest(
            @PathVariable("componentPackageFileName") String componentPackageFileName); @RequestMapping(value = {"/components/{componentPackageFileName:.+}/register", "/rest/components/{componentPackageFileName:.+}/register"}, method = RequestMethod.POST) @ResponseBody ResponseEntity<?> registerComponentRest(
            @PathVariable("componentPackageFileName") String componentPackageFileName); @RequestMapping(value = {"/components", "/rest/components"}, method = RequestMethod.POST) @ResponseBody ResponseEntity<?> uploadComponentRest(@RequestParam("file") MultipartFile file); @RequestMapping(value = {"/component/registerViaFile", "/rest/component/registerViaFile"}, method = RequestMethod.GET) @ResponseBody ResponseMessage registerViaFileRest(
            /*@ApiParam(required = true, defaultValue = "",
                      value = "The path to the JSON component descriptor file")*/
            @RequestParam String filePath
    ); @RequestMapping(value = {"/component/unregisterViaFile", "/rest/component/unregisterViaFile"}, method = RequestMethod.GET) @ResponseBody ResponseMessage unregisterViaFileRest(
            /*@ApiParam(required = true, value = "The path to the JSON component descriptor file")*/
            @RequestParam String filePath,
            @RequestParam(required = false, defaultValue = "true") boolean deletePackage); @RequestMapping(value = {"/components/packages/{componentPackageFileName:.+}/", "/rest/components/packages/{componentPackageFileName:.+}/"}, method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.NO_CONTENT) @ResponseBody void removeComponentPackageRest(
            @PathVariable("componentPackageFileName") String componentPackageFileName); @RequestMapping(value = {"/components/{componentPackageFileName:.+}/reRegister", "/rest/components/{componentPackageFileName:.+}/reRegister"}, method = RequestMethod.POST) @ResponseBody ResponseEntity<?> reRegisterRest(@PathVariable("componentPackageFileName") String componentPackageFileName);  }