JobCallbackUtil { public static Header[] parseHttpHeaders(final String headers) { if (headers == null || headers.length() == 0) { return null; } final String[] headerArray = headers.split(HEADER_ELEMENT_DELIMITER); final List<Header> headerList = new ArrayList<>(headerArray.length); for (int i = 0; i < headerArray.length; i++) { final String headerPair = headerArray[i]; final int index = headerPair.indexOf(HEADER_NAME_VALUE_DELIMITER); if (index != -1) { headerList.add(new BasicHeader(headerPair.substring(0, index), headerPair.substring(index + 1))); } } return headerList.toArray(new BasicHeader[0]); }  static boolean isThereJobCallbackProperty(final Props props,
      final JobCallbackStatusEnum status); static boolean isThereJobCallbackProperty(final Props props,
      final JobCallbackStatusEnum... jobStatuses); static List<HttpRequestBase> parseJobCallbackProperties(final Props props,
      final JobCallbackStatusEnum status, final Map<String, String> contextInfo,
      final int maxNumCallback); static List<HttpRequestBase> parseJobCallbackProperties(final Props props,
      final JobCallbackStatusEnum status, final Map<String, String> contextInfo,
      final int maxNumCallback, final Logger privateLogger); static Header[] parseHttpHeaders(final String headers); static Map<String, String> buildJobContextInfoMap(final Event event,
      final String server); static String replaceTokens(final String value,
      final Map<String, String> contextInfo, final boolean withEncoding);  }
@Test public void noHeaderElementTest() { final Header[] headerArr = JobCallbackUtil.parseHttpHeaders("this is an amazing day"); Assert.assertNotNull(headerArr); Assert.assertEquals(0, headerArr.length); }
@Test public void oneHeaderElementTest() { final String name = "Content-type"; final String value = "application/json"; final String headers = name + JobCallbackConstants.HEADER_NAME_VALUE_DELIMITER + value; Header[] headerArr = JobCallbackUtil.parseHttpHeaders(headers); Assert.assertNotNull(headerArr); Assert.assertEquals(1, headerArr.length); Assert.assertEquals(name, headerArr[0].getName()); Assert.assertEquals(value, headerArr[0].getValue()); final String headersWithExtraDelimiter = name + JobCallbackConstants.HEADER_NAME_VALUE_DELIMITER + value + JobCallbackConstants.HEADER_ELEMENT_DELIMITER; headerArr = JobCallbackUtil.parseHttpHeaders(headersWithExtraDelimiter); Assert.assertNotNull(headerArr); Assert.assertEquals(1, headerArr.length); Assert.assertEquals(name, headerArr[0].getName()); Assert.assertEquals(value, headerArr[0].getValue()); }
@Test public void multipleHeaderElementTest() { final String name1 = "Content-type"; final String value1 = "application/json"; final String name2 = "Accept"; final String value2 = "application/xml"; final String name3 = "User-Agent"; final String value3 = "Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0"; String headers = makeHeaderElement(name1, value1); headers += JobCallbackConstants.HEADER_ELEMENT_DELIMITER; headers += makeHeaderElement(name2, value2); headers += JobCallbackConstants.HEADER_ELEMENT_DELIMITER; headers += makeHeaderElement(name3, value3); System.out.println("headers: " + headers); final Header[] headerArr = JobCallbackUtil.parseHttpHeaders(headers); Assert.assertNotNull(headerArr); Assert.assertEquals(3, headerArr.length); Assert.assertEquals(name1, headerArr[0].getName()); Assert.assertEquals(value1, headerArr[0].getValue()); Assert.assertEquals(name2, headerArr[1].getName()); Assert.assertEquals(value2, headerArr[1].getValue()); Assert.assertEquals(name3, headerArr[2].getName()); Assert.assertEquals(value3, headerArr[2].getValue()); }
@Test public void partialHeaderElementTest() { final String name1 = "Content-type"; final String value1 = "application/json"; final String name2 = "Accept"; final String value2 = ""; final String name3 = "User-Agent"; final String value3 = "Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0"; String headers = makeHeaderElement(name1, value1); headers += JobCallbackConstants.HEADER_ELEMENT_DELIMITER; headers += makeHeaderElement(name2, value2); headers += JobCallbackConstants.HEADER_ELEMENT_DELIMITER; headers += makeHeaderElement(name3, value3); System.out.println("headers: " + headers); final Header[] headerArr = JobCallbackUtil.parseHttpHeaders(headers); Assert.assertNotNull(headerArr); Assert.assertEquals(3, headerArr.length); Assert.assertEquals(name1, headerArr[0].getName()); Assert.assertEquals(value1, headerArr[0].getValue()); Assert.assertEquals(name2, headerArr[1].getName()); Assert.assertEquals(value2, headerArr[1].getValue()); Assert.assertEquals(name3, headerArr[2].getName()); Assert.assertEquals(value3, headerArr[2].getValue()); }