Improved rule URL action

This commit is contained in:
M66B 2023-10-03 16:11:42 +02:00
parent abb876ba0f
commit 5df3cad93f
4 changed files with 89 additions and 6 deletions

View File

@ -43,14 +43,15 @@ import androidx.room.PrimaryKey;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.HttpStatusException;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
@ -1401,6 +1402,10 @@ public class EntityRule {
private boolean onActionUrl(Context context, EntityMessage message, JSONObject jargs, String html) throws JSONException, IOException {
String url = jargs.getString("url");
String method = jargs.optString("method");
if (TextUtils.isEmpty(method))
method = "GET";
InternetAddress iaddr =
(message.from == null || message.from.length == 0
@ -1418,18 +1423,41 @@ public class EntityRule {
url = url.replace("$" + EXTRA_SUBJECT + "$", Uri.encode(message.subject == null ? "" : message.subject));
url = url.replace("$" + EXTRA_RECEIVED + "$", Uri.encode(DTF.format(message.received)));
String body = null;
if ("POST".equals(method) || "PUT".equals(method)) {
Uri u = Uri.parse(url);
body = u.getQuery();
url = u.buildUpon().clearQuery().build().toString();
}
Log.i("GET " + url);
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(false);
connection.setRequestMethod(method);
connection.setDoOutput(body != null);
connection.setReadTimeout(URL_TIMEOUT);
connection.setConnectTimeout(URL_TIMEOUT);
connection.setInstanceFollowRedirects(true);
ConnectionHelper.setUserAgent(context, connection);
connection.connect();
if (body != null)
connection.getOutputStream().write(body.getBytes());
int status = connection.getResponseCode();
if (status < 200 || status > 299) {
String error = "Error " + status + ": " + connection.getResponseMessage();
try {
InputStream is = connection.getErrorStream();
if (is != null)
error += "\n" + Helper.readStream(is);
} catch (Throwable ex) {
Log.w(ex);
}
throw new HttpStatusException(error, status, url);
}
} finally {
if (connection != null)
connection.disconnect();

View File

@ -172,7 +172,9 @@ public class FragmentRule extends FragmentBase {
private EditText etNotes;
private ViewButtonColor btnColorNotes;
private Spinner spUrlMethod;
private EditText etUrl;
private TextView tvUrlHint;
private BottomNavigationView bottom_navigation;
private ContentLoadingProgressBar pbWait;
@ -365,7 +367,9 @@ public class FragmentRule extends FragmentBase {
etNotes = view.findViewById(R.id.etNotes);
btnColorNotes = view.findViewById(R.id.btnColorNotes);
spUrlMethod = view.findViewById(R.id.spUrlMethod);
etUrl = view.findViewById(R.id.etUrl);
tvUrlHint = view.findViewById(R.id.tvUrlHint);
bottom_navigation = view.findViewById(R.id.bottom_navigation);
@ -820,6 +824,15 @@ public class FragmentRule extends FragmentBase {
}
});
tvUrlHint.setText(getString(R.string.title_rule_url_hint,
TextUtils.join(", ", new String[]{
"$" + EntityRule.EXTRA_RULE + "$",
"$" + EntityRule.EXTRA_SENDER + "$",
"$" + EntityRule.EXTRA_NAME + "$",
"$" + EntityRule.EXTRA_SUBJECT + "$",
"$" + EntityRule.EXTRA_RECEIVED + "$",
})));
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
@ -1369,6 +1382,13 @@ public class FragmentRule extends FragmentBase {
case EntityRule.TYPE_URL:
etUrl.setText(jaction.getString("url"));
String method = jaction.optString("method");
if (TextUtils.isEmpty(method))
method = "GET";
int pos = Arrays.asList(getResources().getStringArray(R.array.httpMethodNames))
.indexOf(method);
if (pos >= 0)
spUrlMethod.setSelection(pos);
break;
}
@ -1779,6 +1799,12 @@ public class FragmentRule extends FragmentBase {
case EntityRule.TYPE_URL:
jaction.put("url", etUrl.getText().toString().trim());
int pos = spUrlMethod.getSelectedItemPosition();
String[] methods = getResources().getStringArray(R.array.httpMethodNames);
if (pos >= 0 && pos < methods.length)
jaction.put("method", methods[pos]);
else
jaction.put("method", "GET");
break;
}
}

View File

@ -1291,6 +1291,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnColorNotes" />
<Spinner
android:id="@+id/spUrlMethod"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:entries="@array/httpMethodNames"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvUrl" />
<eu.faircode.email.EditTextPlain
android:id="@+id/etUrl"
android:layout_width="0dp"
@ -1299,7 +1309,18 @@
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvUrl" />
app:layout_constraintTop_toBottomOf="@id/spUrlMethod" />
<TextView
android:id="@+id/tvUrlHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_rule_url_hint"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etUrl" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpReady"
@ -1407,7 +1428,7 @@
android:id="@+id/grpUrl"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="tvUrl,etUrl" />
app:constraint_referenced_ids="tvUrl,spUrlMethod,etUrl,tvUrlHint" />
</androidx.constraintlayout.widget.ConstraintLayout>
</eu.faircode.email.ScrollViewEx>

View File

@ -1982,9 +1982,10 @@
<string name="title_rule_keyword_missing">Keyword missing</string>
<string name="title_rule_automation_hint">This will send the intent \'%1$s\' with the extras \'%2$s\'</string>
<string name="title_rule_notes_missing">Local notes missing</string>
<string name="title_rule_url_missing" translatable="false">URL missing or invalid</string>
<string name="title_rule_url_missing">URL missing or invalid</string>
<string name="title_rule_delete_hint">Permanent deletion is irreversible, so make sure the rule conditions are correct!</string>
<string name="title_rule_local_only_hint" translatable="false">Try to avoid bridging notifications to other devices, such as smartwatches. Not all devices support this.</string>
<string name="title_rule_url_hint">You can use the following placeholders: %1$s</string>
<string name="title_rule_edit_group">Edit group &#8230;</string>
<string name="title_rule_execute">Execute now</string>
@ -2744,6 +2745,13 @@
<item>Mobile</item>
</string-array>
<string-array name="httpMethodNames" translatable="false">
<item>GET</item>
<item>HEAD</item>
<item>POST</item>
<item>PUT</item>
</string-array>
<string name="fingerprint" translatable="false">17BA15C1AF55D925F98B99CEA4375D4CDF4C174B</string>
<string name="fingerprint_fdroid" translatable="false">77CD40058858DC3A38523E01C227A39AA019F88B</string>
<string name="fingerprint_amazon" translatable="false">200D0AA43A8ADBC7BB8237023C1553F4753CA7D2</string>