use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class AccountFragment method show_connectivity_error.
public void show_connectivity_error(Exception e) {
if (e != null) {
e.printStackTrace();
}
if (getActivity() == null) {
return;
}
if (e instanceof OpacErrorException) {
AccountDataSource adatasource = new AccountDataSource(getActivity());
adatasource.invalidateCachedAccountData(account);
dialog_wrong_credentials(e.getMessage());
return;
}
if (getView() != null) {
final FrameLayout errorView = (FrameLayout) getView().findViewById(R.id.error_view);
errorView.removeAllViews();
View connError = getActivity().getLayoutInflater().inflate(R.layout.error_connectivity, errorView);
TextView tvErrBody = (TextView) connError.findViewById(R.id.tvErrBody);
Button btnRetry = (Button) connError.findViewById(R.id.btRetry);
btnRetry.setVisibility(View.VISIBLE);
if (e != null && e instanceof SSLSecurityException) {
tvErrBody.setText(R.string.connection_error_detail_security);
} else if (e != null && e instanceof NotReachableException) {
tvErrBody.setText(R.string.connection_error_detail_nre);
} else if (e != null && e instanceof OpacClient.LibraryRemovedException) {
tvErrBody.setText(R.string.library_removed_error);
btnRetry.setVisibility(View.GONE);
}
btnRetry.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
refresh();
}
});
llLoading.setVisibility(View.GONE);
swipeRefreshLayout.setVisibility(View.GONE);
connError.setVisibility(View.VISIBLE);
}
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class Primo method parseSearchFields.
@Override
public List<SearchField> parseSearchFields() throws IOException, OpacErrorException, JSONException {
start();
String html = httpGet(opac_url + "/action/search.do?mode=Advanced&ct=AdvancedSearch&vid=" + vid, getDefaultEncoding());
Document doc = Jsoup.parse(html);
List<SearchField> fields = new ArrayList<>();
if (doc.select("select#exlidInput_scope_1").size() < 1) {
throw new NotReachableException();
}
Elements options = doc.select("select#exlidInput_scope_1").first().select("option");
for (Element option : options) {
TextSearchField field = new TextSearchField();
field.setDisplayName(option.text());
field.setId(option.val());
field.setHint("");
field.setData(new JSONObject());
field.getData().put("meaning", option.val());
fields.add(field);
}
if (fields.size() == 0) {
// Weird JavaScript, e.g. view-source:http://vopac.nlg.gr/Search/Advanced
Pattern pattern_key = Pattern.compile("searchFields\\[\"([^\"]+)\"\\] = \"([^\"]+)\";");
for (Element script : doc.select("script")) {
if (!script.html().contains("searchFields"))
continue;
for (String line : script.html().split("\n")) {
Matcher matcher = pattern_key.matcher(line);
if (matcher.find()) {
TextSearchField field = new TextSearchField();
field.setDisplayName(matcher.group(2));
field.setId(matcher.group(1));
field.setHint("");
field.setData(new JSONObject());
field.getData().put("meaning", field.getId());
fields.add(field);
}
}
}
}
Elements selects = doc.select("#exlidInput_mediaType_, #exlidInput_publicationDate_, " + "#exlidInput_language_, #exlidSearchIn");
for (Element select : selects) {
DropdownSearchField field = new DropdownSearchField();
if (select.parent().select("label").size() > 0) {
field.setDisplayName(select.parent().select("label").first().text());
} else {
continue;
}
field.setId("#" + select.attr("id"));
for (Element option : select.select("option")) {
if (option.val().equals("all_items")) {
field.addDropdownValue(0, option.val(), option.text());
} else {
field.addDropdownValue(option.val(), option.text());
}
}
field.setData(new JSONObject());
field.getData().put("meaning", field.getId());
fields.add(field);
}
return fields;
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class BiBer1992 method parseSearchFields.
/*
* ----- media types ----- Example Wuerzburg: <td ...><input type="checkbox"
* name="MT" value="1" ...></td> <td ...><img src="../image/spacer.gif.S"
* title="Buch"><br>Buch</td>
*
* Example Friedrichshafen: <td ...><input type="checkbox" name="MS"
* value="1" ...></td> <td ...><img src="../image/spacer.gif.S"
* title="Buch"><br>Buch</td>
*
* Example Offenburg: <input type="radio" name="MT" checked
* value="MTYP0">Alles <input type="radio" name="MT"
* value="MTYP10">Belletristik Unfortunately Biber miss the end
* tag </input>, so opt.text() does not work! (at least Offenburg)
*
* Example Essen, Aschaffenburg: <input type="radio" name="MT" checked
* value="MTYP0"><img src="../image/all.gif.S" title="Alles"> <input
* type="radio" name="MT" value="MTYP7"><img src="../image/cdrom.gif.S"
* title="CD-ROM">
*
* ----- Branches ----- Example Essen,Erkrath: no closing </option> !!!
* cannot be parsed by Jsoup, so not supported <select name="AORT"> <option
* value="ZWST1">Altendorf </select>
*
* Example Hagen, Würzburg, Friedrichshafen: <select name="ZW" class="sel1">
* <option selected value="ZWST0">Alle Bibliotheksorte</option> </select>
*/
@Override
public List<SearchField> parseSearchFields() throws IOException {
List<SearchField> fields = new ArrayList<>();
HttpGet httpget;
if (opacDir.contains("opax")) {
httpget = new HttpGet(opacUrl + "/" + opacDir + "/de/qsel.html.S");
} else {
httpget = new HttpGet(opacUrl + "/" + opacDir + "/de/qsel_main.S");
}
HttpResponse response = http_client.execute(httpget);
if (response.getStatusLine().getStatusCode() == 500) {
throw new NotReachableException(response.getStatusLine().getReasonPhrase());
}
String html = convertStreamToString(response.getEntity().getContent());
HttpUtils.consume(response.getEntity());
Document doc = Jsoup.parse(html);
// get text fields
Elements text_opts = doc.select("form select[name=REG1] option");
for (Element opt : text_opts) {
TextSearchField field = new TextSearchField();
field.setId(opt.attr("value"));
field.setDisplayName(opt.text());
field.setHint("");
fields.add(field);
}
// get media types
Elements mt_opts = doc.select("form input[name~=(MT|MS)]");
if (mt_opts.size() > 0) {
DropdownSearchField mtDropdown = new DropdownSearchField();
mtDropdown.setId(mt_opts.get(0).attr("name"));
mtDropdown.setDisplayName("Medientyp");
for (Element opt : mt_opts) {
if (!opt.val().equals("")) {
String text = opt.text();
if (text.length() == 0) {
// text is empty, check layouts:
// Essen: <input name="MT"><img title="mediatype">
// Schaffenb: <input name="MT"><img alt="mediatype">
Element img = opt.nextElementSibling();
if (img != null && img.tagName().equals("img")) {
text = img.attr("title");
if (text.equals("")) {
text = img.attr("alt");
}
}
}
if (text.length() == 0) {
// text is still empty, check table layout, Example
// Friedrichshafen
// <td><input name="MT"></td> <td><img
// title="mediatype"></td>
Element td1 = opt.parent();
Element td2 = td1.nextElementSibling();
if (td2 != null) {
Elements td2Children = td2.select("img[title]");
if (td2Children.size() > 0) {
text = td2Children.get(0).attr("title");
}
}
}
if (text.length() == 0) {
// text is still empty, check images in label layout, Example
// Wiedenst
// <input type="radio" name="MT" id="MTYP1" value="MTYP1">
// <label for="MTYP1"><img src="http://www.wiedenest.de/bib/image/books
// .png" alt="Bücher" title="Bücher"></label>
Element label = opt.nextElementSibling();
if (label != null) {
Elements td2Children = label.select("img[title]");
if (td2Children.size() > 0) {
text = td2Children.get(0).attr("title");
}
}
}
if (text.length() == 0) {
// text is still empty: missing end tag like Offenburg
text = parse_option_regex(opt);
}
mtDropdown.addDropdownValue(opt.val(), text);
}
}
fields.add(mtDropdown);
}
// get branches
Elements br_opts = doc.select("form select[name=ZW] option");
if (br_opts.size() > 0) {
DropdownSearchField brDropdown = new DropdownSearchField();
brDropdown.setId(br_opts.get(0).parent().attr("name"));
brDropdown.setDisplayName(br_opts.get(0).parent().parent().previousElementSibling().text().replace("\u00a0", "").replace("?", "").trim());
for (Element opt : br_opts) {
brDropdown.addDropdownValue(opt.val(), opt.text());
}
fields.add(brDropdown);
}
Elements sort_opts = doc.select("form select[name=SORTX] option");
if (sort_opts.size() > 0) {
DropdownSearchField sortDropdown = new DropdownSearchField();
sortDropdown.setId(sort_opts.get(0).parent().attr("name"));
sortDropdown.setDisplayName(sort_opts.get(0).parent().parent().previousElementSibling().text().replace("\u00a0", "").replace("?", "").trim());
for (Element opt : sort_opts) {
sortDropdown.addDropdownValue(opt.val(), opt.text());
}
fields.add(sortDropdown);
}
return fields;
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class IOpac method parse_search.
protected SearchRequestResult parse_search(String html, int page) throws OpacErrorException, NotReachableException {
Document doc = Jsoup.parse(html);
if (doc.select("h4").size() > 0) {
if (doc.select("h4").text().trim().startsWith("0 gefundene Medien")) {
// nothing found
return new SearchRequestResult(new ArrayList<SearchResult>(), 0, 1, 1);
} else if (!doc.select("h4").text().trim().contains("gefundene Medien") && !doc.select("h4").text().trim().contains("Es wurden mehr als")) {
// error
throw new OpacErrorException(doc.select("h4").text().trim());
}
} else if (doc.select("h1").size() > 0) {
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_WITH_DESCRIPTION, doc.select("h1").text().trim()));
}
} else {
return null;
}
updateRechnr(doc);
reusehtml = html;
results_total = -1;
if (doc.select("h4").text().trim().contains("Es wurden mehr als")) {
results_total = 200;
} else {
String resultnumstr = doc.select("h4").first().text();
resultnumstr = resultnumstr.substring(0, resultnumstr.indexOf(" ")).trim();
results_total = Integer.parseInt(resultnumstr);
}
List<SearchResult> results = new ArrayList<>();
Elements tables = doc.select("table").first().select("tr:has(td)");
Map<String, Integer> colmap = new HashMap<>();
Element thead = doc.select("table").first().select("tr:has(th)").first();
int j = 0;
for (Element th : thead.select("th")) {
String text = th.text().trim().toLowerCase(Locale.GERMAN);
if (text.contains("cover")) {
colmap.put("cover", j);
} else if (text.contains("titel")) {
colmap.put("title", j);
} else if (text.contains("verfasser")) {
colmap.put("author", j);
} else if (text.contains("mtyp")) {
colmap.put("category", j);
} else if (text.contains("jahr")) {
colmap.put("year", j);
} else if (text.contains("signatur")) {
colmap.put("shelfmark", j);
} else if (text.contains("info")) {
colmap.put("info", j);
} else if (text.contains("abteilung")) {
colmap.put("department", j);
} else if (text.contains("verliehen") || text.contains("verl.")) {
colmap.put("returndate", j);
} else if (text.contains("anz.res")) {
colmap.put("reservations", j);
}
j++;
}
if (colmap.size() == 0) {
colmap.put("cover", 0);
colmap.put("title", 1);
colmap.put("author", 2);
colmap.put("publisher", 3);
colmap.put("year", 4);
colmap.put("department", 5);
colmap.put("shelfmark", 6);
colmap.put("returndate", 7);
colmap.put("category", 8);
}
for (int i = 0; i < tables.size(); i++) {
Element tr = tables.get(i);
SearchResult sr = new SearchResult();
if (tr.select("td").get(colmap.get("cover")).select("img").size() > 0) {
String imgUrl = tr.select("td").get(colmap.get("cover")).select("img").first().attr("src");
sr.setCover(imgUrl);
}
// Media Type
if (colmap.get("category") != null) {
String mType = tr.select("td").get(colmap.get("category")).text().trim().replace("\u00a0", "");
if (data.has("mediatypes")) {
try {
sr.setType(MediaType.valueOf(data.getJSONObject("mediatypes").getString(mType.toLowerCase(Locale.GERMAN))));
} catch (JSONException | IllegalArgumentException e) {
sr.setType(defaulttypes.get(mType.toLowerCase(Locale.GERMAN)));
}
} else {
sr.setType(defaulttypes.get(mType.toLowerCase(Locale.GERMAN)));
}
}
// Title and additional info
String title;
String additionalInfo = "";
if (colmap.get("info") != null) {
Element info = tr.select("td").get(colmap.get("info"));
title = info.select("a[title=Details-Info], a[title=Details-Info1]").text().trim();
String authorIn = info.text().substring(0, info.text().indexOf(title));
if (authorIn.contains(":")) {
authorIn = authorIn.replaceFirst("^([^:]*):(.*)$", "$1");
additionalInfo += " - " + authorIn;
}
} else {
title = tr.select("td").get(colmap.get("title")).text().trim().replace("\u00a0", "");
if (title.contains("(") && title.indexOf("(") > 0) {
additionalInfo += title.substring(title.indexOf("("));
title = title.substring(0, title.indexOf("(") - 1).trim();
}
// Author
if (colmap.containsKey("author")) {
String author = tr.select("td").get(colmap.get("author")).text().trim().replace("\u00a0", "");
additionalInfo += " - " + author;
}
}
// Publisher
if (colmap.containsKey("publisher")) {
String publisher = tr.select("td").get(colmap.get("publisher")).text().trim().replace("\u00a0", "");
additionalInfo += " (" + publisher;
}
// Year
if (colmap.containsKey("year")) {
String year = tr.select("td").get(colmap.get("year")).text().trim().replace("\u00a0", "");
additionalInfo += ", " + year + ")";
}
sr.setInnerhtml("<b>" + title + "</b><br>" + additionalInfo);
// Status
String status = tr.select("td").get(colmap.get("returndate")).text().trim().replace("\u00a0", "");
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);
try {
df.parse(status);
// this is a return date
sr.setStatus(Status.RED);
sr.setInnerhtml(sr.getInnerhtml() + "<br><i>" + stringProvider.getString(StringProvider.LENT_UNTIL) + " " + status + "</i>");
} catch (ParseException e) {
// this is a different status text
String lc = status.toLowerCase(Locale.GERMAN);
if ((lc.equals("") || lc.toLowerCase(Locale.GERMAN).contains("onleihe") || lc.contains("verleihbar") || lc.contains("entleihbar") || lc.contains("ausleihbar")) && !lc.contains("nicht")) {
sr.setStatus(Status.GREEN);
} else {
sr.setStatus(Status.YELLOW);
sr.setInnerhtml(sr.getInnerhtml() + "<br><i>" + status + "</i>");
}
}
// In some libraries (for example search for "atelier" in Preetz)
// the results are sorted differently than their numbers suggest, so
// we need to detect the number ("recno") from the link
String link = tr.select("a[href^=/cgi-bin/di.exe?page=]").attr("href");
Map<String, String> params = getQueryParamsFirst(link);
if (params.containsKey("recno")) {
int recno = Integer.valueOf(params.get("recno"));
sr.setNr(recno - 1);
} else {
// the above should work, but fall back to this if it doesn't
sr.setNr(10 * (page - 1) + i);
}
// In some libraries (for example Preetz) we can detect the media ID
// here using another link present in the search results
Elements idLinks = tr.select("a[href^=/cgi-bin/di.exe?cMedNr]");
if (idLinks.size() > 0) {
Map<String, String> idParams = getQueryParamsFirst(idLinks.first().attr("href"));
String id = idParams.get("cMedNr");
sr.setId(id);
} else {
sr.setId(null);
}
results.add(sr);
}
return new SearchRequestResult(results, results_total, page);
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class OkHttpBaseApi method httpPost.
/**
* Perform a HTTP POST request to a given URL
*
* @param url URL to fetch
* @param data POST data to send
* @param encoding Expected encoding of the response body
* @param ignore_errors If true, status codes above 400 do not raise an exception
* @return Answer content
* @throws NotReachableException Thrown when server returns a HTTP status code greater or equal
* than 400.
*/
public String httpPost(String url, RequestBody data, String encoding, boolean ignore_errors) throws IOException {
Request.Builder requestbuilder = new Request.Builder().url(cleanUrl(url)).header("Accept", "*/*").header("User-Agent", getUserAgent());
if (data.contentType() != null) {
requestbuilder = requestbuilder.header("Content-Type", data.contentType().toString());
}
Request request = requestbuilder.post(data).build();
try {
Response response = http_client.newCall(request).execute();
if (!ignore_errors && response.code() >= 400) {
throw new NotReachableException(response.message());
}
return readBody(response, encoding);
} catch (javax.net.ssl.SSLPeerUnverifiedException e) {
logHttpError(e);
throw new SSLSecurityException(e.getMessage());
} catch (javax.net.ssl.SSLException e) {
// aborted/interrupted handshake/connection
if (e.getMessage().contains("timed out") || e.getMessage().contains("reset by")) {
logHttpError(e);
throw new NotReachableException(e.getMessage());
} else {
logHttpError(e);
throw new SSLSecurityException(e.getMessage());
}
} catch (InterruptedIOException e) {
logHttpError(e);
throw new NotReachableException(e.getMessage());
} catch (UnknownHostException e) {
throw new NotReachableException(e.getMessage());
} catch (IOException e) {
if (e.getMessage() != null && e.getMessage().contains("Request aborted")) {
logHttpError(e);
throw new NotReachableException(e.getMessage());
} else {
throw e;
}
}
}
Aggregations