package net.sourceforge.fenixedu.presentationTier.servlets.ajax; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.domain.notifications.SystemNotifier; import pt.ist.fenixWebFramework.servlets.json.JsonObject; public class SystemNotifierServlet extends HttpServlet { public static String NOTIFIER_ACTIVE = "active"; public static String NOTIFIER_MESSAGE = "message"; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } private void process(HttpServletRequest req, HttpServletResponse resp) throws IOException { SystemNotifier notifier = RootDomainObject.getInstance() .getSystemNotifier(); JsonObject jsonObject = new JsonObject(); if (notifier.isActive()) { jsonObject.addAttribute(NOTIFIER_ACTIVE, "1"); jsonObject.addAttribute(NOTIFIER_MESSAGE, notifier.getMessage()); } else { jsonObject.addAttribute(NOTIFIER_ACTIVE, "0"); } byte[] jsonReply = jsonObject.getJsonString().getBytes("UTF-8"); final OutputStream outputStream = resp.getOutputStream(); resp.setContentType("text"); resp.setContentLength(jsonReply.length); outputStream.write(jsonReply); outputStream.flush(); outputStream.close(); } }