001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Julien Carsique 018 * 019 */ 020 021package org.nuxeo.ecm.core.management.statuses; 022 023import java.io.IOException; 024import java.io.OutputStream; 025 026import javax.servlet.ServletException; 027import javax.servlet.http.HttpServlet; 028import javax.servlet.http.HttpServletRequest; 029import javax.servlet.http.HttpServletResponse; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033 034import org.nuxeo.common.Environment; 035import org.nuxeo.runtime.AbstractRuntimeService; 036import org.nuxeo.runtime.RuntimeService; 037import org.nuxeo.runtime.api.Framework; 038 039/** 040 * Servlet for retrieving Nuxeo services running status 041 */ 042public class StatusServlet extends HttpServlet { 043 044 private static final long serialVersionUID = 1L; 045 046 private static final Log log = LogFactory.getLog(StatusServlet.class); 047 048 public static final String PARAM = "info"; 049 050 public static final String PARAM_STARTED = "started"; 051 052 public static final String PARAM_SUMMARY = "summary"; 053 054 public static final String PARAM_SUMMARY_KEY = "key"; 055 056 public static final String PARAM_RELOAD = "reload"; 057 058 private AbstractRuntimeService runtimeService; 059 060 @Override 061 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 062 String param = req.getParameter(PARAM); 063 if (param != null) { 064 doPost(req, resp); 065 } else { 066 sendResponse(resp, "Ok"); 067 } 068 } 069 070 @Override 071 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 072 StringBuilder response = new StringBuilder(); 073 String requestedInfo = req.getParameter(PARAM); 074 if (requestedInfo.equals(PARAM_STARTED)) { 075 getStartedInfo(response); 076 } else if (requestedInfo.equals(PARAM_SUMMARY)) { 077 String givenKey = req.getParameter(PARAM_SUMMARY_KEY); 078 if (getRuntimeService().getProperty(Environment.SERVER_STATUS_KEY).equals(givenKey)) { 079 getSummaryInfo(response); 080 } else { 081 resp.setStatus(HttpServletResponse.SC_FORBIDDEN); 082 } 083 } else if (requestedInfo.equals(PARAM_RELOAD)) { 084 if (isStarted()) { 085 response.append("reload();"); 086 } else { 087 resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); 088 } 089 } 090 sendResponse(resp, response.toString()); 091 } 092 093 protected void sendResponse(HttpServletResponse resp, String response) throws IOException { 094 resp.setContentType("text/plain"); 095 resp.setContentLength(response.getBytes().length); 096 OutputStream out = resp.getOutputStream(); 097 out.write(response.getBytes()); 098 out.close(); 099 } 100 101 private RuntimeService getRuntimeService() { 102 if (runtimeService == null) { 103 runtimeService = (AbstractRuntimeService) Framework.getRuntime(); 104 } 105 return runtimeService; 106 } 107 108 protected void getSummaryInfo(StringBuilder response) { 109 if (isStarted()) { 110 StringBuilder msg = new StringBuilder(); 111 boolean isFine = runtimeService.getStatusMessage(msg); 112 response.append(isFine).append("\n"); 113 response.append(msg); 114 } else { 115 response.append(false).append("\n"); 116 response.append("Runtime failed to start"); 117 } 118 } 119 120 protected void getStartedInfo(StringBuilder response) { 121 response.append(isStarted()).toString(); 122 } 123 124 private boolean isStarted() { 125 return getRuntimeService() != null && runtimeService.isStarted(); 126 } 127 128 @Override 129 public void init() throws ServletException { 130 log.debug("Ready."); 131 } 132 133}