summaryrefslogtreecommitdiff
path: root/src/eu/deki/paste/DekiEuPaste.java
blob: 234abcbe36b9d7ad893e5fd8f7df0914d17f4d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package eu.deki.paste;

import java.net.URL;
import java.net.URLEncoder;
import java.net.MalformedURLException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.security.KeyStore;
import android.content.Context;
import android.util.Log;
import eu.deki.paste.PasteTask;

public class DekiEuPaste extends PasteTask
{
    private static final String pasteUrl = "https://deki.eu/paste/new";

    public DekiEuPaste(Context parent)
    {
	super(parent);
    }

    @Override
    protected String paste(String title, String content, String delay) throws IOException
    {
	System.setProperty("http.keepAlive", "false");

	String request = "title=" + URLEncoder.encode(title) + "&content=" + URLEncoder.encode(content) + "&expiration=" + URLEncoder.encode(delay);

	URL url;
	try {
	    url = new URL(pasteUrl);
	} catch(MalformedURLException ex) {
	    // will never happen because of hardcoded URL
	    return null;
	}

	InputStream in = parentActivity.getResources().openRawResource(R.raw.deki_eu);
	SSLContext context = null;
	try
	{
	    KeyStore trustedStore = KeyStore.getInstance("BKS");
	    trustedStore.load(in, "abcdefg".toCharArray());
	    TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
	    //TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
	    tmf.init(trustedStore);
	    context = SSLContext.getInstance("TLS");
	    context.init(null, tmf.getTrustManagers(), null);
	}
	catch(Exception ex)
	{
	    Log.i("PasteIt", "Could not initialize certificate verification", ex);
	    return null;
	}
	finally
	{
	    in.close();
	}

	HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
	conn.setSSLSocketFactory(context.getSocketFactory());
	conn.setDoOutput(true);
	conn.setInstanceFollowRedirects(false);
	conn.setFixedLengthStreamingMode(request.getBytes().length);

	OutputStream out = new BufferedOutputStream(conn.getOutputStream());
	out.write(request.getBytes());
	out.close();

	String redirectUrl = conn.getHeaderField("Location");

	conn.disconnect();

	return redirectUrl;
    }
}