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)
84919: Merged PLATFORM1 (5.0/Cloud) to HEAD-BUG-FIX (5.0/Cloud) 83447: ACE-2612: Initial implementation of the Suggester service to support the auto-suggest web script. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@85237 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -106,19 +106,28 @@ public class SolrAdminHTTPClient
|
||||
this.httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
public JSONObject execute(HashMap<String, String>args)
|
||||
{
|
||||
public JSONObject execute(HashMap<String, String> args)
|
||||
{
|
||||
return execute(adminUrl, args);
|
||||
}
|
||||
|
||||
public JSONObject execute(String relativeHandlerPath, HashMap<String, String> args)
|
||||
{
|
||||
ParameterCheck.mandatory("relativeHandlerPath", relativeHandlerPath);
|
||||
ParameterCheck.mandatory("args", args);
|
||||
|
||||
String path = getPath(relativeHandlerPath);
|
||||
try
|
||||
{
|
||||
{
|
||||
URLCodec encoder = new URLCodec();
|
||||
StringBuilder url = new StringBuilder();
|
||||
|
||||
for(String key : args.keySet())
|
||||
|
||||
for (String key : args.keySet())
|
||||
{
|
||||
String value = args.get(key);
|
||||
if(url.length() == 0)
|
||||
if (url.length() == 0)
|
||||
{
|
||||
url.append(adminUrl);
|
||||
url.append(path);
|
||||
url.append("?");
|
||||
url.append(encoder.encode(key, "UTF-8"));
|
||||
url.append("=");
|
||||
@@ -129,27 +138,27 @@ public class SolrAdminHTTPClient
|
||||
url.append("&");
|
||||
url.append(encoder.encode(key, "UTF-8"));
|
||||
url.append("=");
|
||||
url.append(encoder.encode(value, "UTF-8"));
|
||||
url.append(encoder.encode(value, "UTF-8"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//PostMethod post = new PostMethod(url.toString());
|
||||
|
||||
// PostMethod post = new PostMethod(url.toString());
|
||||
GetMethod get = new GetMethod(url.toString());
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
httpClient.executeMethod(get);
|
||||
|
||||
if(get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY)
|
||||
if (get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY)
|
||||
{
|
||||
Header locationHeader = get.getResponseHeader("location");
|
||||
if (locationHeader != null)
|
||||
{
|
||||
String redirectLocation = locationHeader.getValue();
|
||||
get.setURI(new URI(redirectLocation, true));
|
||||
httpClient.executeMethod(get);
|
||||
}
|
||||
Header locationHeader = get.getResponseHeader("location");
|
||||
if (locationHeader != null)
|
||||
{
|
||||
String redirectLocation = locationHeader.getValue();
|
||||
get.setURI(new URI(redirectLocation, true));
|
||||
httpClient.executeMethod(get);
|
||||
}
|
||||
}
|
||||
|
||||
if (get.getStatusCode() != HttpServletResponse.SC_OK)
|
||||
@@ -185,4 +194,20 @@ public class SolrAdminHTTPClient
|
||||
}
|
||||
}
|
||||
|
||||
private String getPath(String path)
|
||||
{
|
||||
if (path.startsWith(baseUrl))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
else if (path.startsWith("/"))
|
||||
{
|
||||
return baseUrl + path;
|
||||
}
|
||||
else
|
||||
{
|
||||
return baseUrl + '/' + path;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.search.impl.solr;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.repo.search.impl.lucene.SolrSuggesterResult;
|
||||
import org.alfresco.service.cmr.search.SuggesterResult;
|
||||
import org.alfresco.service.cmr.search.SuggesterService;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Solr Suggester Service Implementation.
|
||||
*
|
||||
* @author Jamal Kaabi-Mofrad
|
||||
* @since 5.0
|
||||
*/
|
||||
public class SolrSuggesterServiceImpl implements SuggesterService
|
||||
{
|
||||
|
||||
public static final String SUGGESER_PATH = "/alfresco/suggest";
|
||||
|
||||
private boolean enabled;
|
||||
private SolrAdminHTTPClient solrAdminHTTPClient;
|
||||
|
||||
public void setEnabled(boolean isEnabled)
|
||||
{
|
||||
this.enabled = isEnabled;
|
||||
}
|
||||
|
||||
public void setSolrAdminHTTPClient(SolrAdminHTTPClient solrAdminHTTPClient)
|
||||
{
|
||||
this.solrAdminHTTPClient = solrAdminHTTPClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled()
|
||||
{
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SuggesterResult getSuggestions(String term, int limit)
|
||||
{
|
||||
// if it is not enabled, return an empty result set
|
||||
if (!enabled)
|
||||
{
|
||||
return new SolrSuggesterResult();
|
||||
}
|
||||
try
|
||||
{
|
||||
HashMap<String, String> params = new HashMap<>(3);
|
||||
params.put("q", term);
|
||||
if (limit > 0)
|
||||
{
|
||||
params.put("suggest.count", Integer.toString(limit));
|
||||
}
|
||||
params.put("wt", "json");
|
||||
|
||||
JSONObject response = solrAdminHTTPClient.execute(SUGGESER_PATH, params);
|
||||
return new SolrSuggesterResult(response);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("SolrSuggester failed.", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user