public void testAddHeaders() { HttpRequestBase method = new HttpPost(); EasSyncService svc = new EasSyncService(); svc.mAuthString = "auth"; svc.mProtocolVersion = "12.1"; svc.mAccount = null; // With second argument false, there should be no header svc.setHeaders(method, false); Header[] headers = method.getHeaders("X-MS-PolicyKey"); assertEquals(0, headers.length); // With second argument true, there should always be a header // The value will be "0" without an account method.removeHeaders("X-MS-PolicyKey"); svc.setHeaders(method, true); headers = method.getHeaders("X-MS-PolicyKey"); assertEquals(1, headers.length); assertEquals("0", headers[0].getValue()); // With an account, but null security key, the header's value should be "0" Account account = new Account(); account.mSecuritySyncKey = null; svc.mAccount = account; method.removeHeaders("X-MS-PolicyKey"); svc.setHeaders(method, true); headers = method.getHeaders("X-MS-PolicyKey"); assertEquals(1, headers.length); assertEquals("0", headers[0].getValue()); // With an account and security key, the header's value should be the security key account.mSecuritySyncKey = "key"; svc.mAccount = account; method.removeHeaders("X-MS-PolicyKey"); svc.setHeaders(method, true); headers = method.getHeaders("X-MS-PolicyKey"); assertEquals(1, headers.length); assertEquals("key", headers[0].getValue()); }
private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException { HttpRequestBase method = null; HttpEntity httpEntity = null; try { String urlstr = coreUrl + queryParams.toQueryString(); boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod()); if ("GET".equals(req.getMethod())) { method = new HttpGet(urlstr); } else if ("HEAD".equals(req.getMethod())) { method = new HttpHead(urlstr); } else if (isPostOrPutRequest) { HttpEntityEnclosingRequestBase entityRequest = "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr); InputStream in = new CloseShieldInputStream(req.getInputStream()); // Prevent close of container streams HttpEntity entity = new InputStreamEntity(in, req.getContentLength()); entityRequest.setEntity(entity); method = entityRequest; } else if ("DELETE".equals(req.getMethod())) { method = new HttpDelete(urlstr); } else { throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod()); } for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) { String headerName = e.nextElement(); if (!"host".equalsIgnoreCase(headerName) && !"authorization".equalsIgnoreCase(headerName) && !"accept".equalsIgnoreCase(headerName)) { method.addHeader(headerName, req.getHeader(headerName)); } } // These headers not supported for HttpEntityEnclosingRequests if (method instanceof HttpEntityEnclosingRequest) { method.removeHeaders(TRANSFER_ENCODING_HEADER); method.removeHeaders(CONTENT_LENGTH_HEADER); } final HttpResponse response = solrDispatchFilter.httpClient.execute( method, HttpClientUtil.createNewHttpClientRequestContext()); int httpStatus = response.getStatusLine().getStatusCode(); httpEntity = response.getEntity(); resp.setStatus(httpStatus); for (HeaderIterator responseHeaders = response.headerIterator(); responseHeaders.hasNext(); ) { Header header = responseHeaders.nextHeader(); // We pull out these two headers below because they can cause chunked // encoding issues with Tomcat if (header != null && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER) && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) { resp.addHeader(header.getName(), header.getValue()); } } if (httpEntity != null) { if (httpEntity.getContentEncoding() != null) resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue()); if (httpEntity.getContentType() != null) resp.setContentType(httpEntity.getContentType().getValue()); InputStream is = httpEntity.getContent(); OutputStream os = resp.getOutputStream(); IOUtils.copyLarge(is, os); } } catch (IOException e) { sendError( new SolrException( SolrException.ErrorCode.SERVER_ERROR, "Error trying to proxy request for url: " + coreUrl, e)); } finally { Utils.consumeFully(httpEntity); } }