Example usage for org.apache.commons.httpclient HttpStatus SC_BAD_GATEWAY

List of usage examples for org.apache.commons.httpclient HttpStatus SC_BAD_GATEWAY

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_BAD_GATEWAY.

Prototype

int SC_BAD_GATEWAY

To view the source code for org.apache.commons.httpclient HttpStatus SC_BAD_GATEWAY.

Click Source Link

Document

<tt>502 Bad Gateway</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:com.sittinglittleduck.DirBuster.Worker.java

private boolean verifyIfCodeIsValid(int code) {
    return code != HttpStatus.SC_NOT_FOUND && code != 0 && code != HttpStatus.SC_BAD_GATEWAY;
}

From source file:de.lemo.apps.restws.client.InitialisationImpl.java

@Override
public JSONObject taskResult(String taskId) {

    JSONObject json = new JSONObject();
    Response taskResult = null;/*ww w . j a  va  2s  .co  m*/
    try {
        taskResult = taskManager.taskResult(taskId);
    } catch (RuntimeException e) {
        if (!e.getCause().getClass().equals(ConnectionPoolTimeoutException.class)) {
            // ignore ConnectionPoolTimeoutException, rethrow anything else
            //throw e;
        }
    }

    int status = (taskResult == null) ? HttpStatus.SC_GATEWAY_TIMEOUT : taskResult.getStatus();

    if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
        // DMS error (maybe a processing timeout)
        json.put("status", HttpStatus.SC_BAD_GATEWAY);
    } else {
        json.put("status", status);
    }

    if (status == HttpStatus.SC_OK) {
        @SuppressWarnings("unchecked")
        ClientResponse<String> clientResponse = (ClientResponse<String>) taskResult;
        String analysisResult = clientResponse.getEntity(String.class);
        json.put("bideResult", new JSONObject(analysisResult));
    }
    return json;
}

From source file:org.alfresco.integrations.google.docs.webscripts.CreateContent.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    // Set Service Beans
    this.getGoogleDocsServiceSubsystem();

    Map<String, Object> model = new HashMap<String, Object>();

    if (googledocsService.isEnabled()) {

        String contentType = req.getParameter(PARAM_TYPE);
        NodeRef parentNodeRef = new NodeRef(req.getParameter(PARAM_PARENT));

        log.debug("ContentType: " + contentType + "; Parent: " + parentNodeRef);

        NodeRef newNode = null;/*w w w  . j a  va2 s. com*/
        File file = null;
        try {
            Credential credential = googledocsService.getCredential();
            if (contentType.equals(GoogleDocsConstants.DOCUMENT_TYPE)) {
                newNode = createFile(parentNodeRef, contentType, GoogleDocsConstants.MIMETYPE_DOCUMENT);
                file = googledocsService.createDocument(credential, newNode);
            } else if (contentType.equals(GoogleDocsConstants.SPREADSHEET_TYPE)) {
                newNode = createFile(parentNodeRef, contentType, GoogleDocsConstants.MIMETYPE_SPREADSHEET);
                file = googledocsService.createSpreadSheet(credential, newNode);
            } else if (contentType.equals(GoogleDocsConstants.PRESENTATION_TYPE)) {
                newNode = createFile(parentNodeRef, contentType, GoogleDocsConstants.MIMETYPE_PRESENTATION);
                file = googledocsService.createPresentation(credential, newNode);
            } else {
                throw new WebScriptException(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, "Content Type Not Found.");
            }

            googledocsService.decorateNode(newNode, file, googledocsService.getLatestRevision(credential, file),
                    true);

        } catch (GoogleDocsServiceException gdse) {
            if (gdse.getPassedStatusCode() > -1) {
                throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage());
            } else {
                throw new WebScriptException(gdse.getMessage());
            }
        } catch (GoogleDocsTypeException gdte) {
            throw new WebScriptException(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, gdte.getMessage());
        } catch (GoogleDocsAuthenticationException gdae) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage());
        } catch (GoogleDocsRefreshTokenException gdrte) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage());
        } catch (IOException ioe) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, ioe.getMessage(), ioe);
        } catch (Exception e) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
        }

        googledocsService.lockNode(newNode);

        model.put(MODEL_NODEREF, newNode.toString());
        model.put(MODEL_EDITOR_URL, file.getAlternateLink());

    } else {
        throw new WebScriptException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Google Docs Disabled");
    }

    return model;
}

