Search in sources :

Example 1 with EngineConfig

use of org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.EngineConfig in project acs-community-packaging by Alfresco.

the class UIOpenSearch method getRegisteredEngines.

/**
 * Returns a list of OpenSearchEngine objects representing the
 * registered OpenSearch engines.
 *
 * @param context Faces context
 * @return List of registered engines
 */
private List<OpenSearchEngine> getRegisteredEngines(FacesContext context) {
    List<OpenSearchEngine> engines = null;
    // get the web api config service object from spring
    ConfigService cfgSvc = (ConfigService) FacesContextUtils.getRequiredWebApplicationContext(context).getBean("webscripts.config");
    SearchProxy searchProxy = (SearchProxy) FacesContextUtils.getRequiredWebApplicationContext(context).getBean("webscript.org.alfresco.repository.search.searchproxy.get");
    if (cfgSvc != null) {
        // get the OpenSearch configuration
        Config cfg = cfgSvc.getConfig("OpenSearch");
        OpenSearchConfigElement osConfig = (OpenSearchConfigElement) cfg.getConfigElement(OpenSearchConfigElement.CONFIG_ELEMENT_ID);
        if (osConfig != null) {
            // generate the the list of engines with a unique for each
            int id = 1;
            engines = new ArrayList<OpenSearchEngine>();
            Set<EngineConfig> enginesCfg = osConfig.getEngines();
            for (EngineConfig engineCfg : enginesCfg) {
                // resolve engine label
                String label = engineCfg.getLabel();
                String labelId = engineCfg.getLabelId();
                if (labelId != null && labelId.length() > 0) {
                    label = Application.getMessage(context, labelId);
                }
                // locate search engine template url of most appropriate response type
                String url = searchProxy.createUrl(engineCfg, MimetypeMap.MIMETYPE_ATOM);
                if (url == null) {
                    url = searchProxy.createUrl(engineCfg, MimetypeMap.MIMETYPE_RSS);
                }
                if (url != null) {
                    if (url.startsWith("/")) {
                        url = context.getExternalContext().getRequestContextPath() + "/wcservice" + url;
                    }
                    // add the engine
                    OpenSearchEngine engine = new OpenSearchEngine(id, label, url);
                    engines.add(engine);
                    // increase the id counter
                    id++;
                }
            }
        }
    }
    return engines;
}
Also used : ConfigService(org.springframework.extensions.config.ConfigService) EngineConfig(org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.EngineConfig) Config(org.springframework.extensions.config.Config) SearchProxy(org.alfresco.repo.web.scripts.bean.SearchProxy) EngineConfig(org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.EngineConfig) OpenSearchConfigElement(org.alfresco.repo.web.scripts.config.OpenSearchConfigElement)

Example 2 with EngineConfig

use of org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.EngineConfig in project alfresco-remote-api by Alfresco.

the class OpenSearchElementReader method parse.

/**
 * @see org.springframework.extensions.config.xml.elementreader.ConfigElementReader#parse(org.dom4j.Element)
 */
@SuppressWarnings("unchecked")
public ConfigElement parse(Element element) {
    OpenSearchConfigElement configElement = null;
    if (element != null) {
        String elementName = element.getName();
        if (elementName.equals(ELEMENT_OPENSEARCH) == false) {
            throw new ConfigException("OpenSearchElementReader can only parse " + ELEMENT_OPENSEARCH + "elements, the element passed was '" + elementName + "'");
        }
        // go through the registered engines
        configElement = new OpenSearchConfigElement();
        Element pluginsElem = element.element(ELEMENT_ENGINES);
        if (pluginsElem != null) {
            Iterator<Element> engines = pluginsElem.elementIterator(ELEMENT_ENGINE);
            while (engines.hasNext()) {
                // construct engine
                Element engineElem = engines.next();
                String label = engineElem.attributeValue(ATTR_LABEL);
                String labelId = engineElem.attributeValue(ATTR_LABEL_ID);
                String proxy = engineElem.attributeValue(ATTR_PROXY);
                EngineConfig engineCfg = new EngineConfig(label, labelId, proxy);
                // construct urls for engine
                Iterator<Element> urlsConfig = engineElem.elementIterator(ELEMENT_URL);
                while (urlsConfig.hasNext()) {
                    Element urlConfig = urlsConfig.next();
                    String type = urlConfig.attributeValue(ATTR_TYPE);
                    String url = urlConfig.getTextTrim();
                    engineCfg.addUrl(type, url);
                }
                // register engine config
                configElement.addEngine(engineCfg);
            }
        }
        // extract proxy configuration
        String url = null;
        Element proxyElem = element.element(ELEMENT_PROXY);
        if (proxyElem != null) {
            Element urlElem = proxyElem.element(ELEMENT_URL);
            if (urlElem != null) {
                url = urlElem.getTextTrim();
                ProxyConfig proxyCfg = new ProxyConfig(url);
                configElement.setProxy(proxyCfg);
            }
        }
    }
    return configElement;
}
Also used : ConfigElement(org.springframework.extensions.config.ConfigElement) Element(org.dom4j.Element) ConfigException(org.springframework.extensions.config.ConfigException) EngineConfig(org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.EngineConfig) ProxyConfig(org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.ProxyConfig)

