RESTEasyContextMapper extends BaseRegexContextMapper<RESTEasyBindingData> { @Override @SuppressWarnings("unchecked") public void mapTo(Context context, RESTEasyBindingData target) throws Exception { super.mapTo(context, target); Map<String, List<String>> httpHeaders = target.getHeaders(); for (Property property : context.getProperties()) { String name = property.getName(); Object value = property.getValue(); if ((value != null) && (matches(name) || property.hasLabel(EndpointLabel.HTTP.label()))) { if (HTTP_RESPONSE_STATUS.equals(name)) { target.setStatusCode((Integer)property.getValue()); } else { if (value instanceof List) { List<String> stringList = new ArrayList<String>(); for (Object obj : ((List)value)) { if (obj != null) { stringList.add(obj.toString()); } else { stringList.add(null); } } httpHeaders.put(name, stringList); } else if (value instanceof String) { List<String> list = new ArrayList<String>(); list.add(String.valueOf(value)); httpHeaders.put(name, list); } } } } }  @Override void mapFrom(RESTEasyBindingData source, Context context); @Override @SuppressWarnings("unchecked") void mapTo(Context context, RESTEasyBindingData target); static final String HTTP_RESPONSE_STATUS; }
@Test public void mapToTest() throws Exception { RESTEasyContextMapper rcm = new RESTEasyContextMapper(); RESTEasyBindingData rbd = new RESTEasyBindingData(); Context context = new DefaultContext(); context.setProperty("one", Integer.valueOf(1)); rcm.mapTo(context, rbd); Iterator<Map.Entry<String, List<String>>> entries = rbd.getHeaders().entrySet().iterator(); while (entries.hasNext()) { Map.Entry<String, List<String>> entry = entries.next(); List<String> values = entry.getValue(); Assert.assertTrue(values.size() == 1); Assert.assertTrue(entry.getKey().equals("one")); Assert.assertTrue(values.get(0).equals("1")); } RESTEasyBindingData rbd2 = new RESTEasyBindingData(); context.removeProperties(); List<Integer> list = new ArrayList<Integer>(); list.add(Integer.valueOf(1)); list.add(Integer.valueOf(2)); list.add(Integer.valueOf(3)); context.setProperty("numbers", list); rcm.mapTo(context, rbd2); entries = rbd2.getHeaders().entrySet().iterator(); while (entries.hasNext()) { Map.Entry<String, List<String>> entry = entries.next(); List<String> values = entry.getValue(); Assert.assertTrue(values.size() == 3); Assert.assertTrue(entry.getKey().equals("numbers")); for (String value : values) { Assert.assertTrue(values.get(0).equals("1") || values.get(1).equals("2") || values.get(1).equals("3")); } } }