mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
- Spring driven (configure in new web api methods)
- Support for authentication - HTTP Basic authentication implementation git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4645 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
34
config/alfresco/web-api-application-context.xml
Normal file
34
config/alfresco/web-api-application-context.xml
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
|
||||||
|
|
||||||
|
<beans>
|
||||||
|
|
||||||
|
<!-- -->
|
||||||
|
<!-- Web API Authentication -->
|
||||||
|
<!-- -->
|
||||||
|
|
||||||
|
<!-- TODO: For now, experiment with HTTP Basic Authentication -->
|
||||||
|
<alias alias="web.api.Authentication" name="web.api.BasicAuthentication" />
|
||||||
|
|
||||||
|
<bean id="web.api.BasicAuthentication" class="org.alfresco.web.api.BasicAuthentication">
|
||||||
|
<property name="authenticationService" ref="AuthenticationService" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- -->
|
||||||
|
<!-- Web API Services -->
|
||||||
|
<!-- -->
|
||||||
|
|
||||||
|
<bean id="web.api.TextSearchDescription" class="org.alfresco.web.api.services.TextSearchDescription">
|
||||||
|
<property name="httpUri" value="/search/textsearchdescription.xml" />
|
||||||
|
<property name="templateService" ref="TemplateService" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="web.api.TextSearch" class="org.alfresco.web.api.services.TextSearch">
|
||||||
|
<property name="httpUri" value="/search/text" />
|
||||||
|
<property name="serviceRegistry" ref="ServiceRegistry" />
|
||||||
|
<property name="searchService" ref="SearchService" />
|
||||||
|
<property name="templateService" ref="TemplateService"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
</beans>
|
@@ -37,6 +37,16 @@ public class APIRequest extends HttpServletRequestWrapper
|
|||||||
// TODO: Complete list...
|
// TODO: Complete list...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enumeration of "required" Authentication level
|
||||||
|
*/
|
||||||
|
public enum RequiredAuthentication
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
Guest,
|
||||||
|
User
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct
|
* Construct
|
||||||
|
@@ -18,8 +18,6 @@ package org.alfresco.web.api;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API Service
|
* API Service
|
||||||
*
|
*
|
||||||
@@ -29,13 +27,26 @@ public interface APIService
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialise service
|
* Gets the required authentication level for execution of this service
|
||||||
*
|
*
|
||||||
* @param context
|
* @return the required authentication level
|
||||||
*/
|
*/
|
||||||
public void init(ServletContext context);
|
public APIRequest.RequiredAuthentication getRequiredAuthentication();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the HTTP method this service is bound to
|
||||||
|
*
|
||||||
|
* @return HTTP method
|
||||||
|
*/
|
||||||
|
public APIRequest.HttpMethod getHttpMethod();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the HTTP uri this service is bound to
|
||||||
|
*
|
||||||
|
* @return HTTP uri
|
||||||
|
*/
|
||||||
|
public String getHttpUri();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute service
|
* Execute service
|
||||||
*
|
*
|
||||||
|
104
source/java/org/alfresco/web/api/APIServiceMap.java
Normal file
104
source/java/org/alfresco/web/api/APIServiceMap.java
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2005 Alfresco, Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Mozilla Public License version 1.1
|
||||||
|
* with a permitted attribution clause. You may obtain a
|
||||||
|
* copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.alfresco.org/legal/license.txt
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific
|
||||||
|
* language governing permissions and limitations under the
|
||||||
|
* License.
|
||||||
|
*/
|
||||||
|
package org.alfresco.web.api;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.aopalliance.intercept.MethodInterceptor;
|
||||||
|
import org.springframework.aop.framework.ProxyFactory;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registry of Web API Services methods
|
||||||
|
*
|
||||||
|
* @author davidc
|
||||||
|
*/
|
||||||
|
public class APIServiceMap
|
||||||
|
{
|
||||||
|
// TODO: Support different kinds of uri resolution (e.g. regex:/search/.*)
|
||||||
|
|
||||||
|
private List<APIRequest.HttpMethod> methods = new ArrayList<APIRequest.HttpMethod>();
|
||||||
|
private List<String> uris = new ArrayList<String>();
|
||||||
|
private List<APIService> services = new ArrayList<APIService>();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct list of API Services
|
||||||
|
*
|
||||||
|
* @param context
|
||||||
|
*/
|
||||||
|
public APIServiceMap(ApplicationContext context)
|
||||||
|
{
|
||||||
|
// locate authentication interceptor
|
||||||
|
MethodInterceptor authInterceptor = (MethodInterceptor)context.getBean("web.api.Authentication");
|
||||||
|
|
||||||
|
// register all API Services
|
||||||
|
// NOTE: An API Service is one registered in Spring which supports the APIService interface
|
||||||
|
Map<String, APIService> apiServices = context.getBeansOfType(APIService.class, false, false);
|
||||||
|
for (Map.Entry<String, APIService> apiService : apiServices.entrySet())
|
||||||
|
{
|
||||||
|
APIService service = apiService.getValue();
|
||||||
|
APIRequest.HttpMethod method = service.getHttpMethod();
|
||||||
|
String httpUri = service.getHttpUri();
|
||||||
|
if (httpUri == null || httpUri.length() == 0)
|
||||||
|
{
|
||||||
|
throw new APIException("Web API Service " + apiService.getKey() + " does not specify a HTTP URI mapping");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authInterceptor != null && service.getRequiredAuthentication() != APIRequest.RequiredAuthentication.None)
|
||||||
|
{
|
||||||
|
// wrap API Service in appropriate interceptors (e.g. authentication)
|
||||||
|
ProxyFactory authFactory = new ProxyFactory(service);
|
||||||
|
authFactory.addAdvice(authInterceptor);
|
||||||
|
service = (APIService)authFactory.getProxy();
|
||||||
|
}
|
||||||
|
|
||||||
|
methods.add(method);
|
||||||
|
uris.add(httpUri);
|
||||||
|
services.add(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an API Service given an HTTP Method and URI
|
||||||
|
*
|
||||||
|
* @param method
|
||||||
|
* @param uri
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public APIService get(APIRequest.HttpMethod method, String uri)
|
||||||
|
{
|
||||||
|
APIService apiService = null;
|
||||||
|
|
||||||
|
// TODO: Replace with more efficient approach
|
||||||
|
for (int i = 0; i < services.size(); i++)
|
||||||
|
{
|
||||||
|
if (methods.get(i).equals(method) && uris.get(i).equals(uri))
|
||||||
|
{
|
||||||
|
apiService = services.get(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return apiService;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -17,13 +17,14 @@
|
|||||||
package org.alfresco.web.api;
|
package org.alfresco.web.api;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.alfresco.web.app.servlet.BaseServlet;
|
import org.alfresco.web.app.servlet.BaseServlet;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,31 +36,23 @@ public class APIServlet extends BaseServlet
|
|||||||
{
|
{
|
||||||
private static final long serialVersionUID = 4209892938069597860L;
|
private static final long serialVersionUID = 4209892938069597860L;
|
||||||
|
|
||||||
|
|
||||||
// API Services
|
// API Services
|
||||||
// TODO: Define via configuration
|
private APIServiceMap apiServiceMap;
|
||||||
// TODO: Provide mechanism to construct service specific urls (ideally from template)
|
|
||||||
private static Pattern TEXT_SEARCH_DESCRIPTION_URI = Pattern.compile("/search/textsearchdescription.xml");
|
|
||||||
private static Pattern SEARCH_URI = Pattern.compile("/search/text");
|
|
||||||
private static APIService TEXT_SEARCH_DESCRIPTION_SERVICE;
|
|
||||||
private static APIService TEXT_SEARCH_SERVICE;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() throws ServletException
|
public void init() throws ServletException
|
||||||
{
|
{
|
||||||
super.init();
|
super.init();
|
||||||
|
|
||||||
// TODO: Replace with dispatch mechanism (maybe lazy construct)
|
// Retrieve all web api services and index by http url & http method
|
||||||
TEXT_SEARCH_DESCRIPTION_SERVICE = new TextSearchDescriptionService();
|
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
|
||||||
TEXT_SEARCH_DESCRIPTION_SERVICE.init(getServletContext());
|
apiServiceMap = new APIServiceMap(context);
|
||||||
TEXT_SEARCH_SERVICE = new TextSearchService();
|
|
||||||
TEXT_SEARCH_SERVICE.init(getServletContext());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// - authentication
|
// - authentication (as suggested in http://www.xml.com/pub/a/2003/12/17/dive.html)
|
||||||
// - atom
|
// - atom
|
||||||
// - generator
|
// - generator
|
||||||
// - author (authenticated)
|
// - author (authenticated)
|
||||||
@@ -79,31 +72,24 @@ public class APIServlet extends BaseServlet
|
|||||||
APIRequest request = new APIRequest(req);
|
APIRequest request = new APIRequest(req);
|
||||||
APIResponse response = new APIResponse(res);
|
APIResponse response = new APIResponse(res);
|
||||||
|
|
||||||
// TODO: Handle authentication - HTTP Auth?
|
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Execute appropriate service
|
// Execute appropriate service
|
||||||
//
|
//
|
||||||
// TODO: Replace with configurable dispatch mechanism based on HTTP method & uri.
|
|
||||||
// TODO: Handle errors (with appropriate HTTP error responses)
|
// TODO: Handle errors (with appropriate HTTP error responses)
|
||||||
|
|
||||||
APIRequest.HttpMethod method = request.getHttpMethod();
|
APIRequest.HttpMethod method = request.getHttpMethod();
|
||||||
String uri = request.getPathInfo();
|
String uri = request.getPathInfo();
|
||||||
|
|
||||||
if (method == APIRequest.HttpMethod.GET && TEXT_SEARCH_DESCRIPTION_URI.matcher(uri).matches())
|
APIService service = apiServiceMap.get(method, uri);
|
||||||
|
if (service != null)
|
||||||
{
|
{
|
||||||
TEXT_SEARCH_DESCRIPTION_SERVICE.execute(request, response);
|
service.execute(request, response);
|
||||||
}
|
|
||||||
else if (method == APIRequest.HttpMethod.GET && SEARCH_URI.matcher(uri).matches())
|
|
||||||
{
|
|
||||||
TEXT_SEARCH_SERVICE.execute(request, response);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
// TODO: add appropriate error detail
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
138
source/java/org/alfresco/web/api/BasicAuthentication.java
Normal file
138
source/java/org/alfresco/web/api/BasicAuthentication.java
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2005 Alfresco, Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Mozilla Public License version 1.1
|
||||||
|
* with a permitted attribution clause. You may obtain a
|
||||||
|
* copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.alfresco.org/legal/license.txt
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
|
* either express or implied. See the License for the specific
|
||||||
|
* language governing permissions and limitations under the
|
||||||
|
* License.
|
||||||
|
*/
|
||||||
|
package org.alfresco.web.api;
|
||||||
|
|
||||||
|
import org.alfresco.repo.security.authentication.AuthenticationException;
|
||||||
|
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||||
|
import org.alfresco.service.cmr.security.AuthenticationService;
|
||||||
|
import org.alfresco.util.Base64;
|
||||||
|
import org.aopalliance.intercept.MethodInterceptor;
|
||||||
|
import org.aopalliance.intercept.MethodInvocation;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP Basic Authentication Interceptor
|
||||||
|
*
|
||||||
|
* @author davidc
|
||||||
|
*/
|
||||||
|
public class BasicAuthentication implements MethodInterceptor
|
||||||
|
{
|
||||||
|
// dependencies
|
||||||
|
private AuthenticationService authenticationService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param authenticationService
|
||||||
|
*/
|
||||||
|
public void setAuthenticationService(AuthenticationService authenticationService)
|
||||||
|
{
|
||||||
|
this.authenticationService = authenticationService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
|
||||||
|
*/
|
||||||
|
public Object invoke(MethodInvocation invocation)
|
||||||
|
throws Throwable
|
||||||
|
{
|
||||||
|
boolean authorized = false;
|
||||||
|
Object retVal = null;
|
||||||
|
Object[] args = invocation.getArguments();
|
||||||
|
APIRequest request = (APIRequest)args[0];
|
||||||
|
APIService service = (APIService)invocation.getThis();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// validate credentials
|
||||||
|
//
|
||||||
|
|
||||||
|
String authorization = request.getHeader("Authorization");
|
||||||
|
if ((authorization == null || authorization.length() == 0) && service.getRequiredAuthentication().equals(APIRequest.RequiredAuthentication.Guest))
|
||||||
|
{
|
||||||
|
// authenticate as guest, if service allows
|
||||||
|
authenticationService.authenticateAsGuest();
|
||||||
|
authorized = true;
|
||||||
|
}
|
||||||
|
else if (authorization != null && authorization.length() > 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
String[] authorizationParts = authorization.split(" ");
|
||||||
|
if (!authorizationParts[0].equalsIgnoreCase("basic"))
|
||||||
|
{
|
||||||
|
throw new APIException("Authorization '" + authorizationParts[0] + "' not supported.");
|
||||||
|
}
|
||||||
|
String decodedAuthorisation = new String(Base64.decode(authorizationParts[1]));
|
||||||
|
String[] parts = decodedAuthorisation.split(":");
|
||||||
|
|
||||||
|
if (parts.length == 1)
|
||||||
|
{
|
||||||
|
// assume a ticket has been passed
|
||||||
|
authenticationService.validate(parts[0]);
|
||||||
|
authorized = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// assume username and password passed
|
||||||
|
if (parts[0].equals(AuthenticationUtil.getGuestUserName()))
|
||||||
|
{
|
||||||
|
authenticationService.authenticateAsGuest();
|
||||||
|
authorized = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
authenticationService.authenticate(parts[0], parts[1].toCharArray());
|
||||||
|
authorized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(AuthenticationException e)
|
||||||
|
{
|
||||||
|
// failed authentication
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// execute API service or request authorization
|
||||||
|
//
|
||||||
|
|
||||||
|
if (authorized)
|
||||||
|
{
|
||||||
|
retVal = invocation.proceed();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
APIResponse response = (APIResponse)args[1];
|
||||||
|
response.setStatus(401);
|
||||||
|
response.setHeader("WWW-Authenticate", "Basic realm=\"Alfresco\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
// clear authentication
|
||||||
|
// TODO: Consider case where authentication is set before this method is called.
|
||||||
|
// That shouldn't be the case for the web api.
|
||||||
|
if (authorized)
|
||||||
|
{
|
||||||
|
authenticationService.clearCurrentSecurityContext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,439 +1,482 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2005 Alfresco, Inc.
|
* Copyright (C) 2005 Alfresco, Inc.
|
||||||
*
|
*
|
||||||
* Licensed under the Mozilla Public License version 1.1
|
* Licensed under the Mozilla Public License version 1.1
|
||||||
* with a permitted attribution clause. You may obtain a
|
* with a permitted attribution clause. You may obtain a
|
||||||
* copy of the License at
|
* copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.alfresco.org/legal/license.txt
|
* http://www.alfresco.org/legal/license.txt
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing,
|
* Unless required by applicable law or agreed to in writing,
|
||||||
* software distributed under the License is distributed on an
|
* software distributed under the License is distributed on an
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
* either express or implied. See the License for the specific
|
* either express or implied. See the License for the specific
|
||||||
* language governing permissions and limitations under the
|
* language governing permissions and limitations under the
|
||||||
* License.
|
* License.
|
||||||
*/
|
*/
|
||||||
package org.alfresco.web.api;
|
package org.alfresco.web.api.services;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import org.alfresco.service.ServiceRegistry;
|
||||||
|
import org.alfresco.service.cmr.repository.StoreRef;
|
||||||
import org.alfresco.service.ServiceRegistry;
|
import org.alfresco.service.cmr.repository.TemplateNode;
|
||||||
import org.alfresco.service.cmr.repository.StoreRef;
|
import org.alfresco.service.cmr.repository.TemplateService;
|
||||||
import org.alfresco.service.cmr.repository.TemplateNode;
|
import org.alfresco.service.cmr.search.ResultSet;
|
||||||
import org.alfresco.service.cmr.repository.TemplateService;
|
import org.alfresco.service.cmr.search.SearchService;
|
||||||
import org.alfresco.service.cmr.search.ResultSet;
|
import org.alfresco.util.ApplicationContextHelper;
|
||||||
import org.alfresco.service.cmr.search.SearchService;
|
import org.alfresco.util.GUID;
|
||||||
import org.alfresco.util.ApplicationContextHelper;
|
import org.alfresco.web.api.APIException;
|
||||||
import org.alfresco.util.GUID;
|
import org.alfresco.web.api.APIRequest;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.alfresco.web.api.APIResponse;
|
||||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
import org.alfresco.web.api.APIService;
|
||||||
|
import org.alfresco.web.api.APIRequest.HttpMethod;
|
||||||
|
import org.alfresco.web.api.APIRequest.RequiredAuthentication;
|
||||||
/**
|
import org.springframework.context.ApplicationContext;
|
||||||
* Alfresco Text (simple) Search Service
|
|
||||||
*
|
|
||||||
* @author davidc
|
/**
|
||||||
*/
|
* Alfresco Text (simple) Search Service
|
||||||
public class TextSearchService implements APIService
|
*
|
||||||
{
|
* @author davidc
|
||||||
// NOTE: startPage and startIndex are 1 offset.
|
*/
|
||||||
|
public class TextSearch implements APIService
|
||||||
// search parameters
|
{
|
||||||
// TODO: allow configuration of these
|
// NOTE: startPage and startIndex are 1 offset.
|
||||||
private static final StoreRef searchStore = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
|
|
||||||
private static final int itemsPerPage = 10;
|
// search parameters
|
||||||
|
// TODO: allow configuration of these
|
||||||
// dependencies
|
private static final StoreRef searchStore = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
|
||||||
private ServiceRegistry serviceRegistry;
|
private static final int itemsPerPage = 10;
|
||||||
private SearchService searchService;
|
|
||||||
private TemplateService templateService;
|
// dependencies
|
||||||
|
private String uri;
|
||||||
|
private ServiceRegistry serviceRegistry;
|
||||||
|
private SearchService searchService;
|
||||||
/* (non-Javadoc)
|
private TemplateService templateService;
|
||||||
* @see org.alfresco.web.api.APIService#init(javax.servlet.ServletContext)
|
|
||||||
*/
|
|
||||||
public void init(ServletContext context)
|
/**
|
||||||
{
|
* Sets the Http URI
|
||||||
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(context);
|
*
|
||||||
init(appContext);
|
* @param uri
|
||||||
}
|
*/
|
||||||
|
public void setHttpUri(String uri)
|
||||||
/**
|
{
|
||||||
* Internal initialisation
|
this.uri = uri;
|
||||||
*
|
}
|
||||||
* @param context
|
|
||||||
*/
|
/**
|
||||||
private void init(ApplicationContext context)
|
* @param serviceRegistry
|
||||||
{
|
*/
|
||||||
serviceRegistry = (ServiceRegistry)context.getBean(ServiceRegistry.SERVICE_REGISTRY);
|
public void setServiceRegistry(ServiceRegistry serviceRegistry)
|
||||||
searchService = (SearchService)context.getBean(ServiceRegistry.SEARCH_SERVICE.getLocalName());
|
{
|
||||||
templateService = (TemplateService)context.getBean(ServiceRegistry.TEMPLATE_SERVICE.getLocalName());
|
this.serviceRegistry = serviceRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/**
|
||||||
* @see org.alfresco.web.api.APIService#execute(org.alfresco.web.api.APIRequest, org.alfresco.web.api.APIResponse)
|
* @param searchService
|
||||||
*/
|
*/
|
||||||
public void execute(APIRequest req, APIResponse res)
|
public void setSearchService(SearchService searchService)
|
||||||
throws IOException
|
{
|
||||||
{
|
this.searchService = searchService;
|
||||||
//
|
}
|
||||||
// execute the search
|
|
||||||
//
|
/**
|
||||||
|
* @param templateService
|
||||||
String searchTerms = req.getParameter("q");
|
*/
|
||||||
String startPageArg = req.getParameter("p");
|
public void setTemplateService(TemplateService templateService)
|
||||||
int startPage = 1;
|
{
|
||||||
try
|
this.templateService = templateService;
|
||||||
{
|
}
|
||||||
startPage = new Integer(startPageArg);
|
|
||||||
}
|
/* (non-Javadoc)
|
||||||
catch(NumberFormatException e)
|
* @see org.alfresco.web.api.APIService#getRequiredAuthentication()
|
||||||
{
|
*/
|
||||||
// NOTE: use default startPage
|
public RequiredAuthentication getRequiredAuthentication()
|
||||||
}
|
{
|
||||||
|
return APIRequest.RequiredAuthentication.User;
|
||||||
SearchResult results = search(searchTerms, startPage);
|
}
|
||||||
|
|
||||||
//
|
/* (non-Javadoc)
|
||||||
// render the results
|
* @see org.alfresco.web.api.APIService#getHttpMethod()
|
||||||
//
|
*/
|
||||||
|
public HttpMethod getHttpMethod()
|
||||||
String contentType = APIResponse.HTML_TYPE;
|
{
|
||||||
String template = HTML_TEMPLATE;
|
return APIRequest.HttpMethod.GET;
|
||||||
|
}
|
||||||
// TODO: data-drive this
|
|
||||||
String format = req.getParameter("format");
|
/* (non-Javadoc)
|
||||||
if (format != null)
|
* @see org.alfresco.web.api.APIService#getHttpUri()
|
||||||
{
|
*/
|
||||||
if (format.equals("atom"))
|
public String getHttpUri()
|
||||||
{
|
{
|
||||||
contentType = APIResponse.ATOM_TYPE;
|
return this.uri;
|
||||||
template = ATOM_TEMPLATE;
|
}
|
||||||
}
|
|
||||||
}
|
/* (non-Javadoc)
|
||||||
|
* @see org.alfresco.web.api.APIService#execute(org.alfresco.web.api.APIRequest, org.alfresco.web.api.APIResponse)
|
||||||
// execute template
|
*/
|
||||||
Map<String, Object> searchModel = new HashMap<String, Object>(7, 1.0f);
|
public void execute(APIRequest req, APIResponse res)
|
||||||
searchModel.put("request", req);
|
throws IOException
|
||||||
searchModel.put("search", results);
|
{
|
||||||
res.setContentType(contentType + ";charset=UTF-8");
|
//
|
||||||
templateService.processTemplateString(null, template, searchModel, res.getWriter());
|
// execute the search
|
||||||
}
|
//
|
||||||
|
|
||||||
/**
|
String searchTerms = req.getParameter("q");
|
||||||
* Execute the search
|
String startPageArg = req.getParameter("p");
|
||||||
*
|
int startPage = 1;
|
||||||
* @param searchTerms
|
try
|
||||||
* @param startPage
|
{
|
||||||
* @return
|
startPage = new Integer(startPageArg);
|
||||||
*/
|
}
|
||||||
private SearchResult search(String searchTerms, int startPage)
|
catch(NumberFormatException e)
|
||||||
{
|
{
|
||||||
SearchResult searchResult = null;
|
// NOTE: use default startPage
|
||||||
ResultSet results = null;
|
}
|
||||||
|
|
||||||
try
|
SearchResult results = search(searchTerms, startPage);
|
||||||
{
|
|
||||||
// Construct search statement
|
//
|
||||||
String[] terms = searchTerms.split(" ");
|
// render the results
|
||||||
Map<String, Object> statementModel = new HashMap<String, Object>(7, 1.0f);
|
//
|
||||||
statementModel.put("terms", terms);
|
|
||||||
String query = templateService.processTemplateString(null, QUERY_STATEMENT, statementModel);
|
String contentType = APIResponse.HTML_TYPE;
|
||||||
results = searchService.query(searchStore, SearchService.LANGUAGE_LUCENE, query);
|
String template = HTML_TEMPLATE;
|
||||||
|
|
||||||
int totalResults = results.length();
|
// TODO: data-drive this
|
||||||
int totalPages = (totalResults / itemsPerPage);
|
String format = req.getParameter("format");
|
||||||
totalPages += (totalResults % itemsPerPage != 0) ? 1 : 0;
|
if (format != null)
|
||||||
|
{
|
||||||
// are we out-of-range
|
if (format.equals("atom"))
|
||||||
if (totalPages != 0 && (startPage < 1 || startPage > totalPages))
|
{
|
||||||
{
|
contentType = APIResponse.ATOM_TYPE;
|
||||||
throw new APIException("Start page " + startPage + " is outside boundary of " + totalPages + " pages");
|
template = ATOM_TEMPLATE;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
searchResult = new SearchResult();
|
|
||||||
searchResult.setSearchTerms(searchTerms);
|
// execute template
|
||||||
searchResult.setItemsPerPage(itemsPerPage);
|
Map<String, Object> searchModel = new HashMap<String, Object>(7, 1.0f);
|
||||||
searchResult.setStartPage(startPage);
|
searchModel.put("request", req);
|
||||||
searchResult.setTotalPages(totalPages);
|
searchModel.put("search", results);
|
||||||
searchResult.setTotalResults(totalResults);
|
res.setContentType(contentType + ";charset=UTF-8");
|
||||||
searchResult.setStartIndex(((startPage -1) * itemsPerPage) + 1);
|
templateService.processTemplateString(null, template, searchModel, res.getWriter());
|
||||||
searchResult.setTotalPageItems(Math.min(itemsPerPage, totalResults - searchResult.getStartIndex() + 1));
|
}
|
||||||
TemplateNode[] nodes = new TemplateNode[searchResult.getTotalPageItems()];
|
|
||||||
for (int i = 0; i < searchResult.getTotalPageItems(); i++)
|
/**
|
||||||
{
|
* Execute the search
|
||||||
nodes[i] = new TemplateNode(results.getNodeRef(i + searchResult.getStartIndex() - 1), serviceRegistry, null);
|
*
|
||||||
}
|
* @param searchTerms
|
||||||
searchResult.setResults(nodes);
|
* @param startPage
|
||||||
|
* @return
|
||||||
return searchResult;
|
*/
|
||||||
}
|
private SearchResult search(String searchTerms, int startPage)
|
||||||
finally
|
{
|
||||||
{
|
SearchResult searchResult = null;
|
||||||
if (results != null)
|
ResultSet results = null;
|
||||||
{
|
|
||||||
results.close();
|
try
|
||||||
}
|
{
|
||||||
}
|
// Construct search statement
|
||||||
}
|
String[] terms = searchTerms.split(" ");
|
||||||
|
Map<String, Object> statementModel = new HashMap<String, Object>(7, 1.0f);
|
||||||
/**
|
statementModel.put("terms", terms);
|
||||||
* Search Result
|
String query = templateService.processTemplateString(null, QUERY_STATEMENT, statementModel);
|
||||||
*
|
results = searchService.query(searchStore, SearchService.LANGUAGE_LUCENE, query);
|
||||||
* @author davidc
|
|
||||||
*/
|
int totalResults = results.length();
|
||||||
public static class SearchResult
|
int totalPages = (totalResults / itemsPerPage);
|
||||||
{
|
totalPages += (totalResults % itemsPerPage != 0) ? 1 : 0;
|
||||||
private String id;
|
|
||||||
private String searchTerms;
|
// are we out-of-range
|
||||||
private int itemsPerPage;
|
if (totalPages != 0 && (startPage < 1 || startPage > totalPages))
|
||||||
private int totalPages;
|
{
|
||||||
private int totalResults;
|
throw new APIException("Start page " + startPage + " is outside boundary of " + totalPages + " pages");
|
||||||
private int totalPageItems;
|
}
|
||||||
private int startPage;
|
|
||||||
private int startIndex;
|
searchResult = new SearchResult();
|
||||||
private TemplateNode[] results;
|
searchResult.setSearchTerms(searchTerms);
|
||||||
|
searchResult.setItemsPerPage(itemsPerPage);
|
||||||
|
searchResult.setStartPage(startPage);
|
||||||
public int getItemsPerPage()
|
searchResult.setTotalPages(totalPages);
|
||||||
{
|
searchResult.setTotalResults(totalResults);
|
||||||
return itemsPerPage;
|
searchResult.setStartIndex(((startPage -1) * itemsPerPage) + 1);
|
||||||
}
|
searchResult.setTotalPageItems(Math.min(itemsPerPage, totalResults - searchResult.getStartIndex() + 1));
|
||||||
|
TemplateNode[] nodes = new TemplateNode[searchResult.getTotalPageItems()];
|
||||||
/*package*/ void setItemsPerPage(int itemsPerPage)
|
for (int i = 0; i < searchResult.getTotalPageItems(); i++)
|
||||||
{
|
{
|
||||||
this.itemsPerPage = itemsPerPage;
|
nodes[i] = new TemplateNode(results.getNodeRef(i + searchResult.getStartIndex() - 1), serviceRegistry, null);
|
||||||
}
|
}
|
||||||
|
searchResult.setResults(nodes);
|
||||||
public TemplateNode[] getResults()
|
|
||||||
{
|
return searchResult;
|
||||||
return results;
|
}
|
||||||
}
|
finally
|
||||||
|
{
|
||||||
/*package*/ void setResults(TemplateNode[] results)
|
if (results != null)
|
||||||
{
|
{
|
||||||
this.results = results;
|
results.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public int getStartIndex()
|
}
|
||||||
{
|
|
||||||
return startIndex;
|
/**
|
||||||
}
|
* Search Result
|
||||||
|
*
|
||||||
/*package*/ void setStartIndex(int startIndex)
|
* @author davidc
|
||||||
{
|
*/
|
||||||
this.startIndex = startIndex;
|
public static class SearchResult
|
||||||
}
|
{
|
||||||
|
private String id;
|
||||||
public int getStartPage()
|
private String searchTerms;
|
||||||
{
|
private int itemsPerPage;
|
||||||
return startPage;
|
private int totalPages;
|
||||||
}
|
private int totalResults;
|
||||||
|
private int totalPageItems;
|
||||||
/*package*/ void setStartPage(int startPage)
|
private int startPage;
|
||||||
{
|
private int startIndex;
|
||||||
this.startPage = startPage;
|
private TemplateNode[] results;
|
||||||
}
|
|
||||||
|
|
||||||
public int getTotalPageItems()
|
public int getItemsPerPage()
|
||||||
{
|
{
|
||||||
return totalPageItems;
|
return itemsPerPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*package*/ void setTotalPageItems(int totalPageItems)
|
/*package*/ void setItemsPerPage(int itemsPerPage)
|
||||||
{
|
{
|
||||||
this.totalPageItems = totalPageItems;
|
this.itemsPerPage = itemsPerPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTotalPages()
|
public TemplateNode[] getResults()
|
||||||
{
|
{
|
||||||
return totalPages;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*package*/ void setTotalPages(int totalPages)
|
/*package*/ void setResults(TemplateNode[] results)
|
||||||
{
|
{
|
||||||
this.totalPages = totalPages;
|
this.results = results;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTotalResults()
|
public int getStartIndex()
|
||||||
{
|
{
|
||||||
return totalResults;
|
return startIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*package*/ void setTotalResults(int totalResults)
|
/*package*/ void setStartIndex(int startIndex)
|
||||||
{
|
{
|
||||||
this.totalResults = totalResults;
|
this.startIndex = startIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSearchTerms()
|
public int getStartPage()
|
||||||
{
|
{
|
||||||
return searchTerms;
|
return startPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*package*/ void setSearchTerms(String searchTerms)
|
/*package*/ void setStartPage(int startPage)
|
||||||
{
|
{
|
||||||
this.searchTerms = searchTerms;
|
this.startPage = startPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId()
|
public int getTotalPageItems()
|
||||||
{
|
{
|
||||||
if (id == null)
|
return totalPageItems;
|
||||||
{
|
}
|
||||||
id = GUID.generate();
|
|
||||||
}
|
/*package*/ void setTotalPageItems(int totalPageItems)
|
||||||
return id;
|
{
|
||||||
}
|
this.totalPageItems = totalPageItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getTotalPages()
|
||||||
// TODO: place into accessible file
|
{
|
||||||
private final static String ATOM_TEMPLATE =
|
return totalPages;
|
||||||
"<#assign dateformat=\"yyyy-MM-dd\">" +
|
}
|
||||||
"<#assign timeformat=\"HH:mm:sszzz\">" +
|
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
/*package*/ void setTotalPages(int totalPages)
|
||||||
"<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:opensearch=\"http://a9.com/-/spec/opensearch/1.1/\">\n" +
|
{
|
||||||
" <title>Alfresco Search: ${search.searchTerms}</title>\n" +
|
this.totalPages = totalPages;
|
||||||
" <updated>2003-12-13T18:30:02Z</updated>\n" + // TODO:
|
}
|
||||||
" <author>\n" +
|
|
||||||
" <name>Alfresco</name>\n" + // TODO: Issuer of search?
|
public int getTotalResults()
|
||||||
" </author>\n" +
|
{
|
||||||
" <id>urn:uuid:${search.id}</id>\n" +
|
return totalResults;
|
||||||
" <opensearch:totalResults>${search.totalResults}</opensearch:totalResults>\n" +
|
}
|
||||||
" <opensearch:startIndex>${search.startIndex}</opensearch:startIndex>\n" +
|
|
||||||
" <opensearch:itemsPerPage>${search.itemsPerPage}</opensearch:itemsPerPage>\n" +
|
/*package*/ void setTotalResults(int totalResults)
|
||||||
" <opensearch:Query role=\"request\" searchTerms=\"${search.searchTerms}\" startPage=\"${search.startPage}\"/>\n" +
|
{
|
||||||
" <link rel=\"alternate\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage}&format=html\" type=\"text/html\"/>\n" +
|
this.totalResults = totalResults;
|
||||||
" <link rel=\"self\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage}&format=atom\" type=\"application/atom+xml\"/>\n" +
|
}
|
||||||
" <link rel=\"first\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=1&format=atom\" type=\"application/atom+xml\"/>\n" +
|
|
||||||
"<#if search.startPage > 1>" +
|
public String getSearchTerms()
|
||||||
" <link rel=\"previous\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage - 1}&format=atom\" type=\"application/atom+xml\"/>\n" +
|
{
|
||||||
"</#if>" +
|
return searchTerms;
|
||||||
"<#if search.startPage < search.totalPages>" +
|
}
|
||||||
" <link rel=\"next\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage + 1}&format=atom\" type=\"application/atom+xml\"/>\n" +
|
|
||||||
"</#if>" +
|
/*package*/ void setSearchTerms(String searchTerms)
|
||||||
" <link rel=\"last\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.totalPages}&format=atom\" type=\"application/atom+xml\"/>\n" +
|
{
|
||||||
" <link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"${request.servicePath}/search/text/textsearchdescription.xml\"/>\n" +
|
this.searchTerms = searchTerms;
|
||||||
"<#list search.results as row>" +
|
}
|
||||||
" <entry>\n" +
|
|
||||||
" <title>${row.name}</title>\n" +
|
public String getId()
|
||||||
" <link href=\"${request.path}/${row.url}\"/>\n" +
|
{
|
||||||
" <id>urn:uuid:${row.id}</id>\n" +
|
if (id == null)
|
||||||
" <updated>${row.properties.modified?string(dateformat)}T${row.properties.modified?string(timeformat)}</updated>\n" +
|
{
|
||||||
" <summary>${row.properties.description}</summary>\n" +
|
id = GUID.generate();
|
||||||
" </entry>\n" +
|
}
|
||||||
"</#list>" +
|
return id;
|
||||||
"</feed>";
|
}
|
||||||
|
}
|
||||||
// TODO: place into accessible file
|
|
||||||
private final static String HTML_TEMPLATE =
|
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
// TODO: place into accessible file
|
||||||
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
|
private final static String ATOM_TEMPLATE =
|
||||||
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
|
"<#assign dateformat=\"yyyy-MM-dd\">" +
|
||||||
" <head profile=\"http://a9.com/-/spec/opensearch/1.1/\">\n" +
|
"<#assign timeformat=\"HH:mm:sszzz\">" +
|
||||||
" <title>Alfresco Text Search: ${search.searchTerms}</title>\n" +
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||||
" <link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"${request.servicePath}/search/text/textsearchdescription.xml\" title=\"Alfresco Text Search\"/>\n" +
|
"<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:opensearch=\"http://a9.com/-/spec/opensearch/1.1/\">\n" +
|
||||||
" <meta name=\"totalResults\" content=\"${search.totalResults}\"/>\n" +
|
" <title>Alfresco Search: ${search.searchTerms}</title>\n" +
|
||||||
" <meta name=\"startIndex\" content=\"${search.startIndex}\"/>\n" +
|
" <updated>2003-12-13T18:30:02Z</updated>\n" + // TODO:
|
||||||
" <meta name=\"itemsPerPage\" content=\"${search.itemsPerPage}\"/>\n" +
|
" <author>\n" +
|
||||||
" </head>\n" +
|
" <name>Alfresco</name>\n" + // TODO: Issuer of search?
|
||||||
" <body>\n" +
|
" </author>\n" +
|
||||||
" <h2>Alfresco Text Search</h2>\n" +
|
" <id>urn:uuid:${search.id}</id>\n" +
|
||||||
" Results <b>${search.startIndex}</b> - <b>${search.startIndex + search.totalPageItems - 1}</b> of <b>${search.totalResults}</b> for <b>${search.searchTerms}.</b>\n" +
|
" <opensearch:totalResults>${search.totalResults}</opensearch:totalResults>\n" +
|
||||||
" <ul>\n" +
|
" <opensearch:startIndex>${search.startIndex}</opensearch:startIndex>\n" +
|
||||||
"<#list search.results as row>" +
|
" <opensearch:itemsPerPage>${search.itemsPerPage}</opensearch:itemsPerPage>\n" +
|
||||||
" <li>\n" +
|
" <opensearch:Query role=\"request\" searchTerms=\"${search.searchTerms}\" startPage=\"${search.startPage}\"/>\n" +
|
||||||
" <a href=\"${request.path}/${row.url}\">\n" +
|
" <link rel=\"alternate\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage}&format=html\" type=\"text/html\"/>\n" +
|
||||||
" ${row.name}\n" +
|
" <link rel=\"self\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage}&format=atom\" type=\"application/atom+xml\"/>\n" +
|
||||||
" </a>\n" +
|
" <link rel=\"first\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=1&format=atom\" type=\"application/atom+xml\"/>\n" +
|
||||||
" <div>\n" +
|
"<#if search.startPage > 1>" +
|
||||||
" ${row.properties.description}\n" +
|
" <link rel=\"previous\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage - 1}&format=atom\" type=\"application/atom+xml\"/>\n" +
|
||||||
" </div>\n" +
|
"</#if>" +
|
||||||
" </li>\n" +
|
"<#if search.startPage < search.totalPages>" +
|
||||||
"</#list>" +
|
" <link rel=\"next\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage + 1}&format=atom\" type=\"application/atom+xml\"/>\n" +
|
||||||
" </ul>\n" +
|
"</#if>" +
|
||||||
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=1\">first</a>" +
|
" <link rel=\"last\" href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.totalPages}&format=atom\" type=\"application/atom+xml\"/>\n" +
|
||||||
"<#if search.startPage > 1>" +
|
" <link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"${request.servicePath}/search/text/textsearchdescription.xml\"/>\n" +
|
||||||
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage - 1}\">previous</a>" +
|
"<#list search.results as row>" +
|
||||||
"</#if>" +
|
" <entry>\n" +
|
||||||
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage}\">${search.startPage}</a>" +
|
" <title>${row.name}</title>\n" +
|
||||||
"<#if search.startPage < search.totalPages>" +
|
" <link href=\"${request.path}/${row.url}\"/>\n" +
|
||||||
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage + 1}\">next</a>" +
|
" <id>urn:uuid:${row.id}</id>\n" +
|
||||||
"</#if>" +
|
" <updated>${row.properties.modified?string(dateformat)}T${row.properties.modified?string(timeformat)}</updated>\n" +
|
||||||
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.totalPages}\">last</a>" +
|
" <summary>${row.properties.description}</summary>\n" +
|
||||||
" </body>\n" +
|
" </entry>\n" +
|
||||||
"</html>\n";
|
"</#list>" +
|
||||||
|
"</feed>";
|
||||||
// TODO: place into accessible file
|
|
||||||
private final static String QUERY_STATEMENT =
|
// TODO: place into accessible file
|
||||||
"( " +
|
private final static String HTML_TEMPLATE =
|
||||||
" TYPE:\"{http://www.alfresco.org/model/content/1.0}content\" AND " +
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||||
" (" +
|
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
|
||||||
" (" +
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
|
||||||
"<#list 1..terms?size as i>" +
|
" <head profile=\"http://a9.com/-/spec/opensearch/1.1/\">\n" +
|
||||||
" @\\{http\\://www.alfresco.org/model/content/1.0\\}name:${terms[i - 1]}" +
|
" <title>Alfresco Text Search: ${search.searchTerms}</title>\n" +
|
||||||
"<#if (i < terms?size)>" +
|
" <link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"${request.servicePath}/search/text/textsearchdescription.xml\" title=\"Alfresco Text Search\"/>\n" +
|
||||||
" OR " +
|
" <meta name=\"totalResults\" content=\"${search.totalResults}\"/>\n" +
|
||||||
"</#if>" +
|
" <meta name=\"startIndex\" content=\"${search.startIndex}\"/>\n" +
|
||||||
"</#list>" +
|
" <meta name=\"itemsPerPage\" content=\"${search.itemsPerPage}\"/>\n" +
|
||||||
" ) " +
|
" </head>\n" +
|
||||||
" ( " +
|
" <body>\n" +
|
||||||
"<#list 1..terms?size as i>" +
|
" <h2>Alfresco Text Search</h2>\n" +
|
||||||
" TEXT:${terms[i - 1]}" +
|
" Results <b>${search.startIndex}</b> - <b>${search.startIndex + search.totalPageItems - 1}</b> of <b>${search.totalResults}</b> for <b>${search.searchTerms}.</b>\n" +
|
||||||
"<#if (i < terms?size)>" +
|
" <ul>\n" +
|
||||||
" OR " +
|
"<#list search.results as row>" +
|
||||||
"</#if>" +
|
" <li>\n" +
|
||||||
"</#list>" +
|
" <a href=\"${request.path}/${row.url}\">\n" +
|
||||||
" )" +
|
" ${row.name}\n" +
|
||||||
" )" +
|
" </a>\n" +
|
||||||
")";
|
" <div>\n" +
|
||||||
|
" ${row.properties.description}\n" +
|
||||||
|
" </div>\n" +
|
||||||
|
" </li>\n" +
|
||||||
/**
|
"</#list>" +
|
||||||
* Simple test that can be executed outside of web context
|
" </ul>\n" +
|
||||||
*
|
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=1\">first</a>" +
|
||||||
* TODO: Move to test harness
|
"<#if search.startPage > 1>" +
|
||||||
*
|
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage - 1}\">previous</a>" +
|
||||||
* @param args
|
"</#if>" +
|
||||||
* @throws Exception
|
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage}\">${search.startPage}</a>" +
|
||||||
*/
|
"<#if search.startPage < search.totalPages>" +
|
||||||
public static void main(String[] args)
|
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.startPage + 1}\">next</a>" +
|
||||||
throws Exception
|
"</#if>" +
|
||||||
{
|
" <a href=\"${request.servicePath}/search/text?q=${search.searchTerms}&p=${search.totalPages}\">last</a>" +
|
||||||
ApplicationContext context = ApplicationContextHelper.getApplicationContext();
|
" </body>\n" +
|
||||||
TextSearchService method = new TextSearchService();
|
"</html>\n";
|
||||||
method.init(context);
|
|
||||||
method.test();
|
// TODO: place into accessible file
|
||||||
}
|
private final static String QUERY_STATEMENT =
|
||||||
|
"( " +
|
||||||
/**
|
" TYPE:\"{http://www.alfresco.org/model/content/1.0}content\" AND " +
|
||||||
* Simple test that can be executed outside of web context
|
" (" +
|
||||||
*
|
" (" +
|
||||||
* TODO: Move to test harness
|
"<#list 1..terms?size as i>" +
|
||||||
*/
|
" @\\{http\\://www.alfresco.org/model/content/1.0\\}name:${terms[i - 1]}" +
|
||||||
private void test()
|
"<#if (i < terms?size)>" +
|
||||||
{
|
" OR " +
|
||||||
SearchResult result = search("alfresco tutorial", 1);
|
"</#if>" +
|
||||||
|
"</#list>" +
|
||||||
Map<String, Object> searchModel = new HashMap<String, Object>(7, 1.0f);
|
" ) " +
|
||||||
Map<String, Object> request = new HashMap<String, Object>();
|
" ( " +
|
||||||
request.put("servicePath", "http://localhost:8080/alfresco/service");
|
"<#list 1..terms?size as i>" +
|
||||||
request.put("path", "http://localhost:8080/alfresco");
|
" TEXT:${terms[i - 1]}" +
|
||||||
searchModel.put("request", request);
|
"<#if (i < terms?size)>" +
|
||||||
searchModel.put("search", result);
|
" OR " +
|
||||||
|
"</#if>" +
|
||||||
StringWriter rendition = new StringWriter();
|
"</#list>" +
|
||||||
PrintWriter writer = new PrintWriter(rendition);
|
" )" +
|
||||||
templateService.processTemplateString(null, HTML_TEMPLATE, searchModel, writer);
|
" )" +
|
||||||
System.out.println(rendition.toString());
|
")";
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple test that can be executed outside of web context
|
||||||
|
*
|
||||||
|
* TODO: Move to test harness
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static void main(String[] args)
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
ApplicationContext context = ApplicationContextHelper.getApplicationContext();
|
||||||
|
TextSearch method = new TextSearch();
|
||||||
|
method.setServiceRegistry((ServiceRegistry)context.getBean(ServiceRegistry.SERVICE_REGISTRY));
|
||||||
|
method.setTemplateService((TemplateService)context.getBean(ServiceRegistry.TEMPLATE_SERVICE.getLocalName()));
|
||||||
|
method.setSearchService((SearchService)context.getBean(ServiceRegistry.SEARCH_SERVICE.getLocalName()));
|
||||||
|
method.setHttpUri("/search/text");
|
||||||
|
method.test();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple test that can be executed outside of web context
|
||||||
|
*
|
||||||
|
* TODO: Move to test harness
|
||||||
|
*/
|
||||||
|
private void test()
|
||||||
|
{
|
||||||
|
SearchResult result = search("alfresco tutorial", 1);
|
||||||
|
|
||||||
|
Map<String, Object> searchModel = new HashMap<String, Object>(7, 1.0f);
|
||||||
|
Map<String, Object> request = new HashMap<String, Object>();
|
||||||
|
request.put("servicePath", "http://localhost:8080/alfresco/service");
|
||||||
|
request.put("path", "http://localhost:8080/alfresco");
|
||||||
|
searchModel.put("request", request);
|
||||||
|
searchModel.put("search", result);
|
||||||
|
|
||||||
|
StringWriter rendition = new StringWriter();
|
||||||
|
PrintWriter writer = new PrintWriter(rendition);
|
||||||
|
templateService.processTemplateString(null, HTML_TEMPLATE, searchModel, writer);
|
||||||
|
System.out.println(rendition.toString());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -1,75 +1,110 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2005 Alfresco, Inc.
|
* Copyright (C) 2005 Alfresco, Inc.
|
||||||
*
|
*
|
||||||
* Licensed under the Mozilla Public License version 1.1
|
* Licensed under the Mozilla Public License version 1.1
|
||||||
* with a permitted attribution clause. You may obtain a
|
* with a permitted attribution clause. You may obtain a
|
||||||
* copy of the License at
|
* copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.alfresco.org/legal/license.txt
|
* http://www.alfresco.org/legal/license.txt
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing,
|
* Unless required by applicable law or agreed to in writing,
|
||||||
* software distributed under the License is distributed on an
|
* software distributed under the License is distributed on an
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||||
* either express or implied. See the License for the specific
|
* either express or implied. See the License for the specific
|
||||||
* language governing permissions and limitations under the
|
* language governing permissions and limitations under the
|
||||||
* License.
|
* License.
|
||||||
*/
|
*/
|
||||||
package org.alfresco.web.api;
|
package org.alfresco.web.api.services;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import org.alfresco.service.cmr.repository.TemplateService;
|
||||||
|
import org.alfresco.web.api.APIRequest;
|
||||||
import org.alfresco.service.ServiceRegistry;
|
import org.alfresco.web.api.APIResponse;
|
||||||
import org.alfresco.service.cmr.repository.TemplateService;
|
import org.alfresco.web.api.APIService;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.alfresco.web.api.APIRequest.HttpMethod;
|
||||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
import org.alfresco.web.api.APIRequest.RequiredAuthentication;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide OpenSearch Description for an Alfresco Text (simple) Search
|
* Provide OpenSearch Description for an Alfresco Text (simple) Search
|
||||||
*
|
*
|
||||||
* @author davidc
|
* @author davidc
|
||||||
*/
|
*/
|
||||||
public class TextSearchDescriptionService implements APIService
|
public class TextSearchDescription implements APIService
|
||||||
{
|
{
|
||||||
// dependencies
|
// dependencies
|
||||||
private TemplateService templateService;
|
private String uri;
|
||||||
|
private TemplateService templateService;
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see org.alfresco.web.api.APIService#init(javax.servlet.ServletContext)
|
/**
|
||||||
*/
|
* Sets the Http URI
|
||||||
public void init(ServletContext context)
|
*
|
||||||
{
|
* @param uri
|
||||||
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(context);
|
*/
|
||||||
templateService = (TemplateService)appContext.getBean(ServiceRegistry.TEMPLATE_SERVICE.getLocalName());
|
public void setHttpUri(String uri)
|
||||||
}
|
{
|
||||||
|
this.uri = uri;
|
||||||
/* (non-Javadoc)
|
}
|
||||||
* @see org.alfresco.web.api.APIService#execute(org.alfresco.web.api.APIRequest, org.alfresco.web.api.APIResponse)
|
|
||||||
*/
|
/**
|
||||||
public void execute(APIRequest req, APIResponse res)
|
* @param templateService
|
||||||
throws IOException
|
*/
|
||||||
{
|
public void setTemplateService(TemplateService templateService)
|
||||||
// create model for open search template
|
{
|
||||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
this.templateService = templateService;
|
||||||
model.put("request", req);
|
}
|
||||||
|
|
||||||
// execute template
|
|
||||||
res.setContentType(APIResponse.OPEN_SEARCH_DESCRIPTION_TYPE + ";charset=UTF-8");
|
/* (non-Javadoc)
|
||||||
templateService.processTemplateString(null, OPEN_SEARCH_DESCRIPTION, model, res.getWriter());
|
* @see org.alfresco.web.api.APIService#getRequiredAuthentication()
|
||||||
}
|
*/
|
||||||
|
public RequiredAuthentication getRequiredAuthentication()
|
||||||
// TODO: place into accessible file
|
{
|
||||||
private final static String OPEN_SEARCH_DESCRIPTION =
|
return APIRequest.RequiredAuthentication.None;
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
}
|
||||||
"<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\">\n" +
|
|
||||||
" <ShortName>Alfresco Text Search</ShortName>\n" +
|
/* (non-Javadoc)
|
||||||
" <Description>Search all of Alfresco Company Home via text keywords</Description>\n" +
|
* @see org.alfresco.web.api.APIService#getHttpMethod()
|
||||||
" <Url type=\"application/atom+xml\" template=\"${request.servicePath}/search/text?q={searchTerms}&p={startPage?}&format=atom\"/>\n" +
|
*/
|
||||||
" <Url type=\"text/html\" template=\"${request.servicePath}/search/text?q={searchTerms}&p={startPage?}\"/>\n" +
|
public HttpMethod getHttpMethod()
|
||||||
"</OpenSearchDescription>";
|
{
|
||||||
|
return APIRequest.HttpMethod.GET;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.alfresco.web.api.APIService#getHttpUri()
|
||||||
|
*/
|
||||||
|
public String getHttpUri()
|
||||||
|
{
|
||||||
|
return this.uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.alfresco.web.api.APIService#execute(org.alfresco.web.api.APIRequest, org.alfresco.web.api.APIResponse)
|
||||||
|
*/
|
||||||
|
public void execute(APIRequest req, APIResponse res)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
// create model for open search template
|
||||||
|
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||||
|
model.put("request", req);
|
||||||
|
|
||||||
|
// execute template
|
||||||
|
res.setContentType(APIResponse.OPEN_SEARCH_DESCRIPTION_TYPE + ";charset=UTF-8");
|
||||||
|
templateService.processTemplateString(null, OPEN_SEARCH_DESCRIPTION, model, res.getWriter());
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: place into accessible file
|
||||||
|
private final static String OPEN_SEARCH_DESCRIPTION =
|
||||||
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||||
|
"<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\">\n" +
|
||||||
|
" <ShortName>Alfresco Text Search</ShortName>\n" +
|
||||||
|
" <Description>Search all of Alfresco Company Home via text keywords</Description>\n" +
|
||||||
|
" <Url type=\"application/atom+xml\" template=\"${request.servicePath}/search/text?q={searchTerms}&p={startPage?}&format=atom\"/>\n" +
|
||||||
|
" <Url type=\"text/html\" template=\"${request.servicePath}/search/text?q={searchTerms}&p={startPage?}\"/>\n" +
|
||||||
|
"</OpenSearchDescription>";
|
||||||
|
|
||||||
|
}
|
@@ -59,6 +59,7 @@
|
|||||||
<param-value>
|
<param-value>
|
||||||
classpath:alfresco/web-client-application-context.xml
|
classpath:alfresco/web-client-application-context.xml
|
||||||
classpath:web-services-application-context.xml
|
classpath:web-services-application-context.xml
|
||||||
|
classpath:alfresco/web-api-application-context.xml
|
||||||
classpath:alfresco/application-context.xml
|
classpath:alfresco/application-context.xml
|
||||||
</param-value>
|
</param-value>
|
||||||
<description>Spring config file locations</description>
|
<description>Spring config file locations</description>
|
||||||
|
Reference in New Issue
Block a user