Search in sources :

Example 11 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class ArchivedNodesDelete method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    // Current user
    String userID = AuthenticationUtil.getFullyAuthenticatedUser();
    if (userID == null) {
        throw new WebScriptException(HttpServletResponse.SC_UNAUTHORIZED, "Web Script [" + req.getServiceMatch().getWebScript().getDescription() + "] requires user authentication.");
    }
    StoreRef storeRef = parseRequestForStoreRef(req);
    NodeRef nodeRef = parseRequestForNodeRef(req);
    List<NodeRef> nodesToBePurged = new ArrayList<NodeRef>();
    if (nodeRef != null) {
        // check if the current user has the permission to purge the node
        validatePermission(nodeRef, userID);
        // If there is a specific NodeRef, then that is the only Node that should be purged.
        // In this case, the NodeRef points to the actual node to be purged i.e. the node in
        // the archive store.
        nodesToBePurged.add(nodeRef);
    } else {
        // But if there is no specific NodeRef and instead there is only a StoreRef, then
        // all nodes which were originally in that StoreRef should be purged.
        // Create paging
        ScriptPagingDetails paging = new ScriptPagingDetails(maxSizeView, 0);
        PagingResults<NodeRef> result = getArchivedNodesFrom(storeRef, paging, null);
        nodesToBePurged.addAll(result.getPage());
    }
    if (log.isDebugEnabled()) {
        log.debug("Purging " + nodesToBePurged.size() + " nodes");
    }
    // Now having identified the nodes to be purged, we simply have to do it.
    nodeArchiveService.purgeArchivedNodes(nodesToBePurged);
    model.put(PURGED_NODES, nodesToBePurged);
    return model;
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScriptPagingDetails(org.alfresco.util.ScriptPagingDetails)

Example 12 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class AuditQueryGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    final Map<String, Object> model = new HashMap<String, Object>(7);
    String appName = getParamAppName(req);
    String path = getParamPath(req);
    Serializable value = getParamValue(req);
    String valueType = getParamValueType(req);
    Long fromTime = getParamFromTime(req);
    Long toTime = getParamToTime(req);
    Long fromId = getParamFromId(req);
    Long toId = getParamToId(req);
    String user = getParamUser(req);
    boolean forward = getParamForward(req);
    int limit = getParamLimit(req);
    final boolean verbose = getParamVerbose(req);
    if (appName == null) {
        Map<String, AuditApplication> appsByName = auditService.getAuditApplications();
        AuditApplication app = appsByName.get(appName);
        if (app == null) {
            throw new WebScriptException(Status.STATUS_NOT_FOUND, "audit.err.app.notFound", appName);
        }
    }
    // Transform the value to the correct type
    if (value != null && valueType != null) {
        try {
            @SuppressWarnings("unchecked") Class<? extends Serializable> clazz = (Class<? extends Serializable>) Class.forName(valueType);
            value = DefaultTypeConverter.INSTANCE.convert(clazz, value);
        } catch (ClassNotFoundException e) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.value.classNotFound", valueType);
        } catch (Throwable e) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.value.convertFailed", value, valueType);
        }
    }
    // Execute the query
    AuditQueryParameters params = new AuditQueryParameters();
    params.setApplicationName(appName);
    params.setFromTime(fromTime);
    params.setToTime(toTime);
    params.setFromId(fromId);
    params.setToId(toId);
    params.setUser(user);
    params.setForward(forward);
    if (path != null || value != null) {
        params.addSearchKey(path, value);
    }
    final List<Map<String, Object>> entries = new ArrayList<Map<String, Object>>(limit);
    AuditQueryCallback callback = new AuditQueryCallback() {

        @Override
        public boolean valuesRequired() {
            return verbose;
        }

        @Override
        public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
            return true;
        }

        @Override
        public boolean handleAuditEntry(Long entryId, String applicationName, String user, long time, Map<String, Serializable> values) {
            Map<String, Object> entry = new HashMap<String, Object>(11);
            entry.put(JSON_KEY_ENTRY_ID, entryId);
            entry.put(JSON_KEY_ENTRY_APPLICATION, applicationName);
            if (user != null) {
                entry.put(JSON_KEY_ENTRY_USER, user);
            }
            entry.put(JSON_KEY_ENTRY_TIME, new Date(time));
            if (values != null) {
                // Convert values to Strings
                Map<String, String> valueStrings = new HashMap<String, String>(values.size() * 2);
                for (Map.Entry<String, Serializable> mapEntry : values.entrySet()) {
                    String key = mapEntry.getKey();
                    Serializable value = mapEntry.getValue();
                    try {
                        String valueString = DefaultTypeConverter.INSTANCE.convert(String.class, value);
                        valueStrings.put(key, valueString);
                    } catch (TypeConversionException e) {
                        // Use the toString()
                        valueStrings.put(key, value.toString());
                    }
                }
                entry.put(JSON_KEY_ENTRY_VALUES, valueStrings);
            }
            entries.add(entry);
            return true;
        }
    };
    auditService.auditQuery(callback, params, limit);
    model.put(JSON_KEY_ENTRY_COUNT, entries.size());
    model.put(JSON_KEY_ENTRIES, entries);
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
    }
    return model;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) TypeConversionException(org.alfresco.service.cmr.repository.datatype.TypeConversionException) AuditQueryParameters(org.alfresco.service.cmr.audit.AuditQueryParameters) ArrayList(java.util.ArrayList) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) AuditQueryCallback(org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback) AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) Date(java.util.Date) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class WorkflowInstanceGet method buildModel.

