Search in sources :

Example 56 with WebScriptException

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

the class SolrFacetConfigAdminPut method parseRequestForFacetProperties.

private SolrFacetProperties parseRequestForFacetProperties(WebScriptRequest req) {
    JSONObject json = null;
    try {
        json = new JSONObject(new JSONTokener(req.getContent().getContent()));
        // Must exist
        final String filterID = json.getString(PARAM_FILTER_ID);
        final String facetQNameStr = getValue(String.class, json.opt(PARAM_FACET_QNAME), null);
        // Note that in resolving the QName string here, we expect there to be some facet QNames which are not
        // really QNames. These are SOLR/SearchService 'specials', examples being "SITE" or "TAG".
        // These will be resolved here to a QName with no namespace.
        final QName facetQName = (facetQNameStr == null) ? null : FacetQNameUtils.createQName(facetQNameStr, namespaceService);
        final String displayName = getValue(String.class, json.opt(PARAM_DISPLAY_NAME), null);
        final String displayControl = getValue(String.class, json.opt(PARAM_DISPLAY_CONTROL), null);
        final int maxFilters = getValue(Integer.class, json.opt(PARAM_MAX_FILTERS), -1);
        final int hitThreshold = getValue(Integer.class, json.opt(PARAM_HIT_THRESHOLD), -1);
        final int minFilterValueLength = getValue(Integer.class, json.opt(PARAM_MIN_FILTER_VALUE_LENGTH), -1);
        final String sortBy = getValue(String.class, json.opt(PARAM_SORT_BY), null);
        final String scope = getValue(String.class, json.opt(PARAM_SCOPE), null);
        final Boolean isEnabled = getValue(Boolean.class, json.opt(PARAM_IS_ENABLED), null);
        JSONArray scopedSitesJsonArray = getValue(JSONArray.class, json.opt(PARAM_SCOPED_SITES), null);
        final Set<String> scopedSites = getScopedSites(scopedSitesJsonArray);
        final JSONObject customPropJsonObj = getValue(JSONObject.class, json.opt(PARAM_CUSTOM_PROPERTIES), null);
        final Set<CustomProperties> customProps = getCustomProperties(customPropJsonObj);
        SolrFacetProperties fp = new SolrFacetProperties.Builder().filterID(filterID).facetQName(facetQName).displayName(displayName).displayControl(displayControl).maxFilters(maxFilters).hitThreshold(hitThreshold).minFilterValueLength(minFilterValueLength).sortBy(sortBy).scope(scope).isEnabled(isEnabled).scopedSites(scopedSites).customProperties(customProps).build();
        return fp;
    } catch (IOException e) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", e);
    } catch (JSONException e) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", e);
    }
}
Also used : QName(org.alfresco.service.namespace.QName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) SolrFacetProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) CustomProperties(org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties)

Example 57 with WebScriptException

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

the class InviteByTicket method execute.

private Map<String, Object> execute(WebScriptRequest req, Status status) {
    // initialise model to pass on for template to render
    Map<String, Object> model = new HashMap<String, Object>();
    // get inviteId and inviteTicket
    String inviteId = req.getServiceMatch().getTemplateVars().get("inviteId");
    String inviteTicket = req.getServiceMatch().getTemplateVars().get("inviteTicket");
    try {
        Invitation invitation = invitationService.getInvitation(inviteId);
        if (invitation instanceof NominatedInvitation) {
            NominatedInvitation theInvitation = (NominatedInvitation) invitation;
            String ticket = theInvitation.getTicket();
            if (ticket == null || (!ticket.equals(inviteTicket))) {
                throw new WebScriptException(Status.STATUS_NOT_FOUND, "Ticket mismatch");
            }
            // return the invite info
            model.put("invite", toInviteInfo(theInvitation));
            return model;
        } else {
            // Not a nominated invitation
            throw new WebScriptException(Status.STATUS_FORBIDDEN, "Not a nominated invitation");
        }
    } catch (InvitationExceptionNotFound nfe) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "No invite found for given id");
    }
}
Also used : NominatedInvitation(org.alfresco.service.cmr.invitation.NominatedInvitation) WorkflowModelNominatedInvitation(org.alfresco.repo.invitation.WorkflowModelNominatedInvitation) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) NominatedInvitation(org.alfresco.service.cmr.invitation.NominatedInvitation) WorkflowModelNominatedInvitation(org.alfresco.repo.invitation.WorkflowModelNominatedInvitation) Invitation(org.alfresco.service.cmr.invitation.Invitation) InvitationExceptionNotFound(org.alfresco.service.cmr.invitation.InvitationExceptionNotFound)

