@Test public void readAttributes() { final String att1 = "a1"; final String att2 = "a2"; final String att1Value = "aValue1"; final String att2Value = "aValue2"; final Vector<String> attributesList = new Vector<String>(); Collections.addAll(attributesList, att1, att2); final Enumeration<String> attributes = attributesList.elements(); when(httpServletRequest.getAttributeNames()).thenReturn(attributes); when(httpServletRequest.getAttribute(att1)).thenReturn(att1Value); when(httpServletRequest.getAttribute(att2)).thenReturn(att2Value); Map<String, String> result = extractor.getAttributes(httpServletRequest.getClass(), httpServletRequest); Map<String, String> expected = new HashMap<String, String>(); MapUtils.putAll(expected, new Object[][] { { att1, att1Value }, { att2, att2Value } }); assertThat(result, is(equalTo(expected))); assertThat("No new instances", result.get(att1) == att1Value); assertThat("No new instances", result.get(att2) == att2Value); }
public Map<String, String> getAttributes(Class<?> httpServletRequestClass, Object httpServletRequest) { Method attributesMethod = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE_NAMES, httpServletRequestClass); if (null == attributesMethod) { return null; } Method attributeValue = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE, httpServletRequestClass); if (null == attributeValue) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> params = (Enumeration<String>) attributesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> attributes = new HashMap<String, String>(); if (null == params) { if (LOG.isDebugEnabled()) { LOG.debug("Attribute enumeration was <null>"); } return null; } while (params.hasMoreElements()) { String attrName = params.nextElement(); Object value = attributeValue.invoke(httpServletRequest, new Object[] { attrName }); attributes.put(attrName, strConstraint.crop(getAttributeValue(value))); } return attributes; } catch (Exception e) { LOG.error("Invocation of " + attributesMethod.getName() + " to get attributes on given object failed.", e); return null; } }
HttpInformationExtractor { public Map<String, String> getAttributes(Class<?> httpServletRequestClass, Object httpServletRequest) { Method attributesMethod = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE_NAMES, httpServletRequestClass); if (null == attributesMethod) { return null; } Method attributeValue = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE, httpServletRequestClass); if (null == attributeValue) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> params = (Enumeration<String>) attributesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> attributes = new HashMap<String, String>(); if (null == params) { if (LOG.isDebugEnabled()) { LOG.debug("Attribute enumeration was <null>"); } return null; } while (params.hasMoreElements()) { String attrName = params.nextElement(); Object value = attributeValue.invoke(httpServletRequest, new Object[] { attrName }); attributes.put(attrName, strConstraint.crop(getAttributeValue(value))); } return attributes; } catch (Exception e) { LOG.error("Invocation of " + attributesMethod.getName() + " to get attributes on given object failed.", e); return null; } } }
HttpInformationExtractor { public Map<String, String> getAttributes(Class<?> httpServletRequestClass, Object httpServletRequest) { Method attributesMethod = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE_NAMES, httpServletRequestClass); if (null == attributesMethod) { return null; } Method attributeValue = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE, httpServletRequestClass); if (null == attributeValue) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> params = (Enumeration<String>) attributesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> attributes = new HashMap<String, String>(); if (null == params) { if (LOG.isDebugEnabled()) { LOG.debug("Attribute enumeration was <null>"); } return null; } while (params.hasMoreElements()) { String attrName = params.nextElement(); Object value = attributeValue.invoke(httpServletRequest, new Object[] { attrName }); attributes.put(attrName, strConstraint.crop(getAttributeValue(value))); } return attributes; } catch (Exception e) { LOG.error("Invocation of " + attributesMethod.getName() + " to get attributes on given object failed.", e); return null; } } HttpInformationExtractor(StringConstraint strConstraint); }
HttpInformationExtractor { public Map<String, String> getAttributes(Class<?> httpServletRequestClass, Object httpServletRequest) { Method attributesMethod = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE_NAMES, httpServletRequestClass); if (null == attributesMethod) { return null; } Method attributeValue = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE, httpServletRequestClass); if (null == attributeValue) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> params = (Enumeration<String>) attributesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> attributes = new HashMap<String, String>(); if (null == params) { if (LOG.isDebugEnabled()) { LOG.debug("Attribute enumeration was <null>"); } return null; } while (params.hasMoreElements()) { String attrName = params.nextElement(); Object value = attributeValue.invoke(httpServletRequest, new Object[] { attrName }); attributes.put(attrName, strConstraint.crop(getAttributeValue(value))); } return attributes; } catch (Exception e) { LOG.error("Invocation of " + attributesMethod.getName() + " 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> getAttributes(Class<?> httpServletRequestClass, Object httpServletRequest) { Method attributesMethod = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE_NAMES, httpServletRequestClass); if (null == attributesMethod) { return null; } Method attributeValue = retrieveMethod(HttpMethods.SERVLET_GET_ATTRIBUTE, httpServletRequestClass); if (null == attributeValue) { return null; } try { @SuppressWarnings("unchecked") Enumeration<String> params = (Enumeration<String>) attributesMethod.invoke(httpServletRequest, (Object[]) null); Map<String, String> attributes = new HashMap<String, String>(); if (null == params) { if (LOG.isDebugEnabled()) { LOG.debug("Attribute enumeration was <null>"); } return null; } while (params.hasMoreElements()) { String attrName = params.nextElement(); Object value = attributeValue.invoke(httpServletRequest, new Object[] { attrName }); attributes.put(attrName, strConstraint.crop(getAttributeValue(value))); } return attributes; } catch (Exception e) { LOG.error("Invocation of " + attributesMethod.getName() + " 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);  }