-
Notifications
You must be signed in to change notification settings - Fork 0
/
RigDBAccess.java
477 lines (404 loc) · 16.5 KB
/
RigDBAccess.java
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
*
*/
package rigAPI;
import org.apache.http.client.HttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
/**
* @author Steven Schalhorn
*
*/
public class RigDBAccess
implements Serializable {
private static String API_KEY = null;
private static final String APIURL
= "http://bewerbung.rockimgruenen.de/api/";
public RigDBAccess() {
}
public RigDBAccess(String API_KEY) {
this.API_KEY = API_KEY;
}
/**
* @param args Arguments are currently unused
*/
public static void main(String[] args) throws RiGException {
}
/**
* Authenticates an user against the RiG servers. Saves the API-Key
* internally so that other methods don't have to care about
* authentication. Has to be called as first method after instantiating
* this class.
*
* @param user username of an RiG user
* @param password corresponding password of the RiG user
* @return returns the obtained API-Key, even though it is also
* saved internally
* @throws RiGException the errors described in the API-Documentation as
* exceptions
*/
public String authenticate(String user, String password) throws
RiGException {
String pageURL = APIURL + "authenticate.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", user));
params.add(new BasicNameValuePair("password", password));
String result = httpPost(pageURL, params);
if ("NO_USER".equals(result)) {
throw new NoUserException();
} else if ("NO_PASSWORD".equals(result)) {
throw new NoPasswordException();
} else if ("BAD_AUTHENTICATION".equals(result)) {
throw new BadAuthenticationException();
} else if ("BROKEN_APIKEY".equals(result)) {
throw new BrokenAPIKeyException();
}
API_KEY = result;
return result;
}
/**
* Retrieves band informations of a random band from the RiG server
* @return object with all band informations as fields
* @throws RiGException several subclasses of RiGException
*/
public RigBand getBand() throws RiGException {
return getBand(null);
}
/**
* Retrieves band informations from getBand.php
* @param band numerical id of a specific band
* @return object with all band informations as fields
* @throws RiGException several subclasses of RiGException
*/
public RigBand getBand(Integer band) throws RiGException {
String pageURL = APIURL + "read/getBand.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (band != null) {
params.add(new BasicNameValuePair("band", band.toString()));
}
String result = httpPost(pageURL, params);
if ("BAD_BAND".equals(result)) {
throw new BadBandException();
} else if ("GROUP_ONLY".equals(result)) {
throw new GroupOnlyException();
} else if ("BAND_NONEXISTANT".equals(result)) {
throw new BandNonexistentException();
} else if ("ROUND_COMPLETED".equals(result)) {
throw new RoundCompletedException();
}
Document doc = getDocumentFromXMLString(result);
return new RigBand(doc);
}
/**
* Retrieves settings from getSettings.php
* @return object with all settings as fields
* @throws RiGException several subclasses of RiGException
*/
public RigSettings getSettings() throws RiGException {
String pageURL = APIURL + "read/getSettings.php";
String result = httpPost(pageURL);
Document doc = getDocumentFromXMLString(result);
return new RigSettings(doc);
}
/**
* Retrieves statistics from getStatistic.php
* @return object with all statistics as fields
* @throws RiGException several subclasses of RiGException
*/
public RigStatistic getStatistic() throws RiGException {
String pageUrl = APIURL + "read/getStatistic.php";
String result = httpPost(pageUrl);
if ("BAD_APIKEY".equals(result)) {
throw new BadAPIKeyException();
}
Document doc = getDocumentFromXMLString(result);
return new RigStatistic(doc);
}
/**
* Retrieves the toplist from getToplist.php
* @param day Day to show the toplist for
* @return RigToplist instance
* @throws MissingDayException is thrown if no day is given
* @throws BadDayException is thrown when day is not FR or SA
* @throws RiGException is thrown when XML parsing fails
*/
public RigToplist getToplist(Day day)
throws RiGException {
String pageUrl = APIURL + "read/getToplist.php";
Integer dayInt = day.ordinal();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("day", dayInt.toString()));
String result = httpPost(pageUrl, params);
if ("MISSING_DAY".equals(result)) {
throw new MissingDayException();
} else if ("BAD_DAY".equals(result)) {
throw new BadDayException();
} else if ("BAD_APIKEY".equals(result)) {
throw new BadAPIKeyException();
}
Document doc = getDocumentFromXMLString(result);
return new RigToplist(doc);
}
/**
* Retrieves a search result from searchBand.php
* @param bandname pattern to search for
* @return RigBandSearchResult instance
* @throws MissingStringException Search string is too short
* @throws RiGException
*/
public RigBandSearchResult searchBand(String bandname)
throws RiGException {
String pageUrl = APIURL + "read/searchBand.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("string", bandname));
String result = httpPost(pageUrl, params);
if ("MISSING_STRING".equals(result)) {
throw new MissingStringException();
}
Document doc = getDocumentFromXMLString(result);
return new RigBandSearchResult(doc);
}
/**
* Returns a download as String
* @param download File-ID as noted in getBand.php
* @return String represantation of downloaded file
* @throws httpPostException Errors while downloading
* @throws MissingIDException If no download id is given
* @throws BadIDException if an illigal download id is given
*/
public String downloadFile(Integer download) throws RiGException {
String pageUrl = APIURL + "read/downloadFile.php?";
pageUrl = pageUrl
+ "apikey=" + API_KEY
+ "&download=" + download.toString();
String result = httpPost(pageUrl);
if ("MISSING_ID".equals(result)) {
throw new MissingIDException();
} else if ("BAD_ID".equals(result)) {
throw new BadIDException();
}
return result;
}
/**
* Sets the Weekday for a band
* @param band numerical Band id as specified by getBand.php
* @param day Day to set the band to
* @return OK on success
* @throws RiGException General error during execution
* @throws MissingBandException No Band-ID has been given
* @throws MissingValueException No Day has been given
* @throws BadValueException Day is not in acceptable range
* @throws NotInRoundException Given band is not in current round so it
* isn't votable
* @throws GroupOnlyException Last round has been started, only group
* accounts are able to vote
*/
public String setDay(Integer band, Day day) throws RiGException {
String pageUrl = APIURL + "write/setDay.php";
Integer dayVal = day.ordinal();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("band", band.toString()));
params.add(new BasicNameValuePair("value", dayVal.toString()));
String result = httpPost(pageUrl, params);
if ("MISSING_BAND".equals(result)) {
throw new MissingBandException();
} else if ("MISSING_VALUE".equals(result)) {
throw new MissingValueException();
} else if ("BAD_VALUE".equals(result)) {
throw new BadValueException();
} else if ("NOT_IN_ROUND".equals(result)) {
throw new NotInRoundException();
} else if ("GROUP_ONLY".equals(result)) {
throw new GroupOnlyException();
}
return result;
}
/**
* Sets a tag for the given band
* @param band numerical Band id as specified by getBand.php
* @param musikstil tag to set for the band to as defined by getSettings.php
* @return OK if success
* @throws RiGException General error during Execution
* @throws MissingBandException No Band-ID has been given
* @throws MissingValueException No Day has been given
* @throws BadValueException Day is not in acceptable range
* @throws NotInRoundException Given band is not in current round so it
* isn't votable
* @throws GroupOnlyException Last round has been started, only group
* accounts are able to vote
*/
public String setTag(Integer band, Integer musikstil) throws RiGException {
String pageUrl = APIURL + "write/setTag.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("band", band.toString()));
params.add(new BasicNameValuePair("value", musikstil.toString()));
String result = httpPost(pageUrl, params);
if ("MISSING_BAND".equals(result)) {
throw new MissingBandException();
} else if ("MISSING_VALUE".equals(result)) {
throw new MissingValueException();
} else if ("BAD_VALUE".equals(result)) {
throw new BadValueException();
} else if ("NOT_IN_ROUND".equals(result)) {
throw new NotInRoundException();
} else if ("GROUP_ONLY".equals(result)) {
throw new GroupOnlyException();
}
return result;
}
/**
* Votes for a given band
* @param band numerical Band id as specified by getBand.php
* @param rating submit the rating for the band
* @return OK if success
* @throws RiGException General error during Execution
* @throws MissingBandException No Band-ID has been given
* @throws MissingValueException No Day has been given
* @throws BadValueException Day is not in acceptable range
* @throws NotInRoundException Given band is not in current round so it
* isn't votable
* @throws GroupOnlyException Last round has been started, only group
* accounts are able to vote
*/
public String vote(Integer band, Integer rating) throws RiGException {
String pageUrl = APIURL + "write/vote.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("band", band.toString()));
params.add(new BasicNameValuePair("value", rating.toString()));
String result = httpPost(pageUrl, params);
if ("MISSING_BAND".equals(result)) {
throw new MissingBandException();
} else if ("MISSING_VALUE".equals(result)) {
throw new MissingValueException();
} else if ("BAD_VALUE".equals(result)) {
throw new BadValueException();
} else if ("NOT_IN_ROUND".equals(result)) {
throw new NotInRoundException();
} else if ("GROUP_ONLY".equals(result)) {
throw new GroupOnlyException();
}
return result;
}
/**
* Constructs a Document from a given valid xml String
* @param xmlString String conatining a valid xml document
* @return Document generated from given xmlString
* @throws RiGException Any errors occuring during parsing of xmlString
*/
private static Document getDocumentFromXMLString(String xmlString)
throws RiGException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new rigGetBandException(e);
}
StringBuilder xmlStringBuilder = new StringBuilder(xmlString);
ByteArrayInputStream input;
try {
input = new ByteArrayInputStream
(xmlStringBuilder.toString().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RiGException(e);
}
Document doc;
try {
doc = builder.parse(input);
} catch (IOException e) {
throw new RiGException(e);
} catch (SAXException e) {
throw new RiGException(e);
}
return doc;
}
/**
* Gets the content of a given url
* @param url the url of the requested page
* @return returned content from server
* @throws httpPostException the possible IOExceptions and other exceptions,
* that coule happen during communication are
* wrapped
*/
private static String httpPost(String url) throws httpPostException, ConnectionTimeoutException {
return httpPost(url, null);
}
/**
* Sends a list of parameters to a given URL and returns the response.
* @param url the url of the requested page
* @param urlParameters post parameters to be send to given url
* @return Returned content from server
* @throws httpPostException the possible IOExceptions and other exceptions,
* that coule happen during communication are
* wrapped
*/
private static String httpPost(String url,
List<NameValuePair> urlParameters)
throws httpPostException, ConnectionTimeoutException {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
if (urlParameters == null) {
urlParameters = new ArrayList<NameValuePair>();
}
String USER_AGENT = "Mozilla/5.0";
post.setHeader("User-Agent", USER_AGENT);
urlParameters.add(new BasicNameValuePair("apikey", API_KEY));
try {
post.setEntity(new UrlEncodedFormEntity(urlParameters));
} catch (IOException e) {
throw new httpPostException(e);
}
HttpResponse response;
try {
response = client.execute(post);
} catch (IOException e) {
if(e instanceof ConnectionPoolTimeoutException){
throw new ConnectionTimeoutException(e);
}
throw new httpPostException(e);
}
BufferedReader rd;
try {
rd = new BufferedReader(new InputStreamReader
(response.getEntity().getContent()));
} catch (IOException e) {
throw new httpPostException(e);
}
StringBuilder result = new StringBuilder();
String line;
try {
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
throw new httpPostException(e);
}
return result.toString();
}
public static String getApiKey() {
return API_KEY;
}
public static void setApiKey(String API_KEY) {
RigDBAccess.API_KEY = API_KEY;
}
}