fileStagingService/src/main/java/com/ge/research/semtk/services/filestaging/FileStagingServiceRestController.java
@ApiOperation(
			value="Stage a file from a file store"
			)
	@CrossOrigin
	@RequestMapping(value="/stageFile", method= RequestMethod.POST)
	public JSONObject stageFile(@RequestBody FileStagingRequestBody requestBody, @RequestHeader HttpHeaders headers) {
		
		HeadersManager.setHeaders(headers);
		final String ENDPOINT_NAME = "stageFile";
		
		try {
			TableResultSet tableResultSet = new TableResultSet();
			
			try {			
				
				// info about the file to copy
				requestBody.validate();
				final String sourceFile = requestBody.getSourceFile();			// path/name of file to stage
				final String stageFilename = requestBody.getStageFilename();	// name for file when staged 
				final String jobId = requestBody.getJobId();
				
				// info about source/destination
				final String storeType = filestaging_prop.getStoreType();		// store to retrieve from (e.g. s3)
				final String stageDir = filestaging_prop.getStageDirectory();   // local directory in which the file will be staged, e.g. /mnt/isilon/location
				final String stageFilePath = stageDir + "/" + stageFilename;  	// TODO do this using API for correct slashing
				
				LocalLogger.logToStdOut("Stage file from " + storeType + ": " + sourceFile + " to " + stageFilePath);
				
				FileSystemConnector conn = null;
				if(storeType.equals(FileStagingProperties.STORETYPE_DIR)){ 
					conn = new DirectoryConnector(filestaging_prop.getDirectory());
					LocalLogger.logToStdOut(conn.toString());
				}else if(storeType.equals(FileStagingProperties.STORETYPE_S3)){ 
					conn = new S3Connector(filestaging_prop.getS3Region(), filestaging_prop.getS3Bucket());
					LocalLogger.logToStdOut(conn.toString()); 
				}else{
					throw new Exception("Cannot stage file from unsupported store type '" + storeType + "'");
				}
				
				// retrieve file
				conn.getObjectAsLocalFile(sourceFile, stageFilePath);
				
				// send file to Results Service
				String fileId = this.sendToResultsService(jobId, stageFilePath, stageFilename);
				LocalLogger.logToStdOut("File staged to fileId: " + fileId);
				
				// return staged file in a result set
				String[] cols = {Utility.COL_NAME_FILEID}; 
				String[] colTypes = {ResultType.BINARY_FILE_ID.getPrefixedName()};
				Table retTable = new Table(cols, colTypes, null);
				ArrayList<String> row = new ArrayList<String>();
				row.add(fileId);
				retTable.addRow(row);			
				tableResultSet.addResults(retTable);
				tableResultSet.setSuccess(true);
	
			} catch (Exception e) {
				LocalLogger.printStackTrace(e);
				tableResultSet.setSuccess(false);
				tableResultSet.addRationaleMessage(SERVICE_NAME, ENDPOINT_NAME, e.getMessage());
			} 
			
			return tableResultSet.toJson();
		    
		} finally {
	    	HeadersManager.clearHeaders();
	    }
	}
/**
	 * Stage a file: copy from a file store (e.g. S3) to an accessible location, and then send it to the ResultsService.
	 */
