Search in sources :

Example 1 with HttpRequestMediaResource

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;
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestMediaResource(org.olat.core.gui.media.HttpRequestMediaResource) User(org.olat.core.id.User) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) ClientProtocolException(org.apache.http.client.ClientProtocolException) Header(org.apache.http.Header) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 2 with HttpRequestMediaResource

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;
}
Also used : TURequest(org.olat.course.nodes.tu.TURequest) HttpRequestMediaResource(org.olat.core.gui.media.HttpRequestMediaResource) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 3 with HttpRequestMediaResource

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;
}
Also used : TURequest(org.olat.course.nodes.tu.TURequest) HttpRequestMediaResource(org.olat.core.gui.media.HttpRequestMediaResource) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 4 with HttpRequestMediaResource

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;
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestMediaResource(org.olat.core.gui.media.HttpRequestMediaResource) User(org.olat.core.id.User) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) ClientProtocolException(org.apache.http.client.ClientProtocolException) Header(org.apache.http.Header) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)4 Header (org.apache.http.Header)4 HttpResponse (org.apache.http.HttpResponse)4 HttpRequestMediaResource (org.olat.core.gui.media.HttpRequestMediaResource)4 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 HttpEntity (org.apache.http.HttpEntity)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpPost (org.apache.http.client.methods.HttpPost)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2 URIBuilder (org.apache.http.client.utils.URIBuilder)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 NotFoundMediaResource (org.olat.core.gui.media.NotFoundMediaResource)2 User (org.olat.core.id.User)2 SimpleHtmlParser (org.olat.core.util.SimpleHtmlParser)2 TURequest (org.olat.course.nodes.tu.TURequest)2