Search in sources :

Example 1 with WebScriptServletRuntime

use of org.springframework.extensions.webscripts.servlet.WebScriptServletRuntime 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)

Example 2 with WebScriptServletRuntime

use of org.springframework.extensions.webscripts.servlet.WebScriptServletRuntime in project alfresco-remote-api by Alfresco.

the class TenantWebScriptServlet method service.

/* (non-Javadoc) 
     * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    if (logger.isDebugEnabled())
        logger.debug("Processing tenant request (" + req.getMethod() + ") " + req.getRequestURL() + (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
    if (req.getCharacterEncoding() == null) {
        req.setCharacterEncoding("UTF-8");
    }
    setLanguageFromRequestHeader(req);
    try {
        WebScriptServletRuntime runtime = getRuntime(req, res);
        runtime.executeScript();
    } catch (IllegalStateException e) {
        if (e.getMessage().contains("getOutputStream() has already been called for this response")) {
            if (logger.isDebugEnabled()) {
                logger.warn("Client has cut off communication", e);
            } else {
                logger.warn("Client has cut off communication");
            }
        } else {
            throw e;
        }
    } finally {
        // clear threadlocal
        I18NUtil.setLocale(null);
        // clear authentication and tenant context
        AuthenticationUtil.clearCurrentSecurityContext();
    }
}
Also used : WebScriptServletRuntime(org.springframework.extensions.webscripts.servlet.WebScriptServletRuntime)

Aggregations

WebScriptServletRuntime (org.springframework.extensions.webscripts.servlet.WebScriptServletRuntime)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 EngineConfig (org.alfresco.repo.web.scripts.config.OpenSearchConfigElement.EngineConfig)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1