blob: 7a7d70ea57b532acfbf82c871ad1909524674359 (
plain) (
tree)
|
|
package org.openslx.bwlp.sat.web;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Map;
import javax.security.auth.login.LoginException;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.openslx.bwlp.sat.mail.SmtpMailer;
import org.openslx.bwlp.sat.mail.SmtpMailer.EncryptionMode;
import org.openslx.bwlp.sat.maintenance.DeleteOldImages;
import org.openslx.util.Util;
import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.Response;
public class WebRpc {
public static Response handle(String uri, Map<String, String> params) {
if (uri.equals("mailtest")) {
return mailTest(params);
}
if (uri.equals("delete-images")) {
return deleteImages();
}
return WebServer.notFound();
}
private static Response deleteImages() {
StringBuilder res = DeleteOldImages.hardDeleteImages();
if (res == null)
return WebServer.internalServerError();
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/plain; charset=utf-8",
res.toString());
}
private static Response mailTest(Map<String, String> params) {
SmtpMailer smtpc;
String recipient = params.get("recipient");
String host = params.get("host");
String senderAddress = params.get("senderAddress");
String serverName = params.get("serverName");
String replyTo = params.get("replyTo");
String username = params.get("username");
String password = params.get("password");
int port = Util.parseInt(params.get("port"), 0);
EncryptionMode ssl;
try {
ssl = EncryptionMode.valueOf(params.get("ssl"));
} catch (Exception e) {
return WebServer.badRequest("Invalid SSL mode '" + params.get("ssl") + "'");
}
// Validate
if (port < 1 || port > 65535)
return WebServer.badRequest("Invalid port");
if (recipient == null)
return WebServer.badRequest("Missing recipient");
if (host == null)
return WebServer.badRequest("Missing host");
if (senderAddress == null)
return WebServer.badRequest("Missing senderAddress");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
smtpc = new SmtpMailer(host, port, ssl, senderAddress, serverName, replyTo, username, password,
new PrintStream(baos));
} catch (InvalidKeyException | LoginException | NoSuchAlgorithmException | InvalidKeySpecException
| IOException e) {
try {
baos.write("Could not connect to mail server".getBytes(StandardCharsets.UTF_8));
e.printStackTrace(new PrintWriter(baos));
} catch (IOException e1) {
}
smtpc = null;
}
boolean ret = false;
if (smtpc != null) {
ret = smtpc.send(recipient, "bwLehrpool Mail Test", "Test der Mailkonfiguration.\n\nHost: "
+ host + ":" + port + "\nSSL: " + ssl.toString() + "\nLogin: " + username);
}
try {
baos.write(("\n\n-----------------------------------------\nTestergebnis: "
+ (ret ? "" : "nicht ") + "erfolgreich").getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
}
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/plain; charset=utf-8",
new ByteArrayInputStream(baos.toByteArray()));
}
}
|