Java tutorial
/** * ShinyProxy * * Copyright (C) 2016-2018 Open Analytics * * =========================================================================== * * This program is free software: you can redistribute it and/or modify * it under the terms of the Apache License as published by * The Apache Software Foundation, either version 2 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 * Apache License for more details. * * You should have received a copy of the Apache License * along with this program. If not, see <http://www.apache.org/licenses/> */ package eu.openanalytics.shinyproxy.controllers; import java.nio.file.Files; import java.nio.file.Path; import javax.inject.Inject; import javax.mail.internet.MimeMessage; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import eu.openanalytics.containerproxy.model.runtime.Proxy; import eu.openanalytics.containerproxy.service.LogService; @Controller public class IssueController extends BaseController { @Inject LogService logService; @Autowired(required = false) JavaMailSender mailSender; @RequestMapping(value = "/issue", method = RequestMethod.POST) public String postIssue(HttpServletRequest request, HttpServletResponse response) { IssueForm form = new IssueForm(); form.setUserName(getUserName(request)); form.setCurrentLocation(request.getParameter("currentLocation")); form.setAppName(getAppName(form.getCurrentLocation())); form.setCustomMessage(request.getParameter("customMessage")); Proxy activeProxy = null; for (Proxy proxy : proxyService.getProxies(null, false)) { if (proxy.getUserId().equals(form.getUserName()) && proxy.getSpec().getId().equals(form.getAppName())) { activeProxy = proxy; break; } } sendSupportMail(form, activeProxy); //TODO Redirect to current location return "redirect:" + getContextPath(); } public void sendSupportMail(IssueForm form, Proxy proxy) { String supportAddress = getSupportAddress(); if (supportAddress == null) throw new RuntimeException("Cannot send mail: no support address configured"); if (mailSender == null) throw new RuntimeException("Cannot send mail: no smtp settings configured"); try { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); // Headers helper.setFrom(environment.getProperty("proxy.support.mail-from-address", "issues@shinyproxy.io")); helper.addTo(supportAddress); helper.setSubject("ShinyProxy Error Report"); // Body StringBuilder body = new StringBuilder(); String lineSep = System.getProperty("line.separator"); body.append(String.format("This is an error report generated by ShinyProxy%s", lineSep)); body.append(String.format("User: %s%s", form.userName, lineSep)); if (form.appName != null) body.append(String.format("App: %s%s", form.appName, lineSep)); if (form.currentLocation != null) body.append(String.format("Location: %s%s", form.currentLocation, lineSep)); if (form.customMessage != null) body.append(String.format("Message: %s%s", form.customMessage, lineSep)); helper.setText(body.toString()); // Attachments (only if container-logging is enabled) if (logService.isLoggingEnabled() && proxy != null) { Path[] filePaths = logService.getLogFiles(proxy); for (Path p : filePaths) { if (Files.exists(p)) helper.addAttachment(p.toFile().getName(), p.toFile()); } } mailSender.send(message); } catch (Exception e) { throw new RuntimeException("Failed to send email", e); } } public static class IssueForm { private String userName; private String appName; private String currentLocation; private String customMessage; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public String getCurrentLocation() { return currentLocation; } public void setCurrentLocation(String currentLocation) { this.currentLocation = currentLocation; } public String getCustomMessage() { return customMessage; } public void setCustomMessage(String customMessage) { this.customMessage = customMessage; } } }