TargetResource implements ITargetResource { @Override public Response loadTargetArchive( InputStream uploadedInputStream, FormDataContentDisposition fileDetail ) { this.logger.fine( "Request: load targets from an uploaded ZIP file (" + fileDetail.getFileName() + ")." ); File tempZipFile = new File( System.getProperty( "java.io.tmpdir" ), fileDetail.getFileName()); final Set<String> newTargetIds = new HashSet<> (); File dir = null; Response response; try { Utils.copyStream( uploadedInputStream, tempZipFile ); String projectName = fileDetail.getFileName().replace( ".zip", "" ); dir = new File( System.getProperty( "java.io.tmpdir" ), "roboconf/" + projectName ); Utils.extractZipArchive( tempZipFile, dir ); List<ModelError> errors = TargetValidator.parseDirectory( dir ); if( RoboconfErrorHelpers.containsCriticalErrors( errors )) { response = handleError( Status.FORBIDDEN, new RestError( ErrorCode.REST_TARGET_CONTAINS_ERROR ), lang( this.manager )).build(); } else { response = Response.ok().build(); for( File f : Utils.listAllFiles( dir, Constants.FILE_EXT_PROPERTIES )) { String id = this.manager.targetsMngr().createTarget( f, null ); newTargetIds.add( id ); } } } catch( Exception e ) { response = handleError( Status.NOT_ACCEPTABLE, new RestError( ErrorCode.REST_IO_ERROR, e ), lang( this.manager )).build(); this.logger.fine( "An error occurred while deploying targets. Unregistering those that were in the same archive." ); for( String id : newTargetIds ) { try { this.manager.targetsMngr().deleteTarget( id ); } catch( Exception e1 ) { Utils.logException( this.logger, e1 ); this.logger.severe( "Target " + id + " could not be deleted." ); } } } finally { Utils.closeQuietly( uploadedInputStream ); Utils.deleteFilesRecursivelyAndQuietly( dir ); Utils.deleteFilesRecursivelyAndQuietly( tempZipFile ); } return response; } TargetResource( Manager manager ); @Override List<TargetWrapperDescriptor> listTargets( String name, String qualifier ); @Override Response createOrUpdateTarget( String rawProperties, String targetId ); @Override Response loadTargetArchive( InputStream uploadedInputStream, FormDataContentDisposition fileDetail ); @Override Response deleteTarget( String targetId ); @Override Response getTargetProperties( String targetId ); @Override Response findTargetById( String targetId ); @Override Response associateTarget(
			String name,
			String qualifier,
			String instancePathOrComponentName,
			String targetId,
			boolean bind ); @Override Response updateHint( String name, String qualifier, String targetId, boolean bind ); @Override List<TargetUsageItem> findUsageStatistics( String targetId );  }
@Test public void testLoadTargetArchive_ok() throws Exception { Map<String,String> entryToContent = new HashMap<> (); entryToContent.put( "t1.properties", "id: tid-1\nhandler: h" ); entryToContent.put( "t2.properties", "id: tid-2\nhandler: h\nname: my main target" ); File targetFile = this.folder.newFile( "roboconf_targets.zip" ); TestUtils.createZipFile( entryToContent, targetFile ); Assert.assertTrue( targetFile.exists()); Assert.assertEquals( 0, this.manager.targetsMngr().listAllTargets().size()); InputStream in = null; try { FormDataContentDisposition fd = FormDataContentDisposition .name( targetFile.getName()) .fileName( targetFile.getName()).build(); in = new FileInputStream( targetFile ); Assert.assertEquals( Status.OK.getStatusCode(), this.resource.loadTargetArchive( in, fd ).getStatus()); } finally { Utils.closeQuietly( in ); } List<TargetWrapperDescriptor> targetIds = this.manager.targetsMngr().listAllTargets(); Assert.assertEquals( 2, targetIds.size()); Assert.assertEquals( "tid-1", targetIds.get( 0 ).getId()); Assert.assertEquals( "h", targetIds.get( 0 ).getHandler()); Assert.assertNull( targetIds.get( 0 ).getName()); Assert.assertNull( targetIds.get( 0 ).getDescription()); Assert.assertEquals( "tid-2", targetIds.get( 1 ).getId()); Assert.assertEquals( "h", targetIds.get( 1 ).getHandler()); Assert.assertEquals( "my main target", targetIds.get( 1 ).getName()); Assert.assertNull( targetIds.get( 1 ).getDescription()); }
@Test public void testLoadTargetArchive_conflictingTarget_withRevert() throws Exception { Map<String,String> entryToContent = new HashMap<> (); entryToContent.put( "t1.properties", "id: tid-1\nhandler: h" ); entryToContent.put( "t2.properties", "id: tid-2\n\nhandler: h\nnname: my main target" ); entryToContent.put( "t3.properties", "id: tid-3\nhandler: h\nname: my main target" ); File targetFile = this.folder.newFile( "roboconf_targets.zip" ); TestUtils.createZipFile( entryToContent, targetFile ); Assert.assertTrue( targetFile.exists()); Assert.assertNotNull( this.manager.targetsMngr().createTarget( "id: tid-2\nhandler: handler" )); Assert.assertEquals( 1, this.manager.targetsMngr().listAllTargets().size()); InputStream in = null; try { FormDataContentDisposition fd = FormDataContentDisposition .name( targetFile.getName()) .fileName( targetFile.getName()).build(); in = new FileInputStream( targetFile ); Assert.assertEquals( Status.NOT_ACCEPTABLE.getStatusCode(), this.resource.loadTargetArchive( in, fd ).getStatus()); } finally { Utils.closeQuietly( in ); } List<TargetWrapperDescriptor> targetIds = this.manager.targetsMngr().listAllTargets(); Assert.assertEquals( 1, targetIds.size()); }
@Test public void testLoadTargetArchive_invalidTarget() throws Exception { Map<String,String> entryToContent = new HashMap<> (); entryToContent.put( "t1.properties", "id: tid-1\nhandler: h" ); entryToContent.put( "t2.properties", "id: tid-2\n\nnname: my main target" ); entryToContent.put( "t3.properties", "id: tid-3\nhandler: h\nname: my main target" ); File targetFile = this.folder.newFile( "roboconf_targets.zip" ); TestUtils.createZipFile( entryToContent, targetFile ); Assert.assertTrue( targetFile.exists()); Assert.assertEquals( 0, this.manager.targetsMngr().listAllTargets().size()); InputStream in = null; try { FormDataContentDisposition fd = FormDataContentDisposition .name( targetFile.getName()) .fileName( targetFile.getName()).build(); in = new FileInputStream( targetFile ); Assert.assertEquals( Status.FORBIDDEN.getStatusCode(), this.resource.loadTargetArchive( in, fd ).getStatus()); } finally { Utils.closeQuietly( in ); } List<TargetWrapperDescriptor> targetIds = this.manager.targetsMngr().listAllTargets(); Assert.assertEquals( 0, targetIds.size()); }