Implemented MS config POST request

pull/215/head
M66B 7 months ago
parent e5ee0b2023
commit 2fa388d109

@ -32,10 +32,13 @@ import android.util.Xml;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParserFactory;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -73,6 +76,14 @@ import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class EmailProvider implements Parcelable { public class EmailProvider implements Parcelable {
public String id; public String id;
@ -879,7 +890,7 @@ public class EmailProvider implements Parcelable {
for (String link : Misc.getMSUrls(context, domain, email)) for (String link : Misc.getMSUrls(context, domain, email))
try { try {
URL url = new URL(link); URL url = new URL(link);
return getMSAutodiscovery(context, domain, url, true, intf); return getMSAutodiscovery(context, domain, email, url, true, intf);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.i(ex); Log.i(ex);
} }
@ -887,18 +898,47 @@ public class EmailProvider implements Parcelable {
throw new UnknownHostException(domain); throw new UnknownHostException(domain);
} }
private static EmailProvider getMSAutodiscovery(Context context, String domain, URL url, boolean unsafe, IDiscovery intf) throws IOException, XmlPullParserException { private static EmailProvider getMSAutodiscovery(Context context, String domain, String email, URL url, boolean unsafe, IDiscovery intf)
throws IOException, XmlPullParserException, ParserConfigurationException, TransformerException {
EmailProvider provider = new EmailProvider(domain); EmailProvider provider = new EmailProvider(domain);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element xAutodiscover = doc.createElement("Autodiscover");
xAutodiscover.setAttribute("xmlns", "http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006");
doc.appendChild(xAutodiscover);
Element xRequest = doc.createElement("Request");
xAutodiscover.appendChild(xRequest);
Element xEMailAddress = doc.createElement("EMailAddress");
xEMailAddress.setTextContent(email);
xRequest.appendChild(xEMailAddress);
Element xAcceptableResponseSchema = doc.createElement("AcceptableResponseSchema");
xAcceptableResponseSchema.setTextContent("http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a");
xRequest.appendChild(xAcceptableResponseSchema);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
StreamResult result = new StreamResult(bos);
transformer.transform(source, result);
HttpURLConnection request = null; HttpURLConnection request = null;
try { try {
Log.i("Fetching " + url); Log.i("Fetching " + url);
intf.onStatus("MS " + url); intf.onStatus("MS " + url);
request = (HttpURLConnection) url.openConnection(); request = (HttpURLConnection) url.openConnection();
request.setRequestMethod("GET"); request.setRequestMethod("POST");
request.setReadTimeout(MS_TIMEOUT); request.setReadTimeout(MS_TIMEOUT);
request.setConnectTimeout(MS_TIMEOUT); request.setConnectTimeout(MS_TIMEOUT);
request.setDoOutput(true);
request.setDoInput(true); request.setDoInput(true);
ConnectionHelper.setUserAgent(context, request); ConnectionHelper.setUserAgent(context, request);
@ -913,6 +953,8 @@ public class EmailProvider implements Parcelable {
request.connect(); request.connect();
request.getOutputStream().write(bos.toByteArray());
int status = request.getResponseCode(); int status = request.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) if (status != HttpURLConnection.HTTP_OK)
throw new FileNotFoundException("Error " + status + ": " + request.getResponseMessage()); throw new FileNotFoundException("Error " + status + ": " + request.getResponseMessage());
@ -1014,6 +1056,8 @@ public class EmailProvider implements Parcelable {
provider.validate(); provider.validate();
Log.e("MS=" + url); Log.e("MS=" + url);
Log.i("MS imap=" + provider.imap);
Log.i("MS smtp=" + provider.smtp);
return provider; return provider;
} finally { } finally {

Loading…
Cancel
Save