Java tutorial
/* * 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 org.valens.bamboo.events.DeployReplicationEvent; import org.valens.bamboo.events.BuildReplicationEvent; 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.HashMap; 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 AdminServlet extends HttpServlet { private UserManager userManager; private LoginUriProvider loginUriProvider; private TemplateRenderer renderer; private EventPublisher eventPublisher; protected static final Logger log = LoggerFactory.getLogger(AdminServlet.class); public AdminServlet() { } @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 { String username = this.userManager.getRemoteUsername(request); if (username == null || !this.userManager.isSystemAdmin(username)) { this.redirectToLogin(request, response); return; } String scr = (String) request.getParameter("custom.bamboo.template.repl.depl"); HashMap<String, Object> par = new HashMap<String, Object>(); par.put("trigger", this); par.put("eventText", ""); if (scr != null) { if (scr.contains("Replicate Deployments")) { par.put("eventText", "The event has been generated. replication was triggered."); eventPublisher.publish(new DeployReplicationEvent(0)); } if (scr.contains("Replicate Builds")) { par.put("eventText", "The event has been generated. replication was triggered."); eventPublisher.publish(new BuildReplicationEvent("WITHOUT-PAUSE")); } if (scr.contains("Replicate Builds With Server Pause")) { par.put("eventText", "The event has been generated. replication was triggered."); eventPublisher.publish(new BuildReplicationEvent("WITH-PAUSE")); } } response.setContentType("text/html;charset=utf-8"); this.renderer.render("templates/admin.vm", par, (Writer) response.getWriter()); } private void redirectToLogin(HttpServletRequest request, HttpServletResponse response) throws IOException { response.sendRedirect(this.loginUriProvider.getLoginUri(this.getUri(request)).toASCIIString()); } private URI getUri(HttpServletRequest request) { StringBuffer builder = request.getRequestURL(); if (request.getQueryString() != null) { builder.append("?"); builder.append(request.getQueryString()); } return URI.create(builder.toString()); } public UserManager getUserManager() { return userManager; } public void setUserManager(UserManager userManager) { this.userManager = userManager; } public LoginUriProvider getLoginUriProvider() { return loginUriProvider; } public void setLoginUriProvider(LoginUriProvider loginUriProvider) { this.loginUriProvider = loginUriProvider; } public TemplateRenderer getRenderer() { return renderer; } public void setRenderer(TemplateRenderer renderer) { this.renderer = renderer; } public EventPublisher getEventPublisher() { return eventPublisher; } public void setEventPublisher(EventPublisher eventPublisher) { this.eventPublisher = eventPublisher; } }