001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.CompanyConstants;
021    import com.liferay.portal.search.lucene.LuceneHelperUtil;
022    import com.liferay.portal.security.auth.TransientTokenUtil;
023    import com.liferay.portal.service.CompanyLocalServiceUtil;
024    
025    import java.io.IOException;
026    
027    import javax.servlet.http.HttpServlet;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class LuceneServlet extends HttpServlet {
035    
036            @Override
037            public void service(
038                            HttpServletRequest request, HttpServletResponse response)
039                    throws IOException {
040    
041                    String pathInfo = GetterUtil.getString(request.getPathInfo());
042    
043                    if (!pathInfo.equals("/dump")) {
044                            response.sendError(HttpServletResponse.SC_NOT_FOUND);
045    
046                            return;
047                    }
048    
049                    String transientToken = ParamUtil.getString(request, "transientToken");
050    
051                    if (Validator.isNull(transientToken)) {
052                            response.sendError(HttpServletResponse.SC_FORBIDDEN);
053    
054                            return;
055                    }
056    
057                    if (!TransientTokenUtil.checkToken(transientToken)) {
058                            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
059    
060                            return;
061                    }
062    
063                    long companyId = ParamUtil.getLong(request, "companyId");
064    
065                    if (companyId < CompanyConstants.SYSTEM) {
066                            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
067    
068                            return;
069                    }
070    
071                    if (companyId != CompanyConstants.SYSTEM) {
072                            try {
073                                    CompanyLocalServiceUtil.getCompanyById(companyId);
074                            }
075                            catch (Exception e) {
076                                    response.sendError(HttpServletResponse.SC_BAD_REQUEST);
077    
078                                    return;
079                            }
080                    }
081    
082                    LuceneHelperUtil.dumpIndex(companyId, response.getOutputStream());
083            }
084    
085    }