use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class IOpac method parseSearchFields.
@Override
public List<SearchField> parseSearchFields() throws IOException {
List<SearchField> fields = new ArrayList<>();
// Extract all search fields, except media types
String html;
try {
html = httpGet(opac_url + dir + "/search_expert.htm", getDefaultEncoding());
} catch (NotReachableException e) {
html = httpGet(opac_url + dir + "/iopacie.htm", getDefaultEncoding());
}
Document doc = Jsoup.parse(html);
Elements trs = doc.select("form tr:has(input:not([type=submit], [type=reset])), form tr:has(select)");
for (Element tr : trs) {
Elements tds = tr.children();
if (tds.size() == 4) {
// Two search fields next to each other in one row
SearchField field1 = createSearchField(tds.get(0), tds.get(1));
SearchField field2 = createSearchField(tds.get(2), tds.get(3));
if (field1 != null) {
fields.add(field1);
}
if (field2 != null) {
fields.add(field2);
}
} else if (tds.size() == 2 || (tds.size() == 3 && tds.get(2).children().size() == 0)) {
SearchField field = createSearchField(tds.get(0), tds.get(1));
if (field != null) {
fields.add(field);
}
}
}
if (fields.size() == 0 && doc.select("[name=sleStichwort]").size() > 0) {
TextSearchField field = new TextSearchField();
Element input = doc.select("input[name=sleStichwort]").first();
field.setDisplayName(stringProvider.getString(StringProvider.FREE_SEARCH));
field.setId(input.attr("name"));
field.setHint("");
fields.add(field);
}
// Extract available media types.
// We have to parse JavaScript. Doing this with RegEx is evil.
// But not as evil as including a JavaScript VM into the app.
// And I honestly do not see another way.
Pattern pattern_key = Pattern.compile("mtyp\\[[0-9]+\\]\\[\"typ\"\\] = \"([^\"]+)\";");
Pattern pattern_value = Pattern.compile("mtyp\\[[0-9]+\\]\\[\"bez\"\\] = \"([^\"]+)\";");
DropdownSearchField mtyp = new DropdownSearchField();
try {
try {
html = httpGet(opac_url + dir + "/mtyp.js", getDefaultEncoding());
} catch (NotReachableException e) {
html = httpGet(opac_url + "/mtyp.js", getDefaultEncoding());
}
String[] parts = html.split("new Array\\(\\);");
for (String part : parts) {
Matcher matcher1 = pattern_key.matcher(part);
String key = "";
String value = "";
if (matcher1.find()) {
key = matcher1.group(1);
}
Matcher matcher2 = pattern_value.matcher(part);
if (matcher2.find()) {
value = matcher2.group(1);
}
if (!value.equals("")) {
mtyp.addDropdownValue(key, value);
}
}
} catch (IOException e) {
try {
html = httpGet(opac_url + dir + "/frames/search_form.php?bReset=1?bReset=1", getDefaultEncoding());
doc = Jsoup.parse(html);
for (Element opt : doc.select("#imtyp option")) {
mtyp.addDropdownValue(opt.attr("value"), opt.text());
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (mtyp.getDropdownValues() != null && !mtyp.getDropdownValues().isEmpty()) {
mtyp.setDisplayName("Medientypen");
mtyp.setId("Medientyp");
fields.add(mtyp);
}
return fields;
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class IOpac method account.
@Override
public AccountData account(Account account) throws IOException, JSONException, OpacErrorException {
if (!initialised) {
start();
}
Document doc = getAccountPage(account);
AccountData res = new AccountData(account.getId());
List<LentItem> media = new ArrayList<>();
List<ReservedItem> reserved = new ArrayList<>();
parseMediaList(media, doc, data);
parseResList(reserved, doc, data);
res.setLent(media);
res.setReservations(reserved);
if (doc.select("h4:contains(Kontostand)").size() > 0) {
Element h4 = doc.select("h4:contains(Kontostand)").first();
Pattern regex = Pattern.compile("Kontostand (-?\\d+\\.\\d\\d EUR)");
Matcher matcher = regex.matcher(h4.text());
if (matcher.find())
res.setPendingFees(matcher.group(1));
}
if (doc.select("h4:contains(Ausweis g)").size() > 0) {
Element h4 = doc.select("h4:contains(Ausweis g)").first();
Pattern regex = Pattern.compile("Ausweis g.+ltig bis\\s*.\\s*(\\d\\d.\\d\\d.\\d\\d\\d\\d)");
Matcher matcher = regex.matcher(h4.text());
if (matcher.find())
res.setValidUntil(matcher.group(1));
}
if (doc.select(".ReaderAccount_expiredID").size() > 0) {
res.setWarning(doc.select(".ReaderAccount_expiredID").text());
}
if (media.isEmpty() && reserved.isEmpty()) {
if (doc.select("h1").size() > 0) {
// noinspection StatementWithEmptyBody
if (doc.select("h4").text().trim().contains("keine ausgeliehenen Medien")) {
// There is no lent media, but the server is working
// correctly
} else if (doc.select("h1").text().trim().contains("RUNTIME ERROR")) {
// Server Error
throw new NotReachableException("IOPAC RUNTIME ERROR");
} else {
throw new OpacErrorException(stringProvider.getFormattedString(StringProvider.UNKNOWN_ERROR_ACCOUNT_WITH_DESCRIPTION, doc.select("h1").text().trim()));
}
} else {
throw new OpacErrorException(stringProvider.getString(StringProvider.UNKNOWN_ERROR_ACCOUNT));
}
}
return res;
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class OkHttpBaseApi method adapt.
private CompletableFuture<Response> adapt(final Call call, final boolean ignore_errors) {
// based on the similar implementation in Retrofit
// https://github.com/square/retrofit/blob/master/retrofit-adapters/java8/src/main/java
// /retrofit2/adapter/java8/Java8CallAdapterFactory.java
final CompletableFuture<Response> future = new CompletableFuture<Response>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
if (mayInterruptIfRunning) {
call.cancel();
}
return super.cancel(mayInterruptIfRunning);
}
};
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful() || ignore_errors) {
future.complete(response);
} else {
future.completeExceptionally(new NotReachableException(response.message()));
}
}
@Override
public void onFailure(Call call, IOException t) {
future.completeExceptionally(t);
}
});
return future;
}
Aggregations