-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeedClient.java
More file actions
182 lines (145 loc) · 4.76 KB
/
FeedClient.java
File metadata and controls
182 lines (145 loc) · 4.76 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* (c) 2020 Wolfgang Hauptfleisch <dev@augmentedlogic.com>
* This file is part of simplefeedreader
* Licence: Apache v2
**/
package com.augmentedlogic.simplefeedreader;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.net.ssl.HttpsURLConnection;
import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Paths.get;
public class FeedClient
{
private String user_agent = "simplefeedreader 0.11";
private String source = null;
private int status_code = 0;
private int format = 0;
private int connect_timeout = 10000;
private int read_timeout = 10000;
private String username = null;
private String password = null;
private Boolean basic_auth = false;
protected static InputStream toInputStream(String initialString) throws IOException
{
InputStream targetStream = new ByteArrayInputStream(initialString.getBytes("UTF-8"));
return targetStream;
}
protected String getPayloadStream(InputStream inputStream) throws Exception
{
String response_string = "";
StringBuffer response = new StringBuffer();
String inputLine;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
} catch(Exception e) {
throw e;
}
if(inputStream != null) {
response_string = response.toString();
}
return response_string;
}
protected String fetch(URL url) throws Exception
{
String payload = "";
InputStream inputStream = null;
try {
if(this.basic_auth) {
String basic_auth = Base64.getEncoder().encodeToString((this.username + ":" + this.password).getBytes());
}
if(url.getProtocol().toLowerCase().equals("https")) {
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", this.user_agent);
con.setConnectTimeout(this.connect_timeout);
con.setReadTimeout(this.read_timeout);
if(this.basic_auth) {
con.setRequestProperty("Authorization", "Basic " + basic_auth);
}
con.connect();
this.status_code = con.getResponseCode();
inputStream = con.getInputStream();
} else {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", this.user_agent);
con.setConnectTimeout(this.connect_timeout);
con.setReadTimeout(this.read_timeout);
if(this.basic_auth) {
con.setRequestProperty("Authorization", "Basic " + basic_auth);
}
con.connect();
inputStream = con.getInputStream();
this.status_code = con.getResponseCode();
}
payload = getPayloadStream(inputStream);
inputStream.close();
if(payload.contains("www.w3.org/2005/Atom")) {
this.format = 1;
}
if(payload.contains("<rss")) {
this.format = 2;
}
if(payload.contains("<rdf:RDF")) {
this.format = 2;
}
} catch (Exception e) {
throw e;
} finally {
}
return payload;
}
protected String fetchFromFile(String filename) throws IOException
{
Objects.nonNull(filename);
String data = null;
try {
data = new String(readAllBytes(get(filename)));
} catch (IOException e) {
throw e;
}
if(data.contains("www.w3.org/2005/Atom")) {
this.format = 1;
}
if(data.contains("<rss")) {
this.format = 2;
}
return data;
}
/**
* @return the format RSS/Atom
**/
protected int getFormat()
{
return this.format;
}
/**
* @return the HTTP status code of the request
**/
protected int getStatusCode()
{
return this.status_code;
}
protected void setUserAgent(String user_agent)
{
this.user_agent = user_agent;
}
protected void setBasicAuth(String username, String password)
{
this.username = username;
this.password = password;
this.basic_auth = true;
}
protected void setConnectTimeout(int connect_timeout)
{
this.connect_timeout = connect_timeout;
}
protected void setReadTimeout(int read_timeout)
{
this.read_timeout = read_timeout;
}
}