Example 58 with WebScriptException

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

the class CustomModelUploadPost method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    if (!customModelService.isModelAdmin(AuthenticationUtil.getFullyAuthenticatedUser())) {
        throw new WebScriptException(Status.STATUS_FORBIDDEN, PermissionDeniedException.DEFAULT_MESSAGE_ID);
    }
    FormData formData = (FormData) req.parseContent();
    if (formData == null || !formData.getIsMultiPart()) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_not_multi_part_req");
    }
    ImportResult resultData = null;
    boolean processed = false;
    for (FormData.FormField field : formData.getFields()) {
        if (field.getIsFile()) {
            final String fileName = field.getFilename();
            File tempFile = createTempFile(field.getInputStream());
            try (ZipFile zipFile = new ZipFile(tempFile, StandardCharsets.UTF_8)) {
                resultData = processUpload(zipFile, field.getFilename());
            } catch (ZipException ze) {
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_not_zip_format", new Object[] { fileName });
            } catch (IOException io) {
                throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "cmm.rest_api.model.import_process_zip_file_failure", io);
            } finally {
                // now the import is done, delete the temp file
                tempFile.delete();
            }
            processed = true;
            break;
        }
    }
    if (!processed) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_no_zip_file_uploaded");
    }
    // If we get here, then importing the custom model didn't throw any exceptions.
    Map<String, Object> model = new HashMap<>(2);
    model.put("importedModelName", resultData.getImportedModelName());
    model.put("shareExtXMLFragment", resultData.getShareExtXMLFragment());
    return model;
}
Also used : FormData(org.springframework.extensions.webscripts.servlet.FormData) HashMap(java.util.HashMap) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ZipFile(java.util.zip.ZipFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 59 with WebScriptException

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

the class AbstractAssociationsGet method executeImpl.

/**
 * Override  method from DeclarativeWebScript
 */
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    String associationFilter = req.getParameter(REQ_URL_TEMPL_VAR_ASSOCIATION_FILTER);
    String namespacePrefix = req.getParameter(REQ_URL_TEMPL_VAR_NAMESPACE_PREFIX);
    String name = req.getParameter(REQ_URL_TEMPL_VAR_NAME);
    Map<String, Object> model = new HashMap<String, Object>();
    Map<QName, AssociationDefinition> assocdef = new HashMap<QName, AssociationDefinition>();
    QName associationQname = null;
    QName classQname = getClassQname(req);
    if (associationFilter == null) {
        associationFilter = "all";
    }
    // validate association filter
    if (isValidAssociationFilter(associationFilter) == false) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Check the associationFilter - " + associationFilter + " - parameter in the URL");
    }
    // validate  for the presence of both name and namespaceprefix
    if ((name == null && namespacePrefix != null) || (name != null && namespacePrefix == null)) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Missing either name or namespaceprefix parameter in the URL - both combination of name and namespaceprefix is needed");
    }
    // check for association filters
    if (associationFilter.equals("child")) {
        model.put(MODEL_PROP_KEY_ASSOCIATION_DETAILS, this.dictionaryservice.getClass(classQname).getChildAssociations().values());
    } else if (associationFilter.equals("general")) {
        for (AssociationDefinition assocname : this.dictionaryservice.getClass(classQname).getAssociations().values()) {
            if (assocname.isChild() == false) {
                assocdef.put(assocname.getName(), assocname);
            }
        }
        model.put(MODEL_PROP_KEY_ASSOCIATION_DETAILS, assocdef.values());
    } else if (associationFilter.equals("all")) {
        model.put(MODEL_PROP_KEY_ASSOCIATION_DETAILS, this.dictionaryservice.getClass(classQname).getAssociations().values());
    }
    // if both namespaceprefix and name parameters are given then, the combination namespaceprefix_name is used as the index to create the qname
    if (name != null && namespacePrefix != null) {
        // validate the class combination namespaceprefix_name
        associationQname = getAssociationQname(namespacePrefix, name);
        if (this.dictionaryservice.getClass(classQname).getAssociations().get(associationQname) == null) {
            throw new WebScriptException(Status.STATUS_NOT_FOUND, "not a Valid - namespaceprefix_name combination");
        }
        model.put(MODEL_PROP_KEY_INDIVIDUAL_PROPERTY_DEFS, this.dictionaryservice.getClass(classQname).getAssociations().get(associationQname));
    }
    model.put(MODEL_PROP_KEY_MESSAGE_LOOKUP, this.dictionaryservice);
    return model;
}
Also used : AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName)

Example 60 with WebScriptException

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

the class AbstractClassesGet method executeImpl.