Example 3 with EngineConfig

use of org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.EngineConfig in project alfresco-remote-api by Alfresco.

the class SearchProxy method execute.

/* (non-Javadoc)
     * @see org.alfresco.web.scripts.WebScript#execute(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
     */
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
    String extensionPath = req.getExtensionPath();
    String[] extensionPaths = extensionPath.split("/");
    if (extensionPaths.length != 2) {
        throw new WebScriptException("OpenSearch engine has not been specified as /{engine}/{format}");
    }
    // retrieve search engine configuration
    String engine = extensionPaths[0];
    EngineConfig engineConfig = searchConfig.getEngine(engine);
    if (engineConfig == null) {
        throw new WebScriptException("OpenSearch engine '" + engine + "' does not exist");
    }
    // retrieve engine url as specified by format
    String format = extensionPaths[1];
    String mimetype = formatRegistry.getMimeType(null, format);
    if (mimetype == null) {
        throw new WebScriptException("Format '" + format + "' does not map to a registered mimetype");
    }
    Map<String, String> engineUrls = engineConfig.getUrls();
    String engineUrl = engineUrls.get(mimetype);
    if (engineUrl == null) {
        throw new WebScriptException("Url mimetype '" + mimetype + "' does not exist for engine '" + engine + "'");
    }
    // replace template url arguments with actual arguments specified on request
    int engineUrlArgIdx = engineUrl.indexOf("?");
    if (engineUrlArgIdx != -1) {
        engineUrl = engineUrl.substring(0, engineUrlArgIdx);
    }
    if (req.getQueryString() != null) {
        engineUrl += "?" + req.getQueryString();
    }
    if (logger.isDebugEnabled())
        logger.debug("Mapping engine '" + engine + "' (mimetype '" + mimetype + "') to url '" + engineUrl + "'");
    // NOTE: This web script must be executed in a HTTP servlet environment
    if (!(res.getRuntime() instanceof WebScriptServletRuntime)) {
        throw new WebScriptException("Search Proxy must be executed in HTTP Servlet environment");
    }
    HttpServletResponse servletRes = WebScriptServletRuntime.getHttpServletResponse(res);
    SearchEngineHttpProxy proxy = new SearchEngineHttpProxy(req.getServerPath() + req.getServiceContextPath(), engine, engineUrl, servletRes, Collections.singletonMap("User-Agent", req.getHeader("User-Agent")));
    proxy.service();
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) WebScriptServletRuntime(org.springframework.extensions.webscripts.servlet.WebScriptServletRuntime) HttpServletResponse(javax.servlet.http.HttpServletResponse) EngineConfig(org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.EngineConfig)

Aggregations

EngineConfig (org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.EngineConfig)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 SearchProxy (org.alfresco.repo.web.scripts.bean.SearchProxy)1 OpenSearchConfigElement (org.alfresco.repo.web.scripts.config.OpenSearchConfigElement)1 ProxyConfig (org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.ProxyConfig)1 Element (org.dom4j.Element)1 Config (org.springframework.extensions.config.Config)1 ConfigElement (org.springframework.extensions.config.ConfigElement)1 ConfigException (org.springframework.extensions.config.ConfigException)1 ConfigService (org.springframework.extensions.config.ConfigService)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1 WebScriptServletRuntime (org.springframework.extensions.webscripts.servlet.WebScriptServletRuntime)1