From source file:org.alfresco.integrations.google.docs.webscripts.DiscardContent.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    getGoogleDocsServiceSubsystem();/*w  w  w.ja va 2s  .  c  om*/

    Map<String, Object> model = new HashMap<String, Object>();

    Map<String, Serializable> map = parseContent(req);
    final NodeRef nodeRef = (NodeRef) map.get(JSON_KEY_NODEREF);

    if (nodeService.hasAspect(nodeRef, GoogleDocsModel.ASPECT_EDITING_IN_GOOGLE)) {
        try {
            boolean deleted = false;

            if (!Boolean.valueOf(map.get(JSON_KEY_OVERRIDE).toString())) {
                SiteInfo siteInfo = siteService.getSite(nodeRef);
                if (siteInfo == null
                        || siteService.isMember(siteInfo.getShortName(), AuthenticationUtil.getRunAsUser())) {

                    if (googledocsService.hasConcurrentEditors(nodeRef)) {
                        throw new WebScriptException(HttpStatus.SC_CONFLICT,
                                "Node: " + nodeRef.toString() + " has concurrent editors.");
                    }
                } else {
                    throw new AccessDeniedException(
                            "Access Denied.  You do not have the appropriate permissions to perform this operation.");
                }
            }

            deleted = delete(nodeRef);

            model.put(MODEL_SUCCESS, deleted);

        } catch (InvalidNodeRefException ine) {
            throw new WebScriptException(HttpStatus.SC_NOT_FOUND, ine.getMessage());
        } catch (IOException ioe) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, ioe.getMessage());
        } catch (GoogleDocsAuthenticationException gdae) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage());
        } catch (GoogleDocsRefreshTokenException gdrte) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage());
        } catch (GoogleDocsServiceException gdse) {
            if (gdse.getPassedStatusCode() > -1) {
                throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage());
            } else {
                throw new WebScriptException(gdse.getMessage());
            }
        } catch (AccessDeniedException ade) {
            // This code will make changes after the rollback has occurred to clean up the node: remove the lock and the Google
            // Docs aspect. If it has the temporary aspect it will also remove the node from Alfresco
            AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {
                public void afterRollback() {
                    transactionService.getRetryingTransactionHelper()
                            .doInTransaction(new RetryingTransactionCallback<Object>() {
                                public Object execute() throws Throwable {
                                    DriveFile driveFile = googledocsService.getDriveFile(nodeRef);
                                    googledocsService.unlockNode(nodeRef);
                                    boolean deleted = googledocsService.deleteContent(nodeRef, driveFile);

                                    if (deleted) {
                                        AuthenticationUtil
                                                .runAsSystem(new AuthenticationUtil.RunAsWork<Object>() {
                                                    public Object doWork() throws Exception {
                                                        if (nodeService.hasAspect(nodeRef,
                                                                ContentModel.ASPECT_TEMPORARY)) {
                                                            nodeService.deleteNode(nodeRef);
                                                        }

                                                        return null;
                                                    }
                                                });
                                    }

                                    return null;
                                }
                            }, false, true);
                }
            });

            throw new WebScriptException(HttpStatus.SC_FORBIDDEN, ade.getMessage(), ade);
        } catch (Exception e) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
        }
    } else {
        throw new WebScriptException(HttpStatus.SC_NOT_ACCEPTABLE,
                "Missing Google Docs Aspect on " + nodeRef.toString());
    }

    return model;
}

From source file:org.alfresco.integrations.google.docs.webscripts.HasConcurrentEditors.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    getGoogleDocsServiceSubsystem();//  w  w w.j  a v a  2s. c  o m

    Map<String, Object> model = new HashMap<String, Object>();

    String param_nodeRef = req.getParameter(PARAM_NODEREF);
    NodeRef nodeRef = new NodeRef(param_nodeRef);

    try {
        Credential credential = googledocsService.getCredential();

        model.put(MODEL_CONCURRENT_EDITORS, googledocsService.hasConcurrentEditors(credential, nodeRef));
    } catch (GoogleDocsAuthenticationException gdae) {
        throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage());
    } catch (GoogleDocsRefreshTokenException gdrte) {
        throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage());
    } catch (GoogleDocsServiceException gdse) {
        if (gdse.getPassedStatusCode() > -1) {
            throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage());
        } else {
            throw new WebScriptException(gdse.getMessage());
        }
    } catch (Exception e) {
        throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
    }

    return model;
}

