HdfsServiceImpl implements HdfsService { @Override public void write(Path path, byte[] contents, String userMode, String groupMode, String otherMode) throws RestException { FSDataOutputStream fsDataOutputStream; try { FsPermission permission = null; boolean setPermissions = false; if(StringUtils.isNotEmpty(userMode) && StringUtils.isNotEmpty(groupMode) && StringUtils.isNotEmpty(otherMode)) { FsAction userAction = FsAction.getFsAction(userMode); FsAction groupAction = FsAction.getFsAction(groupMode); FsAction otherAction = FsAction.getFsAction(otherMode); if(userAction == null || groupAction == null || otherAction == null){ throw new RestException(String.format("Invalid permission set: user[%s] " + "group[%s] other[%s]", userAction, groupAction, otherAction)); } permission = new FsPermission(userAction, groupAction, otherAction); setPermissions = true; } fsDataOutputStream = getFileSystem().create(path, true); fsDataOutputStream.write(contents); fsDataOutputStream.close(); if(setPermissions) { getFileSystem().setPermission(path, permission); } } catch (IOException e) { throw new RestException(e); } } @Autowired  HdfsServiceImpl(Configuration configuration); @Override List<String> list(Path path); @Override String read(Path path); @Override void write(Path path, byte[] contents, String userMode, String groupMode, String otherMode); @Override boolean delete(Path path, boolean recursive); @Override boolean mkdirs(Path path);  }
@Test public void writeShouldProperlyWriteContents() throws Exception { String contents = "contents"; hdfsService.write(new Path(testDir, "writeTest.txt"), contents.getBytes(UTF_8),null,null,null); assertEquals("contents", FileUtils.readFileToString(new File(testDir, "writeTest.txt"))); }
@Test public void writeShouldProperlyWriteContentsWithPermissions() throws Exception { String contents = "contents"; Path thePath = new Path(testDir,"writeTest2.txt"); hdfsService.write(thePath, contents.getBytes(UTF_8),"rw-","r-x","r--"); assertEquals("contents", FileUtils.readFileToString(new File(testDir, "writeTest2.txt"))); assertEquals(FileSystem.get(configuration).listStatus(thePath)[0].getPermission().toShort(), new FsPermission(FsAction.READ_WRITE,FsAction.READ_EXECUTE,FsAction.READ).toShort()); }