ACS-9009 REST API for setting preferences

This commit is contained in:
Piotr Żurek
2024-11-20 14:26:51 +01:00
parent 1c52d5036f
commit ccebc53c0f
3 changed files with 237 additions and 235 deletions

View File

@@ -1,112 +1,112 @@
/* /*
* #%L * #%L
* Alfresco Remote API * Alfresco Remote API
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.rest.api.impl; package org.alfresco.rest.api.impl;
import org.alfresco.query.PagingResults; import java.io.Serializable;
import org.alfresco.rest.api.People; import java.util.ArrayList;
import org.alfresco.rest.api.Preferences; import java.util.HashMap;
import org.alfresco.rest.api.model.Preference; import java.util.List;
import org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException; import java.util.Map;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.query.PagingResults;
import org.alfresco.service.cmr.preference.PreferenceService; import org.alfresco.rest.api.People;
import org.alfresco.util.Pair; import org.alfresco.rest.api.Preferences;
import org.alfresco.rest.api.model.Preference;
import java.io.Serializable; import org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException;
import java.util.ArrayList; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import java.util.HashMap; import org.alfresco.rest.framework.resource.parameters.Paging;
import java.util.List; import org.alfresco.service.cmr.preference.PreferenceService;
import java.util.Map; import org.alfresco.util.Pair;
/** /**
* Centralises access to preference services and maps between representations. * Centralises access to preference services and maps between representations.
* *
* @author steveglover * @author steveglover
* @since publicapi1.0 * @since publicapi1.0
*/ */
public class PreferencesImpl implements Preferences public class PreferencesImpl implements Preferences
{ {
private People people; private People people;
private PreferenceService preferenceService; private PreferenceService preferenceService;
public void setPeople(People people) public void setPeople(People people)
{ {
this.people = people; this.people = people;
} }
public void setPreferenceService(PreferenceService preferenceService) public void setPreferenceService(PreferenceService preferenceService)
{ {
this.preferenceService = preferenceService; this.preferenceService = preferenceService;
} }
public Preference getPreference(String personId, String preferenceName) public Preference getPreference(String personId, String preferenceName)
{ {
personId = people.validatePerson(personId); personId = people.validatePerson(personId);
Serializable preferenceValue = preferenceService.getPreference(personId, preferenceName); Serializable preferenceValue = preferenceService.getPreference(personId, preferenceName);
if (preferenceValue != null) if (preferenceValue != null)
{ {
return new Preference(preferenceName, preferenceValue); return new Preference(preferenceName, preferenceValue);
} }
else else
{ {
throw new RelationshipResourceNotFoundException(personId, preferenceName); throw new RelationshipResourceNotFoundException(personId, preferenceName);
} }
} }
public CollectionWithPagingInfo<Preference> getPreferences(String personId, Paging paging) public CollectionWithPagingInfo<Preference> getPreferences(String personId, Paging paging)
{ {
personId = people.validatePerson(personId); personId = people.validatePerson(personId);
PagingResults<Pair<String, Serializable>> preferences = preferenceService.getPagedPreferences(personId, null, Util.getPagingRequest(paging)); PagingResults<Pair<String, Serializable>> preferences = preferenceService.getPagedPreferences(personId, null, Util.getPagingRequest(paging));
List<Preference> ret = new ArrayList<Preference>(preferences.getPage().size()); List<Preference> ret = new ArrayList<Preference>(preferences.getPage().size());
for (Pair<String, Serializable> prefEntity : preferences.getPage()) for (Pair<String, Serializable> prefEntity : preferences.getPage())
{ {
Preference pref = new Preference(prefEntity.getFirst(), prefEntity.getSecond()); Preference pref = new Preference(prefEntity.getFirst(), prefEntity.getSecond());
ret.add(pref); ret.add(pref);
} }
return CollectionWithPagingInfo.asPaged(paging, ret, preferences.hasMoreItems(), preferences.getTotalResultCount().getFirst()); return CollectionWithPagingInfo.asPaged(paging, ret, preferences.hasMoreItems(), preferences.getTotalResultCount().getFirst());
} }
@Override @Override
public Preference updatePreference(String personId, Preference preference) public Preference updatePreference(String personId, Preference preference)
{ {
personId = people.validatePerson(personId, true); personId = people.validatePerson(personId, true);
final Map<String, Serializable> preferencesToSet; final Map<String, Serializable> preferencesToSet;
if (preference.getValue() == null || "".equals(preference.getValue())) if (preference.getValue() == null || "".equals(preference.getValue()))
{ {
preferencesToSet = new HashMap<>(1); preferencesToSet = new HashMap<>(1);
preferencesToSet.put(preference.getName(), null); preferencesToSet.put(preference.getName(), null);
} }
else else
{ {
preferencesToSet = Map.of(preference.getName(), preference.getValue()); preferencesToSet = Map.of(preference.getName(), preference.getValue());
} }
preferenceService.setPreferences(personId, preferencesToSet); preferenceService.setPreferences(personId, preferencesToSet);
return new Preference(preference.getName(), preferenceService.getPreference(personId, preference.getName())); return new Preference(preference.getName(), preferenceService.getPreference(personId, preference.getName()));
} }
} }

