hsadmin email address

This commit is contained in:
Peter Hormanns 2020-03-20 12:02:54 +01:00
parent 9e036e98b7
commit ab010e0560
2 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,98 @@
package de.jalin.ldapadmin.hsadmin;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
public class EMailAddressDAO {
private TicketProvider ticketBox;
private XmlRpcClient rpcClient;
public EMailAddressDAO(final TicketProvider ticketBox) throws IOException {
this.ticketBox = ticketBox;
final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("https://config.hostsharing.net:443/hsar/xmlrpc/hsadmin"));
config.setEnabledForExtensions(true);
this.rpcClient = new XmlRpcClient();
this.rpcClient.setConfig(config);
}
public void assertEMailAddressExists(final String emailAddress, final String target) throws IOException, XmlRpcException, EMailAddressNotFound {
final String[] parts = emailAddress.split("@");
if (parts.length == 2) {
final String localpart = parts[0];
final String domain = parts[1];
final String pac = ticketBox.getAdminLogin();
try {
getEMailAddress(pac, localpart, domain);
updateEMailAddress(pac, localpart, domain, target);
} catch (EMailAddressNotFound e) {
createEMailAddress(pac, localpart, domain, target);
}
}
}
private String createEMailAddress(String pac, String localpart, String domain, String target) throws EMailAddressNotFound, XmlRpcException, IOException {
final List<Serializable> xmlRpcParamsList = new ArrayList<Serializable>();
xmlRpcParamsList.add(pac);
xmlRpcParamsList.add(ticketBox.getTicket());
final HashMap<String, Serializable> setParamsMap = new HashMap<String, Serializable>();
xmlRpcParamsList.add(setParamsMap);
setParamsMap.put("localpart", localpart);
setParamsMap.put("domain", domain);
setParamsMap.put("target", target);
final Object[] rpcResult = (Object[])rpcClient.execute("emailaddress.add", xmlRpcParamsList);
if (rpcResult.length == 1) {
@SuppressWarnings("unchecked")
final Map<String, Object> emailAddressMap = (Map<String, Object>) rpcResult[0];
return (String) emailAddressMap.get("emailaddress");
}
throw new EMailAddressNotFound();
}
private String updateEMailAddress(String pac, String localpart, String domain, String target) throws IOException, XmlRpcException, EMailAddressNotFound {
final List<Serializable> xmlRpcParamsList = new ArrayList<Serializable>();
xmlRpcParamsList.add(pac);
xmlRpcParamsList.add(ticketBox.getTicket());
final HashMap<String, Serializable> whereParamsMap = new HashMap<String, Serializable>();
xmlRpcParamsList.add(whereParamsMap);
final HashMap<String, Serializable> setParamsMap = new HashMap<String, Serializable>();
xmlRpcParamsList.add(setParamsMap);
whereParamsMap.put("localpart", localpart);
whereParamsMap.put("domain", domain);
setParamsMap.put("target", target);
final Object[] rpcResult = (Object[])rpcClient.execute("emailaddress.update", xmlRpcParamsList);
if (rpcResult.length == 1) {
@SuppressWarnings("unchecked")
final Map<String, Object> emailAddressMap = (Map<String, Object>) rpcResult[0];
return (String) emailAddressMap.get("emailaddress");
}
throw new EMailAddressNotFound();
}
private String getEMailAddress(String pac, String localpart, String domain) throws EMailAddressNotFound, IOException, XmlRpcException {
final List<Serializable> xmlRpcParamsList = new ArrayList<Serializable>();
xmlRpcParamsList.add(pac);
xmlRpcParamsList.add(ticketBox.getTicket());
final HashMap<String, Serializable> whereParamsMap = new HashMap<String, Serializable>();
xmlRpcParamsList.add(whereParamsMap);
whereParamsMap.put("localpart", localpart);
whereParamsMap.put("domain", domain);
final Object[] rpcResult = (Object[])rpcClient.execute("emailaddress.search", xmlRpcParamsList);
if (rpcResult.length == 1) {
@SuppressWarnings("unchecked")
final Map<String, Object> emailAddressMap = (Map<String, Object>) rpcResult[0];
return (String) emailAddressMap.get("emailaddress");
}
throw new EMailAddressNotFound();
}
}

View File

@ -0,0 +1,7 @@
package de.jalin.ldapadmin.hsadmin;
public class EMailAddressNotFound extends Exception {
private static final long serialVersionUID = 1L;
}