summaryrefslogblamecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java
blob: 6fdd57b69c926f9e88da1227b6bf166cb25b8b6a (plain) (tree)


































































                                                                                                                       
package org.openslx.bwlp.sat.web;

import java.io.IOException;
import java.io.PrintStream;
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.apache.log4j.Logger;
import org.openslx.bwlp.sat.mail.SmtpMailer;
import org.openslx.bwlp.sat.mail.SmtpMailer.EncryptionMode;
import org.openslx.util.Util;

import fi.iki.elonen.NanoHTTPD.Response;

public class WebRpc {

	private static final Logger LOGGER = Logger.getLogger(WebRpc.class);

	public static Response handle(String uri, Map<String, String> params) {
		if (uri.equals("mailtest")) {
			mailTest(params);
		}
		return WebServer.notFound();
	}

	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");
		}
		// 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) {
			LOGGER.error("Could not initialize connection to SMTP server. Mails will not be sent", e);
		}
		return WebServer.internalServerError();
	}

}