mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Merged DEV\EXTENSIONS to HEAD
svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4865 svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4866 . svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4872 svn://svn.alfresco.com:3691/alfresco/BRANCHES/DEV/EXTENSIONS@4884 . Dave and Gavin's search work git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4899 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -1,9 +1,36 @@
|
||||
/*
|
||||
* 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 javax.servlet.ServletContext;
|
||||
|
||||
/**
|
||||
* Interface for marking that a service is API Context aware
|
||||
*
|
||||
* @author davidc
|
||||
*
|
||||
*/
|
||||
public interface APIContextAware
|
||||
{
|
||||
|
||||
/**
|
||||
* Sets the API Context
|
||||
*
|
||||
* @param context api context
|
||||
*/
|
||||
public void setAPIContext(ServletContext context);
|
||||
}
|
||||
|
@@ -89,6 +89,16 @@ public class APIRequest extends HttpServletRequestWrapper
|
||||
{
|
||||
return getPath() + getServletPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the full request URL
|
||||
*
|
||||
* @return request url e.g. http://localhost:port/alfresco/service/text?searchTerms=dsfsdf
|
||||
*/
|
||||
public String getUrl()
|
||||
{
|
||||
return getScheme() + "://" + getServerName() + ":" + getServerPort() + getPathInfo() + (getQueryString() != null ? "?" + getQueryString() : "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the currently authenticated username
|
||||
@@ -120,5 +130,25 @@ public class APIRequest extends HttpServletRequestWrapper
|
||||
String format = getParameter("format");
|
||||
return (format == null || format.length() == 0) ? "" : format;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get User Agent
|
||||
*
|
||||
* TODO: Expand on known agents
|
||||
*
|
||||
* @return MSIE / Firefox
|
||||
*/
|
||||
public String getAgent()
|
||||
{
|
||||
String userAgent = getHeader("user-agent");
|
||||
if (userAgent.indexOf("Firefox/") != -1)
|
||||
{
|
||||
return "Firefox";
|
||||
}
|
||||
else if (userAgent.indexOf("MSIE") != -1)
|
||||
{
|
||||
return "MSIE";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -26,13 +26,13 @@ import javax.servlet.http.HttpServletResponseWrapper;
|
||||
*/
|
||||
public class APIResponse extends HttpServletResponseWrapper
|
||||
{
|
||||
|
||||
// Content Types
|
||||
// API Formats
|
||||
public static final String HTML_FORMAT = "html";
|
||||
public static final String ATOM_FORMAT = "atom";
|
||||
public static final String RSS_FORMAT = "rss";
|
||||
public static final String XML_FORMAT = "xml";
|
||||
public static final String OPENSEARCH_DESCRIPTION_FORMAT = "opensearchdescription";
|
||||
|
||||
public static final String HTML_TYPE = "text/html";
|
||||
public static final String OPEN_SEARCH_DESCRIPTION_TYPE = "application/opensearchdescription+xml";
|
||||
public static final String ATOM_TYPE = "application/atom+xml";
|
||||
public static final String XML_TYPE = "text/xml";
|
||||
|
||||
/**
|
||||
* Construct
|
||||
|
@@ -33,6 +33,11 @@ public interface APIService
|
||||
*/
|
||||
public String getName();
|
||||
|
||||
/**
|
||||
* Gets the description of this service
|
||||
*/
|
||||
public String getDescription();
|
||||
|
||||
/**
|
||||
* Gets the required authentication level for execution of this service
|
||||
*
|
||||
@@ -54,6 +59,13 @@ public interface APIService
|
||||
*/
|
||||
public String getHttpUri();
|
||||
|
||||
/**
|
||||
* Gets the default response format
|
||||
*
|
||||
* @return response format
|
||||
*/
|
||||
public String getDefaultFormat();
|
||||
|
||||
/**
|
||||
* Execute service
|
||||
*
|
||||
|
@@ -86,7 +86,7 @@ public class APIServlet extends BaseServlet
|
||||
String uri = request.getPathInfo();
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Processing request (" + request.getHttpMethod() + ") " + request.getRequestURL() + (request.getQueryString() != null ? "?" + request.getQueryString() : ""));
|
||||
logger.debug("Processing request (" + request.getHttpMethod() + ") " + request.getRequestURL() + (request.getQueryString() != null ? "?" + request.getQueryString() : "") + " (agent: " + request.getAgent() + ")");
|
||||
|
||||
APIService service = apiServiceRegistry.get(method, uri);
|
||||
if (service != null)
|
||||
|
148
source/java/org/alfresco/web/api/APITemplateProcessor.java
Normal file
148
source/java/org/alfresco/web/api/APITemplateProcessor.java
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* 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.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.alfresco.repo.template.FreeMarkerProcessor;
|
||||
import org.alfresco.repo.template.QNameAwareObjectWrapper;
|
||||
import org.alfresco.util.AbstractLifecycleBean;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
import freemarker.cache.MruCacheStorage;
|
||||
import freemarker.cache.TemplateLoader;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* FreeMarker Processor for use in Web API
|
||||
*
|
||||
* Adds the ability to:
|
||||
* - specify template loaders
|
||||
* - caching of templates
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class APITemplateProcessor extends FreeMarkerProcessor implements ApplicationContextAware, ApplicationListener
|
||||
{
|
||||
private ProcessorLifecycle lifecycle = new ProcessorLifecycle();
|
||||
private Set<TemplateLoader> templateLoaders = new HashSet<TemplateLoader>();
|
||||
private String defaultEncoding;
|
||||
private Configuration templateConfig;
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.template.FreeMarkerProcessor#setDefaultEncoding(java.lang.String)
|
||||
*/
|
||||
public void setDefaultEncoding(String defaultEncoding)
|
||||
{
|
||||
this.defaultEncoding = defaultEncoding;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.template.FreeMarkerProcessor#getConfig()
|
||||
*/
|
||||
@Override
|
||||
protected Configuration getConfig()
|
||||
{
|
||||
return templateConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Template Loader
|
||||
*
|
||||
* @param templateLoader template loader
|
||||
*/
|
||||
public void addTemplateLoader(TemplateLoader templateLoader)
|
||||
{
|
||||
templateLoaders.add(templateLoader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise FreeMarker Configuration
|
||||
*/
|
||||
protected void initConfig()
|
||||
{
|
||||
Configuration config = new Configuration();
|
||||
|
||||
// setup template cache
|
||||
config.setCacheStorage(new MruCacheStorage(20, 100));
|
||||
|
||||
// setup template loaders
|
||||
for (TemplateLoader templateLoader : templateLoaders)
|
||||
{
|
||||
config.setTemplateLoader(templateLoader);
|
||||
}
|
||||
|
||||
// use our custom object wrapper that can deal with QNameMap objects directly
|
||||
config.setObjectWrapper(new QNameAwareObjectWrapper());
|
||||
|
||||
// rethrow any exception so we can deal with them
|
||||
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
||||
|
||||
// set template encoding
|
||||
if (defaultEncoding != null)
|
||||
{
|
||||
config.setDefaultEncoding(defaultEncoding);
|
||||
}
|
||||
|
||||
// set output encoding
|
||||
config.setOutputEncoding("UTF-8");
|
||||
|
||||
templateConfig = config;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
|
||||
*/
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
|
||||
{
|
||||
lifecycle.setApplicationContext(applicationContext);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
|
||||
*/
|
||||
public void onApplicationEvent(ApplicationEvent event)
|
||||
{
|
||||
lifecycle.onApplicationEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks into Spring Application Lifecycle
|
||||
*/
|
||||
private class ProcessorLifecycle extends AbstractLifecycleBean
|
||||
{
|
||||
@Override
|
||||
protected void onBootstrap(ApplicationEvent event)
|
||||
{
|
||||
initConfig();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onShutdown(ApplicationEvent event)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
}
|
75
source/java/org/alfresco/web/api/FormatMap.java
Normal file
75
source/java/org/alfresco/web/api/FormatMap.java
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.Map;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
|
||||
/**
|
||||
* A map of mimetypes indexed by format.
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class FormatMap implements InitializingBean
|
||||
{
|
||||
private FormatRegistry registry;
|
||||
private String agent;
|
||||
private Map<String, String> formats;
|
||||
|
||||
|
||||
/**
|
||||
* Sets the Format Registry
|
||||
*
|
||||
* @param registry
|
||||
*/
|
||||
public void setRegistry(FormatRegistry registry)
|
||||
{
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the User Agent for which the formats apply
|
||||
*
|
||||
* @param agent
|
||||
*/
|
||||
public void setAgent(String agent)
|
||||
{
|
||||
this.agent = agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the formats
|
||||
*
|
||||
* @param formats
|
||||
*/
|
||||
public void setFormats(Map<String, String> formats)
|
||||
{
|
||||
this.formats = formats;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception
|
||||
{
|
||||
// Add formats to format registry
|
||||
registry.addFormats(agent, formats);
|
||||
}
|
||||
|
||||
}
|
114
source/java/org/alfresco/web/api/FormatRegistry.java
Normal file
114
source/java/org/alfresco/web/api/FormatRegistry.java
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* 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.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Maintains a registry of mimetypes (indexed by format and user agent)
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class FormatRegistry
|
||||
{
|
||||
// Logger
|
||||
private static final Log logger = LogFactory.getLog(FormatRegistry.class);
|
||||
|
||||
private Map<String, String> formats;
|
||||
private Map<String, Map<String, String>> agentFormats;
|
||||
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*/
|
||||
public FormatRegistry()
|
||||
{
|
||||
formats = new HashMap<String, String>();
|
||||
agentFormats = new HashMap<String, Map<String, String>>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add formats
|
||||
*
|
||||
* @param agent
|
||||
* @param formatsToAdd
|
||||
*/
|
||||
public void addFormats(String agent, Map<String, String> formatsToAdd)
|
||||
{
|
||||
// retrieve formats list for agent
|
||||
Map<String, String> formatsForAgent = formats;
|
||||
if (agent != null)
|
||||
{
|
||||
formatsForAgent = agentFormats.get(agent);
|
||||
if (formatsForAgent == null)
|
||||
{
|
||||
formatsForAgent = new HashMap<String, String>();
|
||||
agentFormats.put(agent, formatsForAgent);
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, String> entry : formatsToAdd.entrySet())
|
||||
{
|
||||
if (logger.isWarnEnabled())
|
||||
{
|
||||
String mimetype = formatsForAgent.get(entry.getKey());
|
||||
if (mimetype != null)
|
||||
{
|
||||
logger.warn("Replacing mime type '" + mimetype + "' with '" + entry.getValue() + "' for API format '" + entry.getKey() + "' (agent: " + agent + ")");
|
||||
}
|
||||
}
|
||||
|
||||
formatsForAgent.put(entry.getKey(), entry.getValue());
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Registered API format '" + entry.getKey() + "' with mime type '" + entry.getValue() + "' (agent: " + agent + ")");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the mimetype for the specified user agent and format
|
||||
*
|
||||
* @param agent
|
||||
* @param format
|
||||
* @return mimetype (or null, if one is not registered)
|
||||
*/
|
||||
public String getMimeType(String agent, String format)
|
||||
{
|
||||
String mimetype = null;
|
||||
|
||||
if (agent != null)
|
||||
{
|
||||
Map<String, String> formatsForAgent = agentFormats.get(agent);
|
||||
if (formatsForAgent != null)
|
||||
{
|
||||
mimetype = formatsForAgent.get(format);
|
||||
}
|
||||
}
|
||||
|
||||
if (mimetype == null)
|
||||
{
|
||||
mimetype = formats.get(format);
|
||||
}
|
||||
|
||||
return mimetype;
|
||||
}
|
||||
|
||||
}
|
98
source/java/org/alfresco/web/api/TemplateClassPathSet.java
Normal file
98
source/java/org/alfresco/web/api/TemplateClassPathSet.java
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.io.File;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
||||
import freemarker.cache.FileTemplateLoader;
|
||||
|
||||
|
||||
/**
|
||||
* A set of template class paths
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class TemplateClassPathSet implements InitializingBean
|
||||
{
|
||||
// Logger
|
||||
private static final Log logger = LogFactory.getLog(TemplateClassPathSet.class);
|
||||
|
||||
private Set<String> classPaths;
|
||||
private APITemplateProcessor templateProcessor;
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*/
|
||||
public TemplateClassPathSet()
|
||||
{
|
||||
resourceLoader = new DefaultResourceLoader();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Template Processor
|
||||
*
|
||||
* @param templateProcessor
|
||||
*/
|
||||
public void setTemplateProcessor(APITemplateProcessor templateProcessor)
|
||||
{
|
||||
this.templateProcessor = templateProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the paths
|
||||
*
|
||||
* @param classPaths
|
||||
*/
|
||||
public void setPaths(Set<String> classPaths)
|
||||
{
|
||||
this.classPaths = classPaths;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception
|
||||
{
|
||||
// Add class paths to template processor
|
||||
for (String classPath : classPaths)
|
||||
{
|
||||
Resource resource = resourceLoader.getResource(classPath);
|
||||
if (resource.exists())
|
||||
{
|
||||
File file = resource.getFile();
|
||||
templateProcessor.addTemplateLoader(new FileTemplateLoader(file));
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Registered template classpath '" + classPath);
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("Template classpath '" + classPath + "' does not exist");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -16,23 +16,27 @@
|
||||
*/
|
||||
package org.alfresco.web.api.services;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.alfresco.repo.template.AbsoluteUrlMethod;
|
||||
import org.alfresco.repo.template.ISO8601DateFormatMethod;
|
||||
import org.alfresco.repo.template.UrlEncodeMethod;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.TemplateService;
|
||||
import org.alfresco.service.cmr.repository.TemplateProcessor;
|
||||
import org.alfresco.service.descriptor.DescriptorService;
|
||||
import org.alfresco.web.api.APIContextAware;
|
||||
import org.alfresco.web.api.APIRequest;
|
||||
import org.alfresco.web.api.APIResponse;
|
||||
import org.alfresco.web.api.APIService;
|
||||
import org.alfresco.web.api.FormatRegistry;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
|
||||
/**
|
||||
@@ -49,7 +53,8 @@ public abstract class APIServiceImpl implements BeanNameAware, APIService, APICo
|
||||
private ServletContext context;
|
||||
private ServiceRegistry serviceRegistry;
|
||||
private DescriptorService descriptorService;
|
||||
private TemplateService templateService;
|
||||
private TemplateProcessor templateProcessor;
|
||||
private FormatRegistry formatRegistry;
|
||||
|
||||
//
|
||||
// Initialisation
|
||||
@@ -80,11 +85,11 @@ public abstract class APIServiceImpl implements BeanNameAware, APIService, APICo
|
||||
}
|
||||
|
||||
/**
|
||||
* @param templateService
|
||||
* @param templateProcessor
|
||||
*/
|
||||
public void setTemplateService(TemplateService templateService)
|
||||
public void setTemplateProcessor(TemplateProcessor templateProcessor)
|
||||
{
|
||||
this.templateService = templateService;
|
||||
this.templateProcessor = templateProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,6 +100,14 @@ public abstract class APIServiceImpl implements BeanNameAware, APIService, APICo
|
||||
this.descriptorService = descriptorService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param formatRegistry
|
||||
*/
|
||||
public void setFormatRegistry(FormatRegistry formatRegistry)
|
||||
{
|
||||
this.formatRegistry = formatRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Http URI
|
||||
*
|
||||
@@ -155,24 +168,39 @@ public abstract class APIServiceImpl implements BeanNameAware, APIService, APICo
|
||||
}
|
||||
|
||||
/**
|
||||
* @return templateService
|
||||
* @return templateProcessor
|
||||
*/
|
||||
protected TemplateService getTemplateService()
|
||||
protected TemplateProcessor getTemplateProcessor()
|
||||
{
|
||||
return templateService;
|
||||
return templateProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a basic template model
|
||||
* @return formatRegistry
|
||||
*/
|
||||
protected FormatRegistry getFormatRegistry()
|
||||
{
|
||||
return formatRegistry;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Basic Templating Support
|
||||
//
|
||||
|
||||
|
||||
/**
|
||||
* Create a basic API model
|
||||
*
|
||||
* @param req api request
|
||||
* @param res api response
|
||||
* @return template model
|
||||
*/
|
||||
protected Map<String, Object> createTemplateModel(APIRequest req, APIResponse res)
|
||||
protected Map<String, Object> createAPIModel(APIRequest req, APIResponse res)
|
||||
{
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
model.put("xmldate", new ISO8601DateFormatMethod());
|
||||
model.put("absurl", new AbsoluteUrlMethod(req.getPath()));
|
||||
model.put("urlencode", new UrlEncodeMethod());
|
||||
model.put("date", new Date());
|
||||
model.put("agent", descriptorService.getServerDescriptor());
|
||||
@@ -182,16 +210,71 @@ public abstract class APIServiceImpl implements BeanNameAware, APIService, APICo
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a template to the API Response
|
||||
* Render a template (identified by path)
|
||||
*
|
||||
* @param template
|
||||
* @param model
|
||||
* @param res
|
||||
* @param templatePath template path
|
||||
* @param model model
|
||||
* @param writer output writer
|
||||
*/
|
||||
protected void renderTemplate(String template, Map<String, Object> model, APIResponse res)
|
||||
throws IOException
|
||||
protected void renderTemplate(String templatePath, Map<String, Object> model, Writer writer)
|
||||
{
|
||||
templateService.processTemplateString(null, template, model, res.getWriter());
|
||||
templateProcessor.process(templatePath, model, writer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a template (contents as string)
|
||||
* @param template the template
|
||||
* @param model model
|
||||
* @param writer output writer
|
||||
*/
|
||||
protected void renderString(String template, Map<String, Object> model, Writer writer)
|
||||
{
|
||||
templateProcessor.processString(template, model, writer);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper to retrieve API Service
|
||||
*
|
||||
* @param name name of service
|
||||
* @return the service
|
||||
*/
|
||||
protected static APIService getMethod(String name)
|
||||
{
|
||||
String[] CONFIG_LOCATIONS = new String[] { "classpath:alfresco/application-context.xml", "classpath:alfresco/web-api-application-context.xml" };
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_LOCATIONS);
|
||||
APIService method = (APIService)context.getBean(name);
|
||||
return method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a base test model (for use stand-alone)
|
||||
*
|
||||
* @return test model
|
||||
*/
|
||||
protected Map<String, Object> createTestModel()
|
||||
{
|
||||
Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
|
||||
|
||||
// create api methods
|
||||
model.put("xmldate", new ISO8601DateFormatMethod());
|
||||
model.put("urlencode", new UrlEncodeMethod());
|
||||
model.put("absurl", new AbsoluteUrlMethod("http://test:8080/test"));
|
||||
model.put("date", new Date());
|
||||
|
||||
// create dummy request model
|
||||
Map<String, Object> request = new HashMap<String, Object>();
|
||||
request.put("servicePath", "http://localhost:8080/alfresco/service");
|
||||
request.put("path", "http://localhost:8080/alfresco");
|
||||
request.put("url", "http://localhost:8080/alfresco/service/testurl");
|
||||
request.put("guest", false);
|
||||
request.put("format", "xml");
|
||||
model.put("request", request);
|
||||
|
||||
// create dummy agent model
|
||||
model.put("agent", getDescriptorService().getServerDescriptor());
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* 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.services;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.cmr.repository.TemplateException;
|
||||
import org.alfresco.web.api.APIException;
|
||||
import org.alfresco.web.api.APIRequest;
|
||||
import org.alfresco.web.api.APIResponse;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Template based implementation of an API Service
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public abstract class APIServiceTemplateImpl extends APIServiceImpl
|
||||
{
|
||||
// Logger
|
||||
private static final Log logger = LogFactory.getLog(APIServiceTemplateImpl.class);
|
||||
|
||||
|
||||
/* (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
|
||||
{
|
||||
// construct data model for template
|
||||
Map<String, Object> model = createAPIModel(req, res);
|
||||
model = createModel(req, res, model);
|
||||
|
||||
// process requested format
|
||||
String format = req.getFormat();
|
||||
if (format == null || format.length() == 0)
|
||||
{
|
||||
format = getDefaultFormat();
|
||||
}
|
||||
String mimetype = getFormatRegistry().getMimeType(req.getAgent(), format);
|
||||
if (mimetype == null)
|
||||
{
|
||||
throw new APIException("API format '" + format + "' does not exist");
|
||||
}
|
||||
|
||||
// render output
|
||||
res.setContentType(mimetype + ";charset=UTF-8");
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Response content type: " + mimetype);
|
||||
|
||||
try
|
||||
{
|
||||
renderTemplate(null, format, model, res.getWriter());
|
||||
}
|
||||
catch(TemplateException e)
|
||||
{
|
||||
throw new APIException("Failed to process format '" + format + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a custom service model
|
||||
*
|
||||
* @param req API request
|
||||
* @param res API response
|
||||
* @param model basic API model
|
||||
* @return custom service model
|
||||
*/
|
||||
protected abstract Map<String, Object> createModel(APIRequest req, APIResponse res, Map<String, Object> model);
|
||||
|
||||
|
||||
/**
|
||||
* Render a template to the API Response
|
||||
*
|
||||
* @param type type of template (null defaults to type VIEW)
|
||||
* @param format template format (null, default format)
|
||||
* @param model data model to render
|
||||
* @param writer where to output
|
||||
*/
|
||||
protected void renderTemplate(String type, String format, Map<String, Object> model, Writer writer)
|
||||
{
|
||||
type = (type == null) ? "view" : type;
|
||||
format = (format == null) ? "" : format;
|
||||
String templatePath = (this.getClass().getSimpleName() + "_" + type + "_" + format).replace(".", "/") + ".ftl";
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Rendering service template '" + templatePath + "'");
|
||||
|
||||
renderTemplate(templatePath, model, writer);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simple test that can be executed outside of web context
|
||||
*/
|
||||
/*package*/ void test(String format)
|
||||
throws IOException
|
||||
{
|
||||
// create test model
|
||||
Map<String, Object> model = createTestModel();
|
||||
|
||||
// render service template to string
|
||||
StringWriter rendition = new StringWriter();
|
||||
PrintWriter writer = new PrintWriter(rendition);
|
||||
renderTemplate(null, format, model, writer);
|
||||
System.out.println(rendition.toString());
|
||||
}
|
||||
|
||||
}
|
241
source/java/org/alfresco/web/api/services/SearchEngines.java
Normal file
241
source/java/org/alfresco/web/api/services/SearchEngines.java
Normal file
@@ -0,0 +1,241 @@
|
||||
/*
|
||||
* 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.services;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.alfresco.config.Config;
|
||||
import org.alfresco.config.ConfigService;
|
||||
import org.alfresco.i18n.I18NUtil;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.web.api.APIRequest;
|
||||
import org.alfresco.web.api.APIResponse;
|
||||
import org.alfresco.web.api.APIRequest.HttpMethod;
|
||||
import org.alfresco.web.api.APIRequest.RequiredAuthentication;
|
||||
import org.alfresco.web.config.OpenSearchConfigElement;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
|
||||
/**
|
||||
* List of (server-side) registered Search Engines
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class SearchEngines extends APIServiceTemplateImpl
|
||||
{
|
||||
// url argument values
|
||||
public static final String URL_ARG_DESCRIPTION = "description";
|
||||
public static final String URL_ARG_TEMPLATE = "template";
|
||||
public static final String URL_ARG_ALL = "all";
|
||||
|
||||
// Logger
|
||||
private static final Log logger = LogFactory.getLog(SearchEngines.class);
|
||||
|
||||
// dependencies
|
||||
protected ConfigService configService;
|
||||
|
||||
/**
|
||||
* @param configService
|
||||
*/
|
||||
public void setConfigService(ConfigService configService)
|
||||
{
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getRequiredAuthentication()
|
||||
*/
|
||||
public RequiredAuthentication getRequiredAuthentication()
|
||||
{
|
||||
return APIRequest.RequiredAuthentication.None;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getHttpMethod()
|
||||
*/
|
||||
public HttpMethod getHttpMethod()
|
||||
{
|
||||
return APIRequest.HttpMethod.GET;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getDefaultFormat()
|
||||
*/
|
||||
public String getDefaultFormat()
|
||||
{
|
||||
return APIResponse.HTML_FORMAT;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getDescription()
|
||||
*/
|
||||
public String getDescription()
|
||||
{
|
||||
return "Retrieve a list of (server-side) registered search engines";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> createModel(APIRequest req, APIResponse res, Map<String, Object> model)
|
||||
{
|
||||
String urlType = req.getParameter("type");
|
||||
if (urlType == null || urlType.length() == 0)
|
||||
{
|
||||
urlType = URL_ARG_DESCRIPTION;
|
||||
}
|
||||
else if (!urlType.equals(URL_ARG_DESCRIPTION) && !urlType.equals(URL_ARG_TEMPLATE) && !urlType.equals(URL_ARG_ALL))
|
||||
{
|
||||
urlType = URL_ARG_DESCRIPTION;
|
||||
}
|
||||
|
||||
//
|
||||
// retrieve open search engines configuration
|
||||
//
|
||||
|
||||
Set<UrlTemplate> urls = getUrls(urlType);
|
||||
model.put("urltype", urlType);
|
||||
model.put("engines", urls);
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve registered search engines
|
||||
*
|
||||
* @return set of search engines
|
||||
*/
|
||||
private Set<UrlTemplate> getUrls(String urlType)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Search Engine parameters: urltype=" + urlType);
|
||||
|
||||
Set<UrlTemplate> urls = new HashSet<UrlTemplate>();
|
||||
Config config = configService.getConfig("OpenSearch");
|
||||
|
||||
OpenSearchConfigElement searchConfig = (OpenSearchConfigElement)config.getConfigElement(OpenSearchConfigElement.CONFIG_ELEMENT_ID);
|
||||
for (OpenSearchConfigElement.EngineConfig engineConfig : searchConfig.getEngines())
|
||||
{
|
||||
Map<String, String> engineUrls = engineConfig.getUrls();
|
||||
for (Map.Entry<String, String> engineUrl : engineUrls.entrySet())
|
||||
{
|
||||
String type = engineUrl.getKey();
|
||||
String url = engineUrl.getValue();
|
||||
|
||||
if ((urlType.equals(URL_ARG_ALL)) ||
|
||||
(urlType.equals(URL_ARG_DESCRIPTION) && type.equals(MimetypeMap.MIMETYPE_OPENSEARCH_DESCRIPTION)) ||
|
||||
(urlType.equals(URL_ARG_TEMPLATE) && !type.equals(MimetypeMap.MIMETYPE_OPENSEARCH_DESCRIPTION)))
|
||||
{
|
||||
String label = engineConfig.getLabel();
|
||||
String labelId = engineConfig.getLabelId();
|
||||
if (labelId != null && labelId.length() > 0)
|
||||
{
|
||||
String i18nLabel = I18NUtil.getMessage(labelId);
|
||||
label = (i18nLabel == null) ? "$$" + labelId + "$$" : i18nLabel;
|
||||
}
|
||||
urls.add(new UrlTemplate(label, type, url));
|
||||
}
|
||||
|
||||
// TODO: Extract URL templates from OpenSearch description
|
||||
else if (urlType.equals(URL_ARG_TEMPLATE) &&
|
||||
type.equals(MimetypeMap.MIMETYPE_OPENSEARCH_DESCRIPTION))
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Retrieved " + urls.size() + " engine registrations");
|
||||
|
||||
return urls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model object for representing a registered search engine
|
||||
*/
|
||||
public static class UrlTemplate
|
||||
{
|
||||
private String type;
|
||||
private String label;
|
||||
private String url;
|
||||
private UrlTemplate engine;
|
||||
|
||||
public UrlTemplate(String label, String type, String url)
|
||||
{
|
||||
this.label = label;
|
||||
this.type = type;
|
||||
this.url = url;
|
||||
this.engine = null;
|
||||
}
|
||||
|
||||
public UrlTemplate(String label, String type, String url, UrlTemplate engine)
|
||||
{
|
||||
this(label, type, url);
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public String getLabel()
|
||||
{
|
||||
return label;
|
||||
}
|
||||
|
||||
public String getType()
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getUrl()
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getUrlType()
|
||||
{
|
||||
return (type.equals(MimetypeMap.MIMETYPE_OPENSEARCH_DESCRIPTION) ? "description" : "template");
|
||||
}
|
||||
|
||||
public UrlTemplate getEngine()
|
||||
{
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simple test that can be executed outside of web context
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
{
|
||||
SearchEngines service = (SearchEngines)APIServiceImpl.getMethod("web.api.SearchEngines");
|
||||
service.test(APIResponse.ATOM_FORMAT);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.services.APIServiceImpl#createTestModel()
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> createTestModel()
|
||||
{
|
||||
Map<String, Object> model = super.createTestModel();
|
||||
Set<UrlTemplate> urls = getUrls(URL_ARG_ALL);
|
||||
model.put("urltype", "template");
|
||||
model.put("engines", urls);
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
125
source/java/org/alfresco/web/api/services/Services.java
Normal file
125
source/java/org/alfresco/web/api/services/Services.java
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* 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.services;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.web.api.APIRequest;
|
||||
import org.alfresco.web.api.APIResponse;
|
||||
import org.alfresco.web.api.APIService;
|
||||
import org.alfresco.web.api.APIRequest.HttpMethod;
|
||||
import org.alfresco.web.api.APIRequest.RequiredAuthentication;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the list of available Web APIs
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class Services extends APIServiceTemplateImpl implements ApplicationContextAware
|
||||
{
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
|
||||
*/
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
|
||||
{
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getDescription()
|
||||
*/
|
||||
public String getDescription()
|
||||
{
|
||||
return "Retrieve the list of available Alfresco Web APIs";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getRequiredAuthentication()
|
||||
*/
|
||||
public RequiredAuthentication getRequiredAuthentication()
|
||||
{
|
||||
return RequiredAuthentication.None;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getHttpMethod()
|
||||
*/
|
||||
public HttpMethod getHttpMethod()
|
||||
{
|
||||
return HttpMethod.GET;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getDefaultFormat()
|
||||
*/
|
||||
public String getDefaultFormat()
|
||||
{
|
||||
return APIResponse.HTML_FORMAT;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.services.APIServiceTemplateImpl#createModel(org.alfresco.web.api.APIRequest, org.alfresco.web.api.APIResponse, java.util.Map)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> createModel(APIRequest req, APIResponse res, Map<String, Object> model)
|
||||
{
|
||||
model.put("services", getServices());
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the collection of API Services
|
||||
*
|
||||
* @return api services
|
||||
*/
|
||||
private Collection<APIService> getServices()
|
||||
{
|
||||
Map<String, APIService> services = applicationContext.getBeansOfType(APIService.class, false, false);
|
||||
return services.values();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simple test that can be executed outside of web context
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
{
|
||||
Services service = (Services)APIServiceImpl.getMethod("web.api.Services");
|
||||
service.test(APIResponse.HTML_FORMAT);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.services.APIServiceImpl#createTestModel()
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> createTestModel()
|
||||
{
|
||||
Map<String, Object> model = super.createTestModel();
|
||||
model.put("services", getServices());
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
@@ -16,29 +16,22 @@
|
||||
*/
|
||||
package org.alfresco.web.api.services;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Date;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.i18n.I18NUtil;
|
||||
import org.alfresco.repo.template.ISO8601DateFormatMethod;
|
||||
import org.alfresco.repo.template.UrlEncodeMethod;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.cmr.repository.TemplateImageResolver;
|
||||
import org.alfresco.service.cmr.repository.TemplateNode;
|
||||
import org.alfresco.service.cmr.repository.TemplateService;
|
||||
import org.alfresco.service.cmr.search.ResultSet;
|
||||
import org.alfresco.service.cmr.search.SearchParameters;
|
||||
import org.alfresco.service.cmr.search.SearchService;
|
||||
import org.alfresco.service.descriptor.DescriptorService;
|
||||
import org.alfresco.util.ApplicationContextHelper;
|
||||
import org.alfresco.util.GUID;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
import org.alfresco.web.api.APIException;
|
||||
import org.alfresco.web.api.APIRequest;
|
||||
import org.alfresco.web.api.APIResponse;
|
||||
@@ -47,7 +40,6 @@ import org.alfresco.web.api.APIRequest.RequiredAuthentication;
|
||||
import org.alfresco.web.ui.common.Utils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
|
||||
/**
|
||||
@@ -55,7 +47,7 @@ import org.springframework.context.ApplicationContext;
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class TextSearch extends APIServiceImpl
|
||||
public class TextSearch extends APIServiceTemplateImpl
|
||||
{
|
||||
// Logger
|
||||
private static final Log logger = LogFactory.getLog(TextSearch.class);
|
||||
@@ -64,6 +56,7 @@ public class TextSearch extends APIServiceImpl
|
||||
// TODO: allow configuration of search store
|
||||
protected static final StoreRef SEARCH_STORE = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
|
||||
protected static final int DEFAULT_ITEMS_PER_PAGE = 10;
|
||||
protected static final String QUERY_TEMPLATE_TYPE = "query";
|
||||
|
||||
// dependencies
|
||||
protected SearchService searchService;
|
||||
@@ -102,16 +95,33 @@ public class TextSearch extends APIServiceImpl
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#execute(org.alfresco.web.api.APIRequest, org.alfresco.web.api.APIResponse)
|
||||
* @see org.alfresco.web.api.APIService#getDefaultFormat()
|
||||
*/
|
||||
public void execute(APIRequest req, APIResponse res)
|
||||
throws IOException
|
||||
public String getDefaultFormat()
|
||||
{
|
||||
return APIResponse.HTML_FORMAT;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getDescription()
|
||||
*/
|
||||
public String getDescription()
|
||||
{
|
||||
return "Issue an Alfresco Web Client keyword search";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.services.APIServiceTemplateImpl#createModel(org.alfresco.web.api.APIRequest, org.alfresco.web.api.APIResponse, java.util.Map)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> createModel(APIRequest req, APIResponse res, Map<String, Object> model)
|
||||
{
|
||||
//
|
||||
// process parameters
|
||||
// process arguments
|
||||
//
|
||||
|
||||
String searchTerms = req.getParameter("q");
|
||||
ParameterCheck.mandatoryString("q", searchTerms);
|
||||
String startPageArg = req.getParameter("p");
|
||||
int startPage = 1;
|
||||
try
|
||||
@@ -147,31 +157,13 @@ public class TextSearch extends APIServiceImpl
|
||||
SearchResult results = search(searchTerms, startPage, itemsPerPage, locale);
|
||||
|
||||
//
|
||||
// render the results
|
||||
// append to model
|
||||
//
|
||||
|
||||
String contentType = APIResponse.HTML_TYPE;
|
||||
String template = HTML_TEMPLATE;
|
||||
|
||||
// TODO: data-drive this
|
||||
String format = req.getFormat();
|
||||
if (format.equals("atom"))
|
||||
{
|
||||
contentType = APIResponse.ATOM_TYPE;
|
||||
template = ATOM_TEMPLATE;
|
||||
}
|
||||
else if (format.equals("xml"))
|
||||
{
|
||||
contentType = APIResponse.XML_TYPE;
|
||||
template = ATOM_TEMPLATE;
|
||||
}
|
||||
|
||||
Map<String, Object> model = createTemplateModel(req, res);
|
||||
model.put("search", results);
|
||||
res.setContentType(contentType + ";charset=UTF-8");
|
||||
renderTemplate(template, model, res);
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Execute the search
|
||||
*
|
||||
@@ -190,7 +182,9 @@ public class TextSearch extends APIServiceImpl
|
||||
String[] terms = searchTerms.split(" ");
|
||||
Map<String, Object> statementModel = new HashMap<String, Object>(7, 1.0f);
|
||||
statementModel.put("terms", terms);
|
||||
String query = getTemplateService().processTemplateString(null, QUERY_STATEMENT, statementModel);
|
||||
Writer queryWriter = new StringWriter(1024);
|
||||
renderTemplate(QUERY_TEMPLATE_TYPE, null, statementModel, queryWriter);
|
||||
String query = queryWriter.toString();
|
||||
|
||||
// execute query
|
||||
if (logger.isDebugEnabled())
|
||||
@@ -408,162 +402,27 @@ public class TextSearch extends APIServiceImpl
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO: place into accessible file
|
||||
private final static String ATOM_TEMPLATE =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||
"<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:opensearch=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:relevance=\"http://a9.com/-/opensearch/extensions/relevance/1.0/\">\n" +
|
||||
" <generator version=\"${agent.version}\">Alfresco (${agent.edition})</generator>\n" +
|
||||
" <title>Alfresco Search: ${search.searchTerms}</title>\n" +
|
||||
" <updated>${xmldate(date)}</updated>\n" +
|
||||
" <icon>${request.path}/images/logo/AlfrescoLogo16.ico</icon>\n" +
|
||||
" <author>\n" +
|
||||
" <name><#if request.authenticatedUsername?exists>${request.authenticatedUsername}<#else>unknown</#if></name>\n" +
|
||||
" </author>\n" +
|
||||
" <id>urn:uuid:${search.id}</id>\n" +
|
||||
" <opensearch:totalResults>${search.totalResults}</opensearch:totalResults>\n" +
|
||||
" <opensearch:startIndex>${search.startIndex}</opensearch:startIndex>\n" +
|
||||
" <opensearch:itemsPerPage>${search.itemsPerPage}</opensearch:itemsPerPage>\n" +
|
||||
" <opensearch:Query role=\"request\" searchTerms=\"${search.searchTerms}\" startPage=\"${search.startPage}\" count=\"${search.itemsPerPage}\" language=\"${search.localeId}\"/>\n" +
|
||||
" <link rel=\"alternate\" href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=${search.startPage}&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}&format=html\" type=\"text/html\"/>\n" +
|
||||
" <link rel=\"self\" href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=${search.startPage}&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}&format=${request.format}\" type=\"application/atom+xml\"/>\n" +
|
||||
" <link rel=\"first\" href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=1&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}&format=${request.format}\" type=\"application/atom+xml\"/>\n" +
|
||||
"<#if search.startPage > 1>" +
|
||||
" <link rel=\"previous\" href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=${search.startPage - 1}&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}&format=${request.format}\" type=\"application/atom+xml\"/>\n" +
|
||||
"</#if>" +
|
||||
"<#if search.startPage < search.totalPages>" +
|
||||
" <link rel=\"next\" href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=${search.startPage + 1}&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}&format=${request.format}\" type=\"application/atom+xml\"/>\n" +
|
||||
"</#if>" +
|
||||
" <link rel=\"last\" href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=${search.totalPages}&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}&format=${request.format}\" type=\"application/atom+xml\"/>\n" +
|
||||
" <link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"${request.servicePath}/search/text/textsearchdescription.xml\"/>\n" +
|
||||
"<#list search.results as row>" +
|
||||
" <entry>\n" +
|
||||
" <title>${row.name}</title>\n" +
|
||||
" <link href=\"${request.path}${row.url}\"/>\n" +
|
||||
" <icon>${request.path}${row.icon16}</icon>\n" + // TODO: Standard for entry icons?
|
||||
" <id>urn:uuid:${row.id}</id>\n" +
|
||||
" <updated>${xmldate(row.properties.modified)}</updated>\n" +
|
||||
" <summary>${row.properties.description}</summary>\n" +
|
||||
" <author>\n" +
|
||||
" <name>${row.properties.creator}</name>\n" +
|
||||
" </author>\n" +
|
||||
" <relevance:score>${row.score}</relevance:score>\n" +
|
||||
" </entry>\n" +
|
||||
"</#list>" +
|
||||
"</feed>";
|
||||
|
||||
// TODO: place into accessible file
|
||||
private final static String HTML_TEMPLATE =
|
||||
"<?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" +
|
||||
" <head profile=\"http://a9.com/-/spec/opensearch/1.1/\">\n" +
|
||||
" <title>Alfresco Text Search: ${search.searchTerms}</title>\n" +
|
||||
" <link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"${request.servicePath}/search/text/textsearchdescription.xml\" title=\"Alfresco Text Search\"/>\n" +
|
||||
" <meta name=\"totalResults\" content=\"${search.totalResults}\"/>\n" +
|
||||
" <meta name=\"startIndex\" content=\"${search.startIndex}\"/>\n" +
|
||||
" <meta name=\"itemsPerPage\" content=\"${search.itemsPerPage}\"/>\n" +
|
||||
" </head>\n" +
|
||||
" <body>\n" +
|
||||
" <h2>Alfresco Text Search</h2>\n" +
|
||||
" Results <b>${search.startIndex}</b> - <b>${search.startIndex + search.totalPageItems - 1}</b> of <b>${search.totalResults}</b> for <b>${search.searchTerms}</b> " +
|
||||
"visible to user <b><#if request.authenticatedUsername?exists>${request.authenticatedUsername}<#else>unknown</#if>.</b>\n" +
|
||||
" <ul>\n" +
|
||||
"<#list search.results as row>" +
|
||||
" <li>\n" +
|
||||
" <img src=\"${request.path}${row.icon16}\"/>" +
|
||||
" <a href=\"${request.path}${row.url}\">${row.name}</a>\n" +
|
||||
" <div>\n" +
|
||||
" ${row.properties.description}\n" +
|
||||
" </div>\n" +
|
||||
" </li>\n" +
|
||||
"</#list>" +
|
||||
" </ul>\n" +
|
||||
" <a href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=1&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}\">first</a>" +
|
||||
"<#if search.startPage > 1>" +
|
||||
" <a href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=${search.startPage - 1}&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}\">previous</a>" +
|
||||
"</#if>" +
|
||||
" <a href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=${search.startPage}&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}\">${search.startPage}</a>" +
|
||||
"<#if search.startPage < search.totalPages>" +
|
||||
" <a href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=${search.startPage + 1}&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}\">next</a>" +
|
||||
"</#if>" +
|
||||
" <a href=\"${request.servicePath}/search/text?q=${urlencode(search.searchTerms)}&p=${search.totalPages}&c=${search.itemsPerPage}&l=${search.localeId}&guest=${request.guest?string(\"true\",\"\")}\">last</a>" +
|
||||
" </body>\n" +
|
||||
"</html>\n";
|
||||
|
||||
// TODO: place into accessible file
|
||||
private final static String QUERY_STATEMENT =
|
||||
"(" +
|
||||
"TYPE:\"{http://www.alfresco.org/model/content/1.0}content\" AND " +
|
||||
"(" +
|
||||
"(" +
|
||||
"<#list 1..terms?size as i>" +
|
||||
"@\\{http\\://www.alfresco.org/model/content/1.0\\}name:${terms[i - 1]}" +
|
||||
"<#if (i < terms?size)>" +
|
||||
" OR " +
|
||||
"</#if>" +
|
||||
"</#list>" +
|
||||
") " +
|
||||
"(" +
|
||||
"<#list 1..terms?size as i>" +
|
||||
"TEXT:${terms[i - 1]}" +
|
||||
"<#if (i < terms?size)>" +
|
||||
" OR " +
|
||||
"</#if>" +
|
||||
"</#list>" +
|
||||
")" +
|
||||
")" +
|
||||
")";
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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.setDescriptorService((DescriptorService)context.getBean(ServiceRegistry.DESCRIPTOR_SERVICE.getLocalName()));
|
||||
method.setHttpUri("/search/text");
|
||||
method.test();
|
||||
TextSearch service = (TextSearch)APIServiceImpl.getMethod("web.api.TextSearch");
|
||||
service.test(APIResponse.HTML_FORMAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple test that can be executed outside of web context
|
||||
*
|
||||
* TODO: Move to test harness
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.services.APIServiceImpl#createTestModel()
|
||||
*/
|
||||
private void test()
|
||||
@Override
|
||||
protected Map<String, Object> createTestModel()
|
||||
{
|
||||
Map<String, Object> model = super.createTestModel();
|
||||
SearchResult result = search("alfresco tutorial", 1, 5, I18NUtil.getLocale());
|
||||
|
||||
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");
|
||||
request.put("guest", false);
|
||||
request.put("format", "xml");
|
||||
searchModel.put("xmldate", new ISO8601DateFormatMethod());
|
||||
searchModel.put("urlencode", new UrlEncodeMethod());
|
||||
searchModel.put("date", new Date());
|
||||
searchModel.put("agent", getDescriptorService().getServerDescriptor());
|
||||
searchModel.put("request", request);
|
||||
searchModel.put("search", result);
|
||||
|
||||
StringWriter rendition = new StringWriter();
|
||||
PrintWriter writer = new PrintWriter(rendition);
|
||||
getTemplateService().processTemplateString(null, ATOM_TEMPLATE, searchModel, writer);
|
||||
System.out.println(rendition.toString());
|
||||
model.put("search", result);
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -16,7 +16,6 @@
|
||||
*/
|
||||
package org.alfresco.web.api.services;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.web.api.APIRequest;
|
||||
@@ -30,7 +29,7 @@ import org.alfresco.web.api.APIRequest.RequiredAuthentication;
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class TextSearchDescription extends APIServiceImpl
|
||||
public class TextSearchDescription extends APIServiceTemplateImpl
|
||||
{
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -50,27 +49,38 @@ public class TextSearchDescription extends APIServiceImpl
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#execute(org.alfresco.web.api.APIRequest, org.alfresco.web.api.APIResponse)
|
||||
* @see org.alfresco.web.api.APIService#getDefaultFormat()
|
||||
*/
|
||||
public void execute(APIRequest req, APIResponse res)
|
||||
throws IOException
|
||||
public String getDefaultFormat()
|
||||
{
|
||||
Map<String, Object> model = createTemplateModel(req, res);
|
||||
res.setContentType(APIResponse.OPEN_SEARCH_DESCRIPTION_TYPE + ";charset=UTF-8");
|
||||
renderTemplate(OPEN_SEARCH_DESCRIPTION, model, res);
|
||||
return APIResponse.OPENSEARCH_DESCRIPTION_FORMAT;
|
||||
}
|
||||
|
||||
// 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/\" xmlns:alf=\"http://www.alfresco.org\">\n" +
|
||||
" <ShortName>Alfresco Text Search</ShortName>\n" +
|
||||
" <LongName>Alfresco ${agent.edition} Text Search ${agent.version}</LongName>\n" +
|
||||
" <Description>Search Alfresco \"company home\" using text keywords</Description>\n" +
|
||||
" <Url type=\"application/atom+xml\" template=\"${request.servicePath}/search/text?q={searchTerms}&p={startPage?}&c={count?}&l={language?}&guest={alf:guest?}&format=atom\"/>\n" +
|
||||
" <Url type=\"text/xml\" template=\"${request.servicePath}/search/text?q={searchTerms}&p={startPage?}&c={count?}&l={language?}&guest={alf:guest?}&format=xml\"/>\n" +
|
||||
" <Url type=\"text/html\" template=\"${request.servicePath}/search/text?q={searchTerms}&p={startPage?}&c={count?}&l={language?}&guest={alf:guest?}\"/>\n" +
|
||||
" <Image height=\"16\" width=\"16\" type=\"image/x-icon\">${request.path}/images/logo/AlfrescoLogo16.ico</Image>\n" +
|
||||
"</OpenSearchDescription>";
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.services.APIServiceTemplateImpl#createModel(org.alfresco.web.api.APIRequest, org.alfresco.web.api.APIResponse, java.util.Map)
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> createModel(APIRequest req, APIResponse res, Map<String, Object> model)
|
||||
{
|
||||
return model;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.web.api.APIService#getDescription()
|
||||
*/
|
||||
public String getDescription()
|
||||
{
|
||||
return "Retrieve the OpenSearch Description for the Alfresco Web Client keyword search";
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple test that can be executed outside of web context
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
{
|
||||
TextSearchDescription service = (TextSearchDescription)APIServiceImpl.getMethod("web.api.TextSearchDescription");
|
||||
service.test(APIResponse.OPENSEARCH_DESCRIPTION_FORMAT);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user