Http method selectable

This commit is contained in:
Jens 2023-12-11 22:50:07 +01:00
parent 3c8c0f14f2
commit 1b8dc5de5f
6 changed files with 124 additions and 28 deletions

View File

@ -648,6 +648,7 @@ public class Action
{ {
String username = null; String username = null;
String password = null; String password = null;
String method = ActivityManageActionTriggerUrl.methodGet;
String url; String url;
String[] components = getParameter2().split(";"); String[] components = getParameter2().split(";");
@ -657,6 +658,9 @@ public class Action
username = components[0]; username = components[0];
password = components[1]; password = components[1];
url = components[2]; url = components[2];
if(components.length >= 4)
method = components[3];
} }
else // compatibility for very old versions which haven't upgraded, yet. else // compatibility for very old versions which haven't upgraded, yet.
url = components[0]; url = components[0];
@ -670,7 +674,7 @@ public class Action
Miscellaneous.logEvent("i", "HTTP", "Attempting download of " + url, 4); //getResources().getString("attemptingDownloadOf"); Miscellaneous.logEvent("i", "HTTP", "Attempting download of " + url, 4); //getResources().getString("attemptingDownloadOf");
if(this.getParameter1()) // use authentication if(this.getParameter1()) // use authentication
new DownloadTask().execute(url, username, password); new DownloadTask().execute(url, username, password, methodnochindiesignaturaufnehmen);
else else
new DownloadTask().execute(url, null, null); new DownloadTask().execute(url, null, null);
} }
@ -692,16 +696,19 @@ public class Action
String urlUsername = null; String urlUsername = null;
String urlPassword = null; String urlPassword = null;
String method = ActivityManageActionTriggerUrl.methodGet;
if(parameters.length >= 3) if(parameters.length >= 3)
{ {
urlUsername=parameters[1]; urlUsername = parameters[1];
urlPassword=parameters[2]; urlPassword = parameters[2];
if(parameters.length >= 4)
method = parameters[3];
} }
String response = "httpError"; String response = "httpError";
HttpGet post;
if(Settings.httpAttempts < 1) if(Settings.httpAttempts < 1)
Miscellaneous.logEvent("w", "HTTP Request", Miscellaneous.getAnyContext().getResources().getString(R.string.cantDownloadTooFewRequestsInSettings), 3); Miscellaneous.logEvent("w", "HTTP Request", Miscellaneous.getAnyContext().getResources().getString(R.string.cantDownloadTooFewRequestsInSettings), 3);
while(attempts <= Settings.httpAttempts && response.equals("httpError")) while(attempts <= Settings.httpAttempts && response.equals("httpError"))
@ -710,9 +717,9 @@ public class Action
// Either thorough checking or no encryption // Either thorough checking or no encryption
if(!Settings.httpAcceptAllCertificates || !urlString.toLowerCase(Locale.getDefault()).contains("https")) if(!Settings.httpAcceptAllCertificates || !urlString.toLowerCase(Locale.getDefault()).contains("https"))
response = Miscellaneous.downloadURL(urlString, urlUsername, urlPassword); response = Miscellaneous.downloadURL(urlString, urlUsername, urlPassword, method);
else else
response = Miscellaneous.downloadURLwithoutCertificateChecking(urlString, urlUsername, urlPassword); response = Miscellaneous.downloadURLwithoutCertificateChecking(urlString, urlUsername, urlPassword, method);
try try
{ {

View File

@ -13,6 +13,7 @@ import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TableLayout; import android.widget.TableLayout;
import android.widget.Toast; import android.widget.Toast;
@ -26,12 +27,16 @@ public class ActivityManageActionTriggerUrl extends Activity
EditText etTriggerUrl, etTriggerUrlUsername, etTriggerUrlPassword; EditText etTriggerUrl, etTriggerUrlUsername, etTriggerUrlPassword;
ListView lvTriggerUrlPostParameters; ListView lvTriggerUrlPostParameters;
CheckBox chkTriggerUrlUseAuthentication; CheckBox chkTriggerUrlUseAuthentication;
RadioButton rbTriggerUrlMethodGet, rbTriggerUrlMethodPost;
TableLayout tlTriggerUrlAuthentication; TableLayout tlTriggerUrlAuthentication;
ArrayAdapter<Map<String,String>> lvTriggerUrlPostParametersAdapter; ArrayAdapter<Map<String,String>> lvTriggerUrlPostParametersAdapter;
// private String existingUrl = ""; // private String existingUrl = "";
public static final String methodGet = "GET";
public static final String methodPost = "POST";
public static boolean edit = false; public static boolean edit = false;
public static Action resultingAction = null; public static Action resultingAction = null;
@ -49,6 +54,9 @@ public class ActivityManageActionTriggerUrl extends Activity
lvTriggerUrlPostParameters = (ListView)findViewById(R.id.lvTriggerUrlPostParameters); lvTriggerUrlPostParameters = (ListView)findViewById(R.id.lvTriggerUrlPostParameters);
tlTriggerUrlAuthentication = (TableLayout)findViewById(R.id.tlTriggerUrlAuthentication); tlTriggerUrlAuthentication = (TableLayout)findViewById(R.id.tlTriggerUrlAuthentication);
bSaveTriggerUrl = (Button)findViewById(R.id.bSaveSpeakText); bSaveTriggerUrl = (Button)findViewById(R.id.bSaveSpeakText);
rbTriggerUrlMethodGet = (RadioButton) findViewById(R.id.rbTriggerUrlMethodGet);
rbTriggerUrlMethodPost = (RadioButton) findViewById(R.id.rbTriggerUrlMethodPost);
bSaveTriggerUrl.setOnClickListener(new OnClickListener() bSaveTriggerUrl.setOnClickListener(new OnClickListener()
{ {
@Override @Override
@ -59,6 +67,8 @@ public class ActivityManageActionTriggerUrl extends Activity
if(resultingAction == null) if(resultingAction == null)
{ {
resultingAction = new Action(); resultingAction = new Action();
}
resultingAction.setAction(Action_Enum.triggerUrl); resultingAction.setAction(Action_Enum.triggerUrl);
resultingAction.setParameter1(chkTriggerUrlUseAuthentication.isChecked()); resultingAction.setParameter1(chkTriggerUrlUseAuthentication.isChecked());
@ -71,12 +81,17 @@ public class ActivityManageActionTriggerUrl extends Activity
if(password == null) if(password == null)
password = ""; password = "";
String method = methodGet;
if(rbTriggerUrlMethodPost.isChecked())
method = methodPost;
ActivityManageActionTriggerUrl.resultingAction.setParameter2( ActivityManageActionTriggerUrl.resultingAction.setParameter2(
username + ";" + username + ";" +
password + ";" + password + ";" +
etTriggerUrl.getText().toString().trim() etTriggerUrl.getText().toString().trim() + ";" +
method
); );
}
backToRuleManager(); backToRuleManager();
} }
else else
@ -84,16 +99,25 @@ public class ActivityManageActionTriggerUrl extends Activity
} }
}); });
chkTriggerUrlUseAuthentication.setOnCheckedChangeListener(new OnCheckedChangeListener() chkTriggerUrlUseAuthentication.setOnCheckedChangeListener(new OnCheckedChangeListener()
{ {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ {
if(isChecked) if(isChecked)
{
tlTriggerUrlAuthentication.setVisibility(View.VISIBLE); tlTriggerUrlAuthentication.setVisibility(View.VISIBLE);
rbTriggerUrlMethodGet.setChecked(false);
rbTriggerUrlMethodPost.setChecked(true);
rbTriggerUrlMethodGet.setEnabled(false);
rbTriggerUrlMethodPost.setEnabled(false);
}
else else
{
tlTriggerUrlAuthentication.setVisibility(View.GONE); tlTriggerUrlAuthentication.setVisibility(View.GONE);
rbTriggerUrlMethodGet.setEnabled(true);
rbTriggerUrlMethodPost.setEnabled(true);
}
etTriggerUrlUsername.setEnabled(isChecked); etTriggerUrlUsername.setEnabled(isChecked);
etTriggerUrlPassword.setEnabled(isChecked); etTriggerUrlPassword.setEnabled(isChecked);
@ -110,7 +134,6 @@ public class ActivityManageActionTriggerUrl extends Activity
}); });
updateListView(); updateListView();
ActivityManageActionTriggerUrl.edit = getIntent().getBooleanExtra("edit", false); ActivityManageActionTriggerUrl.edit = getIntent().getBooleanExtra("edit", false);
if(edit) if(edit)
{ {
@ -123,6 +146,20 @@ public class ActivityManageActionTriggerUrl extends Activity
chkTriggerUrlUseAuthentication.setChecked(ActivityManageActionTriggerUrl.resultingAction.getParameter1()); chkTriggerUrlUseAuthentication.setChecked(ActivityManageActionTriggerUrl.resultingAction.getParameter1());
etTriggerUrlUsername.setText(components[0]); etTriggerUrlUsername.setText(components[0]);
etTriggerUrlPassword.setText(components[1]); etTriggerUrlPassword.setText(components[1]);
if(components.length >= 4)
{
switch(components[3])
{
case methodPost:
rbTriggerUrlMethodPost.setChecked(true);
break;
case methodGet:
default:
rbTriggerUrlMethodGet.setChecked(true);
break;
}
}
} }
else else
etTriggerUrl.setText(components[0]); etTriggerUrl.setText(components[0]);
@ -142,12 +179,17 @@ public class ActivityManageActionTriggerUrl extends Activity
if(password == null) if(password == null)
password = ""; password = "";
String method = methodGet;
if(rbTriggerUrlMethodPost.isChecked())
method = methodPost;
ActivityManageActionTriggerUrl.resultingAction.setParameter1(chkTriggerUrlUseAuthentication.isChecked()); ActivityManageActionTriggerUrl.resultingAction.setParameter1(chkTriggerUrlUseAuthentication.isChecked());
ActivityManageActionTriggerUrl.resultingAction.setParameter2( ActivityManageActionTriggerUrl.resultingAction.setParameter2(
username + ";" + username + ";" +
password + ";" + password + ";" +
etTriggerUrl.getText().toString() etTriggerUrl.getText().toString() + ";" +
method
); );
} }

