Example usage for org.springframework.web.client HttpStatusCodeException getStatusCode

List of usage examples for org.springframework.web.client HttpStatusCodeException getStatusCode

Introduction

In this page you can find the example usage for org.springframework.web.client HttpStatusCodeException getStatusCode.

Prototype

public HttpStatus getStatusCode() 

Source Link

Document

Return the HTTP status code.

Usage

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

public GoogleUserProfile getGoogleUserProfile()
        throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException {
    log.debug("Get Google Docs user metadata");
    UserInfoOperations userInfoOperations = getConnection().getApi().userOperations();

    GoogleUserProfile googleUserProfile = null;

    try {/*  w w  w .j  av  a 2  s .  c  om*/
        googleUserProfile = userInfoOperations.getUserProfile();
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce.getStatusCode().value());
    }

    return googleUserProfile;
}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

/**
 * Delete Google Drive Folder//from   w  w w .ja v a  2s .c o m
 * 
 * @param folderId
 * @throws GoogleDocsAuthenticationException
 * @throws GoogleDocsRefreshTokenException
 * @throws GoogleDocsServiceException
 */
private void deleteFolder(String folderId)
        throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException {
    DriveOperations driveOperations = getDriveOperations(getConnection());

    try {
        driveOperations.delete(folderId);
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce, hsce.getStatusCode().value());
    }
}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

public FileRevision getLatestRevision(DriveFile driveFile)
        throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException {
    FileRevision fileRevision = null;/*from www  . j  a v  a2 s.  co m*/

    DriveOperations driveOperations = getDriveOperations(getConnection());

    List<FileRevision> fileRevisions = driveOperations.getRevisions(driveFile.getId());

    try {
        if (fileRevisions != null) {
            Collections.sort(fileRevisions, new FileRevisionComparator());

            fileRevision = fileRevisions.get(fileRevisions.size() - 1);
        }
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce.getStatusCode().value());
    }

    return fileRevision;
}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

/**
 * Create new folder in Google Drive/*from w w  w  . j  a  v a 2 s  . c  om*/
 * 
 * @param parentId
 * @param folderName
 * @return
 * @throws GoogleDocsServiceException
 * @throws GoogleDocsAuthenticationException
 * @throws GoogleDocsRefreshTokenException
 */
private DriveFile createFolder(String parentId, String folderName)
        throws GoogleDocsServiceException, GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException {
    DriveFile driveFile = null;
    DriveOperations driveOperations = getDriveOperations(getConnection());

    try {
        driveFile = driveOperations.createFolder(parentId, folderName);
        driveFile = driveOperations.hide(driveFile.getId());
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce, hsce.getStatusCode().value());
    }

    return driveFile;
}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

public boolean deleteContent(NodeRef nodeRef, DriveFile driveFile)
        throws GoogleDocsAuthenticationException, GoogleDocsServiceException, GoogleDocsRefreshTokenException {
    log.debug("Delete Google Doc for " + nodeRef);
    boolean deleted = false;

    DriveOperations driveOperations = getDriveOperations(getConnection());

    try {/* www .jav a 2s .  c o  m*/
        driveOperations.delete(driveFile.getId());

        // Delete the Working directory in Google Drive (if it exists....this should handle any migration issues)
        deleteWorkingDirectory(nodeRef);

        unDecorateNode(nodeRef);
        deleted = true;
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce.getStatusCode().value());
    }

    log.debug("Deleted: " + deleted);
    return deleted;
}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

public FileRevision getLatestRevision(NodeRef nodeRef)
        throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException {
    FileRevision fileRevision = null;//from  w ww .j  a v a  2s.c o m

    DriveOperations driveOperations = getDriveOperations(getConnection());

    try {
        if (nodeService.getProperty(nodeRef, GoogleDocsModel.PROP_RESOURCE_ID) != null) {
            List<FileRevision> fileRevisions = driveOperations.getRevisions(
                    nodeService.getProperty(nodeRef, GoogleDocsModel.PROP_RESOURCE_ID).toString());

            if (fileRevisions != null) {
                Collections.sort(fileRevisions, new FileRevisionComparator());

                fileRevision = fileRevisions.get(fileRevisions.size() - 1);
            }
        }
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce.getStatusCode().value());
    }

    return fileRevision;
}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

private List<DriveFile> getFolder(String parentId, String folderName)
        throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException {

    List<DriveFile> driveFiles = new ArrayList<DriveFile>();
    DriveFilesPage page = null;/*from   ww  w.j  a va2 s. c o m*/
    DriveOperations driveOperations = getDriveOperations(getConnection());

    try {
        do {
            if (page == null) {
                page = driveOperations.driveFileQuery().titleIs(folderName).isFolder().getPage();
            } else {
                page = driveOperations.driveFileQuery().fromPage(page.getNextPageToken()).getPage();
            }

            List<DriveFile> childfolders = page.getItems();
            if (childfolders != null && !childfolders.isEmpty()) {
                driveFiles.addAll(childfolders);
            }
        } while (page.getNextPageToken() != null);
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce, hsce.getStatusCode().value());
    }

    return driveFiles;
}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

