Skip to content
Snippets Groups Projects
Commit e86426f4 authored by uhensler's avatar uhensler
Browse files

OO-2916: Better exception handling of unavailable cover images, more...

OO-2916: Better exception handling of unavailable cover images, more validations of page from/to, utf-8 of infodoc response
parent 55a8f548
No related branches found
No related tags found
No related merge requests found
...@@ -37,6 +37,7 @@ import org.olat.core.gui.control.Controller; ...@@ -37,6 +37,7 @@ import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.Event; import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl; import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.generic.closablewrapper.CloseableModalController; import org.olat.core.gui.control.generic.closablewrapper.CloseableModalController;
import org.olat.core.logging.AssertException;
import org.olat.core.util.StringHelper; import org.olat.core.util.StringHelper;
import org.olat.course.nodes.EdubaseCourseNode; import org.olat.course.nodes.EdubaseCourseNode;
import org.olat.modules.ModuleConfiguration; import org.olat.modules.ModuleConfiguration;
...@@ -138,21 +139,23 @@ public class EdubaseBookSectionListController extends FormBasicController { ...@@ -138,21 +139,23 @@ public class EdubaseBookSectionListController extends FormBasicController {
allOk = false; allOk = false;
} }
} }
allOk &= validateInt(bookSectionWrapper.getPageFromEl()); allOk &= validatePositiveInt(bookSectionWrapper.getPageFromEl());
allOk &= validateInt(bookSectionWrapper.getPageToEl()); allOk &= validatePositiveInt(bookSectionWrapper.getPageToEl());
allOk &= validateToHigherFrom(bookSectionWrapper.getPageFromEl(), bookSectionWrapper.getPageToEl());
} }
return allOk & super.validateFormLogic(ureq); return allOk & super.validateFormLogic(ureq);
} }
private boolean validateInt(TextElement el) { private boolean validatePositiveInt(TextElement el) {
boolean allOk = true; boolean allOk = true;
if(el.isVisible()) { if(el.isVisible()) {
String value = el.getValue(); String value = el.getValue();
if(StringHelper.containsNonWhitespace(value)) { if(StringHelper.containsNonWhitespace(value)) {
try { try {
Integer.parseInt(value); Integer intValue = Integer.parseInt(value);
if (intValue <= 0) throw new AssertException("negativ number");
} catch(Exception e) { } catch(Exception e) {
el.setErrorKey("form.error.wrong.int", null); el.setErrorKey("form.error.wrong.int", null);
allOk = false; allOk = false;
...@@ -162,6 +165,23 @@ public class EdubaseBookSectionListController extends FormBasicController { ...@@ -162,6 +165,23 @@ public class EdubaseBookSectionListController extends FormBasicController {
return allOk; return allOk;
} }
private boolean validateToHigherFrom(TextElement fromEl, TextElement toEl) {
boolean allOk = true;
try {
Integer from = Integer.parseInt(fromEl.getValue());
Integer to = Integer.parseInt(toEl.getValue());
if (from > 0 && to > 0 && from > to) {
toEl.setErrorKey("form.error.page.to.higher.from", null);
allOk = false;
}
} catch (Exception e) {
// validate only if integers
}
return allOk;
}
@Override @Override
protected void formInnerEvent(UserRequest ureq, FormItem source, FormEvent event) { protected void formInnerEvent(UserRequest ureq, FormItem source, FormEvent event) {
...@@ -443,14 +463,14 @@ public class EdubaseBookSectionListController extends FormBasicController { ...@@ -443,14 +463,14 @@ public class EdubaseBookSectionListController extends FormBasicController {
public BookSection getBookSection() { public BookSection getBookSection() {
bookSection.setBookId(bookIdEl.getValue()); bookSection.setBookId(bookIdEl.getValue());
if (StringHelper.containsNonWhitespace(pageFromEl.getValue())) { try {
bookSection.setPageFrom(Integer.valueOf(pageFromEl.getValue())); bookSection.setPageFrom(Integer.valueOf(pageFromEl.getValue()));
} else { } catch (Exception e) {
bookSection.setPageFrom(null); bookSection.setPageFrom(null);
} }
if (StringHelper.containsNonWhitespace(pageToEl.getValue())) { try {
bookSection.setPageTo(Integer.valueOf(pageToEl.getValue())); bookSection.setPageTo(Integer.valueOf(pageToEl.getValue()));
} else { } catch (Exception e) {
bookSection.setPageTo(null); bookSection.setPageTo(null);
} }
if (StringHelper.containsNonWhitespace(titleEl.getValue())) { if (StringHelper.containsNonWhitespace(titleEl.getValue())) {
......
...@@ -20,10 +20,8 @@ ...@@ -20,10 +20,8 @@
<div>$r.render(${bookSection.getDownLinkEl().getComponent().getComponentName()})</div> <div>$r.render(${bookSection.getDownLinkEl().getComponent().getComponentName()})</div>
#end #end
</td> </td>
<td class="o_edubase_bs_cover" calss="cover${bookSection.getBookIdEl().getValue()}"> <td class="o_edubase_bs_cover">
<object data="$!{bookSection.getCoverUrl()}"> <img src="$!{bookSection.getBookSection().getCoverUrl()}" onerror="if (this.src != '$r.staticLink('images/edubase/empty_cover.png')') this.src = '$r.staticLink('images/edubase/empty_cover.png')';" />
<img src="$r.staticLink('images/edubase/empty_cover.png')" alt="COVER">
</object>
</td> </td>
</tbody> </tbody>
</table> </table>
......
...@@ -2,9 +2,7 @@ ...@@ -2,9 +2,7 @@
#foreach($bookSection in $bookSections) #foreach($bookSection in $bookSections)
<div class="o_edubase_booksection"> <div class="o_edubase_booksection">
#set( $runindex = $run + $foreach.index) #set( $runindex = $run + $foreach.index)
<object data="$!{bookSection.getCoverUrl()}"> <img src="$!{bookSection.getCoverUrl()}" onerror="if (this.src != '$r.staticLink('images/edubase/empty_cover.png')') this.src = '$r.staticLink('images/edubase/empty_cover.png')';">
<img src="$r.staticLink('images/edubase/empty_cover.png')" alt="COVER" onClick="$r.javaScriptCommand($runindex);">
</object>
<div> <div>
$r.escapeHtml($bookSection.getTitle()) $r.escapeHtml($bookSection.getTitle())
</div> </div>
......
...@@ -2,9 +2,7 @@ ...@@ -2,9 +2,7 @@
#foreach($bookSection in $bookSections) #foreach($bookSection in $bookSections)
<div class="row"> <div class="row">
<div class="col-sm-2 o_edubase_cover o_block_top"> <div class="col-sm-2 o_edubase_cover o_block_top">
<object data="$!{bookSection.getCoverUrl()}"> <img src="$!{bookSection.getCoverUrl()}" onerror="if (this.src != '$r.staticLink('images/edubase/empty_cover.png')') this.src = '$r.staticLink('images/edubase/empty_cover.png')';"/>
<img src="$r.staticLink('images/edubase/empty_cover.png')" alt="COVER">
</object>
</div> </div>
<div class="col-sm-10 o_edubase_booksection"> <div class="col-sm-10 o_edubase_booksection">
<div class="row"> <div class="row">
......
...@@ -2,9 +2,7 @@ ...@@ -2,9 +2,7 @@
#foreach($bookSection in $bookSections) #foreach($bookSection in $bookSections)
<div class="o_edubase_pv_fig"> <div class="o_edubase_pv_fig">
<figure> <figure>
<object data="$!{bookSection.getCoverUrl()}"> <img src="$!{bookSection.getCoverUrl()}" onerror="if (this.src != '$r.staticLink('images/edubase/empty_cover.png')') this.src = '$r.staticLink('images/edubase/empty_cover.png')';" />
<img src="$r.staticLink('images/edubase/empty_cover.png')" alt="COVER">
</object>
<figcaption>$r.escapeHtml($bookSection.getTitle())</figcaption> <figcaption>$r.escapeHtml($bookSection.getTitle())</figcaption>
</figure> </figure>
</div> </div>
......
...@@ -18,6 +18,7 @@ edubase.book.section.page.from.to=Seite {0} bis {1} ...@@ -18,6 +18,7 @@ edubase.book.section.page.from.to=Seite {0} bis {1}
edubase.book.section.page.to=Seite bis edubase.book.section.page.to=Seite bis
edubase.book.section.title=Buchtitel edubase.book.section.title=Buchtitel
edubase.with.description.enabled=Leseauftrag edubase.with.description.enabled=Leseauftrag
form.error.page.to.higher.from=Seite bis muss gr\u00F6sser sein als Seite von
form.error.wrong.int=Falsches Zahlenformat. Beispiele\: 2, 10, 144 form.error.wrong.int=Falsches Zahlenformat. Beispiele\: 2, 10, 144
form.error.wrong.section.id=Ung\u00FCltiger Wert form.error.wrong.section.id=Ung\u00FCltiger Wert
link.text=Edubase link.text=Edubase
......
...@@ -18,6 +18,7 @@ edubase.book.section.page.from.to=Page {0} to {1} ...@@ -18,6 +18,7 @@ edubase.book.section.page.from.to=Page {0} to {1}
edubase.book.section.page.to=Page to edubase.book.section.page.to=Page to
edubase.book.section.title=Book title edubase.book.section.title=Book title
edubase.with.description.enabled=Reading order edubase.with.description.enabled=Reading order
form.error.page.to.higher.from=Page to has to be higher than page from
form.error.wrong.int=Wrong numeral format. Examples\: 2, 10, 144 form.error.wrong.int=Wrong numeral format. Examples\: 2, 10, 144
form.error.wrong.section.id=Invalid value form.error.wrong.section.id=Invalid value
link.text=Edubase link.text=Edubase
......
...@@ -22,25 +22,17 @@ package org.olat.modules.edubase.manager; ...@@ -22,25 +22,17 @@ package org.olat.modules.edubase.manager;
import java.io.EOFException; import java.io.EOFException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Optional; import java.util.Optional;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.olat.basesecurity.AuthHelper; import org.olat.basesecurity.AuthHelper;
import org.olat.core.helpers.Settings;
import org.olat.core.id.IdentityEnvironment; import org.olat.core.id.IdentityEnvironment;
import org.olat.core.logging.OLog; import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing; import org.olat.core.logging.Tracing;
...@@ -158,9 +150,9 @@ public class EdubaseManagerImpl implements EdubaseManager { ...@@ -158,9 +150,9 @@ public class EdubaseManagerImpl implements EdubaseManager {
String url = String.format(edubaseModule.getInfoverUrl(), bookId); String url = String.format(edubaseModule.getInfoverUrl(), bookId);
HttpGet request = new HttpGet(url); HttpGet request = new HttpGet(url);
request.setConfig(requestConfig); request.setConfig(requestConfig);
try (CloseableHttpClient httpClient = createAllwaysTrustingHttpClient(); try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(request);) { CloseableHttpResponse httpResponse = httpClient.execute(request);) {
String json = EntityUtils.toString(httpResponse.getEntity()); String json = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
infoReponse = objectMapper.readValue(json, BookDetailsImpl.class); infoReponse = objectMapper.readValue(json, BookDetailsImpl.class);
} catch (SocketTimeoutException socketTimeoutException) { } catch (SocketTimeoutException socketTimeoutException) {
...@@ -171,27 +163,8 @@ public class EdubaseManagerImpl implements EdubaseManager { ...@@ -171,27 +163,8 @@ public class EdubaseManagerImpl implements EdubaseManager {
} catch(Exception e) { } catch(Exception e) {
log.error("", e); log.error("", e);
} }
return infoReponse; return infoReponse;
} }
private CloseableHttpClient createAllwaysTrustingHttpClient()
throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
CloseableHttpClient httpClient;
if (Settings.isDebuging()) {
httpClient = HttpClients.custom()
.setHostnameVerifier(new AllowAllHostnameVerifier())
.setSslcontext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
}).build()).build();
} else {
httpClient = HttpClients.createDefault();
}
return httpClient;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment