Example usage for org.apache.http.client.entity GzipCompressingEntity writeTo

List of usage examples for org.apache.http.client.entity GzipCompressingEntity writeTo

Introduction

In this page you can find the example usage for org.apache.http.client.entity GzipCompressingEntity writeTo.

Prototype

@Override
    public void writeTo(final OutputStream outstream) throws IOException 

Source Link

Usage

From source file:io.hops.hopsworks.api.kibana.KibanaProxyServlet.java

/**
 * Copy response body data (the entity) from the proxy to the servlet client.
 *
 * @param proxyResponse//w ww  .j  a v a2 s . c o m
 * @param servletResponse
 * @param kibanaFilter
 * @param email
 */
protected void copyResponseEntity(HttpResponse proxyResponse, HttpServletResponse servletResponse,
        KibanaFilter kibanaFilter, String email) throws IOException {
    if (kibanaFilter == null) {
        super.copyResponseEntity(proxyResponse, servletResponse);
    } else {
        switch (kibanaFilter) {

        case LEGACY_SCROLL_START:
        case KIBANA_DEFAULT_INDEX:
            return;
        case KIBANA_SAVED_OBJECTS_API:
        case ELASTICSEARCH_SEARCH:
            HttpEntity entity = proxyResponse.getEntity();
            if (entity != null) {
                GzipDecompressingEntity gzipEntity = new GzipDecompressingEntity(entity);
                String resp = EntityUtils.toString(gzipEntity);
                BasicHttpEntity basic = new BasicHttpEntity();
                //Remove all projects other than the current one and check
                //if user is authorizer to access it
                JSONObject indices = new JSONObject(resp);
                LOG.log(Level.FINE, "indices:{0}", indices.toString());
                JSONArray hits = null;

                String projectName = currentProjects.get(email);
                List<String> projects = new ArrayList();
                //If we don't have the current project, filter out based on all user's projects
                if (Strings.isNullOrEmpty(projectName)) {
                    List<String> projectNames = projectController.findProjectNamesByUser(email, true);
                    if (projectNames != null && !projectNames.isEmpty()) {
                        projects.addAll(projectNames);
                    }
                } else {
                    projects.add(projectName);
                }
                if (kibanaFilter == KibanaFilter.ELASTICSEARCH_SEARCH
                        && HopsUtils.jsonKeyExists(indices, "buckets")) {
                    hits = indices.getJSONObject("aggregations").getJSONObject("indices")
                            .getJSONArray("buckets");
                } else if (kibanaFilter == KibanaFilter.KIBANA_SAVED_OBJECTS_API
                        && indices.has("saved_objects")) {
                    hits = indices.getJSONArray("saved_objects");
                }
                if (hits != null) {
                    LOG.log(Level.FINE, "hits:{0}", hits);
                    for (int i = hits.length() - 1; i >= 0; i--) {
                        String objectId = null;
                        switch (kibanaFilter) {
                        case ELASTICSEARCH_SEARCH:
                            objectId = hits.getJSONObject(i).getString("key");
                            break;
                        case KIBANA_SAVED_OBJECTS_API:
                            objectId = elasticController.getIndexFromKibana(hits.getJSONObject(i));
                            break;
                        default:
                            break;
                        }
                        if (!Strings.isNullOrEmpty(objectId)
                                && (!isAuthorizedKibanaObject(objectId, email, projects))) {
                            hits.remove(i);
                            LOG.log(Level.FINE, "removed objectId:{0}", objectId);
                        }
                    }
                }

                InputStream in = IOUtils.toInputStream(indices.toString());

                OutputStream servletOutputStream = servletResponse.getOutputStream();
                basic.setContent(in);
                GzipCompressingEntity compress = new GzipCompressingEntity(basic);
                compress.writeTo(servletOutputStream);

            }
            break;
        default:
            super.copyResponseEntity(proxyResponse, servletResponse);
            break;
        }

    }
}