about summary refs log tree commit diff stats
path: root/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java
blob: adca86eb2ecb51bb31ff5ffbf303e3016802314f (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fourisland.instadisc.Item;

import com.fourisland.instadisc.Database.Subscription;
import com.fourisland.instadisc.Database.Wrapper;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;

/**
 *
 * @author hatkirby
 */
public class SubscriptionFile {

    public HashMap<String, String> headerMap;

    public SubscriptionFile(URL url, JLabel status) {
        status.setText("Checking....");
        try {
            HttpURLConnection urc = (HttpURLConnection) url.openConnection();
            Thread th = new Thread(new SubscriptionFileThread(urc, status));
            th.start();
        } catch (IOException ex) {
            Logger.getLogger(SubscriptionFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

class SubscriptionFileThread implements Runnable {

    HttpURLConnection urc;
    JLabel status;

    public SubscriptionFileThread(HttpURLConnection urc, JLabel status) {
        this.urc = urc;
        this.status = status;
    }

    public void run() {
        InputStream is = null;
        try {
            is = urc.getInputStream();
            int[] buffer = new int[1000];
            int rs = 0;
            int i = 0;

            while (rs != -1) {
                try {
                    rs = is.read();

                    if (rs != -1) {
                        buffer[i] = rs;
                    }
                    i++;
                } catch (IOException ex) {
                    Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            StringBuilder result = new StringBuilder();
            int j = 0;
            for (j = 0; j < i; j++) {
                result.append(Character.toString((char) buffer[j]));
            }

            String[] headers = result.toString().split("\n");
            HashMap<String, String> headerMap = new HashMap<String, String>();
            i = 0;
            while (1 == 1) {
                try {
                    String[] nameVal = headers[i].split(": ");
                    String name = nameVal[0];
                    String value = nameVal[1].trim();
                    headerMap.put(name, value);
                } catch (Exception ex) {
                    break;
                }
                i++;
            }

            if (headerMap.containsKey("Subscription")) {
                if (headerMap.containsKey("Title")) {
                    if (headerMap.containsKey("Category")) {
                        Subscription s = new Subscription();
                        s.setURL(headerMap.get("Subscription"));
                        s.setTitle(headerMap.get("Title"));
                        s.setCategory(headerMap.get("Category"));
                        Wrapper.addSubscription(s);

                        status.setText("You've sucessfully subscribed to that website");
                    } else {
                        status.setText("Error: Subscription file is not well-formed");
                    }
                } else {
                    status.setText("Error: Subscription file is not well-formed");
                }
            } else {
                status.setText("Error: Subscription file is not well-formed");
            }
        } catch (IOException ex) {
            Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}