View File

@ -44,7 +44,10 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion; import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams; import org.apache.http.params.BasicHttpParams;
@ -120,7 +123,7 @@ public class Miscellaneous extends Service
public static final String lineSeparator = System.getProperty("line.separator"); public static final String lineSeparator = System.getProperty("line.separator");
public static String downloadURL(String url, String username, String password) public static String downloadURL(String url, String username, String password, String method)
{ {
HttpClient httpclient = new DefaultHttpClient(); HttpClient httpclient = new DefaultHttpClient();
StringBuilder responseBody = new StringBuilder(); StringBuilder responseBody = new StringBuilder();
@ -148,6 +151,8 @@ public class Miscellaneous extends Service
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setRequestProperty ("Authorization", "Basic " + encodedCredentials); connection.setRequestProperty ("Authorization", "Basic " + encodedCredentials);
} }
else if(method.equals(ActivityManageActionTriggerUrl.methodPost))
connection.setRequestMethod("POST");
InputStream content = (InputStream)connection.getInputStream(); InputStream content = (InputStream)connection.getInputStream();
BufferedReader in = new BufferedReader (new InputStreamReader (content)); BufferedReader in = new BufferedReader (new InputStreamReader (content));
@ -174,10 +179,8 @@ public class Miscellaneous extends Service
} }
} }
public static String downloadURLwithoutCertificateChecking(String url, String username, String password) public static String downloadURLwithoutCertificateChecking(String url, String username, String password, String method)
{ {
// HttpClient httpclient = new DefaultHttpClient();
// StringBuilder responseBody = new StringBuilder();
boolean errorFound = false; boolean errorFound = false;
try try
@ -188,19 +191,32 @@ public class Miscellaneous extends Service
HttpClient httpclient = new DefaultHttpClient(params); HttpClient httpclient = new DefaultHttpClient(params);
httpclient = Actions.getInsecureSslClient(httpclient); httpclient = Actions.getInsecureSslClient(httpclient);
HttpPost httppost = new HttpPost(url); HttpEntityEnclosingRequestBase httpRequest;
switch (method)
{
case ActivityManageActionTriggerUrl.methodPost:
httpRequest = new HttpPost(url);
break;
case ActivityManageActionTriggerUrl.methodGet:
default:
httpRequest = new HttpEntityEnclosingRequestBase()
{
@Override
public String getMethod()
{somethingMissing
return null;
}
};
}
// Add http simple authentication if specified // Add http simple authentication if specified
if(username != null && password != null) if(username != null && password != null)
{ {
String encodedCredentials = Base64.encodeToString(new String(username + ":" + password).getBytes(), Base64.DEFAULT); String encodedCredentials = Base64.encodeToString(new String(username + ":" + password).getBytes(), Base64.DEFAULT);
// List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); httpRequest.addHeader("Authorization", "Basic " + encodedCredentials);
httppost.addHeader("Authorization", "Basic " + encodedCredentials);
// nameValuePairs.add(new BasicNameValuePair("Authorization", "Basic " + encodedCredentials));
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
} }
HttpResponse response = httpclient.execute(httppost); HttpResponse response = httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
if (entity != null) if (entity != null)
{ {

View File

@ -90,6 +90,34 @@
</LinearLayout> </LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/method"
android:layout_marginTop="@dimen/fab_margin"
android:textStyle="bold" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbTriggerUrlMethodGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/methodGet" />
<RadioButton
android:id="@+id/rbTriggerUrlMethodPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/default_margin"
android:text="@string/methodPost" />
</RadioGroup>
<ListView <ListView
android:id="@+id/lvTriggerUrlPostParameters" android:id="@+id/lvTriggerUrlPostParameters"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -889,4 +889,7 @@
<string name="wifiTriggerDisconnectionHint">This trigger will be valid if you just disconnected from the wifi specified above OR while the service is still starting and if you\'re not connected to any wifi. If you want the trigger to fire only when you\'re explicitly disconnecting from a certain wifi, add another trigger \"service is not starting\".</string> <string name="wifiTriggerDisconnectionHint">This trigger will be valid if you just disconnected from the wifi specified above OR while the service is still starting and if you\'re not connected to any wifi. If you want the trigger to fire only when you\'re explicitly disconnecting from a certain wifi, add another trigger \"service is not starting\".</string>
<string name="className">Class full name</string> <string name="className">Class full name</string>
<string name="startAppByStartForegroundService">by startForegroundService()</string> <string name="startAppByStartForegroundService">by startForegroundService()</string>
<string name="method">Method</string>
<string name="methodGet" translatable="false">GET</string>
<string name="methodPost" translatable="false">POST</string>
</resources> </resources>