org.valens.bamboo.servlets.LoggingServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.valens.bamboo.servlets.LoggingServlet.java

Source

/*
 * Copyright (C) 2015 Valens Soft
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.valens.bamboo.servlets;

import com.atlassian.bamboo.deployments.environments.events.EnvironmentConfigUpdatedEvent;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.sal.api.auth.LoginUriProvider;
import com.atlassian.sal.api.user.UserManager;
import com.atlassian.templaterenderer.TemplateRenderer;
import java.io.IOException;
import java.io.Writer;
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class LoggingServlet extends HttpServlet {
    protected static final Logger jlog = LoggerFactory.getLogger(LoggingServlet.class);
    private static Queue<String> log = new LinkedList<String>();

    public LoggingServlet() {

    }

    public static void debug(String s) {
        log.add(new Date(System.currentTimeMillis()).toString() + " " + s);
        log.add("<br/>");
        jlog.debug(s);
        if (log.size() > 100)
            while (log.size() > 100)
                log.remove();
    }

    public static void warn(String s) {
        log.add(new Date(System.currentTimeMillis()).toString() + " " + s);
        log.add("<br/>");
        jlog.warn(s);
        if (log.size() > 100)
            while (log.size() > 100)
                log.remove();

    }

    public static void error(String s, Exception e) {
        log.add(new Date(System.currentTimeMillis()).toString() + " " + s);
        log.add(new Date(System.currentTimeMillis()).toString() + " " + e.getMessage());
        log.add("<br/>");
        jlog.error(s, e);
        if (log.size() > 100)
            while (log.size() > 100)
                log.remove();
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doGet(request, response);
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        response.setContentType("text/html;charset=utf-8");
        StringBuffer s = new StringBuffer();
        for (String aux : log)
            s.append(aux);
        response.getWriter().append(s);
    }

}