Stage a file: copy from a file store  to an accessible location, and then send it to the ResultsService.
Stage a file : copy from a file store to an accessible location and then send it to the ResultsService .
@ApiOperation(
			value="Stage a file from a file store"
			)
	@CrossOrigin
	@RequestMapping(value="/stageFile", method= RequestMethod.POST)
	public JSONObject stageFile(@RequestBody FileStagingRequestBody requestBody, @RequestHeader HttpHeaders headers) {
		HeadersManager.setHeaders(headers);
		final String ENDPOINT_NAME = "stageFile";
		try {
			TableResultSet tableResultSet = new TableResultSet();
			try {			
				requestBody.validate();
				final String sourceFile = requestBody.getSourceFile();			
				final String stageFilename = requestBody.getStageFilename();	
				final String jobId = requestBody.getJobId();
				final String storeType = filestaging_prop.getStoreType();		
				final String stageDir = filestaging_prop.getStageDirectory();   
				final String stageFilePath = stageDir + "/" + stageFilename;  	
				LocalLogger.logToStdOut("Stage file from " + storeType + ": " + sourceFile + " to " + stageFilePath);
				FileSystemConnector conn = null;
				if(storeType.equals(FileStagingProperties.STORETYPE_DIR)){ 
					conn = new DirectoryConnector(filestaging_prop.getDirectory());
					LocalLogger.logToStdOut(conn.toString());
				}else if(storeType.equals(FileStagingProperties.STORETYPE_S3)){ 
					conn = new S3Connector(filestaging_prop.getS3Region(), filestaging_prop.getS3Bucket());
					LocalLogger.logToStdOut(conn.toString()); 
				}else{
					throw new Exception("Cannot stage file from unsupported store type '" + storeType + "'");
				}
				conn.getObjectAsLocalFile(sourceFile, stageFilePath);
				String fileId = this.sendToResultsService(jobId, stageFilePath, stageFilename);
				LocalLogger.logToStdOut("File staged to fileId: " + fileId);
				String[] cols = {Utility.COL_NAME_FILEID}; 
				String[] colTypes = {ResultType.BINARY_FILE_ID.getPrefixedName()};
				Table retTable = new Table(cols, colTypes, null);
				ArrayList<String> row = new ArrayList<String>();
				row.add(fileId);
				retTable.addRow(row);			
				tableResultSet.addResults(retTable);
				tableResultSet.setSuccess(true);
			} catch (Exception e) {
				LocalLogger.printStackTrace(e);
				tableResultSet.setSuccess(false);
				tableResultSet.addRationaleMessage(SERVICE_NAME, ENDPOINT_NAME, e.getMessage());
			} 
			return tableResultSet.toJson();
		} finally {
	    	HeadersManager.clearHeaders();
	    }
	}
@ ApiOperation ( value = "Stage a file from a file store" ) @ CrossOrigin @ RequestMapping ( value = "/stageFile" , method = RequestMethod . POST ) public JSONObject stageFile ( @ RequestBody FileStagingRequestBody requestBody , @ RequestHeader HttpHeaders headers ) { HeadersManager . setHeaders ( headers ) ; final String ENDPOINT_NAME = "stageFile" ; try { TableResultSet tableResultSet = new TableResultSet ( ) ; try { requestBody . validate ( ) ; final String sourceFile = requestBody . getSourceFile ( ) ; final String stageFilename = requestBody . getStageFilename ( ) ; final String jobId = requestBody . getJobId ( ) ; final String storeType = filestaging_prop . getStoreType ( ) ; final String stageDir = filestaging_prop . getStageDirectory ( ) ; final String stageFilePath = stageDir + "/" + stageFilename ; LocalLogger . logToStdOut ( "Stage file from " + storeType + ": " + sourceFile + " to " + stageFilePath ) ; FileSystemConnector conn = null ; if ( storeType . equals ( FileStagingProperties . STORETYPE_DIR ) ) { conn = new DirectoryConnector ( filestaging_prop . getDirectory ( ) ) ; LocalLogger . logToStdOut ( conn . toString ( ) ) ; } else if ( storeType . equals ( FileStagingProperties . STORETYPE_S3 ) ) { conn = new S3Connector ( filestaging_prop . getS3Region ( ) , filestaging_prop . getS3Bucket ( ) ) ; LocalLogger . logToStdOut ( conn . toString ( ) ) ; } else { throw new Exception ( "Cannot stage file from unsupported store type '" + storeType + "'" ) ; } conn . getObjectAsLocalFile ( sourceFile , stageFilePath ) ; String fileId = this . sendToResultsService ( jobId , stageFilePath , stageFilename ) ; LocalLogger . logToStdOut ( "File staged to fileId: " + fileId ) ; String [ ] cols = { Utility . COL_NAME_FILEID } ; String [ ] colTypes = { ResultType . BINARY_FILE_ID . getPrefixedName ( ) } ; Table retTable = new Table ( cols , colTypes , null ) ; ArrayList < String > row = new ArrayList < String > ( ) ; row . add ( fileId ) ; retTable . addRow ( row ) ; tableResultSet . addResults ( retTable ) ; tableResultSet . setSuccess ( true ) ; } catch ( Exception e ) { LocalLogger . printStackTrace ( e ) ; tableResultSet . setSuccess ( false ) ; tableResultSet . addRationaleMessage ( SERVICE_NAME , ENDPOINT_NAME , e . getMessage ( ) ) ; } return tableResultSet . toJson ( ) ; } finally { HeadersManager . clearHeaders ( ) ; } }
Stage a file: copy from a file store (e.g.
Stage a file : copy from a file store ( e . g .
// info about the file to copy // path/name of file to stage // name for file when staged  // info about source/destination // store to retrieve from (e.g. s3) // local directory in which the file will be staged, e.g. /mnt/isilon/location // TODO do this using API for correct slashing // retrieve file // send file to Results Service // return staged file in a result set