mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged HEAD-BUG-FIX (5.0/Cloud) to HEAD (5.0/Cloud)
80736: Merged WAT1 (5.0/Cloud) to HEAD-BUG-FIX (5.0/Cloud) 78923: ACE-2370: Changed facet config web-scripts URL from /api/solr/facet-config TO /api/facet/facet-config to fix the facets retrieval when Solr SSL is enabled. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@83034 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -1,229 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2014 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.repo.web.scripts.solr.facet;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.repo.search.impl.solr.facet.SolrFacetModel;
|
||||
import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties;
|
||||
import org.alfresco.repo.search.impl.solr.facet.SolrFacetService;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.DeclarativeWebScript;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* This class is an abstract base class for the various web script controllers
|
||||
* in the SolrFacetService.
|
||||
*
|
||||
* @author Jamal Kaabi-Mofrad
|
||||
*/
|
||||
public abstract class AbstractSolrFacetConfigAdminWebScript extends DeclarativeWebScript
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(AbstractSolrFacetConfigAdminWebScript.class);
|
||||
|
||||
protected static final String PARAM_FILTER_ID = "filterID";
|
||||
protected static final String PARAM_FACET_QNAME = "facetQName";
|
||||
protected static final String PARAM_DISPLAY_NAME = "displayName";
|
||||
protected static final String PARAM_DISPLAY_CONTROL = "displayControl";
|
||||
protected static final String PARAM_MAX_FILTERS = "maxFilters";
|
||||
protected static final String PARAM_HIT_THRESHOLD = "hitThreshold";
|
||||
protected static final String PARAM_MIN_FILTER_VALUE_LENGTH = "minFilterValueLength";
|
||||
protected static final String PARAM_SORT_BY = "sortBy";
|
||||
protected static final String PARAM_SCOPE = "scope";
|
||||
protected static final String PARAM_SCOPED_SITES = "scopedSites";
|
||||
protected static final String PARAM_INDEX = "index";
|
||||
protected static final String PARAM_IS_ENABLED = "isEnabled";
|
||||
protected static final String PARAM_CUSTOM_PROPERTIES = "customProperties";
|
||||
protected static final String CUSTOM_PARAM_NAME = "name";
|
||||
protected static final String CUSTOM_PARAM_VALUE = "value";
|
||||
|
||||
protected SolrFacetService facetService;
|
||||
|
||||
/**
|
||||
* @param facetService the facetService to set
|
||||
*/
|
||||
public void setFacetService(SolrFacetService facetService)
|
||||
{
|
||||
this.facetService = facetService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(final WebScriptRequest req, final Status status, final Cache cache)
|
||||
{
|
||||
validateCurrentUser();
|
||||
|
||||
return unprotectedExecuteImpl(req, status, cache);
|
||||
}
|
||||
|
||||
protected void validateCurrentUser()
|
||||
{
|
||||
String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
|
||||
// check the current user access rights
|
||||
if (!facetService.isSearchAdmin(currentUser))
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "Access denied.");
|
||||
}
|
||||
}
|
||||
|
||||
protected <T> T getValue(Class<T> clazz, Object value, T defaultValue) throws JSONException
|
||||
{
|
||||
if (JSONObject.NULL.equals(value))
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return clazz.cast(value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JSONException("JSONObject[" + value +"] is not an instance of [" + clazz.getName() +"]");
|
||||
}
|
||||
}
|
||||
|
||||
protected Set<CustomProperties> getCustomProperties(JSONObject customPropsJsonObj) throws JSONException
|
||||
{
|
||||
if (customPropsJsonObj == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
JSONArray keys = customPropsJsonObj.names();
|
||||
if (keys == null)
|
||||
{
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
Set<CustomProperties> customProps = new HashSet<>(keys.length());
|
||||
for (int i = 0, length = keys.length(); i < length; i++)
|
||||
{
|
||||
JSONObject jsonObj = customPropsJsonObj.getJSONObject((String) keys.get(i));
|
||||
|
||||
QName name = resolveToQName(getValue(String.class, jsonObj.opt(CUSTOM_PARAM_NAME), null));
|
||||
validateMandatoryCustomProps(name, CUSTOM_PARAM_NAME);
|
||||
|
||||
Serializable value = null;
|
||||
Object customPropValue = jsonObj.opt(CUSTOM_PARAM_VALUE);
|
||||
validateMandatoryCustomProps(customPropValue, CUSTOM_PARAM_VALUE);
|
||||
|
||||
if(customPropValue instanceof JSONArray)
|
||||
{
|
||||
JSONArray array = (JSONArray) customPropValue;
|
||||
ArrayList<Serializable> list = new ArrayList<>(array.length());
|
||||
for(int j = 0; j < array.length(); j++)
|
||||
{
|
||||
list.add(getSerializableValue(array.get(j)));
|
||||
}
|
||||
value = list;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = getSerializableValue(customPropValue);
|
||||
}
|
||||
|
||||
customProps.add(new CustomProperties(name, value));
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled() && customProps.size() > 0)
|
||||
{
|
||||
logger.debug("Processed custom properties:" + customProps);
|
||||
}
|
||||
|
||||
return customProps;
|
||||
}
|
||||
|
||||
protected Set<String> getScopedSites(JSONArray scopedSitesJsonArray) throws JSONException
|
||||
{
|
||||
if (scopedSitesJsonArray == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Set<String> scopedSites = new HashSet<String>(scopedSitesJsonArray.length());
|
||||
for (int i = 0, length = scopedSitesJsonArray.length(); i < length; i++)
|
||||
{
|
||||
String site = scopedSitesJsonArray.getString(i);
|
||||
scopedSites.add(site);
|
||||
}
|
||||
return scopedSites;
|
||||
}
|
||||
|
||||
private void validateMandatoryCustomProps(Object obj, String paramName) throws JSONException
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new JSONException("Invalid JSONObject in the Custom Properties JSON. [" + paramName + "] cannot be null.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Serializable getSerializableValue(Object object) throws JSONException
|
||||
{
|
||||
if (!(object instanceof Serializable))
|
||||
{
|
||||
throw new JSONException("Invalid value in the Custom Properties JSON. [" + object + "] must be an instance of Serializable.");
|
||||
}
|
||||
return (Serializable) object;
|
||||
}
|
||||
|
||||
private QName resolveToQName(String qnameStr) throws JSONException
|
||||
{
|
||||
QName typeQName = null;
|
||||
if (qnameStr == null)
|
||||
{
|
||||
return typeQName;
|
||||
}
|
||||
if(qnameStr.charAt(0) == QName.NAMESPACE_BEGIN && qnameStr.indexOf("solrfacetcustomproperty") < 0)
|
||||
{
|
||||
throw new JSONException("Invalid name in the Custom Properties JSON. Namespace URL must be [" + SolrFacetModel.SOLR_FACET_CUSTOM_PROPERTY_URL + "]");
|
||||
}
|
||||
else if(qnameStr.charAt(0) == QName.NAMESPACE_BEGIN)
|
||||
{
|
||||
typeQName = QName.createQName(qnameStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
typeQName = QName.createQName(SolrFacetModel.SOLR_FACET_CUSTOM_PROPERTY_URL, qnameStr);
|
||||
}
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Resolved facet's custom property name [" + qnameStr + "] into [" + typeQName + "]");System.out.println("QQQQQQQQQQQQQQQQQQQQQQQQQQQQQ:Resolved facet's custom property name [" + qnameStr + "] into [" + typeQName + "]");
|
||||
}
|
||||
return typeQName;
|
||||
}
|
||||
|
||||
abstract protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache);
|
||||
}
|
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2014 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.repo.web.scripts.solr.facet;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* This class is the controller for the "solr-facet-config-admin.delete" web script.
|
||||
*
|
||||
* @author Jamal Kaabi-Mofrad
|
||||
*/
|
||||
public class SolrFacetConfigAdminDelete extends AbstractSolrFacetConfigAdminWebScript
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(SolrFacetConfigAdminDelete.class);
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache)
|
||||
{
|
||||
// get the filterID parameter.
|
||||
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
|
||||
String filterID = templateVars.get("filterID");
|
||||
|
||||
if (filterID == null)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Filter id not provided");
|
||||
}
|
||||
facetService.deleteFacet(filterID);
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>(1);
|
||||
model.put("success", true);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Facet [" + filterID + "] has been deleted successfully");
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2014 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.repo.web.scripts.solr.facet;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* This class is the controller for the "solr-facet-config-admin.get" web script.
|
||||
*
|
||||
* @author Jamal Kaabi-Mofrad
|
||||
*/
|
||||
public class SolrFacetConfigAdminGet extends AbstractSolrFacetConfigAdminWebScript
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(SolrFacetConfigAdminGet.class);
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> executeImpl(final WebScriptRequest req, final Status status, final Cache cache)
|
||||
{
|
||||
// Allow all authenticated users view the filters
|
||||
return unprotectedExecuteImpl(req, status, cache);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache)
|
||||
{
|
||||
// get the filterID parameter.
|
||||
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
|
||||
String filterID = templateVars.get("filterID");
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>(1);
|
||||
|
||||
if (filterID == null)
|
||||
{
|
||||
model.put("filters", facetService.getFacets());
|
||||
}
|
||||
else
|
||||
{
|
||||
SolrFacetProperties fp = facetService.getFacet(filterID);
|
||||
if (fp == null)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Filter not found");
|
||||
}
|
||||
model.put("filter", fp);
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Retrieved all available facets: " + model.values());
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
@@ -1,120 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2014 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.repo.web.scripts.solr.facet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties;
|
||||
import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* This class is the controller for the "solr-facet-config-admin.post" web scripts.
|
||||
*
|
||||
* @author Jamal Kaabi-Mofrad
|
||||
*/
|
||||
public class SolrFacetConfigAdminPost extends AbstractSolrFacetConfigAdminWebScript
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(SolrFacetConfigAdminPost.class);
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache)
|
||||
{
|
||||
try
|
||||
{
|
||||
SolrFacetProperties fp = parseRequestForFacetProperties(req);
|
||||
facetService.createFacetNode(fp);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Created facet node: " + fp);
|
||||
}
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not save the facet configuration.", t);
|
||||
}
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>(1);
|
||||
return model;
|
||||
}
|
||||
|
||||
private SolrFacetProperties parseRequestForFacetProperties(WebScriptRequest req)
|
||||
{
|
||||
JSONObject json = null;
|
||||
try
|
||||
{
|
||||
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
|
||||
|
||||
final String filterID = json.getString(PARAM_FILTER_ID);
|
||||
final String facetQNameStr = json.getString(PARAM_FACET_QNAME);
|
||||
final QName facetQName = QName.createQName(facetQNameStr);
|
||||
final String displayName = json.getString(PARAM_DISPLAY_NAME);
|
||||
final String displayControl = json.getString(PARAM_DISPLAY_CONTROL);
|
||||
final int maxFilters = json.getInt(PARAM_MAX_FILTERS);
|
||||
final int hitThreshold = json.getInt(PARAM_HIT_THRESHOLD);
|
||||
final int minFilterValueLength = json.getInt(PARAM_MIN_FILTER_VALUE_LENGTH);
|
||||
final String sortBy = json.getString(PARAM_SORT_BY);
|
||||
// Optional params
|
||||
final String scope = getValue(String.class, json.opt(PARAM_SCOPE), "ALL");
|
||||
final boolean isEnabled = getValue(Boolean.class, json.opt(PARAM_IS_ENABLED), false);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,178 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2014 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.repo.web.scripts.solr.facet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.alfresco.repo.search.impl.solr.facet.Exceptions.UnrecognisedFacetId;
|
||||
import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties.CustomProperties;
|
||||
import org.alfresco.repo.search.impl.solr.facet.SolrFacetProperties;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.collections.CollectionUtils;
|
||||
import org.alfresco.util.collections.Function;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* This class is the controller for the "solr-facet-config-admin.put" web scripts.
|
||||
*
|
||||
* @author Jamal Kaabi-Mofrad
|
||||
* @author Neil Mc Erlean
|
||||
* @since 5.0
|
||||
*/
|
||||
public class SolrFacetConfigAdminPut extends AbstractSolrFacetConfigAdminWebScript
|
||||
{
|
||||
private static final Log logger = LogFactory.getLog(SolrFacetConfigAdminPost.class);
|
||||
|
||||
protected static final String PARAM_RELATIVE_POS = "relativePos";
|
||||
protected static final String URL_PARAM_FILTER_ID = "filterID";
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> unprotectedExecuteImpl(WebScriptRequest req, Status status, Cache cache)
|
||||
{
|
||||
final String relativePosString = req.getParameter(PARAM_RELATIVE_POS);
|
||||
try
|
||||
{
|
||||
if (relativePosString != null)
|
||||
{
|
||||
// This is a request to 'move' (reposition) the specified facet.
|
||||
|
||||
// We need the relative position that the facet will move.
|
||||
final int relativePos;
|
||||
|
||||
try { relativePos = Integer.parseInt(relativePosString); }
|
||||
catch (NumberFormatException nfe)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
|
||||
"Cannot move facet as could not parse relative position: '" + relativePosString + "'");
|
||||
}
|
||||
|
||||
// And we need the filterID for the facet we're moving.
|
||||
final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
|
||||
String filterId = templateVars.get(URL_PARAM_FILTER_ID);
|
||||
|
||||
if (filterId == null) { throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Illegal null filterId"); }
|
||||
|
||||
// So let's move the filter...
|
||||
try
|
||||
{
|
||||
// Get the current sequence of filter IDs.
|
||||
List<SolrFacetProperties> facets = facetService.getFacets();
|
||||
List<String> facetIDs = CollectionUtils.transform(facets, new Function<SolrFacetProperties, String>()
|
||||
{
|
||||
@Override public String apply(SolrFacetProperties value)
|
||||
{
|
||||
return value.getFilterID();
|
||||
}
|
||||
});
|
||||
|
||||
List<String> reorderedIDs = CollectionUtils.moveRight(relativePos, filterId, facetIDs);
|
||||
|
||||
this.facetService.reorderFacets(reorderedIDs);
|
||||
|
||||
if (logger.isDebugEnabled()) { logger.debug("Moved facet " + filterId + " to relative position: " + relativePos); }
|
||||
}
|
||||
catch (UnrecognisedFacetId ufi)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Unrecognised filter ID: " + ufi.getFacetId());
|
||||
}
|
||||
}
|
||||
// TODO Allow for simultaneous move and update of facet.
|
||||
else
|
||||
{
|
||||
SolrFacetProperties fp = parseRequestForFacetProperties(req);
|
||||
facetService.updateFacet(fp);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Updated facet node: " + fp);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not update the facet configuration.", t);
|
||||
}
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>(1);
|
||||
return model;
|
||||
}
|
||||
|
||||
private SolrFacetProperties parseRequestForFacetProperties(WebScriptRequest req)
|
||||
{
|
||||
JSONObject json = null;
|
||||
try
|
||||
{
|
||||
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
|
||||
|
||||
final String filterID = json.getString(PARAM_FILTER_ID); // Must exist
|
||||
|
||||
final String facetQNameStr = getValue(String.class, json.opt(PARAM_FACET_QNAME), null);
|
||||
final QName facetQName = (facetQNameStr == null) ? null : QName.createQName(facetQNameStr);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user