Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / network / IscRequestHelper.java
1 /* IscRequestHelper.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp.network;
17 import android.util.Log;
18
19 import com.loopj.android.http.AsyncHttpClient;
20 import com.loopj.android.http.AsyncHttpResponseHandler;
21
22 import net.fortuna.ical4j.data.CalendarBuilder;
23 import net.fortuna.ical4j.data.ParserException;
24 import net.fortuna.ical4j.model.Calendar;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.UnsupportedEncodingException;
30
31 import cz.msebera.android.httpclient.Header;
32
33 /**
34  * Created by pk910 on 20.01.2016.
35  */
36 public abstract class IscRequestHelper {
37
38     protected void requestCalenderFromWeb(String url) {
39         AsyncHttpClient client = new AsyncHttpClient();
40         Log.i("HTTPClient", "Request: "+url);
41         client.get(url, new AsyncHttpResponseHandler() {
42             @Override
43             public void onStart() {
44             }
45             @Override
46             public void onSuccess(int statusCode, Header[] headers, byte[] response) {
47                 String plainisc = new String(response);
48                 // Google Calendar parse error!
49                 plainisc = plainisc.replaceAll("(X-APPLE-TRAVEL-ADVISORY-BEHAVIOR;)?ACKNOWLEDGED[:=][0-9TZ]+(:AUTOMATIC)?[\\r\\n]*", "");
50                 response = plainisc.getBytes();
51
52                 InputStream inputStream = new ByteArrayInputStream(response);
53                 parseIsc(inputStream);
54             }
55             @Override
56             public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
57                 String error = null;
58                 try {
59                     error = new String(errorResponse, "US-ASCII");
60                 } catch (Exception e1) {
61                 }
62                 Log.i("HTTPClient", "  Error: " + statusCode + " - " + error);
63                 onCalendarRequestFail(statusCode, error);
64             }
65             @Override
66             public void onRetry(int retryNo) {
67             }
68         });
69     }
70
71     private void parseIsc(InputStream input) {
72         CalendarBuilder builder = new CalendarBuilder();
73         Calendar calendar = null;
74         try {
75             calendar = builder.build(input);
76         } catch (IOException e) {
77             e.printStackTrace();
78         } catch (ParserException e) {
79             e.printStackTrace();
80         }
81         if(calendar == null)
82             return;
83         /*
84         ComponentList events = calendar.getComponents(Component.VEVENT);
85         ListIterator<Component> iterator = events.listIterator();
86         while (iterator.hasNext()) {
87             Component event = iterator.next();
88             event.getProperties();
89         }
90         */
91         onCalendarReceived(calendar);
92     }
93
94     protected abstract void onCalendarReceived(Calendar calendar);
95     protected abstract void onCalendarRequestFail(int statusCode, String errorMessage);
96
97 }