From 458b62213912505669fb40b68f4a36300d91d4ab Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Mon, 26 Dec 2022 11:04:33 -0700 Subject: [PATCH] set cors --- .../asgardius/page/s3manager/AccountAdd.java | 2 +- .../page/s3manager/BucketSelect.java | 2 +- .../asgardius/page/s3manager/CorsConfig.java | 142 +++++++++++++++++- .../page/s3manager/ObjectSelect.java | 2 +- app/src/main/res/values-es/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 6 files changed, 147 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/asgardius/page/s3manager/AccountAdd.java b/app/src/main/java/asgardius/page/s3manager/AccountAdd.java index 5a7dc62..ca01106 100644 --- a/app/src/main/java/asgardius/page/s3manager/AccountAdd.java +++ b/app/src/main/java/asgardius/page/s3manager/AccountAdd.java @@ -178,7 +178,7 @@ public class AccountAdd extends AppCompatActivity { @Override public void run() { - Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show(); } }); } diff --git a/app/src/main/java/asgardius/page/s3manager/BucketSelect.java b/app/src/main/java/asgardius/page/s3manager/BucketSelect.java index 59c7dc0..1fc9d46 100644 --- a/app/src/main/java/asgardius/page/s3manager/BucketSelect.java +++ b/app/src/main/java/asgardius/page/s3manager/BucketSelect.java @@ -123,7 +123,7 @@ public class BucketSelect extends AppCompatActivity { @Override public void run() { - Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show(); } }); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/asgardius/page/s3manager/CorsConfig.java b/app/src/main/java/asgardius/page/s3manager/CorsConfig.java index 3016e2d..cf7e047 100644 --- a/app/src/main/java/asgardius/page/s3manager/CorsConfig.java +++ b/app/src/main/java/asgardius/page/s3manager/CorsConfig.java @@ -22,6 +22,7 @@ import com.amazonaws.services.s3.model.S3ObjectSummary; import java.net.URI; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.List; public class CorsConfig extends AppCompatActivity { @@ -36,6 +37,7 @@ public class CorsConfig extends AppCompatActivity { boolean allorigins, pdfcompatible, found = false; TextView origins; Button allowall, allowpdf, deletecors; + BucketCrossOriginConfiguration corsconfig; @Override protected void onCreate(Bundle savedInstanceState) { @@ -81,8 +83,8 @@ public class CorsConfig extends AppCompatActivity { if (!corsRules.isEmpty()) { found = true; for (CORSRule rule: corsRules) { - System.out.println("allowOrigins: "+rule.getAllowedOrigins()); - System.out.println("AllowedMethod: "+rule.getAllowedMethods()); + //System.out.println("allowOrigins: "+rule.getAllowedOrigins()); + //System.out.println("AllowedMethod: "+rule.getAllowedMethods()); if (rule.getAllowedOrigins().toString().equals("[*]")) { allorigins = true; } else if (rule.getAllowedOrigins().toString().contains("https://"+pdfendpoint.getHost())) { @@ -106,6 +108,7 @@ public class CorsConfig extends AppCompatActivity { } else if (found) { origins.setText(getResources().getString(R.string.cors_npdf)); deletecors.setVisibility(View.VISIBLE); + allowpdf.setVisibility(View.VISIBLE); allowall.setVisibility(View.VISIBLE); } else { origins.setText(getResources().getString(R.string.cors_none)); @@ -130,5 +133,140 @@ public class CorsConfig extends AppCompatActivity { } }); getCors.start(); + allowall.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View view) { + //buttonaction + setAllowall(); + } + }); + allowpdf.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View view) { + //buttonaction + setAllowpdf(); + } + }); + deletecors.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View view) { + //buttonaction + setDeletecors(); + } + }); + } + + public void setAllowall() { + Thread addAll = new Thread(new Runnable() { + + @Override + public void run() { + try { + //Your code goes here + List allowMethods = new ArrayList<>(); + allowMethods.add(CORSRule.AllowedMethods.GET); + List allowOrigins = new ArrayList<>(); + allowOrigins.add("*"); + CORSRule corsRules = new CORSRule().withAllowedMethods(allowMethods).withAllowedOrigins(allowOrigins); + corsconfig = new BucketCrossOriginConfiguration().withRules(corsRules); + s3client.setBucketCrossOriginConfiguration(bucket, corsconfig); + + runOnUiThread(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_ok), Toast.LENGTH_SHORT).show(); + finish(); + } + }); + + } catch (Exception e) { + e.printStackTrace(); + runOnUiThread(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_error), Toast.LENGTH_SHORT).show(); + } + }); + //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + } + } + }); + addAll.start(); + } + + public void setAllowpdf() { + Thread addPdf = new Thread(new Runnable() { + + @Override + public void run() { + try { + //Your code goes here + List allowMethods = new ArrayList<>(); + allowMethods.add(CORSRule.AllowedMethods.GET); + List allowOrigins = new ArrayList<>(); + allowOrigins.add("https://"+pdfendpoint.getHost()); + CORSRule corsRules = new CORSRule().withAllowedMethods(allowMethods).withAllowedOrigins(allowOrigins); + corsconfig = new BucketCrossOriginConfiguration().withRules(corsRules); + s3client.setBucketCrossOriginConfiguration(bucket, corsconfig); + + runOnUiThread(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_ok), Toast.LENGTH_SHORT).show(); + finish(); + } + }); + + } catch (Exception e) { + e.printStackTrace(); + runOnUiThread(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_error), Toast.LENGTH_SHORT).show(); + } + }); + //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + } + } + }); + addPdf.start(); + } + + public void setDeletecors() { + Thread delCors = new Thread(new Runnable() { + + @Override + public void run() { + try { + //Your code goes here + s3client.deleteBucketCrossOriginConfiguration(bucket); + + runOnUiThread(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_ok), Toast.LENGTH_SHORT).show(); + finish(); + } + }); + + } catch (Exception e) { + e.printStackTrace(); + runOnUiThread(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.cors_error), Toast.LENGTH_SHORT).show(); + } + }); + //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + } + } + }); + delCors.start(); } } \ No newline at end of file diff --git a/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java b/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java index 05a15fd..3e639dd 100644 --- a/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java +++ b/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java @@ -237,7 +237,7 @@ public class ObjectSelect extends AppCompatActivity { @Override public void run() { - Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show(); } }); //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 56bf0e5..a096f39 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -104,4 +104,6 @@ Permitir todo Permitir pdf Eliminar CORS + No se pudo cambiar la política CORS + Política CORS cambiada exitosamente \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f616bcb..87eb01b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -107,4 +107,6 @@ Allow all Allow pdf Delete CORS + Cannot change CORS policy + CORS policy changed successfully \ No newline at end of file