001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.main;
029
030import org.opencms.file.CmsObject;
031import org.opencms.search.solr.spellchecking.CmsSolrSpellchecker;
032import org.opencms.site.CmsSite;
033import org.opencms.util.CmsStringUtil;
034
035import java.io.IOException;
036
037import javax.servlet.http.HttpServlet;
038import javax.servlet.http.HttpServletRequest;
039import javax.servlet.http.HttpServletResponse;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * Handles spell check requests.<p>
045 */
046public class OpenCmsSpellcheckHandler extends HttpServlet implements I_CmsRequestHandler {
047
048    /** A constant for the optional 'baseUri' parameter. */
049    public static final String PARAM_BASE_URI = "baseUri";
050
051    /** A constant for the HTTP 'referer'. */
052    protected static final String HEADER_REFERER_KEY = "referer";
053
054    /** The spell check handler name. */
055    private static final String HANDLER_NAME = "SpellcheckDictionary";
056
057    /** The handler names used by this request handler. */
058    private static final String[] HANDLER_NAMES = new String[] {HANDLER_NAME};
059
060    /** The log object for this class. */
061    private static final Log LOG = CmsLog.getLog(OpenCmsSpellcheckHandler.class);
062
063    /** The serial version id. */
064    private static final long serialVersionUID = -6028091947126209813L;
065
066    /**
067     * Returns the path to the spell check handler.<p>
068     *
069     * @return the path to the spell check handler
070     */
071    public static String getSpellcheckHandlerPath() {
072
073        return OpenCmsServlet.HANDLE_PATH + HANDLER_NAME;
074    }
075
076    /**
077     * Checks if the spell check request handler is configured.<p>
078     *
079     * @return <code>true</code> if the spell check request handler is configured
080     */
081    public static boolean isSpellcheckingEnabled() {
082
083        return OpenCmsCore.getInstance().getRequestHandler(HANDLER_NAME) != null;
084    }
085
086    /**
087     * @see org.opencms.main.I_CmsRequestHandler#getHandlerNames()
088     */
089    public String[] getHandlerNames() {
090
091        return HANDLER_NAMES;
092    }
093
094    /**
095     * @see org.opencms.main.I_CmsRequestHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
096     */
097    public void handle(HttpServletRequest req, HttpServletResponse res, String name) throws IOException {
098
099        CmsObject cms;
100        try {
101            cms = getCmsObject(req);
102
103            CmsSolrSpellchecker dict = OpenCms.getSearchManager().getSolrDictionary();
104            if (dict != null) {
105                dict.getSpellcheckingResult(res, req, cms);
106            }
107        } catch (CmsException e) {
108            LOG.error(e.getLocalizedMessage(), e);
109        }
110    }
111
112    /**
113     * Returns the CMS object.<p>
114     *
115     * @param req the request
116     *
117     * @return the CMS object
118     *
119     * @throws CmsException if something goes wrong
120     */
121    protected CmsObject getCmsObject(HttpServletRequest req) throws CmsException {
122
123        CmsObject cms = OpenCmsCore.getInstance().initCmsObjectFromSession(req);
124        // use the guest user as fall back
125        if (cms == null) {
126            cms = OpenCmsCore.getInstance().initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
127            String siteRoot = OpenCmsCore.getInstance().getSiteManager().matchRequest(req).getSiteRoot();
128            cms.getRequestContext().setSiteRoot(siteRoot);
129        }
130        String baseUri = getBaseUri(req, cms);
131        if (baseUri != null) {
132            cms.getRequestContext().setUri(baseUri);
133        }
134        return cms;
135    }
136
137    /**
138     * Returns the base URI.<p>
139     *
140     * @param req the servlet request
141     * @param cms the CmsObject
142     *
143     * @return the base URI
144     */
145    private String getBaseUri(HttpServletRequest req, CmsObject cms) {
146
147        String baseUri = req.getParameter(PARAM_BASE_URI);
148        if (CmsStringUtil.isEmptyOrWhitespaceOnly(baseUri)) {
149            String referer = req.getHeader(HEADER_REFERER_KEY);
150            CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(cms.getRequestContext().getSiteRoot());
151            if (site != null) {
152                String prefix = site.getServerPrefix(cms, "/") + OpenCms.getStaticExportManager().getVfsPrefix();
153                if ((referer != null) && referer.startsWith(prefix)) {
154                    baseUri = referer.substring(prefix.length());
155                }
156            }
157        }
158        return baseUri;
159    }
160}