@Override
protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    // getting workflow instance id from request parameters
    String workflowInstanceId = params.get("workflow_instance_id");
    boolean includeTasks = getIncludeTasks(req);
    WorkflowInstance workflowInstance = workflowService.getWorkflowById(workflowInstanceId);
    // task was not found -> return 404
    if (workflowInstance == null) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find workflow instance with id: " + workflowInstanceId);
    }
    Map<String, Object> model = new HashMap<String, Object>();
    // build the model for ftl
    model.put("workflowInstance", modelBuilder.buildDetailed(workflowInstance, includeTasks));
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance)

Example 14 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class Invites method executeImpl.

/*
     * (non-Javadoc)
     * 
     * @see
     * org.alfresco.web.scripts.DeclarativeWebScript#executeImpl(org.alfresco
     * .web.scripts.WebScriptRequest,
     * org.alfresco.web.scripts.WebScriptResponse)
     */
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
    // initialise model to pass on for template to render
    Map<String, Object> model = new HashMap<String, Object>();
    // Get parameter names
    String[] paramNames = req.getParameterNames();
    // handle no parameters given on URL
    if ((paramNames == null) || (paramNames.length == 0)) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "No parameters have been provided on URL");
    }
    // get URL request parameters, checking if at least one has been provided
    // check if 'inviterUserName' parameter provided
    String inviterUserName = req.getParameter(PARAM_INVITER_USER_NAME);
    boolean inviterUserNameProvided = (inviterUserName != null) && (inviterUserName.length() != 0);
    // check if 'inviteeUserName' parameter provided
    String inviteeUserName = req.getParameter(PARAM_INVITEE_USER_NAME);
    boolean inviteeUserNameProvided = (inviteeUserName != null) && (inviteeUserName.length() != 0);
    // check if 'siteShortName' parameter provided
    String siteShortName = req.getParameter(PARAM_SITE_SHORT_NAME);
    boolean siteShortNameProvided = (siteShortName != null) && (siteShortName.length() != 0);
    // check if 'inviteId' parameter provided
    String inviteId = req.getParameter(PARAM_INVITE_ID);
    boolean inviteIdProvided = (inviteId != null) && (inviteId.length() != 0);
    // 'inviteId' URL request parameters has not been provided
    if (!(inviterUserNameProvided || inviteeUserNameProvided || siteShortNameProvided || inviteIdProvided)) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "At least one of the following URL request parameters must be provided in URL " + "'inviterUserName', 'inviteeUserName', 'siteShortName' or 'inviteId'");
    }
    // InviteInfo List to place onto model
    List<InviteInfo> inviteInfoList = new ArrayList<InviteInfo>();
    // query properties
    if (inviteIdProvided) {
        NominatedInvitation invitation = (NominatedInvitation) invitationService.getInvitation(inviteId);
        Map<String, SiteInfo> siteInfoCache = new HashMap<String, SiteInfo>(2);
        inviteInfoList.add(toInviteInfo(siteInfoCache, invitation));
    } else // 'inviteId' has not been provided, so create the query properties from
    // the invite URL request
    // parameters
    // - because this web script class will terminate with a web script
    // exception if none of the required
    // request parameters are provided, at least one of these query
    // properties will be set
    // at this point
    {
        InvitationSearchCriteriaImpl criteria = new InvitationSearchCriteriaImpl();
        criteria.setInvitationType(InvitationSearchCriteria.InvitationType.NOMINATED);
        criteria.setResourceType(Invitation.ResourceType.WEB_SITE);
        if (inviterUserNameProvided) {
            criteria.setInviter(inviterUserName);
        }
        if (inviteeUserNameProvided) {
            criteria.setInvitee(inviteeUserName);
        }
        if (siteShortNameProvided) {
            criteria.setResourceName(siteShortName);
        }
        // MNT-9905 Pending Invites created by one site manager aren't visible to other site managers
        String currentUser = AuthenticationUtil.getRunAsUser();
        List<Invitation> invitations;
        if (siteShortNameProvided == true && (SiteModel.SITE_MANAGER).equals(siteService.getMembersRole(siteShortName, currentUser)) && inviterUserNameProvided == false && inviteeUserNameProvided == false) {
            final InvitationSearchCriteriaImpl crit = criteria;
            RunAsWork<List<Invitation>> runAsSystem = new RunAsWork<List<Invitation>>() {

                @Override
                public List<Invitation> doWork() throws Exception {
                    return invitationService.searchInvitation(crit);
                }
            };
            invitations = AuthenticationUtil.runAs(runAsSystem, AuthenticationUtil.getSystemUserName());
        } else {
            invitations = invitationService.searchInvitation(criteria);
        }
        // Put InviteInfo objects (containing workflow path properties
        // wf:inviterUserName, wf:inviteeUserName, wf:siteShortName,
        // and invite id property (from workflow instance id))
        // onto model for each invite workflow task returned by the query
        Map<String, SiteInfo> siteInfoCache = new HashMap<String, SiteInfo>(invitations.size() * 2);
        for (Invitation invitation : invitations) {
            inviteInfoList.add(toInviteInfo(siteInfoCache, (NominatedInvitation) invitation));
        }
    }
    // put the list of invite infos onto model to be passed onto template
    // for rendering
    model.put(MODEL_KEY_NAME_INVITES, inviteInfoList);
    return model;
}
Also used : InviteInfo(org.alfresco.repo.invitation.site.InviteInfo) SiteInfo(org.alfresco.service.cmr.site.SiteInfo) NominatedInvitation(org.alfresco.service.cmr.invitation.NominatedInvitation) HashMap(java.util.HashMap) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) ArrayList(java.util.ArrayList) NominatedInvitation(org.alfresco.service.cmr.invitation.NominatedInvitation) Invitation(org.alfresco.service.cmr.invitation.Invitation) InvitationSearchCriteriaImpl(org.alfresco.repo.invitation.InvitationSearchCriteriaImpl) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with WebScriptException