/**
 * Override method from DeclarativeWebScript
 */
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    String classFilter = getValidInput(req.getParameter(REQ_URL_TEMPL_VAR_CLASS_FILTER));
    String namespacePrefix = getValidInput(req.getParameter(REQ_URL_TEMPL_VAR_NAMESPACE_PREFIX));
    String name = getValidInput(req.getParameter(REQ_URL_TEMPL_VAR_NAME));
    Map<QName, ClassDefinition> classdef = new HashMap<QName, ClassDefinition>();
    Map<QName, Collection<PropertyDefinition>> propdef = new HashMap<QName, Collection<PropertyDefinition>>();
    Map<QName, Collection<AssociationDefinition>> assocdef = new HashMap<QName, Collection<AssociationDefinition>>();
    Map<String, Object> model = new HashMap<String, Object>();
    List<QName> qnames = new ArrayList<QName>();
    QName classQname = null;
    QName myModel = null;
    // if classfilter is not given, then it defaults to all
    if (classFilter == null) {
        classFilter = "all";
    }
    // validate classfilter
    if (isValidClassFilter(classFilter) == false) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Check the classfilter - " + classFilter + " provided in the URL");
    }
    // name alone has no meaning without namespaceprefix
    if (namespacePrefix == null && name != null) {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "Missing namespaceprefix parameter in the URL - both combination of name and namespaceprefix is needed");
    }
    // validate the namespaceprefix and name parameters => if namespaceprefix is given, then name has to be validated along with it
    if (namespacePrefix != null) {
        // validate name parameter if present along with the namespaceprefix
        if (name != null) {
            classQname = getClassQname(namespacePrefix, name);
            classdef.put(classQname, this.dictionaryservice.getClass(classQname));
            propdef.put(classQname, this.dictionaryservice.getClass(classQname).getProperties().values());
            assocdef.put(classQname, this.dictionaryservice.getClass(classQname).getAssociations().values());
        } else {
            // if name is not given then the model is extracted from the namespaceprefix, there can be more than one model associated with one namespaceprefix
            String namespaceUri = namespaceService.getNamespaceURI(namespacePrefix);
            for (QName qnameObj : this.dictionaryservice.getAllModels()) {
                if (qnameObj.getNamespaceURI().equals(namespaceUri)) {
                    name = qnameObj.getLocalName();
                    myModel = getQNameForModel(namespacePrefix, name);
                    // check the classfilter to pull out either all or type or aspects
                    if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE1)) {
                        qnames.addAll(this.dictionaryservice.getAspects(myModel));
                        qnames.addAll(this.dictionaryservice.getTypes(myModel));
                    } else if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE3)) {
                        qnames.addAll(this.dictionaryservice.getTypes(myModel));
                    } else if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE2)) {
                        qnames.addAll(this.dictionaryservice.getAspects(myModel));
                    }
                }
            }
        }
    }
    // if namespacePrefix is null, then check the classfilter to pull out either all or type or aspects
    if (myModel == null) {
        if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE1)) {
            qnames.addAll(this.dictionaryservice.getAllAspects());
            qnames.addAll(this.dictionaryservice.getAllTypes());
        } else if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE3)) {
            qnames.addAll(this.dictionaryservice.getAllTypes());
        } else if (classFilter.equalsIgnoreCase(CLASS_FILTER_OPTION_TYPE2)) {
            qnames.addAll(this.dictionaryservice.getAllAspects());
        }
    }
    if (classdef.isEmpty() == true) {
        for (QName qnameObj : qnames) {
            classdef.put(qnameObj, this.dictionaryservice.getClass(qnameObj));
            propdef.put(qnameObj, this.dictionaryservice.getClass(qnameObj).getProperties().values());
            assocdef.put(qnameObj, this.dictionaryservice.getClass(qnameObj).getAssociations().values());
        }
    }
    List<ClassDefinition> classDefinitions = new ArrayList<ClassDefinition>(classdef.values());
    Collections.sort(classDefinitions, new DictionaryComparators.ClassDefinitionComparator(dictionaryservice));
    model.put(MODEL_PROP_KEY_CLASS_DEFS, classDefinitions);
    model.put(MODEL_PROP_KEY_PROPERTY_DETAILS, reorderedValues(classDefinitions, propdef));
    model.put(MODEL_PROP_KEY_ASSOCIATION_DETAILS, reorderedValues(classDefinitions, assocdef));
    model.put(MODEL_PROP_KEY_MESSAGE_LOOKUP, dictionaryservice);
    return model;
}
Also used : HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) ClassDefinition(org.alfresco.service.cmr.dictionary.ClassDefinition) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Collection(java.util.Collection)

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