From source file:org.alfresco.integrations.google.docs.webscripts.IsLatestRevision.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    getGoogleDocsServiceSubsystem();//  ww w .ja va2  s  .c om

    Map<String, Object> model = new HashMap<String, Object>();

    /* Get the nodeRef to test */
    String param_nodeRef = req.getParameter(PARAM_NODEREF);
    NodeRef nodeRef = new NodeRef(param_nodeRef);
    log.debug("Comparing Node Revision Id from Alfresco and Google: " + nodeRef);

    /* The revision Id persisted on the node */
    String currentRevision = null;
    /* The latest revision Id from Google for the file */
    String latestRevision = null;

    try {
        /* The node needs the editingInGoogle aspect if not then tell return 412 */
        if (nodeService.hasAspect(nodeRef, GoogleDocsModel.ASPECT_EDITING_IN_GOOGLE)) {
            Credential credential = googledocsService.getCredential();

            /* get the nodes revision Id null if not found */
            Serializable property = nodeService.getProperty(nodeRef, GoogleDocsModel.PROP_REVISION_ID);
            currentRevision = property != null ? property.toString() : null;
            log.debug("currentRevision: " + currentRevision);

            /* get the latest revision Id null if not found */
            Revision revision = googledocsService.getLatestRevision(credential, nodeRef);
            latestRevision = revision != null ? revision.getId() : null;
            log.debug("latestRevision: " + latestRevision);

            /* compare the revision Ids */
            if (currentRevision != null && latestRevision != null) {

                isLatestRevision = currentRevision.equals(latestRevision);
            }

            model.put(MODEL_IS_LATEST_REVISION, isLatestRevision);
        } else {
            throw new WebScriptException(HttpStatus.SC_PRECONDITION_FAILED,
                    "Node: " + nodeRef.toString() + " has no revision Ids.");
        }
    } catch (GoogleDocsAuthenticationException gdae) {
        throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage());
    } catch (GoogleDocsServiceException gdse) {
        if (gdse.getPassedStatusCode() > -1) {
            throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage());
        } else {
            throw new WebScriptException(gdse.getMessage());
        }
    } catch (GoogleDocsRefreshTokenException gdrte) {
        throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage());
    } catch (IOException ioe) {
        throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, ioe.getMessage());
    }

    return model;
}

From source file:org.alfresco.integrations.google.docs.webscripts.RemoveContent.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    getGoogleDocsServiceSubsystem();/*  ww  w.j a  va2 s . c  o  m*/

    Map<String, Object> model = new HashMap<String, Object>();

    boolean success = false;

    /* The node we are working on */
    Map<String, Serializable> map = parseContent(req);
    final NodeRef nodeRef = (NodeRef) map.get(JSON_KEY_NODEREF);

    /* Make sure the node is currently "checked out" to Google */
    if (nodeService.hasAspect(nodeRef, GoogleDocsModel.ASPECT_EDITING_IN_GOOGLE)) {
        try {
            Credential credential = googledocsService.getCredential();

            /* Get the metadata for the file we are working on */
            File file = googledocsService.getDriveFile(credential, nodeRef);
            /* remove it from users Google account and free it in the repo */
            googledocsService.removeContent(credential, nodeRef, file, (Boolean) map.get(JSON_KEY_FORCE));

            /* if we reach this point all should be completed */
            success = true;

        } catch (GoogleDocsAuthenticationException gdae) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage());
        } catch (GoogleDocsServiceException gdse) {
            if (gdse.getPassedStatusCode() > -1) {
                throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage());
            } else {
                throw new WebScriptException(gdse.getMessage());
            }
        } catch (GoogleDocsRefreshTokenException gdrte) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage());
        } catch (ConstraintException ce) {
            throw new WebScriptException(GoogleDocsConstants.STATUS_INTEGIRTY_VIOLATION, ce.getMessage(), ce);
        } catch (AccessDeniedException ade) {
            // This code will make changes after the rollback has occurred to clean up the node
            // (remove the lock and the Google Docs aspect
            AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {
                public void afterRollback() {
                    log.debug("Rollback Save to node: " + nodeRef);
                    transactionService.getRetryingTransactionHelper()
                            .doInTransaction(new RetryingTransactionCallback<Object>() {
                                public Object execute() throws Throwable {
                                    return AuthenticationUtil
                                            .runAsSystem(new AuthenticationUtil.RunAsWork<Object>() {
                                                public Object doWork() throws Exception {
                                                    googledocsService.unlockNode(nodeRef);
                                                    googledocsService.unDecorateNode(nodeRef);

                                                    // If the node was just created ('Create Content') it will
                                                    // have the temporary aspect and should be completely removed.
                                                    if (nodeService.hasAspect(nodeRef,
                                                            ContentModel.ASPECT_TEMPORARY)) {
                                                        nodeService.deleteNode(nodeRef);
                                                    }

                                                    return null;
                                                }
                                            });
                                }
                            }, false, true);
                }
            });

            throw new WebScriptException(HttpStatus.SC_FORBIDDEN, ade.getMessage(), ade);
        } catch (Exception e) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
        }
    }

    model.put(MODEL_SUCCESSFUL, success);

    return model;
}

