use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class ApacheBaseApi method httpGet.
/**
* Perform a HTTP GET request to a given URL
*
* @param url URL to fetch
* @param encoding Expected encoding of the response body
* @param ignore_errors If true, status codes above 400 do not raise an exception
* @param cookieStore If set, the given cookieStore is used instead of the built-in one.
* @return Answer content
* @throws NotReachableException Thrown when server returns a HTTP status code greater or equal
* than 400.
*/
public String httpGet(String url, String encoding, boolean ignore_errors, CookieStore cookieStore) throws IOException {
HttpGet httpget = new HttpGet(cleanUrl(url));
HttpResponse response;
String html;
httpget.setHeader("Accept", "*/*");
try {
if (cookieStore != null) {
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
response = http_client.execute(httpget, localContext);
} else {
response = http_client.execute(httpget);
}
if (!ignore_errors && response.getStatusLine().getStatusCode() >= 400) {
HttpUtils.consume(response.getEntity());
throw new NotReachableException(response.getStatusLine().getReasonPhrase());
}
html = convertStreamToString(response.getEntity().getContent(), encoding);
HttpUtils.consume(response.getEntity());
} 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;
}
}
return html;
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class Bibliotheca method parse_account.
public static AccountData parse_account(Account acc, Document doc, JSONObject data, ReportHandler reportHandler, JSONObject headers_lent, JSONObject headers_reservations) throws JSONException, NotReachableException {
if (doc.select(".kontozeile_center table").size() == 0) {
throw new NotReachableException();
}
Map<String, Integer> copymap = new HashMap<>();
Elements headerCells = doc.select(".kontozeile_center table").get(0).select("tr.exemplarmenubar").get(0).children();
JSONArray headersList = new JSONArray();
JSONArray unknownHeaders = new JSONArray();
int i = 0;
for (Element headerCell : headerCells) {
String header = headerCell.text();
headersList.put(header);
if (headers_lent.has(header)) {
if (!headers_lent.isNull(header))
copymap.put(headers_lent.getString(header), i);
} else {
unknownHeaders.put(header);
}
i++;
}
if (unknownHeaders.length() > 0) {
// send report
JSONObject reportData = new JSONObject();
reportData.put("headers", headersList);
reportData.put("unknown_headers", unknownHeaders);
Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - lent", DateTime.now(), reportData);
reportHandler.sendReport(report);
// fallback to JSON
JSONObject accounttable = data.getJSONObject("accounttable");
copymap = jsonToMap(accounttable);
}
List<LentItem> media = new ArrayList<>();
Elements exemplartrs = doc.select(".kontozeile_center table").get(0).select("tr.tabKonto");
DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
DateTimeFormatter fmt2 = DateTimeFormat.forPattern("d/M/yyyy").withLocale(Locale.GERMAN);
for (Element tr : exemplartrs) {
LentItem item = new LentItem();
for (Entry<String, Integer> entry : copymap.entrySet()) {
String key = entry.getKey();
int index = entry.getValue();
if (key.equals("prolongurl")) {
if (tr.child(index).children().size() > 0) {
item.setProlongData(tr.child(index).child(0).attr("href"));
item.setRenewable(!tr.child(index).child(0).attr("href").contains("vermsg"));
}
} else if (key.equals("returndate")) {
try {
item.setDeadline(fmt.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e1) {
try {
item.setDeadline(fmt2.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e2) {
e2.printStackTrace();
}
}
} else {
item.set(key, tr.child(index).text());
}
}
media.add(item);
}
copymap = new HashMap<>();
headerCells = doc.select(".kontozeile_center table").get(1).select("tr.exemplarmenubar").get(0).children();
headersList = new JSONArray();
unknownHeaders = new JSONArray();
i = 0;
for (Element headerCell : headerCells) {
String header = headerCell.text();
headersList.put(header);
if (headers_reservations.has(header)) {
if (!headers_reservations.isNull(header)) {
copymap.put(headers_reservations.getString(header), i);
}
} else {
unknownHeaders.put(header);
}
i++;
}
if (unknownHeaders.length() > 0) {
// send report
JSONObject reportData = new JSONObject();
reportData.put("headers", headersList);
reportData.put("unknown_headers", unknownHeaders);
Report report = new Report(acc.getLibrary(), "bibliotheca", "unknown header - reservations", DateTime.now(), reportData);
reportHandler.sendReport(report);
// fallback to JSON
JSONObject reservationtable = data.getJSONObject("reservationtable");
copymap = jsonToMap(reservationtable);
}
List<ReservedItem> reservations = new ArrayList<>();
exemplartrs = doc.select(".kontozeile_center table").get(1).select("tr.tabKonto");
for (Element tr : exemplartrs) {
ReservedItem item = new ReservedItem();
for (Entry<String, Integer> entry : copymap.entrySet()) {
String key = entry.getKey();
int index = entry.getValue();
if (key.equals("cancelurl")) {
if (tr.child(index).children().size() > 0) {
item.setCancelData(tr.child(index).child(0).attr("href"));
}
} else if (key.equals("availability")) {
try {
item.setReadyDate(fmt.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e1) {
try {
item.setReadyDate(fmt2.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e2) {
e2.printStackTrace();
}
}
} else if (key.equals("expirationdate")) {
try {
item.setExpirationDate(fmt.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e1) {
try {
item.setExpirationDate(fmt2.parseLocalDate(tr.child(index).text()));
} catch (IllegalArgumentException e2) {
item.setStatus(tr.child(index).text());
}
}
} else {
item.set(key, tr.child(index).text());
}
}
reservations.add(item);
}
AccountData res = new AccountData(acc.getId());
for (Element row : doc.select(".kontozeile_center, div[align=center]")) {
String text = row.text().trim();
if (text.matches(".*Ausstehende Geb.+hren:[^0-9]+([0-9.,]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*")) {
text = text.replaceAll(".*Ausstehende Geb.+hren:[^0-9]+([0-9.," + "]+)[^0-9€A-Z]*(€|EUR|CHF|Fr.).*", "$1 $2");
res.setPendingFees(text);
}
if (text.matches("Ihr Ausweis ist g.ltig bis:.*")) {
text = text.replaceAll("Ihr Ausweis ist g.ltig bis:[^A-Za-z0-9]+", "");
res.setValidUntil(text);
} else if (text.matches("Ausweis g.ltig bis:.*")) {
text = text.replaceAll("Ausweis g.ltig bis:[^A-Za-z0-9]+", "");
res.setValidUntil(text);
}
}
res.setLent(media);
res.setReservations(reservations);
return res;
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class Adis method htmlPost.
public Document htmlPost(String url, List<NameValuePair> data) throws IOException {
HttpPost httppost = new HttpPost(cleanUrl(url));
boolean rcf = false;
for (NameValuePair nv : data) {
if (nv.getName().equals("requestCount")) {
rcf = true;
break;
}
}
if (!rcf) {
data.add(new BasicNameValuePair("requestCount", s_requestCount + ""));
}
httppost.setEntity(new UrlEncodedFormEntity(data, getDefaultEncoding()));
HttpResponse response;
try {
response = http_client.execute(httppost);
} catch (javax.net.ssl.SSLPeerUnverifiedException 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")) {
e.printStackTrace();
throw new NotReachableException(e.getMessage());
} else {
throw new SSLSecurityException(e.getMessage());
}
} catch (InterruptedIOException e) {
e.printStackTrace();
throw new NotReachableException(e.getMessage());
} catch (IOException e) {
if (e.getMessage() != null && e.getMessage().contains("Request aborted")) {
e.printStackTrace();
throw new NotReachableException(e.getMessage());
} else {
throw e;
}
}
if (response.getStatusLine().getStatusCode() >= 400) {
throw new NotReachableException(response.getStatusLine().getReasonPhrase());
}
String html = convertStreamToString(response.getEntity().getContent(), getDefaultEncoding());
HttpUtils.consume(response.getEntity());
Document doc = Jsoup.parse(html);
Pattern patRequestCount = Pattern.compile(".*requestCount=([0-9]+)[^0-9].*");
for (Element a : doc.select("a")) {
Matcher objid_matcher = patRequestCount.matcher(a.attr("href"));
if (objid_matcher.matches()) {
s_requestCount = Integer.parseInt(objid_matcher.group(1));
}
}
doc.setBaseUri(url);
return doc;
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class Adis method htmlGet.
public Document htmlGet(String url) throws IOException {
if (!url.contains("requestCount") && s_requestCount >= 0) {
url = url + (url.contains("?") ? "&" : "?") + "requestCount=" + s_requestCount;
}
HttpGet httpget = new HttpGet(cleanUrl(url));
HttpResponse response;
try {
response = http_client.execute(httpget);
} catch (javax.net.ssl.SSLPeerUnverifiedException 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")) {
e.printStackTrace();
throw new NotReachableException(e.getMessage());
} else {
throw new SSLSecurityException(e.getMessage());
}
} catch (InterruptedIOException e) {
e.printStackTrace();
throw new NotReachableException(e.getMessage());
} catch (IOException e) {
if (e.getMessage() != null && e.getMessage().contains("Request aborted")) {
e.printStackTrace();
throw new NotReachableException(e.getMessage());
} else {
throw e;
}
}
if (response.getStatusLine().getStatusCode() >= 400) {
throw new NotReachableException(response.getStatusLine().getReasonPhrase());
}
String html = convertStreamToString(response.getEntity().getContent(), getDefaultEncoding());
HttpUtils.consume(response.getEntity());
Document doc = Jsoup.parse(html);
Pattern patRequestCount = Pattern.compile("requestCount=([0-9]+)");
for (Element a : doc.select("a")) {
Matcher objid_matcher = patRequestCount.matcher(a.attr("href"));
if (objid_matcher.matches()) {
s_requestCount = Integer.parseInt(objid_matcher.group(1));
}
}
doc.setBaseUri(url);
return doc;
}
use of de.geeksfactory.opacclient.networking.NotReachableException in project opacclient by opacapp.
the class IOpac method cancel.
@Override
public CancelResult cancel(String media, Account account, int useraction, String selection) throws IOException, OpacErrorException {
String html = httpGet(opac_url + "/" + media, getDefaultEncoding());
Document doc = Jsoup.parse(html);
try {
Element form = doc.select("form[name=form1]").first();
String sessionid = form.select("input[name=sessionid]").attr("value");
String kndnr = form.select("input[name=kndnr]").attr("value");
String mednr = form.select("input[name=mednr]").attr("value");
httpGet(opac_url + "/cgi-bin/di.exe?mode=9&kndnr=" + kndnr + "&mednr=" + mednr + "&sessionid=" + sessionid + "&psh100=Stornieren", getDefaultEncoding());
return new CancelResult(MultiStepResult.Status.OK);
} catch (Throwable e) {
e.printStackTrace();
throw new NotReachableException(e.getMessage());
}
}
Aggregations