use of org.springframework.extensions.webscripts.WebScriptException in project alfresco-remote-api by Alfresco.

the class LinkGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(SiteInfo site, String linkName, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    // Try to find the link
    LinkInfo link = linksService.getLink(site.getShortName(), linkName);
    if (link == null) {
        String message = "No link found with that name";
        throw new WebScriptException(Status.STATUS_NOT_FOUND, message);
    }
    // Build the model
    model.put(PARAM_ITEM, renderLink(link));
    model.put("node", link.getNodeRef());
    model.put("link", link);
    model.put("site", site);
    model.put("siteId", site.getShortName());
    // All done
    return model;
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) JSONObject(org.json.simple.JSONObject) LinkInfo(org.alfresco.service.cmr.links.LinkInfo)

Aggregations

WebScriptException (org.springframework.extensions.webscripts.WebScriptException)204 HashMap (java.util.HashMap)94 NodeRef (org.alfresco.service.cmr.repository.NodeRef)67 IOException (java.io.IOException)60 JSONException (org.json.JSONException)48 JSONObject (org.json.JSONObject)44 ArrayList (java.util.ArrayList)32 QName (org.alfresco.service.namespace.QName)31 JSONTokener (org.json.JSONTokener)29 JSONObject (org.json.simple.JSONObject)25 JSONArray (org.json.JSONArray)18 Map (java.util.Map)12 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)11 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)11 StoreRef (org.alfresco.service.cmr.repository.StoreRef)10 File (java.io.File)9 Date (java.util.Date)8 JSONParser (org.json.simple.parser.JSONParser)8 Serializable (java.io.Serializable)7 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)7