android: Add a simple HTTP(S) fetcher for CRLs
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / logic / SimpleFetcher.java
1 /*
2 * Copyright (C) 2017 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.logic;
17
18 import android.support.annotation.Keep;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.HttpURLConnection;
24 import java.net.URL;
25
26 @Keep
27 public class SimpleFetcher
28 {
29 public static byte[] fetch(String uri) throws IOException
30 {
31 URL url = new URL(uri);
32 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
33 conn.setConnectTimeout(10000);
34 conn.setReadTimeout(10000);
35 try
36 {
37 return streamToArray(conn.getInputStream());
38 }
39 finally
40 {
41 conn.disconnect();
42 }
43 }
44
45 private static byte[] streamToArray(InputStream in)
46 {
47 ByteArrayOutputStream out = new ByteArrayOutputStream();
48 byte[] buf = new byte[1024];
49 int len;
50
51 try
52 {
53 while ((len = in.read(buf)) != -1)
54 {
55 out.write(buf, 0, len);
56 }
57 return out.toByteArray();
58 }
59 catch (IOException e)
60 {
61 e.printStackTrace();
62 }
63 return null;
64 }
65 }