/** * Remove unwanted headers from the given header map. * * @param headers Header map * @param nHttpConfiguration NHttp transporter base configurations */ private void removeUnwantedHeadersFromHeaderMap( Map headers, NHttpConfiguration nHttpConfiguration) { Iterator iter = headers.keySet().iterator(); while (iter.hasNext()) { String headerName = (String) iter.next(); if (HTTP.CONN_DIRECTIVE.equalsIgnoreCase(headerName) || HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName) || HTTP.CONTENT_TYPE.equalsIgnoreCase(headerName) || HTTP.CONTENT_LEN.equalsIgnoreCase(headerName)) { iter.remove(); } if (HTTP.SERVER_HEADER.equalsIgnoreCase(headerName) && !nHttpConfiguration.isPreserveHttpHeader(HTTP.SERVER_HEADER)) { iter.remove(); } if (HTTP.USER_AGENT.equalsIgnoreCase(headerName) && !nHttpConfiguration.isPreserveHttpHeader(HTTP.USER_AGENT)) { iter.remove(); } if (HTTP.DATE_HEADER.equalsIgnoreCase(headerName) && !nHttpConfiguration.isPreserveHttpHeader(HTTP.DATE_HEADER)) { iter.remove(); } } }
/** * Add the given headers to the given HTTP request. * * @param httpRequest the request to add the headers to * @param headers the headers to add */ static void addHeaders(HttpUriRequest httpRequest, HttpHeaders headers) { for (Map.Entry<String, List<String>> entry : headers.entrySet()) { String headerName = entry.getKey(); if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265 String headerValue = StringUtils.collectionToDelimitedString(entry.getValue(), "; "); httpRequest.addHeader(headerName, headerValue); } else if (!HTTP.CONTENT_LEN.equalsIgnoreCase(headerName) && !HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName)) { for (String headerValue : entry.getValue()) { httpRequest.addHeader(headerName, headerValue); } } } }
/** * Extract the content length from the incoming message * * @param msgContext current MessageContext * @return the length of the message */ private int extractContentLength(MessageContext msgContext) { Map headers = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS); if (headers == null || headers.isEmpty()) { return -1; } for (Object o : headers.keySet()) { String headerName = (String) o; if (HTTP.CONTENT_LEN.equalsIgnoreCase(headerName)) { Object value = headers.get(headerName); if (value != null && value instanceof String) { try { return Integer.parseInt((String) value); } catch (NumberFormatException e) { return -1; } } } } return -1; }