From source file:org.alfresco.integrations.google.docs.webscripts.SaveContent.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    getGoogleDocsServiceSubsystem();/* w w  w.j ava2 s.  c  om*/

    Map<String, Object> model = new HashMap<String, Object>();

    boolean success = false;

    Map<String, Serializable> map = parseContent(req);
    final NodeRef nodeRef = (NodeRef) map.get(JSON_KEY_NODEREF);
    log.debug("Saving Node to Alfresco from Google: " + nodeRef);

    try {
        SiteInfo siteInfo = siteService.getSite(nodeRef);
        if (siteInfo == null
                || siteService.isMember(siteInfo.getShortName(), AuthenticationUtil.getRunAsUser())) {

            if (!(Boolean) map.get(JSON_KEY_OVERRIDE)) {
                log.debug("Check for Concurent Users.");
                if (googledocsService.hasConcurrentEditors(nodeRef)) {
                    throw new WebScriptException(HttpStatus.SC_CONFLICT,
                            "Node: " + nodeRef.toString() + " has concurrent editors.");
                }
            }

            // Should the content be removed from the users Google Drive Account
            boolean removeFromDrive = (map.get(JSON_KEY_REMOVEFROMDRIVE) != null)
                    ? (Boolean) map.get(JSON_KEY_REMOVEFROMDRIVE)
                    : true;

            String contentType = googledocsService.getContentType(nodeRef);
            log.debug("NodeRef: " + nodeRef + "; ContentType: " + contentType);
            if (contentType.equals(GoogleDocsConstants.DOCUMENT_TYPE)) {
                if (googledocsService.isGoogleDocsLockOwner(nodeRef)) {
                    googledocsService.unlockNode(nodeRef);

                    if (removeFromDrive) {
                        googledocsService.getDocument(nodeRef);
                    } else {
                        googledocsService.getDocument(nodeRef, removeFromDrive);
                    }
                    success = true; // TODO Make getDocument return boolean
                } else {
                    throw new WebScriptException(HttpStatus.SC_FORBIDDEN,
                            "Document is locked by another user.");
                }
            } else if (contentType.equals(GoogleDocsConstants.SPREADSHEET_TYPE)) {
                if (googledocsService.isGoogleDocsLockOwner(nodeRef)) {
                    googledocsService.unlockNode(nodeRef);

                    if (removeFromDrive) {
                        googledocsService.getSpreadSheet(nodeRef);
                    } else {
                        googledocsService.getSpreadSheet(nodeRef, removeFromDrive);
                    }
                    success = true; // TODO Make getSpreadsheet return
                                    // boolean
                } else {
                    throw new WebScriptException(HttpStatus.SC_FORBIDDEN,
                            "Document is locked by another user.");
                }
            } else if (contentType.equals(GoogleDocsConstants.PRESENTATION_TYPE)) {
                if (googledocsService.isGoogleDocsLockOwner(nodeRef)) {
                    googledocsService.unlockNode(nodeRef);

                    if (removeFromDrive) {
                        googledocsService.getPresentation(nodeRef);
                    } else {
                        googledocsService.getPresentation(nodeRef, removeFromDrive);
                    }
                    success = true; // TODO Make getPresentation return
                                    // boolean
                } else {
                    throw new WebScriptException(HttpStatus.SC_FORBIDDEN,
                            "Document is locked by another user.");
                }
            } else {
                throw new WebScriptException(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE,
                        "Content Type: " + contentType + " unknown.");
            }

            // Finish this off with a version create or update
            Map<String, Serializable> versionProperties = new HashMap<String, Serializable>();
            if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE)) {
                versionProperties.put(Version2Model.PROP_VERSION_TYPE, map.get(JSON_KEY_MAJORVERSION));
                versionProperties.put(Version2Model.PROP_DESCRIPTION, map.get(JSON_KEY_DESCRIPTION));
            } else {
                versionProperties.put(Version2Model.PROP_VERSION_TYPE, VersionType.MAJOR);

                nodeService.setProperty(nodeRef, ContentModel.PROP_AUTO_VERSION, true);
                nodeService.setProperty(nodeRef, ContentModel.PROP_AUTO_VERSION_PROPS, true);
            }

            log.debug("Version Node:" + nodeRef + "; Version Properties: " + versionProperties);
            Version version = versionService.createVersion(nodeRef, versionProperties);

            model.put(MODEL_VERSION, version.getVersionLabel());

            if (!removeFromDrive) {
                googledocsService.lockNode(nodeRef);
            }

        } else {
            throw new AccessDeniedException(
                    "Access Denied.  You do not have the appropriate permissions to perform this operation.");
        }

    } catch (GoogleDocsAuthenticationException gdae) {
        throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage());
    } catch (GoogleDocsServiceException gdse) {
        if (gdse.getPassedStatusCode() > -1) {
            throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage());
        } else {
            throw new WebScriptException(gdse.getMessage());
        }
    } catch (GoogleDocsRefreshTokenException gdrte) {
        throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage());
    } catch (ConstraintException ce) {
        throw new WebScriptException(GoogleDocsConstants.STATUS_INTEGIRTY_VIOLATION, ce.getMessage(), ce);
    } catch (AccessDeniedException ade) {
        // This code will make changes after the rollback has occurred to clean up the node (remove the lock and the Google Docs
        // aspect
        AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {
            public void afterRollback() {
                log.debug("Rollback Save to node: " + nodeRef);
                transactionService.getRetryingTransactionHelper()
                        .doInTransaction(new RetryingTransactionCallback<Object>() {
                            public Object execute() throws Throwable {
                                return AuthenticationUtil
                                        .runAsSystem(new AuthenticationUtil.RunAsWork<Object>() {
                                            public Object doWork() throws Exception {
                                                googledocsService.unlockNode(nodeRef);
                                                googledocsService.unDecorateNode(nodeRef);

                                                // If the node was just created ('Create Content') it will have the temporary aspect and should
                                                // be completely removed.
                                                if (nodeService.hasAspect(nodeRef,
                                                        ContentModel.ASPECT_TEMPORARY)) {
                                                    nodeService.deleteNode(nodeRef);
                                                }

                                                return null;
                                            }
                                        });
                            }
                        }, false, true);
            }
        });

        throw new WebScriptException(HttpStatus.SC_FORBIDDEN, ade.getMessage(), ade);
    } catch (Exception e) {
        throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
    }

    model.put(MODEL_SUCCESS, success);

    return model;
}