View File

@@ -1,105 +1,106 @@
/* /*
* #%L * #%L
* Alfresco Remote API * Alfresco Remote API
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.rest.api.people; package org.alfresco.rest.api.people;
import org.alfresco.rest.api.Preferences; import org.apache.commons.logging.Log;
import org.alfresco.rest.api.model.Preference; import org.apache.commons.logging.LogFactory;
import org.alfresco.rest.framework.WebApiDescription; import org.springframework.beans.factory.InitializingBean;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.RelationshipResource; import org.alfresco.rest.api.Preferences;
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction; import org.alfresco.rest.api.model.Preference;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.WebApiDescription;
import org.alfresco.rest.framework.resource.parameters.Parameters; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.util.ParameterCheck; import org.alfresco.rest.framework.resource.RelationshipResource;
import org.apache.commons.logging.Log; import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
import org.apache.commons.logging.LogFactory; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.springframework.beans.factory.InitializingBean; import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.util.ParameterCheck;
@RelationshipResource(name = "preferences", entityResource = PeopleEntityResource.class, title = "Person Preferences")
public class PersonPreferencesRelation implements RelationshipResourceAction.Read<Preference>, RelationshipResourceAction.ReadById<Preference>, RelationshipResourceAction.Update<Preference>, InitializingBean @RelationshipResource(name = "preferences", entityResource = PeopleEntityResource.class, title = "Person Preferences")
{ public class PersonPreferencesRelation implements RelationshipResourceAction.Read<Preference>, RelationshipResourceAction.ReadById<Preference>, RelationshipResourceAction.Update<Preference>, InitializingBean
private static final Log logger = LogFactory.getLog(PersonPreferencesRelation.class); {
private static final Log logger = LogFactory.getLog(PersonPreferencesRelation.class);
private Preferences preferences;
private Preferences preferences;
public void setPreferences(Preferences preferences)
{ public void setPreferences(Preferences preferences)
this.preferences = preferences; {
} this.preferences = preferences;
}
@Override
public void afterPropertiesSet() @Override
{ public void afterPropertiesSet()
ParameterCheck.mandatory("preferences", this.preferences); {
} ParameterCheck.mandatory("preferences", this.preferences);
}
/**
* Returns a paged list of preferences for the user personId. /**
* * Returns a paged list of preferences for the user personId.
* If personId does not exist, NotFoundException (status 404). *
* * If personId does not exist, NotFoundException (status 404).
* @see org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction.Read#readAll(java.lang.String, org.alfresco.rest.framework.resource.parameters.Parameters) *
*/ * @see org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction.Read#readAll(java.lang.String, org.alfresco.rest.framework.resource.parameters.Parameters)
@Override */
@WebApiDescription(title = "A paged list of the persons preferences.") @Override
public CollectionWithPagingInfo<Preference> readAll(String personId, Parameters parameters) @WebApiDescription(title = "A paged list of the persons preferences.")
{ public CollectionWithPagingInfo<Preference> readAll(String personId, Parameters parameters)
return preferences.getPreferences(personId, parameters.getPaging()); {
} return preferences.getPreferences(personId, parameters.getPaging());
}
/**
* Returns information regarding the preference 'preferenceName' for user personId. /**
* * Returns information regarding the preference 'preferenceName' for user personId.
* If personId does not exist, NotFoundException (status 404). *
* * If personId does not exist, NotFoundException (status 404).
* @see org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction.ReadById#readById(String, String, org.alfresco.rest.framework.resource.parameters.Parameters) *
*/ * @see org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction.ReadById#readById(String, String, org.alfresco.rest.framework.resource.parameters.Parameters)
@Override */
@WebApiDescription(title = "Preference value for preference 'preferenceName' for person 'personId'.") @Override
public Preference readById(String personId, String preferenceName, Parameters parameters) @WebApiDescription(title = "Preference value for preference 'preferenceName' for person 'personId'.")
{ public Preference readById(String personId, String preferenceName, Parameters parameters)
// fix for REPO-855 {
String url = parameters.getRequest().getURL(); // fix for REPO-855
if (url.matches(".*workspace://SpacesStore/.*")) String url = parameters.getRequest().getURL();
{ if (url.matches(".*workspace://SpacesStore/.*"))
preferenceName += url.substring(url.indexOf("//SpacesStore/")); {
} preferenceName += url.substring(url.indexOf("//SpacesStore/"));
// }
//
return preferences.getPreference(personId, preferenceName);
} return preferences.getPreference(personId, preferenceName);
}
@Override
@WebApiDescription(title = "Upsert preference value for person 'personId'.") @Override
public Preference update(String personId, Preference preference, Parameters parameters) @WebApiDescription(title = "Upsert preference value for person 'personId'.")
{ public Preference update(String personId, Preference preference, Parameters parameters)
if (preference.getName() == null || preference.getName().isBlank()) {
{ if (preference.getName() == null || preference.getName().isBlank())
throw new InvalidArgumentException(); {
} throw new InvalidArgumentException();
return preferences.updatePreference(personId, preference); }
} return preferences.updatePreference(personId, preference);
} }
}

