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);
}
}
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");
}
}
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;
}
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;
}
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;
}
Aggregations