-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFeedReader.java
More file actions
125 lines (104 loc) · 3.6 KB
/
SimpleFeedReader.java
File metadata and controls
125 lines (104 loc) · 3.6 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
/**
* (c) 2020 Wolfgang Hauptfleisch <dev@augmentedlogic.com>
* This file is part of simplefeedreader
* Licence: Apache v2
**/
package com.augmentedlogic.simplefeedreader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.*;
public class SimpleFeedReader
{
private String user_agent = "simple-feed-reader";
private Boolean plain_text= false;
private int status_code = 0;
private int connect_timeout = 10000;
private int read_timeout = 10000;
private int format = 0;
private String payload = null;
private String username = null;
private String password = null;
private Boolean basic_auth = false;
private static final int FORMAT_NONE = 0;
private static final int FORMAT_ATOM = 1;
private static final int FORMAT_RSS = 2;
public Feed readSource(String source) throws Exception
{
Feed feed = null;
try {
FeedClient fc = new FeedClient();
if(source.startsWith("http:") || source.startsWith("https:")) {
fc.setUserAgent(this.user_agent);
fc.setConnectTimeout(this.connect_timeout);
fc.setReadTimeout(this.read_timeout);
if(this.basic_auth) {
fc.setBasicAuth(this.username, this.password);
}
try {
this.payload = fc.fetch(new URL(source));
this.status_code = fc.getStatusCode();
} catch(Exception e) {
throw e;
}
} else {
this.payload = fc.fetchFromFile(source);
}
this.format = fc.getFormat();
InputStream xmlstream = FeedClient.toInputStream(this.payload);
switch(this.format)
{
case SimpleFeedReader.FORMAT_ATOM: {
AtomReader atomReader = new AtomReader();
atomReader.setPlainText(this.plain_text);
feed = atomReader.readFeed(xmlstream);
break;
}
case SimpleFeedReader.FORMAT_RSS: {
RssReader rssReader = new RssReader();
rssReader.setPlainText(this.plain_text);
feed = rssReader.readFeed(xmlstream);
break;
}
}
} catch (IOException e) {
throw e;
}
return feed;
}
public void setUserAgent(String user_agent)
{
this.user_agent = user_agent;
}
public void setPlainText(Boolean plain_text)
{
this.plain_text = plain_text;
}
public void setConnectTimeout(int connect_timeout)
{
this.connect_timeout = connect_timeout;
}
public void setReadTimeout(int read_timeout)
{
this.read_timeout = read_timeout;
}
public void setBasicAuth(String username, String password)
{
this.username = username;
this.password = password;
this.basic_auth = true;
}
public int getStatusCode()
{
return this.status_code;
}
public int getFormat()
{
return this.format;
}
public String getPayload()
{
return this.payload;
}
}