View File

@@ -25,6 +25,20 @@
*/ */
package org.alfresco.rest.framework.webscripts; package org.alfresco.rest.framework.webscripts;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WrappingWebScriptRequest;
import org.springframework.extensions.webscripts.servlet.WebScriptServletRequest;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.rest.framework.core.ResourceLocator; import org.alfresco.rest.framework.core.ResourceLocator;
import org.alfresco.rest.framework.core.ResourceMetadata; import org.alfresco.rest.framework.core.ResourceMetadata;
@@ -42,19 +56,6 @@ import org.alfresco.rest.framework.resource.parameters.Params;
import org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams; import org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams;
import org.alfresco.rest.framework.tools.RecognizedParamsExtractor; import org.alfresco.rest.framework.tools.RecognizedParamsExtractor;
import org.alfresco.rest.framework.tools.RequestReader; import org.alfresco.rest.framework.tools.RequestReader;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WrappingWebScriptRequest;
import org.springframework.extensions.webscripts.servlet.WebScriptServletRequest;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Map;
/** /**
* Handles the HTTP PUT for a Resource, equivalent to CRUD Update * Handles the HTTP PUT for a Resource, equivalent to CRUD Update
@@ -131,7 +132,7 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
} }
} }
//Fall through to unsupported. // Fall through to unsupported.
default: default:
throw new UnsupportedResourceOperationException("PUT not supported for this request."); throw new UnsupportedResourceOperationException("PUT not supported for this request.");
} }
@@ -141,7 +142,7 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
* Returns the basic content info from the request. * Returns the basic content info from the request.
* *
* @param req * @param req
* WebScriptRequest * WebScriptRequest
* @return BasicContentInfo * @return BasicContentInfo
*/ */
private BasicContentInfo getContentInfo(WebScriptRequest req) private BasicContentInfo getContentInfo(WebScriptRequest req)
@@ -167,7 +168,7 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
* Returns the input stream for the request * Returns the input stream for the request
* *
* @param req * @param req
* WebScriptRequest * WebScriptRequest
* @return InputStream * @return InputStream
*/ */
private InputStream getStream(WebScriptRequest req) private InputStream getStream(WebScriptRequest req)
@@ -196,9 +197,9 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
* Executes the action on the resource * Executes the action on the resource
* *
* @param resource * @param resource
* ResourceWithMetadata * ResourceWithMetadata
* @param params * @param params
* parameters to use * parameters to use
* @return anObject the result of the execute * @return anObject the result of the execute
*/ */
@Override @Override