use of org.olat.core.gui.media.HttpRequestMediaResource in project OpenOLAT by OpenOLAT.
the class TunnelMapper method handle.
@Override
public MediaResource handle(String relPath, HttpServletRequest hreq) {
String method = hreq.getMethod();
String uri = relPath;
HttpUriRequest meth = null;
try {
URIBuilder builder = new URIBuilder();
builder.setScheme(proto).setHost(host).setPort(port.intValue());
if (uri == null) {
uri = (startUri == null) ? "" : startUri;
}
if (uri.length() > 0 && uri.charAt(0) != '/') {
uri = "/" + uri;
}
if (StringHelper.containsNonWhitespace(uri)) {
builder.setPath(uri);
}
if (method.equals("GET")) {
String queryString = hreq.getQueryString();
if (StringHelper.containsNonWhitespace(queryString)) {
builder.setCustomQuery(queryString);
}
meth = new HttpGet(builder.build());
} else if (method.equals("POST")) {
Map<String, String[]> params = hreq.getParameterMap();
HttpPost pmeth = new HttpPost(builder.build());
List<BasicNameValuePair> pairs = new ArrayList<>();
for (String key : params.keySet()) {
String[] vals = params.get(key);
for (String val : vals) {
pairs.add(new BasicNameValuePair(key, val));
}
}
HttpEntity entity = new UrlEncodedFormEntity(pairs, "UTF-8");
pmeth.setEntity(entity);
meth = pmeth;
}
// test page e.g. http://cgi.algonet.se/htbin/cgiwrap/ug/test.py
if ("enabled".equals(CoreSpringFactory.getImpl(BaseSecurityModule.class).getUserInfosTunnelCourseBuildingBlock())) {
User u = ident.getUser();
meth.addHeader("X-OLAT-USERNAME", ident.getName());
meth.addHeader("X-OLAT-LASTNAME", u.getProperty(UserConstants.LASTNAME, null));
meth.addHeader("X-OLAT-FIRSTNAME", u.getProperty(UserConstants.FIRSTNAME, null));
meth.addHeader("X-OLAT-EMAIL", u.getProperty(UserConstants.EMAIL, null));
meth.addHeader("X-OLAT-USERIP", ipAddress);
}
HttpResponse response = httpClient.execute(meth);
if (response == null) {
// error
return new NotFoundMediaResource();
}
// get or post successfully
Header responseHeader = response.getFirstHeader("Content-Type");
if (responseHeader == null) {
// error
EntityUtils.consumeQuietly(response.getEntity());
return new NotFoundMediaResource();
}
return new HttpRequestMediaResource(response);
} catch (ClientProtocolException | URISyntaxException e) {
log.error("", e);
return null;
} catch (IOException e) {
log.error("Error loading URI: " + (meth == null ? "???" : meth.getURI()), e);
return null;
}
}
use of org.olat.core.gui.media.HttpRequestMediaResource in project openolat by klemens.
the class TunnelComponent method getAsyncMediaResource.
/**
* @see org.olat.core.gui.media.AsyncMediaResponsible#getAsyncMediaResource(org.olat.core.gui.UserRequest)
*/
public MediaResource getAsyncMediaResource(UserRequest ureq) {
String moduleURI = ureq.getModuleURI();
// ../index.php instead everything works as expected
if (moduleURI == null) {
// after a click on some other component e.g.
if (!firstCall)
return null;
// reset first call
firstCall = false;
}
TURequest tureq = new TURequest(config, ureq);
fillTURequestWithUserInfo(tureq, ureq);
HttpResponse response = fetch(tureq, httpClientInstance);
if (response == null) {
setFetchError();
return null;
}
Header responseHeader = response.getFirstHeader("Content-Type");
if (responseHeader == null) {
setFetchError();
return null;
}
String mimeType = responseHeader.getValue();
if (mimeType != null && mimeType.startsWith("text/html")) {
// we have html content, let doDispatch handle it for
// inline rendering, update hreq for next content request
String body;
try {
body = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
log.warn("Problems when tunneling URL::" + tureq.getUri(), e);
return null;
}
SimpleHtmlParser parser = new SimpleHtmlParser(body);
if (!parser.isValidHtml()) {
// asynchronuous
return new HttpRequestMediaResource(response);
}
htmlHead = parser.getHtmlHead();
jsOnLoad = parser.getJsOnLoad();
htmlContent = parser.getHtmlContent();
setDirty(true);
} else {
// this is a async browser
return new HttpRequestMediaResource(response);
}
// refetch
return null;
}
use of org.olat.core.gui.media.HttpRequestMediaResource in project OpenOLAT by OpenOLAT.
the class TunnelComponent method getAsyncMediaResource.
/**
* @see org.olat.core.gui.media.AsyncMediaResponsible#getAsyncMediaResource(org.olat.core.gui.UserRequest)
*/
public MediaResource getAsyncMediaResource(UserRequest ureq) {
String moduleURI = ureq.getModuleURI();
// ../index.php instead everything works as expected
if (moduleURI == null) {
// after a click on some other component e.g.
if (!firstCall)
return null;
// reset first call
firstCall = false;
}
TURequest tureq = new TURequest(config, ureq);
fillTURequestWithUserInfo(tureq, ureq);
HttpResponse response = fetch(tureq, httpClientInstance);
if (response == null) {
setFetchError();
return null;
}
Header responseHeader = response.getFirstHeader("Content-Type");
if (responseHeader == null) {
setFetchError();
return null;
}
String mimeType = responseHeader.getValue();
if (mimeType != null && mimeType.startsWith("text/html")) {
// we have html content, let doDispatch handle it for
// inline rendering, update hreq for next content request
String body;
try {
body = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
log.warn("Problems when tunneling URL::" + tureq.getUri(), e);
return null;
}
SimpleHtmlParser parser = new SimpleHtmlParser(body);
if (!parser.isValidHtml()) {
// asynchronuous
return new HttpRequestMediaResource(response);
}
htmlHead = parser.getHtmlHead();
jsOnLoad = parser.getJsOnLoad();
htmlContent = parser.getHtmlContent();
setDirty(true);
} else {
// this is a async browser
return new HttpRequestMediaResource(response);
}
// refetch
return null;
}
use of org.olat.core.gui.media.HttpRequestMediaResource in project openolat by klemens.
the class TunnelMapper method handle.
@Override
public MediaResource handle(String relPath, HttpServletRequest hreq) {
String method = hreq.getMethod();
String uri = relPath;
HttpUriRequest meth = null;
try {
URIBuilder builder = new URIBuilder();
builder.setScheme(proto).setHost(host).setPort(port.intValue());
if (uri == null) {
uri = (startUri == null) ? "" : startUri;
}
if (uri.length() > 0 && uri.charAt(0) != '/') {
uri = "/" + uri;
}
if (StringHelper.containsNonWhitespace(uri)) {
builder.setPath(uri);
}
if (method.equals("GET")) {
String queryString = hreq.getQueryString();
if (StringHelper.containsNonWhitespace(queryString)) {
builder.setCustomQuery(queryString);
}
meth = new HttpGet(builder.build());
} else if (method.equals("POST")) {
Map<String, String[]> params = hreq.getParameterMap();
HttpPost pmeth = new HttpPost(builder.build());
List<BasicNameValuePair> pairs = new ArrayList<>();
for (String key : params.keySet()) {
String[] vals = params.get(key);
for (String val : vals) {
pairs.add(new BasicNameValuePair(key, val));
}
}
HttpEntity entity = new UrlEncodedFormEntity(pairs, "UTF-8");
pmeth.setEntity(entity);
meth = pmeth;
}
// test page e.g. http://cgi.algonet.se/htbin/cgiwrap/ug/test.py
if ("enabled".equals(CoreSpringFactory.getImpl(BaseSecurityModule.class).getUserInfosTunnelCourseBuildingBlock())) {
User u = ident.getUser();
meth.addHeader("X-OLAT-USERNAME", ident.getName());
meth.addHeader("X-OLAT-LASTNAME", u.getProperty(UserConstants.LASTNAME, null));
meth.addHeader("X-OLAT-FIRSTNAME", u.getProperty(UserConstants.FIRSTNAME, null));
meth.addHeader("X-OLAT-EMAIL", u.getProperty(UserConstants.EMAIL, null));
meth.addHeader("X-OLAT-USERIP", ipAddress);
}
HttpResponse response = httpClient.execute(meth);
if (response == null) {
// error
return new NotFoundMediaResource();
}
// get or post successfully
Header responseHeader = response.getFirstHeader("Content-Type");
if (responseHeader == null) {
// error
EntityUtils.consumeQuietly(response.getEntity());
return new NotFoundMediaResource();
}
return new HttpRequestMediaResource(response);
} catch (ClientProtocolException | URISyntaxException e) {
log.error("", e);
return null;
} catch (IOException e) {
log.error("Error loading URI: " + (meth == null ? "???" : meth.getURI()), e);
return null;
}
}
Aggregations