From source file:org.alfresco.integrations.google.docs.webscripts.UploadContent.java

@SuppressWarnings("unchecked")
@Override//from   w  w w  .ja  v a  2  s. c o m
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    getGoogleDocsServiceSubsystem();

    Map<String, Object> model = new HashMap<String, Object>();

    if (googledocsService.isEnabled()) {

        String param_nodeRef = req.getParameter(PARAM_NODEREF);
        NodeRef nodeRef = new NodeRef(param_nodeRef);

        Map<String, Serializable> jsonParams = parseContent(req);

        DriveFile driveFile;
        try {
            if (nodeService.hasAspect(nodeRef, GoogleDocsModel.ASPECT_EDITING_IN_GOOGLE)) {
                // Check the doc exists in Google - it may have been removed accidentally
                try {
                    driveFile = googledocsService.getDriveFile(nodeRef);
                } catch (GoogleDocsServiceException gdse) {
                    driveFile = googledocsService.uploadFile(nodeRef);
                    if (log.isDebugEnabled()) {
                        log.debug(nodeRef + " Uploaded to Google.");
                    }
                    // Re-apply the previous permissions, if they exist
                    if (nodeService.getProperty(nodeRef, GoogleDocsModel.PROP_CURRENT_PERMISSIONS) != null) {
                        googledocsService.addRemotePermissions(driveFile, googledocsService
                                .getGooglePermissions(nodeRef, GoogleDocsModel.PROP_CURRENT_PERMISSIONS), true);
                    }
                }
            } else {
                driveFile = googledocsService.uploadFile(nodeRef);
                if (log.isDebugEnabled()) {
                    log.debug(nodeRef + " Uploaded to Google.");
                }
            }

            if (jsonParams.containsKey(PARAM_PERMISSIONS)) {
                if (log.isDebugEnabled()) {
                    log.debug("Adding permissions to remote object");
                }
                googledocsService.addRemotePermissions(driveFile,
                        (List<GooglePermission>) jsonParams.get(PARAM_PERMISSIONS),
                        ((Boolean) jsonParams.get(PARAM_SEND_EMAIL)).booleanValue());
            }

            // If this is a non-cloud instance of Alfresco, we need to make the
            // node versionable before we start working on it. We want the the
            // version component to be triggered on save. The versionable aspect
            // is only added if this is existing content, not if it was just
            // created where the document is the initial version when saved
            if (!nodeService.hasAspect(nodeRef, ContentModel.ASPECT_TEMPORARY)
                    && !nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE)) {
                Map<String, Serializable> versionProperties = new HashMap<String, Serializable>();
                versionProperties.put(Version2Model.PROP_VERSION_TYPE, VersionType.MAJOR);

                nodeService.setProperty(nodeRef, ContentModel.PROP_AUTO_VERSION, true);
                // autoVersionOnUpdateProps now set to false to follow Share upload scripts (fixes GOOGLEDOCS-111)
                nodeService.setProperty(nodeRef, ContentModel.PROP_AUTO_VERSION_PROPS, false);

                log.debug("Version Node:" + nodeRef + "; Version Properties: " + versionProperties);
                versionService.createVersion(nodeRef, versionProperties);
            }

            if (googledocsService.isLockedByGoogleDocs(nodeRef)) {
                googledocsService.unlockNode(nodeRef);
            }

            googledocsService.decorateNode(nodeRef, driveFile, googledocsService.getLatestRevision(driveFile),
                    (List<GooglePermission>) jsonParams.get(PARAM_PERMISSIONS), false);
            googledocsService.lockNode(nodeRef);
        } catch (GoogleDocsAuthenticationException gdae) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage());
        } catch (GoogleDocsServiceException gdse) {
            if (gdse.getPassedStatusCode() > -1) {
                throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage());
            } else {
                throw new WebScriptException(gdse.getMessage());
            }
        } catch (GoogleDocsRefreshTokenException gdrte) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage());
        } catch (IOException ioe) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, ioe.getMessage(), ioe);
        } catch (Exception e) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
        }

        model.put(MODEL_NODEREF, nodeRef.toString());

    } else {
        throw new WebScriptException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Google Docs Disabled");
    }

    return model;
}

