@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request instanceof HttpEntityEnclosingRequest) { final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request; final HttpEntity entity = entityRequest.getEntity(); if (entity != null) { final GzipCompressingEntity zippedEntity = new GzipCompressingEntity(entity); entityRequest.setEntity(zippedEntity); request.removeHeaders(HTTP.CONTENT_ENCODING); request.addHeader(zippedEntity.getContentEncoding()); request.removeHeaders(HTTP.CONTENT_LEN); request.removeHeaders(HTTP.TRANSFER_ENCODING); request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING); } } }
@Test public void shouldWrapEntity() throws Exception { final HttpEntityEnclosingRequest request = mock(HttpEntityEnclosingRequest.class); final HttpEntity entity = mock(HttpEntity.class); when(entity.getContentType()).thenReturn(new BasicHeader(HTTP.CONTENT_TYPE, "application/xml")); when(request.getEntity()).thenReturn(entity); unit.process(request, context); verify(request).setEntity(argThat(allOf( instanceOf(GzipCompressingEntity.class), hasFeature((GzipCompressingEntity e) -> e.getContentType().getValue(), is("application/xml"))))); }
/** * Method for executing HTTP PUT request */ public HttpResponse doPUT(String uri, byte[] data, Map<String, String> requestHeaders) throws Exception { HttpPut request = new HttpPut(constructUrl(uri)); if (data != null) { if (this.requestGzipEnabled) { request.addHeader(CONTENT_ENCODING, COMPRESSION_TYPE); request.setEntity(new GzipCompressingEntity(new ByteArrayEntity(data))); } else { request.setEntity(new ByteArrayEntity(data)); } } setRequestHeaders(request, requestHeaders); return execute(request); }
/** * Method for executing HTTP POST request */ public HttpResponse doPOST(String uri, byte[] data, Map<String, String> requestHeaders) throws Exception { HttpPost request = new HttpPost(constructUrl(uri)); if (data != null) { if (this.requestGzipEnabled) { request.addHeader(CONTENT_ENCODING, COMPRESSION_TYPE); request.setEntity(new GzipCompressingEntity(new ByteArrayEntity(data))); } else { request.setEntity(new ByteArrayEntity(data)); } } setRequestHeaders(request, requestHeaders); return execute(request); }
/** * Method for executing HTTP POST request with form params */ public HttpResponse doPOST(String uri, List<NameValuePair> formParams , Map<String, String> requestHeaders) throws Exception { HttpPost request = new HttpPost(constructUrl(uri)); if (this.requestGzipEnabled) { request.addHeader(CONTENT_ENCODING, COMPRESSION_TYPE); request.setEntity(new GzipCompressingEntity(new UrlEncodedFormEntity(formParams))); } else { request.setEntity(new UrlEncodedFormEntity(formParams)); } setRequestHeaders(request, requestHeaders); return execute(request); }
/** * * @param request * @param data */ private void setRequestBody(HttpEntityEnclosingRequestBase request, byte[] data) { if (data != null) { if (this.requestGzipEnabled) { request.addHeader(CONTENT_ENCODING, COMPRESSION_TYPE); request.setEntity(new GzipCompressingEntity(new ByteArrayEntity(data))); } else { request.setEntity(new ByteArrayEntity(data)); } } }
/** * Copy response body data (the entity) from the proxy to the servlet client. * * @param proxyResponse * @param servletResponse * @param kibanaFilter * @param email * @param index * @throws java.io.IOException */ protected void copyResponseEntity(HttpResponse proxyResponse, HttpServletResponse servletResponse, KibanaFilter kibanaFilter, String email, String index) throws IOException { if (kibanaFilter == null) { super.copyResponseEntity(proxyResponse, servletResponse); } else { switch (kibanaFilter) { case KIBANA_INDEXPATTERN_SEARCH: HttpEntity entity = proxyResponse.getEntity(); if (entity != null) { GzipDecompressingEntity gzipEntity = new GzipDecompressingEntity( entity); String resp = EntityUtils.toString(gzipEntity); BasicHttpEntity basic = new BasicHttpEntity(); JSONObject indices = new JSONObject(resp); //Remove all projects other than the current one and check //if user is authorizer to access it List<String> projects = projectController.findProjectNamesByUser(email, true); JSONArray hits = indices.getJSONObject("hits").getJSONArray("hits"); for (int i = hits.length() - 1; i >= 0; i--) { String projectName = hits.getJSONObject(i).getString("_id"); if (index != null) { if ((!currentProjects.get(email).equalsIgnoreCase(projectName) || !projects.contains(projectName)) && !projectName.equals(Settings.KIBANA_DEFAULT_INDEX) && !projectName.equals(index)) { hits.remove(i); } } else { if ((!currentProjects.get(email).equalsIgnoreCase(projectName) || !projects.contains(projectName)) && !projectName.equals(Settings.KIBANA_DEFAULT_INDEX)) { hits.remove(i); } } } 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; } } }