franta-hg@63
|
1 |
/*
|
franta-hg@63
|
2 |
* SONEWS News Server
|
franta-hg@63
|
3 |
* see AUTHORS for the list of contributors
|
franta-hg@63
|
4 |
*
|
franta-hg@63
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@63
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@63
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@63
|
8 |
* (at your option) any later version.
|
franta-hg@63
|
9 |
*
|
franta-hg@63
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@63
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@63
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@63
|
13 |
* GNU General Public License for more details.
|
franta-hg@63
|
14 |
*
|
franta-hg@63
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@63
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@63
|
17 |
*/
|
franta-hg@63
|
18 |
package org.sonews.storage.impl;
|
franta-hg@63
|
19 |
|
franta-hg@68
|
20 |
import java.sql.Connection;
|
franta-hg@68
|
21 |
import java.sql.DriverManager;
|
franta-hg@68
|
22 |
import java.sql.PreparedStatement;
|
franta-hg@68
|
23 |
import java.sql.ResultSet;
|
franta-hg@68
|
24 |
import java.sql.Statement;
|
franta-hg@68
|
25 |
import java.util.ArrayList;
|
franta-hg@65
|
26 |
import java.util.Collections;
|
franta-hg@64
|
27 |
import java.util.List;
|
franta-hg@65
|
28 |
import java.util.logging.Level;
|
franta-hg@66
|
29 |
import java.util.logging.Logger;
|
franta-hg@68
|
30 |
import org.sonews.config.Config;
|
franta-hg@101
|
31 |
import org.sonews.daemon.Connections;
|
franta-hg@64
|
32 |
import org.sonews.feed.Subscription;
|
franta-hg@64
|
33 |
import org.sonews.storage.Article;
|
franta-hg@64
|
34 |
import org.sonews.storage.ArticleHead;
|
franta-hg@72
|
35 |
import org.sonews.storage.DrupalArticle;
|
franta-hg@72
|
36 |
import org.sonews.storage.DrupalMessage;
|
franta-hg@64
|
37 |
import org.sonews.storage.Group;
|
franta-hg@64
|
38 |
import org.sonews.storage.Storage;
|
franta-hg@64
|
39 |
import org.sonews.storage.StorageBackendException;
|
franta-hg@64
|
40 |
import org.sonews.util.Pair;
|
franta-hg@64
|
41 |
|
franta-hg@63
|
42 |
/**
|
franta-hg@63
|
43 |
*
|
franta-hg@63
|
44 |
* @author František Kučera (frantovo.cz)
|
franta-hg@63
|
45 |
*/
|
franta-hg@64
|
46 |
public class DrupalDatabase implements Storage {
|
franta-hg@67
|
47 |
|
franta-hg@66
|
48 |
private static final Logger log = Logger.getLogger(DrupalDatabase.class.getName());
|
franta-hg@70
|
49 |
public static final String CHARSET = "UTF-8";
|
franta-hg@68
|
50 |
public static final String CRLF = "\r\n";
|
franta-hg@68
|
51 |
protected Connection conn = null;
|
franta-hg@70
|
52 |
// TODO: správná doména
|
franta-hg@72
|
53 |
private String myDomain = "nntp.i1984.cz";
|
franta-hg@68
|
54 |
|
franta-hg@68
|
55 |
public DrupalDatabase() throws StorageBackendException {
|
franta-hg@68
|
56 |
connectDatabase();
|
franta-hg@68
|
57 |
}
|
franta-hg@68
|
58 |
|
franta-hg@68
|
59 |
private void connectDatabase() throws StorageBackendException {
|
franta-hg@68
|
60 |
try {
|
franta-hg@68
|
61 |
// Load database driver
|
franta-hg@68
|
62 |
String driverClass = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DBMSDRIVER, "java.lang.Object");
|
franta-hg@68
|
63 |
Class.forName(driverClass);
|
franta-hg@68
|
64 |
|
franta-hg@68
|
65 |
// Establish database connection
|
franta-hg@68
|
66 |
String url = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DATABASE, "<not specified>");
|
franta-hg@68
|
67 |
String username = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_USER, "root");
|
franta-hg@68
|
68 |
String password = Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_PASSWORD, "");
|
franta-hg@68
|
69 |
conn = DriverManager.getConnection(url, username, password);
|
franta-hg@68
|
70 |
|
franta-hg@68
|
71 |
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
|
franta-hg@68
|
72 |
if (conn.getTransactionIsolation() != Connection.TRANSACTION_SERIALIZABLE) {
|
franta-hg@68
|
73 |
log.warning("Database is NOT fully serializable!");
|
franta-hg@68
|
74 |
}
|
franta-hg@68
|
75 |
} catch (Exception e) {
|
franta-hg@68
|
76 |
throw new StorageBackendException(e);
|
franta-hg@68
|
77 |
}
|
franta-hg@68
|
78 |
}
|
franta-hg@68
|
79 |
|
franta-hg@68
|
80 |
protected static void close(Connection connection, Statement statement, ResultSet resultSet) {
|
franta-hg@68
|
81 |
if (resultSet != null) {
|
franta-hg@68
|
82 |
try {
|
franta-hg@68
|
83 |
resultSet.close();
|
franta-hg@68
|
84 |
} catch (Exception e) {
|
franta-hg@68
|
85 |
}
|
franta-hg@68
|
86 |
}
|
franta-hg@68
|
87 |
if (statement != null) {
|
franta-hg@68
|
88 |
try {
|
franta-hg@68
|
89 |
statement.close();
|
franta-hg@68
|
90 |
} catch (Exception e) {
|
franta-hg@68
|
91 |
}
|
franta-hg@68
|
92 |
}
|
franta-hg@68
|
93 |
if (connection != null) {
|
franta-hg@68
|
94 |
try {
|
franta-hg@68
|
95 |
connection.close();
|
franta-hg@68
|
96 |
} catch (Exception e) {
|
franta-hg@68
|
97 |
}
|
franta-hg@68
|
98 |
}
|
franta-hg@68
|
99 |
}
|
franta-hg@64
|
100 |
|
franta-hg@64
|
101 |
/**
|
franta-hg@68
|
102 |
*
|
franta-hg@70
|
103 |
* @param messageID <{0}-{1}-{2}@domain.tld> where {0} is nntp_id and {1} is group_id and {2} is group_name
|
franta-hg@68
|
104 |
* @return array where [0] = nntp_id and [1] = group_id and [2] = group_name or returns null if messageID is invalid
|
franta-hg@64
|
105 |
*/
|
franta-hg@68
|
106 |
private static String[] parseMessageID(String messageID) {
|
franta-hg@70
|
107 |
if (messageID.matches("<[0-9]+\\-[0-9]+\\-[a-z0-9\\.]+@.+>")) {
|
franta-hg@70
|
108 |
return messageID.substring(1).split("@")[0].split("\\-");
|
franta-hg@68
|
109 |
} else {
|
franta-hg@68
|
110 |
return null;
|
franta-hg@68
|
111 |
}
|
franta-hg@68
|
112 |
}
|
franta-hg@68
|
113 |
|
franta-hg@68
|
114 |
private static Long parseArticleID(String messageID) {
|
franta-hg@68
|
115 |
String[] localPart = parseMessageID(messageID);
|
franta-hg@68
|
116 |
if (localPart == null) {
|
franta-hg@68
|
117 |
return null;
|
franta-hg@68
|
118 |
} else {
|
franta-hg@68
|
119 |
return Long.parseLong(localPart[0]);
|
franta-hg@68
|
120 |
}
|
franta-hg@68
|
121 |
}
|
franta-hg@68
|
122 |
|
franta-hg@68
|
123 |
private static Long parseGroupID(String messageID) {
|
franta-hg@68
|
124 |
String[] localPart = parseMessageID(messageID);
|
franta-hg@68
|
125 |
if (localPart == null) {
|
franta-hg@68
|
126 |
return null;
|
franta-hg@68
|
127 |
} else {
|
franta-hg@68
|
128 |
return Long.parseLong(localPart[1]);
|
franta-hg@72
|
129 |
// If needed:
|
franta-hg@72
|
130 |
// parseGroupName() will be same as this method, just with:
|
franta-hg@72
|
131 |
// return localPart[2];
|
franta-hg@68
|
132 |
}
|
franta-hg@68
|
133 |
}
|
franta-hg@68
|
134 |
|
franta-hg@64
|
135 |
@Override
|
franta-hg@67
|
136 |
public List<Group> getGroups() throws StorageBackendException {
|
franta-hg@68
|
137 |
PreparedStatement ps = null;
|
franta-hg@68
|
138 |
ResultSet rs = null;
|
franta-hg@68
|
139 |
try {
|
franta-hg@68
|
140 |
ps = conn.prepareStatement("SELECT * FROM nntp_group");
|
franta-hg@68
|
141 |
rs = ps.executeQuery();
|
franta-hg@68
|
142 |
List<Group> skupiny = new ArrayList<Group>();
|
franta-hg@68
|
143 |
|
franta-hg@68
|
144 |
while (rs.next()) {
|
franta-hg@68
|
145 |
skupiny.add(new Group(rs.getString("name"), rs.getInt("id"), Group.READONLY));
|
franta-hg@68
|
146 |
}
|
franta-hg@68
|
147 |
|
franta-hg@68
|
148 |
return skupiny;
|
franta-hg@68
|
149 |
} catch (Exception e) {
|
franta-hg@68
|
150 |
throw new StorageBackendException(e);
|
franta-hg@68
|
151 |
} finally {
|
franta-hg@68
|
152 |
close(null, ps, rs);
|
franta-hg@68
|
153 |
}
|
franta-hg@64
|
154 |
}
|
franta-hg@64
|
155 |
|
franta-hg@64
|
156 |
@Override
|
franta-hg@67
|
157 |
public Group getGroup(String name) throws StorageBackendException {
|
franta-hg@68
|
158 |
PreparedStatement ps = null;
|
franta-hg@68
|
159 |
ResultSet rs = null;
|
franta-hg@68
|
160 |
try {
|
franta-hg@68
|
161 |
ps = conn.prepareStatement("SELECT * FROM nntp_group WHERE name = ?");
|
franta-hg@68
|
162 |
ps.setString(1, name);
|
franta-hg@68
|
163 |
rs = ps.executeQuery();
|
franta-hg@68
|
164 |
|
franta-hg@68
|
165 |
while (rs.next()) {
|
franta-hg@68
|
166 |
return new Group(rs.getString("name"), rs.getInt("id"), Group.READONLY);
|
franta-hg@68
|
167 |
}
|
franta-hg@68
|
168 |
|
franta-hg@68
|
169 |
return null;
|
franta-hg@68
|
170 |
} catch (Exception e) {
|
franta-hg@68
|
171 |
throw new StorageBackendException(e);
|
franta-hg@68
|
172 |
} finally {
|
franta-hg@68
|
173 |
close(null, ps, rs);
|
franta-hg@68
|
174 |
}
|
franta-hg@64
|
175 |
}
|
franta-hg@64
|
176 |
|
franta-hg@64
|
177 |
@Override
|
franta-hg@67
|
178 |
public boolean isGroupExisting(String groupname) throws StorageBackendException {
|
franta-hg@68
|
179 |
return getGroup(groupname) != null;
|
franta-hg@64
|
180 |
}
|
franta-hg@64
|
181 |
|
franta-hg@64
|
182 |
@Override
|
franta-hg@64
|
183 |
public Article getArticle(String messageID) throws StorageBackendException {
|
franta-hg@68
|
184 |
Long articleID = parseArticleID(messageID);
|
franta-hg@68
|
185 |
Long groupID = parseGroupID(messageID);
|
franta-hg@68
|
186 |
|
franta-hg@68
|
187 |
if (articleID == null || groupID == null) {
|
franta-hg@68
|
188 |
log.log(Level.SEVERE, "Invalid messageID: {0}", new Object[]{messageID});
|
franta-hg@68
|
189 |
return null;
|
franta-hg@68
|
190 |
} else {
|
franta-hg@68
|
191 |
return getArticle(articleID, groupID);
|
franta-hg@68
|
192 |
}
|
franta-hg@64
|
193 |
}
|
franta-hg@64
|
194 |
|
franta-hg@64
|
195 |
@Override
|
franta-hg@68
|
196 |
public Article getArticle(long articleID, long groupID) throws StorageBackendException {
|
franta-hg@68
|
197 |
PreparedStatement ps = null;
|
franta-hg@68
|
198 |
ResultSet rs = null;
|
franta-hg@68
|
199 |
try {
|
franta-hg@68
|
200 |
ps = conn.prepareStatement("SELECT * FROM nntp_article WHERE id = ? AND group_id = ?");
|
franta-hg@68
|
201 |
ps.setLong(1, articleID);
|
franta-hg@68
|
202 |
ps.setLong(2, groupID);
|
franta-hg@68
|
203 |
rs = ps.executeQuery();
|
franta-hg@68
|
204 |
|
franta-hg@68
|
205 |
if (rs.next()) {
|
franta-hg@72
|
206 |
DrupalMessage m = new DrupalMessage(rs, myDomain, true);
|
franta-hg@72
|
207 |
return new DrupalArticle(m);
|
franta-hg@68
|
208 |
} else {
|
franta-hg@68
|
209 |
return null;
|
franta-hg@68
|
210 |
}
|
franta-hg@68
|
211 |
} catch (Exception e) {
|
franta-hg@68
|
212 |
throw new StorageBackendException(e);
|
franta-hg@68
|
213 |
} finally {
|
franta-hg@68
|
214 |
close(null, ps, rs);
|
franta-hg@68
|
215 |
}
|
franta-hg@64
|
216 |
}
|
franta-hg@64
|
217 |
|
franta-hg@64
|
218 |
@Override
|
franta-hg@64
|
219 |
public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, long first, long last) throws StorageBackendException {
|
franta-hg@68
|
220 |
PreparedStatement ps = null;
|
franta-hg@68
|
221 |
ResultSet rs = null;
|
franta-hg@68
|
222 |
try {
|
franta-hg@70
|
223 |
ps = conn.prepareStatement("SELECT * FROM nntp_article WHERE group_id = ? AND id >= ? AND id <= ? ORDER BY id");
|
franta-hg@68
|
224 |
ps.setLong(1, group.getInternalID());
|
franta-hg@68
|
225 |
ps.setLong(2, first);
|
franta-hg@68
|
226 |
ps.setLong(3, last);
|
franta-hg@68
|
227 |
rs = ps.executeQuery();
|
franta-hg@68
|
228 |
|
franta-hg@68
|
229 |
List<Pair<Long, ArticleHead>> heads = new ArrayList<Pair<Long, ArticleHead>>();
|
franta-hg@68
|
230 |
|
franta-hg@68
|
231 |
while (rs.next()) {
|
franta-hg@72
|
232 |
DrupalMessage m = new DrupalMessage(rs, myDomain, false);
|
franta-hg@72
|
233 |
String headers = m.getHeaders();
|
franta-hg@68
|
234 |
heads.add(new Pair<Long, ArticleHead>(rs.getLong("id"), new ArticleHead(headers)));
|
franta-hg@68
|
235 |
}
|
franta-hg@68
|
236 |
|
franta-hg@68
|
237 |
return heads;
|
franta-hg@68
|
238 |
} catch (Exception e) {
|
franta-hg@68
|
239 |
throw new StorageBackendException(e);
|
franta-hg@68
|
240 |
} finally {
|
franta-hg@68
|
241 |
close(null, ps, rs);
|
franta-hg@68
|
242 |
}
|
franta-hg@64
|
243 |
}
|
franta-hg@64
|
244 |
|
franta-hg@64
|
245 |
@Override
|
franta-hg@68
|
246 |
public long getArticleIndex(Article article, Group group) throws StorageBackendException {
|
franta-hg@68
|
247 |
Long id = parseArticleID(article.getMessageID());
|
franta-hg@68
|
248 |
if (id == null) {
|
franta-hg@68
|
249 |
throw new StorageBackendException("Invalid messageID: " + article.getMessageID());
|
franta-hg@68
|
250 |
} else {
|
franta-hg@68
|
251 |
return id;
|
franta-hg@68
|
252 |
}
|
franta-hg@64
|
253 |
}
|
franta-hg@64
|
254 |
|
franta-hg@64
|
255 |
@Override
|
franta-hg@64
|
256 |
public List<Long> getArticleNumbers(long groupID) throws StorageBackendException {
|
franta-hg@68
|
257 |
PreparedStatement ps = null;
|
franta-hg@68
|
258 |
ResultSet rs = null;
|
franta-hg@68
|
259 |
try {
|
franta-hg@68
|
260 |
ps = conn.prepareStatement("SELECT id FROM nntp_article WHERE group_id = ?");
|
franta-hg@68
|
261 |
ps.setLong(1, groupID);
|
franta-hg@68
|
262 |
rs = ps.executeQuery();
|
franta-hg@68
|
263 |
List<Long> articleNumbers = new ArrayList<Long>();
|
franta-hg@68
|
264 |
while (rs.next()) {
|
franta-hg@68
|
265 |
articleNumbers.add(rs.getLong(1));
|
franta-hg@68
|
266 |
}
|
franta-hg@68
|
267 |
return articleNumbers;
|
franta-hg@68
|
268 |
} catch (Exception e) {
|
franta-hg@68
|
269 |
throw new StorageBackendException(e);
|
franta-hg@68
|
270 |
} finally {
|
franta-hg@68
|
271 |
close(null, ps, rs);
|
franta-hg@68
|
272 |
}
|
franta-hg@64
|
273 |
}
|
franta-hg@64
|
274 |
|
franta-hg@64
|
275 |
@Override
|
franta-hg@67
|
276 |
public int getFirstArticleNumber(Group group) throws StorageBackendException {
|
franta-hg@68
|
277 |
PreparedStatement ps = null;
|
franta-hg@68
|
278 |
ResultSet rs = null;
|
franta-hg@68
|
279 |
try {
|
franta-hg@68
|
280 |
ps = conn.prepareStatement("SELECT min(id) FROM nntp_article WHERE group_id = ?");
|
franta-hg@68
|
281 |
ps.setLong(1, group.getInternalID());
|
franta-hg@68
|
282 |
rs = ps.executeQuery();
|
franta-hg@68
|
283 |
rs.next();
|
franta-hg@68
|
284 |
return rs.getInt(1);
|
franta-hg@68
|
285 |
} catch (Exception e) {
|
franta-hg@68
|
286 |
throw new StorageBackendException(e);
|
franta-hg@68
|
287 |
} finally {
|
franta-hg@68
|
288 |
close(null, ps, rs);
|
franta-hg@68
|
289 |
}
|
franta-hg@67
|
290 |
}
|
franta-hg@67
|
291 |
|
franta-hg@67
|
292 |
@Override
|
franta-hg@67
|
293 |
public int getLastArticleNumber(Group group) throws StorageBackendException {
|
franta-hg@68
|
294 |
PreparedStatement ps = null;
|
franta-hg@68
|
295 |
ResultSet rs = null;
|
franta-hg@68
|
296 |
try {
|
franta-hg@68
|
297 |
ps = conn.prepareStatement("SELECT max(id) FROM nntp_article WHERE group_id = ?");
|
franta-hg@68
|
298 |
ps.setLong(1, group.getInternalID());
|
franta-hg@68
|
299 |
rs = ps.executeQuery();
|
franta-hg@68
|
300 |
rs.next();
|
franta-hg@68
|
301 |
return rs.getInt(1);
|
franta-hg@68
|
302 |
} catch (Exception e) {
|
franta-hg@68
|
303 |
throw new StorageBackendException(e);
|
franta-hg@68
|
304 |
} finally {
|
franta-hg@68
|
305 |
close(null, ps, rs);
|
franta-hg@68
|
306 |
}
|
franta-hg@67
|
307 |
}
|
franta-hg@67
|
308 |
|
franta-hg@67
|
309 |
@Override
|
franta-hg@67
|
310 |
public boolean isArticleExisting(String messageID) throws StorageBackendException {
|
franta-hg@68
|
311 |
Long articleID = parseArticleID(messageID);
|
franta-hg@68
|
312 |
Long groupID = parseGroupID(messageID);
|
franta-hg@68
|
313 |
|
franta-hg@68
|
314 |
if (articleID == null || groupID == null) {
|
franta-hg@68
|
315 |
return false;
|
franta-hg@68
|
316 |
} else {
|
franta-hg@68
|
317 |
PreparedStatement ps = null;
|
franta-hg@68
|
318 |
ResultSet rs = null;
|
franta-hg@68
|
319 |
try {
|
franta-hg@68
|
320 |
ps = conn.prepareStatement("SELECT count(*) FROM nntp_article WHERE id = ? AND group_id = ?");
|
franta-hg@68
|
321 |
ps.setLong(1, articleID);
|
franta-hg@68
|
322 |
ps.setLong(2, groupID);
|
franta-hg@68
|
323 |
rs = ps.executeQuery();
|
franta-hg@68
|
324 |
|
franta-hg@68
|
325 |
rs.next();
|
franta-hg@68
|
326 |
return rs.getInt(1) == 1;
|
franta-hg@68
|
327 |
} catch (Exception e) {
|
franta-hg@68
|
328 |
throw new StorageBackendException(e);
|
franta-hg@68
|
329 |
} finally {
|
franta-hg@68
|
330 |
close(null, ps, rs);
|
franta-hg@68
|
331 |
}
|
franta-hg@68
|
332 |
}
|
franta-hg@67
|
333 |
}
|
franta-hg@67
|
334 |
|
franta-hg@67
|
335 |
@Override
|
franta-hg@67
|
336 |
public int countArticles() throws StorageBackendException {
|
franta-hg@69
|
337 |
PreparedStatement ps = null;
|
franta-hg@69
|
338 |
ResultSet rs = null;
|
franta-hg@69
|
339 |
try {
|
franta-hg@69
|
340 |
ps = conn.prepareStatement("SELECT count(*) FROM nntp_article");
|
franta-hg@69
|
341 |
rs = ps.executeQuery();
|
franta-hg@69
|
342 |
rs.next();
|
franta-hg@69
|
343 |
return rs.getInt(1);
|
franta-hg@69
|
344 |
} catch (Exception e) {
|
franta-hg@69
|
345 |
throw new StorageBackendException(e);
|
franta-hg@69
|
346 |
} finally {
|
franta-hg@69
|
347 |
close(null, ps, rs);
|
franta-hg@69
|
348 |
}
|
franta-hg@67
|
349 |
}
|
franta-hg@67
|
350 |
|
franta-hg@67
|
351 |
@Override
|
franta-hg@67
|
352 |
public int countGroups() throws StorageBackendException {
|
franta-hg@69
|
353 |
PreparedStatement ps = null;
|
franta-hg@69
|
354 |
ResultSet rs = null;
|
franta-hg@69
|
355 |
try {
|
franta-hg@69
|
356 |
ps = conn.prepareStatement("SELECT count(*) FROM nntp_group");
|
franta-hg@69
|
357 |
rs = ps.executeQuery();
|
franta-hg@69
|
358 |
rs.next();
|
franta-hg@69
|
359 |
return rs.getInt(1);
|
franta-hg@69
|
360 |
} catch (Exception e) {
|
franta-hg@69
|
361 |
throw new StorageBackendException(e);
|
franta-hg@69
|
362 |
} finally {
|
franta-hg@69
|
363 |
close(null, ps, rs);
|
franta-hg@69
|
364 |
}
|
franta-hg@67
|
365 |
}
|
franta-hg@67
|
366 |
|
franta-hg@67
|
367 |
@Override
|
franta-hg@72
|
368 |
public int getPostingsCount(String groupname) throws StorageBackendException {
|
franta-hg@72
|
369 |
PreparedStatement ps = null;
|
franta-hg@72
|
370 |
ResultSet rs = null;
|
franta-hg@72
|
371 |
try {
|
franta-hg@72
|
372 |
ps = conn.prepareStatement("SELECT count(*) FROM nntp_article WHERE group_name = ?");
|
franta-hg@72
|
373 |
ps.setString(1, groupname);
|
franta-hg@72
|
374 |
rs = ps.executeQuery();
|
franta-hg@72
|
375 |
rs.next();
|
franta-hg@72
|
376 |
return rs.getInt(1);
|
franta-hg@72
|
377 |
} catch (Exception e) {
|
franta-hg@72
|
378 |
throw new StorageBackendException(e);
|
franta-hg@72
|
379 |
} finally {
|
franta-hg@72
|
380 |
close(null, ps, rs);
|
franta-hg@72
|
381 |
}
|
franta-hg@72
|
382 |
}
|
franta-hg@72
|
383 |
|
franta-hg@72
|
384 |
@Override
|
franta-hg@72
|
385 |
public List<Pair<Long, String>> getArticleHeaders(Group group, long start, long end, String header, String pattern) throws StorageBackendException {
|
franta-hg@72
|
386 |
log.log(Level.SEVERE, "TODO: getArticleHeaders {0} / {1} / {2} / {3} / {4}", new Object[]{group, start, end, header, pattern});
|
franta-hg@72
|
387 |
/** TODO: */
|
franta-hg@72
|
388 |
return Collections.emptyList();
|
franta-hg@72
|
389 |
}
|
franta-hg@72
|
390 |
|
franta-hg@101
|
391 |
/**
|
franta-hg@101
|
392 |
* Checks username and password.
|
franta-hg@101
|
393 |
* @param username
|
franta-hg@101
|
394 |
* @param password
|
franta-hg@101
|
395 |
* @return true if credentials are valid | false otherwise
|
franta-hg@101
|
396 |
* @throws StorageBackendException it there is any error during authentication process
|
franta-hg@101
|
397 |
* (but should not be thrown if only bad thing is wrong username or password)
|
franta-hg@101
|
398 |
*/
|
franta-hg@101
|
399 |
@Override
|
franta-hg@101
|
400 |
public boolean authenticateUser(String username, char[] password) throws StorageBackendException {
|
franta-hg@101
|
401 |
PreparedStatement ps = null;
|
franta-hg@101
|
402 |
ResultSet rs = null;
|
franta-hg@101
|
403 |
try {
|
franta-hg@101
|
404 |
ps = conn.prepareStatement("SELECT nntp_login(?, ?)");
|
franta-hg@101
|
405 |
ps.setString(1, username);
|
franta-hg@101
|
406 |
ps.setString(2, String.copyValueOf(password));
|
franta-hg@101
|
407 |
rs = ps.executeQuery();
|
franta-hg@101
|
408 |
rs.next();
|
franta-hg@101
|
409 |
return rs.getInt(1) == 1;
|
franta-hg@101
|
410 |
} catch (Exception e) {
|
franta-hg@101
|
411 |
throw new StorageBackendException(e);
|
franta-hg@101
|
412 |
} finally {
|
franta-hg@101
|
413 |
close(null, ps, rs);
|
franta-hg@101
|
414 |
}
|
franta-hg@101
|
415 |
}
|
franta-hg@101
|
416 |
|
franta-hg@72
|
417 |
@Override
|
franta-hg@72
|
418 |
public void addArticle(Article art) throws StorageBackendException {
|
franta-hg@101
|
419 |
if (art.getAuthenticatedUser() == null) {
|
franta-hg@101
|
420 |
log.log(Level.SEVERE, "User was not authenticated, so his article was rejected.");
|
franta-hg@101
|
421 |
throw new StorageBackendException("User must be authenticated to post articles");
|
franta-hg@101
|
422 |
} else {
|
franta-hg@101
|
423 |
|
franta-hg@101
|
424 |
log.log(Level.INFO, "User ''{0}'' has posted an article", art.getAuthenticatedUser());
|
franta-hg@101
|
425 |
}
|
franta-hg@72
|
426 |
}
|
franta-hg@72
|
427 |
|
franta-hg@72
|
428 |
@Override
|
franta-hg@72
|
429 |
public void addEvent(long timestamp, int type, long groupID) throws StorageBackendException {
|
franta-hg@72
|
430 |
log.log(Level.SEVERE, "TODO: addEvent {0} / {1} / {2}", new Object[]{timestamp, type, groupID});
|
franta-hg@72
|
431 |
}
|
franta-hg@72
|
432 |
|
franta-hg@72
|
433 |
@Override
|
franta-hg@72
|
434 |
public void addGroup(String groupname, int flags) throws StorageBackendException {
|
franta-hg@72
|
435 |
log.log(Level.SEVERE, "TODO: addGroup {0} / {1}", new Object[]{groupname, flags});
|
franta-hg@72
|
436 |
}
|
franta-hg@72
|
437 |
|
franta-hg@72
|
438 |
@Override
|
franta-hg@67
|
439 |
public void delete(String messageID) throws StorageBackendException {
|
franta-hg@67
|
440 |
log.log(Level.SEVERE, "TODO: delete {0}", new Object[]{messageID});
|
franta-hg@67
|
441 |
}
|
franta-hg@67
|
442 |
|
franta-hg@67
|
443 |
@Override
|
franta-hg@64
|
444 |
public String getConfigValue(String key) throws StorageBackendException {
|
franta-hg@69
|
445 |
//log.log(Level.SEVERE, "TODO: getConfigValue {0}", new Object[]{key});
|
franta-hg@65
|
446 |
return null;
|
franta-hg@64
|
447 |
}
|
franta-hg@64
|
448 |
|
franta-hg@64
|
449 |
@Override
|
franta-hg@72
|
450 |
public void setConfigValue(String key, String value) throws StorageBackendException {
|
franta-hg@72
|
451 |
log.log(Level.SEVERE, "TODO: setConfigValue {0} = {1}", new Object[]{key, value});
|
franta-hg@72
|
452 |
}
|
franta-hg@72
|
453 |
|
franta-hg@72
|
454 |
@Override
|
franta-hg@64
|
455 |
public int getEventsCount(int eventType, long startTimestamp, long endTimestamp, Group group) throws StorageBackendException {
|
franta-hg@66
|
456 |
log.log(Level.SEVERE, "TODO: getEventsCount {0} / {1} / {2} / {3}", new Object[]{eventType, startTimestamp, endTimestamp, group});
|
franta-hg@65
|
457 |
return 0;
|
franta-hg@64
|
458 |
}
|
franta-hg@64
|
459 |
|
franta-hg@64
|
460 |
@Override
|
franta-hg@64
|
461 |
public double getEventsPerHour(int key, long gid) throws StorageBackendException {
|
franta-hg@66
|
462 |
log.log(Level.SEVERE, "TODO: getEventsPerHour {0} / {1}", new Object[]{key, gid});
|
franta-hg@65
|
463 |
return 0;
|
franta-hg@64
|
464 |
}
|
franta-hg@64
|
465 |
|
franta-hg@64
|
466 |
@Override
|
franta-hg@64
|
467 |
public List<String> getGroupsForList(String listAddress) throws StorageBackendException {
|
franta-hg@66
|
468 |
log.log(Level.SEVERE, "TODO: getGroupsForList {0}", new Object[]{listAddress});
|
franta-hg@65
|
469 |
return Collections.emptyList();
|
franta-hg@64
|
470 |
}
|
franta-hg@64
|
471 |
|
franta-hg@64
|
472 |
@Override
|
franta-hg@64
|
473 |
public List<String> getListsForGroup(String groupname) throws StorageBackendException {
|
franta-hg@66
|
474 |
log.log(Level.SEVERE, "TODO: getListsForGroup {0}", new Object[]{groupname});
|
franta-hg@65
|
475 |
return Collections.emptyList();
|
franta-hg@64
|
476 |
}
|
franta-hg@64
|
477 |
|
franta-hg@64
|
478 |
@Override
|
franta-hg@64
|
479 |
public String getOldestArticle() throws StorageBackendException {
|
franta-hg@66
|
480 |
log.log(Level.SEVERE, "TODO: getOldestArticle");
|
franta-hg@65
|
481 |
return null;
|
franta-hg@64
|
482 |
}
|
franta-hg@64
|
483 |
|
franta-hg@64
|
484 |
@Override
|
franta-hg@64
|
485 |
public List<Subscription> getSubscriptions(int type) throws StorageBackendException {
|
franta-hg@66
|
486 |
log.log(Level.SEVERE, "TODO: getSubscriptions {0}", new Object[]{type});
|
franta-hg@65
|
487 |
return Collections.emptyList();
|
franta-hg@64
|
488 |
}
|
franta-hg@64
|
489 |
|
franta-hg@64
|
490 |
@Override
|
franta-hg@64
|
491 |
public void purgeGroup(Group group) throws StorageBackendException {
|
franta-hg@66
|
492 |
log.log(Level.SEVERE, "TODO: purgeGroup {0}", new Object[]{group});
|
franta-hg@64
|
493 |
}
|
franta-hg@64
|
494 |
|
franta-hg@64
|
495 |
@Override
|
franta-hg@64
|
496 |
public boolean update(Article article) throws StorageBackendException {
|
franta-hg@66
|
497 |
log.log(Level.SEVERE, "TODO: update {0}", new Object[]{article});
|
franta-hg@65
|
498 |
throw new StorageBackendException("Not implemented yet.");
|
franta-hg@64
|
499 |
}
|
franta-hg@64
|
500 |
|
franta-hg@64
|
501 |
@Override
|
franta-hg@64
|
502 |
public boolean update(Group group) throws StorageBackendException {
|
franta-hg@66
|
503 |
log.log(Level.SEVERE, "TODO: update {0}", new Object[]{group});
|
franta-hg@65
|
504 |
throw new StorageBackendException("Not implemented yet.");
|
franta-hg@64
|
505 |
}
|
franta-hg@63
|
506 |
}
|