From source file:org.alfresco.integrations.google.docs.webscripts.UserProfile.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    getGoogleDocsServiceSubsystem();/*  ww w  . j a  v a 2s  .  c  o m*/

    Map<String, Object> model = new HashMap<String, Object>();

    boolean authenticated = false;

    if (googledocsService.isAuthenticated()) {
        authenticated = true;
        try {
            Credential credential = googledocsService.getCredential();

            User user = googledocsService.getDriveUser(credential);
            model.put(MODEL_EMAIL, user.getEmailAddress());
            model.put(MODEL_NAME, user.getDisplayName());
            //model.put(MODEL_FIRSTNAME, user.getFirstName()); TODO Get first name?
            //model.put(MODEL_LASTNAME, profile.getLastName()); TODO Get last name?
            model.put(MODEL_ID, user.getPermissionId());
        } catch (GoogleDocsAuthenticationException gdae) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage());
        } catch (GoogleDocsRefreshTokenException gdrte) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage());
        } catch (GoogleDocsServiceException gdse) {
            if (gdse.getPassedStatusCode() > -1) {
                throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage());
            } else {
                throw new WebScriptException(gdse.getMessage());
            }
        } catch (IOException ioe) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, ioe.getMessage());
        }
    }

    model.put(MODEL_AUTHENTICATED, authenticated);

    return model;
}