001/*
002 * (C) Copyright 2011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Julien Carsique
016 *
017 */
018
019package org.nuxeo.ecm.core.management.statuses;
020
021import java.io.IOException;
022import java.io.OutputStream;
023
024import javax.servlet.ServletException;
025import javax.servlet.http.HttpServlet;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031
032import org.nuxeo.common.Environment;
033import org.nuxeo.runtime.RuntimeService;
034import org.nuxeo.runtime.api.Framework;
035import org.nuxeo.runtime.osgi.OSGiRuntimeService;
036
037/**
038 * Servlet for retrieving Nuxeo services running status
039 */
040public class StatusServlet extends HttpServlet {
041
042    private static final long serialVersionUID = 1L;
043
044    private static final Log log = LogFactory.getLog(StatusServlet.class);
045
046    public static final String PARAM = "info";
047
048    public static final String PARAM_STARTED = "started";
049
050    public static final String PARAM_SUMMARY = "summary";
051
052    public static final String PARAM_SUMMARY_KEY = "key";
053
054    public static final String PARAM_RELOAD = "reload";
055
056    private OSGiRuntimeService runtimeService;
057
058    @Override
059    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
060        String param = req.getParameter(PARAM);
061        if (param != null) {
062            doPost(req, resp);
063        } else {
064            sendResponse(resp, "Ok");
065        }
066    }
067
068    @Override
069    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
070        StringBuilder response = new StringBuilder();
071        String requestedInfo = req.getParameter(PARAM);
072        if (requestedInfo.equals(PARAM_STARTED)) {
073            getStartedInfo(response);
074        } else if (requestedInfo.equals(PARAM_SUMMARY)) {
075            String givenKey = req.getParameter(PARAM_SUMMARY_KEY);
076            if (getRuntimeService().getProperty(Environment.SERVER_STATUS_KEY).equals(givenKey)) {
077                getSummaryInfo(response);
078            } else {
079                resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
080            }
081        } else if (requestedInfo.equals(PARAM_RELOAD)) {
082            if (isStarted()) {
083                response.append("reload();");
084            } else {
085                resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
086            }
087        }
088        sendResponse(resp, response.toString());
089    }
090
091    protected void sendResponse(HttpServletResponse resp, String response) throws IOException {
092        resp.setContentType("text/plain");
093        resp.setContentLength(response.getBytes().length);
094        OutputStream out = resp.getOutputStream();
095        out.write(response.getBytes());
096        out.close();
097    }
098
099    private RuntimeService getRuntimeService() {
100        if (runtimeService == null) {
101            runtimeService = (OSGiRuntimeService) Framework.getRuntime();
102        }
103        return runtimeService;
104    }
105
106    protected void getSummaryInfo(StringBuilder response) {
107        if (isStarted()) {
108            StringBuilder msg = new StringBuilder();
109            boolean isFine = runtimeService.getStatusMessage(msg);
110            response.append(isFine).append("\n");
111            response.append(msg);
112        } else {
113            response.append(false).append("\n");
114            response.append("Runtime failed to start");
115        }
116    }
117
118    protected void getStartedInfo(StringBuilder response) {
119        response.append(isStarted()).toString();
120    }
121
122    private boolean isStarted() {
123        return getRuntimeService() != null && runtimeService.isStarted();
124    }
125
126    @Override
127    public void init() throws ServletException {
128        log.debug("Ready.");
129    }
130
131}