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

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

Introduction

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

Prototype

@Override
@Nullable
public String getMessage() 

Source Link

Document

Return the detail message, including the message from the nested exception if there is one.

Usage

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

public FileRevision getLatestRevision(NodeRef nodeRef)
        throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException {
    FileRevision fileRevision = null;/* w  w  w.j  a  va2  s. 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  w w  w  .j a v  a 2 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   ww w  .java  2 s . c  o m

    // 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.
 * /*www . ja v  a  2s.c  om*/
 * @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 ww . j ava2 s .co  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);
    }
}

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.
 * //ww  w  .  j  a v a 2  s.c o m
 * @param nodeRef
 * @param resourceID
 * @throws GoogleDocsAuthenticationException
 * @throws GoogleDocsServiceException
 * @throws GoogleDocsRefreshTokenException
 */
private void getPresentation(NodeRef nodeRef, String resourceID, boolean removeFromDrive)
        throws GoogleDocsAuthenticationException, GoogleDocsServiceException, GoogleDocsRefreshTokenException,
        IOException {
    log.debug("Get Google Presentation 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);
    }
}

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

public boolean hasConcurrentEditors(NodeRef nodeRef)
        throws GoogleDocsAuthenticationException, GoogleDocsRefreshTokenException, GoogleDocsServiceException {
    log.debug(/*  www .  j  a v a 2  s.c o m*/
            "Check for Concurrent Editors (Edits that have occured in the last " + idleThreshold + " seconds)");
    DriveOperations driveOperations = getDriveOperations(getConnection());

    String resourceID = nodeService.getProperty(nodeRef, GoogleDocsModel.PROP_RESOURCE_ID).toString();

    boolean concurrentChange = false;

    try {
        List<FileRevision> fileRevisionList = driveOperations
                .getRevisions(resourceID.substring(resourceID.lastIndexOf(':') + 1));

        if (fileRevisionList.size() > 1) {
            log.debug("Revisions Found");
            Collections.sort(fileRevisionList, Collections.reverseOrder(new FileRevisionComparator()));

            // Find any revisions occurring within the last 'idleThreshold'
            // seconds
            List<FileRevision> workingList = new ArrayList<FileRevision>();

            Calendar bufferTime = Calendar.getInstance();
            bufferTime.add(Calendar.SECOND, -idleThreshold);

            for (FileRevision entry : fileRevisionList) {
                if (entry.getModifiedDate().after(new Date(bufferTime.getTimeInMillis()))) {
                    workingList.add(entry);
                } else {
                    // once we past 'idleThreshold' seconds get out of here
                    break;
                }
            }

            // If there any revisions that occurred within the last
            // 'idleThreshold' seconds of time....
            if (workingList.size() > 0) {
                log.debug("Revisions within threshhold found");
                // Filter the current user from the list
                for (int i = workingList.size() - 1; i >= 0; i--) {
                    FileRevision fileRevision = workingList.get(i);
                    String name = getGoogleUserProfile().getName();

                    // if there is no author -- the entry is the initial
                    // creation
                    if (fileRevision.getLastModifyingUserName() != null) {
                        if (fileRevision.getLastModifyingUserName().equals(name)) {
                            workingList.remove(i);
                        }
                    } else {
                        workingList.remove(i);
                    }
                }
            }

            // Are there are changes by other users within the last
            // 'idleThreshold' seconds
            if (workingList.size() > 0) {
                log.debug("Revisions not made by current user found.");
                concurrentChange = true;
            }

        } else {
            String name = getGoogleUserProfile().getName();

            // if the authors list is empty -- the author was the original
            // creator and it is the initial copy
            if (fileRevisionList.get(0).getLastModifyingUserName() != null) {

                if (!fileRevisionList.get(0).getLastModifyingUserName().equals(name)) {
                    Calendar bufferTime = Calendar.getInstance();
                    bufferTime.add(Calendar.SECOND, -idleThreshold);

                    if (fileRevisionList.get(0).getModifiedDate()
                            .before(new Date(bufferTime.getTimeInMillis()))) {
                        log.debug("Revisions not made by current user found.");
                        concurrentChange = true;
                    }
                }
            }
        }

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

    log.debug("Concurrent Edits: " + concurrentChange);
    return concurrentChange;
}

From source file:org.mskcc.cbio.portal.util.SessionServiceUtil.java

/**
 * Return cohort object if there is success response from 
 * session-service API, else it would return null
 * @param virtualStudyId/*from ww  w .j a v a 2 s  .c o m*/
 * @return cohort object
 */
public VirtualStudy getVirtualStudyData(String virtualStudyId) {
    if (!GlobalProperties.getSessionServiceUrl().equals("")) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpEntity<String> headers = new HttpEntity<String>(getHttpHeaders());

            ResponseEntity<VirtualStudy> responseEntity = restTemplate.exchange(
                    GlobalProperties.getSessionServiceUrl() + "virtual_study/" + virtualStudyId, HttpMethod.GET,
                    headers, VirtualStudy.class);
            return responseEntity.getBody();
        } catch (HttpStatusCodeException exception) {
            LOG.warn("SessionServiceUtil.getVirtualCohortData(): HttpStatusCodeException = '"
                    + exception.getStatusCode() + "'");
        } catch (Exception exception) {
            LOG.warn("SessionServiceUtil.getVirtualCohortData(): Exception = '" + exception.getMessage() + "'");
        }
    }
    return null;
}