@Test public void copyShouldNotRemoveItem() throws OneDriveException, RefreshAccessException { DriveItem testFolder = addThreeFolders().get(0); String parentId = testFolder.id; DriveItem testFile = addThreeFiles().get(0); String testFileId = testFile.id; String testFileName = testFile.name; oneDriveAPI.copy(parentId, testFileName, testFileId, true); boolean isFilePresent = graphClient.me() .drive() .root() .children() .buildRequest() .get() .getCurrentPage() .stream() .anyMatch(item -> item.id.equals(testFileId)); assertTrue(isFilePresent); }
public DriveItem copy(String parentId, String fileName, String fileId, boolean isFile) throws RefreshAccessException, OneDriveException { if (LOG.isDebugEnabled()) { LOG.debug(">> copy {}->{}/{}", fileName, parentId, fileName); } String request = "{\n" + " \"parentReference\" : {\n" + " \"id\" : \"" + parentId + "\"\n" + " },\n" + " \"name\" : \"" + fileName + "\"\n" + " }"; HttpPost httppost = new HttpPost("https: StringEntity stringEntity = new StringEntity(request, "UTF-8"); httppost.setEntity(stringEntity); httppost.addHeader("Authorization", "Bearer " + getAccessToken()); httppost.addHeader("Content-type", "application/json"); String location; try (CloseableHttpResponse response = (CloseableHttpResponse) httpclient.execute(httppost)) { location = response.getHeaders("Location")[0].getValue(); } catch (IOException e) { throw new OneDriveException(e); } if (LOG.isDebugEnabled()) { LOG.debug("Location:= " + location); } JsonParser parser = new JsonParser(); String status; String responseBody; do { try { responseBody = getCopyResponseBody(location); } catch (IOException e) { throw new OneDriveException("Unable to retrieve copy response body for " + fileName, e); } status = parser.parse(responseBody.trim()).getAsJsonObject().get("status").getAsString(); try { Thread.sleep(1000); } catch (InterruptedException e) { LOG.warn("Thread interrupted while sleeping on copy of " + fileName, e); } if (LOG.isDebugEnabled()) { LOG.debug(">> Copy status: {} for {}", status, fileName); } } while (!status.equalsIgnoreCase("failed") && !status.equalsIgnoreCase("completed")); if (status.equalsIgnoreCase("failed")) { Set<String> fileNames = getFiles(parentId, isFile ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName).stream() .filter((item) -> isFile == (item.file != null)) .map((item) -> item.name) .collect(Collectors.toSet()); if (!fileNames.contains(fileName)) { throw new OneDriveException("Error copying file " + fileName + "[" + fileId + "] to parent " + parentId); } String newItemName = generateItemName(fileName, 1, isFile); for (int i = 2; i <= fileNames.size() && fileNames.contains(newItemName); i++) { newItemName = generateItemName(fileName, i, isFile); } return copy(parentId, newItemName, fileId, isFile); } else { String resourceId = parser.parse(responseBody).getAsJsonObject().get("resourceId").getAsString(); return getItem(resourceId); } }
OneDriveAPI { public DriveItem copy(String parentId, String fileName, String fileId, boolean isFile) throws RefreshAccessException, OneDriveException { if (LOG.isDebugEnabled()) { LOG.debug(">> copy {}->{}/{}", fileName, parentId, fileName); } String request = "{\n" + " \"parentReference\" : {\n" + " \"id\" : \"" + parentId + "\"\n" + " },\n" + " \"name\" : \"" + fileName + "\"\n" + " }"; HttpPost httppost = new HttpPost("https: StringEntity stringEntity = new StringEntity(request, "UTF-8"); httppost.setEntity(stringEntity); httppost.addHeader("Authorization", "Bearer " + getAccessToken()); httppost.addHeader("Content-type", "application/json"); String location; try (CloseableHttpResponse response = (CloseableHttpResponse) httpclient.execute(httppost)) { location = response.getHeaders("Location")[0].getValue(); } catch (IOException e) { throw new OneDriveException(e); } if (LOG.isDebugEnabled()) { LOG.debug("Location:= " + location); } JsonParser parser = new JsonParser(); String status; String responseBody; do { try { responseBody = getCopyResponseBody(location); } catch (IOException e) { throw new OneDriveException("Unable to retrieve copy response body for " + fileName, e); } status = parser.parse(responseBody.trim()).getAsJsonObject().get("status").getAsString(); try { Thread.sleep(1000); } catch (InterruptedException e) { LOG.warn("Thread interrupted while sleeping on copy of " + fileName, e); } if (LOG.isDebugEnabled()) { LOG.debug(">> Copy status: {} for {}", status, fileName); } } while (!status.equalsIgnoreCase("failed") && !status.equalsIgnoreCase("completed")); if (status.equalsIgnoreCase("failed")) { Set<String> fileNames = getFiles(parentId, isFile ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName).stream() .filter((item) -> isFile == (item.file != null)) .map((item) -> item.name) .collect(Collectors.toSet()); if (!fileNames.contains(fileName)) { throw new OneDriveException("Error copying file " + fileName + "[" + fileId + "] to parent " + parentId); } String newItemName = generateItemName(fileName, 1, isFile); for (int i = 2; i <= fileNames.size() && fileNames.contains(newItemName); i++) { newItemName = generateItemName(fileName, i, isFile); } return copy(parentId, newItemName, fileId, isFile); } else { String resourceId = parser.parse(responseBody).getAsJsonObject().get("resourceId").getAsString(); return getItem(resourceId); } } }
OneDriveAPI { public DriveItem copy(String parentId, String fileName, String fileId, boolean isFile) throws RefreshAccessException, OneDriveException { if (LOG.isDebugEnabled()) { LOG.debug(">> copy {}->{}/{}", fileName, parentId, fileName); } String request = "{\n" + " \"parentReference\" : {\n" + " \"id\" : \"" + parentId + "\"\n" + " },\n" + " \"name\" : \"" + fileName + "\"\n" + " }"; HttpPost httppost = new HttpPost("https: StringEntity stringEntity = new StringEntity(request, "UTF-8"); httppost.setEntity(stringEntity); httppost.addHeader("Authorization", "Bearer " + getAccessToken()); httppost.addHeader("Content-type", "application/json"); String location; try (CloseableHttpResponse response = (CloseableHttpResponse) httpclient.execute(httppost)) { location = response.getHeaders("Location")[0].getValue(); } catch (IOException e) { throw new OneDriveException(e); } if (LOG.isDebugEnabled()) { LOG.debug("Location:= " + location); } JsonParser parser = new JsonParser(); String status; String responseBody; do { try { responseBody = getCopyResponseBody(location); } catch (IOException e) { throw new OneDriveException("Unable to retrieve copy response body for " + fileName, e); } status = parser.parse(responseBody.trim()).getAsJsonObject().get("status").getAsString(); try { Thread.sleep(1000); } catch (InterruptedException e) { LOG.warn("Thread interrupted while sleeping on copy of " + fileName, e); } if (LOG.isDebugEnabled()) { LOG.debug(">> Copy status: {} for {}", status, fileName); } } while (!status.equalsIgnoreCase("failed") && !status.equalsIgnoreCase("completed")); if (status.equalsIgnoreCase("failed")) { Set<String> fileNames = getFiles(parentId, isFile ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName).stream() .filter((item) -> isFile == (item.file != null)) .map((item) -> item.name) .collect(Collectors.toSet()); if (!fileNames.contains(fileName)) { throw new OneDriveException("Error copying file " + fileName + "[" + fileId + "] to parent " + parentId); } String newItemName = generateItemName(fileName, 1, isFile); for (int i = 2; i <= fileNames.size() && fileNames.contains(newItemName); i++) { newItemName = generateItemName(fileName, i, isFile); } return copy(parentId, newItemName, fileId, isFile); } else { String resourceId = parser.parse(responseBody).getAsJsonObject().get("resourceId").getAsString(); return getItem(resourceId); } } OneDriveAPI(String clientId, String clientSecret, String authCode, String redirectUrl);   OneDriveAPI(String clientId,
              String clientSecret,
              String accessToken,
              String refreshToken,
              long expirationTime,
              String redirectUrl); }
OneDriveAPI { public DriveItem copy(String parentId, String fileName, String fileId, boolean isFile) throws RefreshAccessException, OneDriveException { if (LOG.isDebugEnabled()) { LOG.debug(">> copy {}->{}/{}", fileName, parentId, fileName); } String request = "{\n" + " \"parentReference\" : {\n" + " \"id\" : \"" + parentId + "\"\n" + " },\n" + " \"name\" : \"" + fileName + "\"\n" + " }"; HttpPost httppost = new HttpPost("https: StringEntity stringEntity = new StringEntity(request, "UTF-8"); httppost.setEntity(stringEntity); httppost.addHeader("Authorization", "Bearer " + getAccessToken()); httppost.addHeader("Content-type", "application/json"); String location; try (CloseableHttpResponse response = (CloseableHttpResponse) httpclient.execute(httppost)) { location = response.getHeaders("Location")[0].getValue(); } catch (IOException e) { throw new OneDriveException(e); } if (LOG.isDebugEnabled()) { LOG.debug("Location:= " + location); } JsonParser parser = new JsonParser(); String status; String responseBody; do { try { responseBody = getCopyResponseBody(location); } catch (IOException e) { throw new OneDriveException("Unable to retrieve copy response body for " + fileName, e); } status = parser.parse(responseBody.trim()).getAsJsonObject().get("status").getAsString(); try { Thread.sleep(1000); } catch (InterruptedException e) { LOG.warn("Thread interrupted while sleeping on copy of " + fileName, e); } if (LOG.isDebugEnabled()) { LOG.debug(">> Copy status: {} for {}", status, fileName); } } while (!status.equalsIgnoreCase("failed") && !status.equalsIgnoreCase("completed")); if (status.equalsIgnoreCase("failed")) { Set<String> fileNames = getFiles(parentId, isFile ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName).stream() .filter((item) -> isFile == (item.file != null)) .map((item) -> item.name) .collect(Collectors.toSet()); if (!fileNames.contains(fileName)) { throw new OneDriveException("Error copying file " + fileName + "[" + fileId + "] to parent " + parentId); } String newItemName = generateItemName(fileName, 1, isFile); for (int i = 2; i <= fileNames.size() && fileNames.contains(newItemName); i++) { newItemName = generateItemName(fileName, i, isFile); } return copy(parentId, newItemName, fileId, isFile); } else { String resourceId = parser.parse(responseBody).getAsJsonObject().get("resourceId").getAsString(); return getItem(resourceId); } } OneDriveAPI(String clientId, String clientSecret, String authCode, String redirectUrl);   OneDriveAPI(String clientId,
              String clientSecret,
              String accessToken,
              String refreshToken,
              long expirationTime,
              String redirectUrl); SharingLink createLink(String itemId, String type); void updateToken(OneDriveStoredToken newToken); void removeFolder(String fileId); void removeFile(String fileId); User getUser(); synchronized String getRootId(); synchronized String getUserId(); DriveItem createFolder(String parentId, String name, Calendar created); DriveItem copyFile(String parentId, String fileName, String fileId); DriveItem copyFolder(String parentId, String name, String folderId); OneDriveStoredToken getStoredToken(); @Deprecated String getNotificationUrl(); Subscription getSubscription(); DriveItem copy(String parentId, String fileName, String fileId, boolean isFile); IDriveItemCollectionPage getDriveItemCollectionPage(String folderId); @Deprecated List<DriveItem> getFiles(String folderId); ChangesIterator changes(String deltaToken); String getInsertUploadUrl(String parentId, String name, String conflictBehavior); DriveItem insert(String parentId, String fileName, Calendar created, Calendar modified, InputStream inputStream, String conflictBehavior); DriveItem updateFileContent(String itemId, Calendar created, Calendar modified, InputStream inputStream); DeltaDriveFiles getAllFiles(); DriveItem updateFile(DriveItem driveItem); DriveItem getItem(String itemId); Drive getDrive(); String getBusinessPreviewLink(String itemId); SimpleChildIterator getSimpleChildIterator(Collection<HashSetCompatibleDriveItem> items); ChildIterator getChildIterator(String folderId); }
OneDriveAPI { public DriveItem copy(String parentId, String fileName, String fileId, boolean isFile) throws RefreshAccessException, OneDriveException { if (LOG.isDebugEnabled()) { LOG.debug(">> copy {}->{}/{}", fileName, parentId, fileName); } String request = "{\n" + " \"parentReference\" : {\n" + " \"id\" : \"" + parentId + "\"\n" + " },\n" + " \"name\" : \"" + fileName + "\"\n" + " }"; HttpPost httppost = new HttpPost("https: StringEntity stringEntity = new StringEntity(request, "UTF-8"); httppost.setEntity(stringEntity); httppost.addHeader("Authorization", "Bearer " + getAccessToken()); httppost.addHeader("Content-type", "application/json"); String location; try (CloseableHttpResponse response = (CloseableHttpResponse) httpclient.execute(httppost)) { location = response.getHeaders("Location")[0].getValue(); } catch (IOException e) { throw new OneDriveException(e); } if (LOG.isDebugEnabled()) { LOG.debug("Location:= " + location); } JsonParser parser = new JsonParser(); String status; String responseBody; do { try { responseBody = getCopyResponseBody(location); } catch (IOException e) { throw new OneDriveException("Unable to retrieve copy response body for " + fileName, e); } status = parser.parse(responseBody.trim()).getAsJsonObject().get("status").getAsString(); try { Thread.sleep(1000); } catch (InterruptedException e) { LOG.warn("Thread interrupted while sleeping on copy of " + fileName, e); } if (LOG.isDebugEnabled()) { LOG.debug(">> Copy status: {} for {}", status, fileName); } } while (!status.equalsIgnoreCase("failed") && !status.equalsIgnoreCase("completed")); if (status.equalsIgnoreCase("failed")) { Set<String> fileNames = getFiles(parentId, isFile ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName).stream() .filter((item) -> isFile == (item.file != null)) .map((item) -> item.name) .collect(Collectors.toSet()); if (!fileNames.contains(fileName)) { throw new OneDriveException("Error copying file " + fileName + "[" + fileId + "] to parent " + parentId); } String newItemName = generateItemName(fileName, 1, isFile); for (int i = 2; i <= fileNames.size() && fileNames.contains(newItemName); i++) { newItemName = generateItemName(fileName, i, isFile); } return copy(parentId, newItemName, fileId, isFile); } else { String resourceId = parser.parse(responseBody).getAsJsonObject().get("resourceId").getAsString(); return getItem(resourceId); } } OneDriveAPI(String clientId, String clientSecret, String authCode, String redirectUrl);   OneDriveAPI(String clientId,
              String clientSecret,
              String accessToken,
              String refreshToken,
              long expirationTime,
              String redirectUrl); SharingLink createLink(String itemId, String type); void updateToken(OneDriveStoredToken newToken); void removeFolder(String fileId); void removeFile(String fileId); User getUser(); synchronized String getRootId(); synchronized String getUserId(); DriveItem createFolder(String parentId, String name, Calendar created); DriveItem copyFile(String parentId, String fileName, String fileId); DriveItem copyFolder(String parentId, String name, String folderId); OneDriveStoredToken getStoredToken(); @Deprecated String getNotificationUrl(); Subscription getSubscription(); DriveItem copy(String parentId, String fileName, String fileId, boolean isFile); IDriveItemCollectionPage getDriveItemCollectionPage(String folderId); @Deprecated List<DriveItem> getFiles(String folderId); ChangesIterator changes(String deltaToken); String getInsertUploadUrl(String parentId, String name, String conflictBehavior); DriveItem insert(String parentId, String fileName, Calendar created, Calendar modified, InputStream inputStream, String conflictBehavior); DriveItem updateFileContent(String itemId, Calendar created, Calendar modified, InputStream inputStream); DeltaDriveFiles getAllFiles(); DriveItem updateFile(DriveItem driveItem); DriveItem getItem(String itemId); Drive getDrive(); String getBusinessPreviewLink(String itemId); SimpleChildIterator getSimpleChildIterator(Collection<HashSetCompatibleDriveItem> items); ChildIterator getChildIterator(String folderId); final static String SCOPES; }