aboutsummaryrefslogtreecommitdiff
path: root/DocumentService/src/eu/stork/documentservice/utils/Utils.java
diff options
context:
space:
mode:
Diffstat (limited to 'DocumentService/src/eu/stork/documentservice/utils/Utils.java')
-rw-r--r--DocumentService/src/eu/stork/documentservice/utils/Utils.java172
1 files changed, 172 insertions, 0 deletions
diff --git a/DocumentService/src/eu/stork/documentservice/utils/Utils.java b/DocumentService/src/eu/stork/documentservice/utils/Utils.java
new file mode 100644
index 000000000..45072a0be
--- /dev/null
+++ b/DocumentService/src/eu/stork/documentservice/utils/Utils.java
@@ -0,0 +1,172 @@
+package eu.stork.documentservice.utils;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.bouncycastle.util.encoders.Base64;
+import org.bouncycastle.util.encoders.UrlBase64;
+
+public class Utils {
+
+ public static boolean saveData(byte[] data, String PATH, String fileName)
+ {
+ boolean ok = false;
+ if (data != null)
+ {
+ try
+ {
+ FileOutputStream fos;
+ File outputDir = new File(PATH);
+ File saveFile = new File(outputDir, fileName);
+ fos = new FileOutputStream(saveFile);
+ fos.write(data);
+ fos.flush();
+ fos.close();
+ ok = true;
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ return ok;
+ }
+
+ public static byte[] readData(String fileName)
+ {
+ byte[] data = null;
+
+ if (fileName != "")
+ {
+ try
+ {
+ FileInputStream fis;
+ File inputDir = new File(fileName);
+ fis = new FileInputStream(inputDir);
+ data = new byte[(int)inputDir.length()];
+ fis.read(data);
+ fis.close();
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ return data;
+ }
+
+ public static String readString(String fileName)
+ {
+ File file = new File(fileName);
+ StringBuilder text = new StringBuilder();
+
+ try {
+ BufferedReader br = new BufferedReader(new FileReader(file));
+ String line;
+
+ while ((line = br.readLine()) != null) {
+ text.append(line);
+ }
+ br.close();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ return text.toString();
+ }
+
+ public static InputStream getStream(String string, String codePage) throws UnsupportedEncodingException
+ {
+ return new ByteArrayInputStream(string.getBytes(codePage));
+ }
+
+ public static String encodeBase64bytes(final byte[] bytes, boolean urlSafe) {
+ try {
+ if (urlSafe)
+ return new String(UrlBase64.encode(bytes), "UTF8");
+ else
+ return new String(Base64.encode(bytes), "UTF8");
+ } catch (UnsupportedEncodingException e) {
+ return null;
+ }
+ }
+
+ public static byte[] decodeBase64String(final String base64string, boolean urlSave) {
+ if (urlSave)
+ return UrlBase64.decode(base64string);
+ else
+ return Base64.decode(base64string);
+ }
+
+ // HTTP GET request
+ public static String sendGet(String url) throws Exception
+ {
+ URL obj = new URL(url);
+ HttpURLConnection con = (HttpURLConnection) obj.openConnection();
+
+ // optional default is GET
+ con.setRequestMethod("GET");
+
+ //add request header
+ con.setRequestProperty("User-Agent", "unknown");
+
+ //int responseCode = con.getResponseCode();
+
+ BufferedReader in = new BufferedReader(
+ new InputStreamReader(con.getInputStream()));
+ String inputLine;
+ StringBuffer response = new StringBuffer();
+
+ while ((inputLine = in.readLine()) != null) {
+ response.append(inputLine);
+ }
+ in.close();
+
+ return response.toString();
+
+ }
+
+ // HTTP POST request
+ public static String sendPost(String url, String urlParameters) throws Exception
+ {
+ URL obj = new URL(url);
+ HttpURLConnection con = (HttpURLConnection) obj.openConnection();
+
+ //add request header
+ con.setRequestMethod("POST");
+ con.setRequestProperty("User-Agent", "unknown");
+ con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
+
+ // Send post request
+ con.setDoOutput(true);
+ DataOutputStream wr = new DataOutputStream(con.getOutputStream());
+ wr.writeBytes(urlParameters);
+ wr.flush();
+ wr.close();
+
+ //int responseCode = con.getResponseCode();
+
+ BufferedReader in = new BufferedReader(
+ new InputStreamReader(con.getInputStream()));
+ String inputLine;
+ StringBuffer response = new StringBuffer();
+
+ while ((inputLine = in.readLine()) != null) {
+ response.append(inputLine);
+ }
+ in.close();
+
+ return response.toString();
+ }
+}