/* * Copyright (C) 2005 Alfresco, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * As a special exception to the terms and conditions of version 2.0 of * the GPL, you may redistribute this Program in connection with Free/Libre * and Open Source Software ("FLOSS") applications as described in Alfresco's * FLOSS exception. You should have recieved a copy of the text describing * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ 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; protected SearchProxy searchProxy; /** * @param configService */ public void setConfigService(ConfigService configService) { this.configService = configService; } /** * @param searchProxy */ public void setSearchProxy(SearchProxy searchProxy) { this.searchProxy = searchProxy; } /* (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 createModel(APIRequest req, APIResponse res, Map 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 urls = getUrls(urlType); model.put("urltype", urlType); model.put("engines", urls); return model; } /** * Retrieve registered search engines * * @return set of search engines */ private Set getUrls(String urlType) { if (logger.isDebugEnabled()) logger.debug("Search Engine parameters: urltype=" + urlType); Set urls = new HashSet(); Config config = configService.getConfig("OpenSearch"); OpenSearchConfigElement searchConfig = (OpenSearchConfigElement)config.getConfigElement(OpenSearchConfigElement.CONFIG_ELEMENT_ID); for (OpenSearchConfigElement.EngineConfig engineConfig : searchConfig.getEngines()) { Map engineUrls = engineConfig.getUrls(); for (Map.Entry engineUrl : engineUrls.entrySet()) { String type = engineUrl.getKey(); String url = searchProxy.createUrl(engineConfig, type); 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); if (i18nLabel == null && label == null) { 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 createTestModel() { Map model = super.createTestModel(); Set urls = getUrls(URL_ARG_ALL); model.put("urltype", "template"); model.put("engines", urls); return model; } }