public DriveFile uploadFile(NodeRef nodeRef) throws GoogleDocsAuthenticationException,
        GoogleDocsServiceException, GoogleDocsRefreshTokenException, IOException {
    log.debug("Upload " + nodeRef + " to Google");
    DriveOperations driveOperations = getDriveOperations(getConnection());

    DriveFile driveFile = null;/*from  w ww.  j a  v  a  2s .  com*/

    // It makes me want to cry that they don't support inputStreams.
    File file = null;

    try {
        // Get the reader
        ContentReader reader = fileFolderService.getReader(nodeRef);

        file = File.createTempFile(nodeRef.getId(), ".tmp", TempFileProvider.getTempDir());
        reader.getContent(file);

        // Get the mimetype
        FileInfo fileInfo = fileFolderService.getFileInfo(nodeRef);
        String mimetype = fileInfo.getContentData().getMimetype();

        // Create the working Directory
        DriveFile workingDir = createWorkingDirectory(nodeRef);

        driveFile = new DriveFile.Builder().setParents(workingDir.getId()).setTitle(fileInfo.getName())
                .setHidden(true).setMimeType(mimetype).build();

        UploadParameters uploadParameters = new UploadParameters().setConvert(true);

        driveFile = driveOperations.upload(new FileSystemResource(file), driveFile, uploadParameters);

    } catch (IOException ioe) {
        throw ioe;
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce.getStatusCode().value());
    } finally {
        if (file != null) {
            file.delete();
        }
    }

    return driveFile;
}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

/**
 * Get the Document from the users Google Drive account. The Document and its working directory will be removed from their
 * Google Drive account. The editingInGoogle aspect will be removed.
 * //w  ww .  ja  v  a 2s .  co m
 * @param nodeRef
 * @param resourceID
 * @throws GoogleDocsAuthenticationException
 * @throws GoogleDocsServiceException
 * @throws GoogleDocsRefreshTokenException
 */
private void getDocument(NodeRef nodeRef, String resourceID, boolean removeFromDrive)
        throws GoogleDocsAuthenticationException, GoogleDocsServiceException, IOException,
        GoogleDocsRefreshTokenException {
    log.debug("Get Google Document for node: " + nodeRef);
    DriveOperations driveOperations = getDriveOperations(getConnection());

    try {
        String mimetype = null;

        mimetype = validateMimeType(fileFolderService.getFileInfo(nodeRef).getContentData().getMimetype());
        log.debug("Current mimetype: " + fileFolderService.getFileInfo(nodeRef).getContentData().getMimetype()
                + "; Mimetype of Google Doc: " + mimetype);
        log.debug("Export format: " + mimetype);

        DriveFile driveFile = driveOperations.getFile(resourceID.substring(resourceID.lastIndexOf(':') + 1));

        InputStream inputstream = getFileInputStream(driveFile, mimetype);

        ContentWriter writer = fileFolderService.getWriter(nodeRef);
        writer.setMimetype(mimetype);
        writer.putContent(inputstream);

        renameNode(nodeRef, driveFile.getTitle());

        saveSharedInfo(nodeRef, resourceID);

        if (removeFromDrive) {
            deleteContent(nodeRef, driveFile);
        } else {
            nodeService.setProperty(nodeRef, GoogleDocsModel.PROP_REVISION_ID,
                    getLatestRevision(driveFile).getId());
        }

        postActivity(nodeRef);

        if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_TEMPORARY) && removeFromDrive) {
            nodeService.removeAspect(nodeRef, ContentModel.ASPECT_TEMPORARY);
            log.debug("Temporary Aspect Removed");
        }
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce.getStatusCode().value());
    } catch (JSONException jsonException) {
        throw new GoogleDocsAuthenticationException(
                "Unable to create activity entry: " + jsonException.getMessage(), jsonException);
    }
}

From source file:org.alfresco.integrations.google.docs.service.GoogleDocsServiceImpl.java

/**
 * Get the Document from the users Google Drive account. The Document and its working directory will be removed from their
 * Google Drive account. The editingInGoogle aspect will be removed.
 * //from w w w .  jav a 2s .c o m
 * @param nodeRef
 * @param resourceID
 * @throws GoogleDocsAuthenticationException
 * @throws GoogleDocsServiceException
 * @throws GoogleDocsRefreshTokenException
 */
private void getSpreadSheet(NodeRef nodeRef, String resourceID, boolean removeFromDrive)
        throws GoogleDocsAuthenticationException, GoogleDocsServiceException, GoogleDocsRefreshTokenException,
        IOException {
    log.debug("Get Google Spreadsheet for node: " + nodeRef);
    DriveOperations driveOperations = getDriveOperations(getConnection());

    try {
        String mimetype = null;

        mimetype = validateMimeType(fileFolderService.getFileInfo(nodeRef).getContentData().getMimetype());
        log.debug("Current mimetype: " + fileFolderService.getFileInfo(nodeRef).getContentData().getMimetype()
                + "; Mimetype of Google Doc: " + mimetype);
        log.debug("Export format: " + mimetype);

        DriveFile driveFile = driveOperations.getFile(resourceID.substring(resourceID.lastIndexOf(':') + 1));

        InputStream inputStream = getFileInputStream(driveFile, mimetype);

        ContentWriter writer = fileFolderService.getWriter(nodeRef);
        writer.setMimetype(mimetype);
        writer.putContent(inputStream);

        renameNode(nodeRef, driveFile.getTitle());

        saveSharedInfo(nodeRef, resourceID);

        if (removeFromDrive) {
            deleteContent(nodeRef, driveFile);
        } else {
            nodeService.setProperty(nodeRef, GoogleDocsModel.PROP_REVISION_ID,
                    getLatestRevision(driveFile).getId());
        }

        postActivity(nodeRef);

        if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_TEMPORARY) && removeFromDrive) {
            nodeService.removeAspect(nodeRef, ContentModel.ASPECT_TEMPORARY);
            log.debug("Temporary Aspect Removed");
        }
    } catch (IOException ioe) {
        throw ioe;
    } catch (HttpStatusCodeException hsce) {
        throw new GoogleDocsServiceException(hsce.getMessage(), hsce.getStatusCode().value());
    } catch (JSONException jsonException) {
        throw new GoogleDocsAuthenticationException(
                "Unable to create activity entry: " + jsonException.getMessage(), jsonException);
    }
}