@Test public void readHeaders() { final String h1 = "h1"; final String h2 = "h2"; final String h1Value = "hValue1"; final String h2Value = "hValue2"; final Vector<String> headersList = new Vector<String>(); Collections.addAll(headersList, h1, h2); final Enumeration<String> headers = headersList.elements(); when(httpServletRequest.getHeaderNames()).thenReturn(headers); when(httpServletRequest.getHeader(h1)).thenReturn(h1Value); when(httpServletRequest.getHeader(h2)).thenReturn(h2Value); Map<String, String> result = extractor.getHeaders(httpServletRequest.getClass(), httpServletRequest); Map<String, String> expected = new HashMap<String, String>(); MapUtils.putAll(expected, new Object[][] { { h1, h1Value }, { h2, h2Value } }); assertThat(result, is(equalTo(expected))); assertThat("No new instances", result.get(h1) == h1Value); assertThat("No new instances", result.get(h2) == h2Value); }
public Map<String, String> getHeaders(Class<?> httpServletRequestClass, Object httpServletRequest) { Method headerNamesMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER_NAMES, httpServletRequestClass); if (null == headerNamesMethod) { return null; } Method headerValueMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER, httpServletRequestClass); if (null == headerValueMethod) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> headers = (Enumeration<String>) headerNamesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> headersResult = new HashMap<String, String>(); if (headers != null) { while (headers.hasMoreElements()) { String headerName = headers.nextElement(); String headerValue = (String) headerValueMethod.invoke(httpServletRequest, new Object[] { headerName }); headersResult.put(headerName, strConstraint.crop(headerValue)); } return headersResult; } } catch (Exception e) { LOG.error("Invocation of to get attributes on given object failed.", e); } return null; }
HttpInformationExtractor { public Map<String, String> getHeaders(Class<?> httpServletRequestClass, Object httpServletRequest) { Method headerNamesMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER_NAMES, httpServletRequestClass); if (null == headerNamesMethod) { return null; } Method headerValueMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER, httpServletRequestClass); if (null == headerValueMethod) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> headers = (Enumeration<String>) headerNamesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> headersResult = new HashMap<String, String>(); if (headers != null) { while (headers.hasMoreElements()) { String headerName = headers.nextElement(); String headerValue = (String) headerValueMethod.invoke(httpServletRequest, new Object[] { headerName }); headersResult.put(headerName, strConstraint.crop(headerValue)); } return headersResult; } } catch (Exception e) { LOG.error("Invocation of to get attributes on given object failed.", e); } return null; } }
HttpInformationExtractor { public Map<String, String> getHeaders(Class<?> httpServletRequestClass, Object httpServletRequest) { Method headerNamesMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER_NAMES, httpServletRequestClass); if (null == headerNamesMethod) { return null; } Method headerValueMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER, httpServletRequestClass); if (null == headerValueMethod) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> headers = (Enumeration<String>) headerNamesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> headersResult = new HashMap<String, String>(); if (headers != null) { while (headers.hasMoreElements()) { String headerName = headers.nextElement(); String headerValue = (String) headerValueMethod.invoke(httpServletRequest, new Object[] { headerName }); headersResult.put(headerName, strConstraint.crop(headerValue)); } return headersResult; } } catch (Exception e) { LOG.error("Invocation of to get attributes on given object failed.", e); } return null; } HttpInformationExtractor(StringConstraint strConstraint); }
HttpInformationExtractor { public Map<String, String> getHeaders(Class<?> httpServletRequestClass, Object httpServletRequest) { Method headerNamesMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER_NAMES, httpServletRequestClass); if (null == headerNamesMethod) { return null; } Method headerValueMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER, httpServletRequestClass); if (null == headerValueMethod) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> headers = (Enumeration<String>) headerNamesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> headersResult = new HashMap<String, String>(); if (headers != null) { while (headers.hasMoreElements()) { String headerName = headers.nextElement(); String headerValue = (String) headerValueMethod.invoke(httpServletRequest, new Object[] { headerName }); headersResult.put(headerName, strConstraint.crop(headerValue)); } return headersResult; } } catch (Exception e) { LOG.error("Invocation of to get attributes on given object failed.", e); } return null; } HttpInformationExtractor(StringConstraint strConstraint); String getRequestUri(Class<?> httpServletRequestClass, Object httpServletRequest); String getRequestMethod(Class<?> httpServletRequestClass, Object httpServletRequest); Map<String, String[]> getParameterMap(Class<?> httpServletRequestClass, Object httpServletRequest); Map<String, String> getAttributes(Class<?> httpServletRequestClass, Object httpServletRequest); Map<String, String> getHeaders(Class<?> httpServletRequestClass, Object httpServletRequest); Map<String, String> getSessionAttributes(Class<?> httpServletRequestClass, Object httpServletRequest); int getResponseStatus(Class<?> httpServletResponseClass, Object httpServletResponse); String getScheme(Class<?> httpServletRequestClass, Object httpServletRequest); String getServerName(Class<?> httpServletRequestClass, Object httpServletRequest); int getServerPort(Class<?> httpServletRequestClass, Object httpServletRequest); String getQueryString(Class<?> httpServletRequestClass, Object httpServletRequest); }
HttpInformationExtractor { public Map<String, String> getHeaders(Class<?> httpServletRequestClass, Object httpServletRequest) { Method headerNamesMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER_NAMES, httpServletRequestClass); if (null == headerNamesMethod) { return null; } Method headerValueMethod = retrieveMethod(HttpMethods.SERVLET_GET_HEADER, httpServletRequestClass); if (null == headerValueMethod) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> headers = (Enumeration<String>) headerNamesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> headersResult = new HashMap<String, String>(); if (headers != null) { while (headers.hasMoreElements()) { String headerName = headers.nextElement(); String headerValue = (String) headerValueMethod.invoke(httpServletRequest, new Object[] { headerName }); headersResult.put(headerName, strConstraint.crop(headerValue)); } return headersResult; } } catch (Exception e) { LOG.error("Invocation of to get attributes on given object failed.", e); } return null; } HttpInformationExtractor(StringConstraint strConstraint); String getRequestUri(Class<?> httpServletRequestClass, Object httpServletRequest); String getRequestMethod(Class<?> httpServletRequestClass, Object httpServletRequest); Map<String, String[]> getParameterMap(Class<?> httpServletRequestClass, Object httpServletRequest); Map<String, String> getAttributes(Class<?> httpServletRequestClass, Object httpServletRequest); Map<String, String> getHeaders(Class<?> httpServletRequestClass, Object httpServletRequest); Map<String, String> getSessionAttributes(Class<?> httpServletRequestClass, Object httpServletRequest); int getResponseStatus(Class<?> httpServletResponseClass, Object httpServletResponse); String getScheme(Class<?> httpServletRequestClass, Object httpServletRequest); String getServerName(Class<?> httpServletRequestClass, Object httpServletRequest); int getServerPort(Class<?> httpServletRequestClass, Object httpServletRequest); String getQueryString(Class<?> httpServletRequestClass, Object httpServletRequest);  }