@Test(expected = IllegalArgumentException.class) public void validateHeaderValueMatchErrorTest() { Mockito.when(httpHeadersMock.getRequestHeader(VitamHttpHeader.METHOD_OVERRIDE.getName())).thenReturn(Collections .singletonList("got")); final MultivaluedHashMap<String, String> wanted = new MultivaluedHashMap<>(); wanted.add(VitamHttpHeader.METHOD_OVERRIDE.getName(), HttpMethod.GET); HttpHeaderHelper.validateHeaderValue(httpHeadersMock, wanted); }
public static void validateHeaderValue(HttpHeaders headers, MultivaluedHashMap<String, String> wantedHeaders) { ParametersChecker.checkParameter("Headers cannot be null", headers); final List<String> errorDetails = new ArrayList<>(); for (final String wantedHeaderName : wantedHeaders.keySet()) { final List<String> wantedValues = toLowerCaseList(wantedHeaders.get(wantedHeaderName)); final VitamHttpHeader vitamHeader = VitamHttpHeader.get(wantedHeaderName); final List<String> headersValues = toLowerCaseList(headers.getRequestHeader(wantedHeaderName)); List<String> tmpHeadersValues; final List<String> tmpWantedValues = toLowerCaseList(wantedValues); if (headersValues.isEmpty()) { errorDetails.add(String.format("Header %s values are null", wantedHeaderName)); } else { tmpHeadersValues = toLowerCaseList(headersValues); if (vitamHeader != null) { errorDetails .addAll(collectNonMatchingItems(vitamHeader, wantedValues, wantedHeaderName, "Wanted " + "value %s for header %s does not match with define regular %s")); errorDetails .addAll(collectNonMatchingItems(vitamHeader, headersValues, wantedHeaderName, "Found " + "value %s for header %s does not match with define regular %s")); } tmpHeadersValues.removeAll(wantedValues); tmpWantedValues.removeAll(headersValues); if (!tmpHeadersValues.isEmpty()) { LOGGER.warn("Some values ({}) from header {} were not check (not asked)", Joiner.on(", ").join(tmpHeadersValues), wantedHeaderName); } } if (!tmpWantedValues.isEmpty()) { errorDetails.add(String.format("Some WANTED values from header %s were not found (%s)", wantedHeaderName, Joiner.on(", ").join(tmpWantedValues))); } } if (!errorDetails.isEmpty()) { throw new IllegalArgumentException( String.format("There are %d errors %n%s", errorDetails.size(), Joiner.on("\n").join(errorDetails))); } }
HttpHeaderHelper { public static void validateHeaderValue(HttpHeaders headers, MultivaluedHashMap<String, String> wantedHeaders) { ParametersChecker.checkParameter("Headers cannot be null", headers); final List<String> errorDetails = new ArrayList<>(); for (final String wantedHeaderName : wantedHeaders.keySet()) { final List<String> wantedValues = toLowerCaseList(wantedHeaders.get(wantedHeaderName)); final VitamHttpHeader vitamHeader = VitamHttpHeader.get(wantedHeaderName); final List<String> headersValues = toLowerCaseList(headers.getRequestHeader(wantedHeaderName)); List<String> tmpHeadersValues; final List<String> tmpWantedValues = toLowerCaseList(wantedValues); if (headersValues.isEmpty()) { errorDetails.add(String.format("Header %s values are null", wantedHeaderName)); } else { tmpHeadersValues = toLowerCaseList(headersValues); if (vitamHeader != null) { errorDetails .addAll(collectNonMatchingItems(vitamHeader, wantedValues, wantedHeaderName, "Wanted " + "value %s for header %s does not match with define regular %s")); errorDetails .addAll(collectNonMatchingItems(vitamHeader, headersValues, wantedHeaderName, "Found " + "value %s for header %s does not match with define regular %s")); } tmpHeadersValues.removeAll(wantedValues); tmpWantedValues.removeAll(headersValues); if (!tmpHeadersValues.isEmpty()) { LOGGER.warn("Some values ({}) from header {} were not check (not asked)", Joiner.on(", ").join(tmpHeadersValues), wantedHeaderName); } } if (!tmpWantedValues.isEmpty()) { errorDetails.add(String.format("Some WANTED values from header %s were not found (%s)", wantedHeaderName, Joiner.on(", ").join(tmpWantedValues))); } } if (!errorDetails.isEmpty()) { throw new IllegalArgumentException( String.format("There are %d errors %n%s", errorDetails.size(), Joiner.on("\n").join(errorDetails))); } } }
HttpHeaderHelper { public static void validateHeaderValue(HttpHeaders headers, MultivaluedHashMap<String, String> wantedHeaders) { ParametersChecker.checkParameter("Headers cannot be null", headers); final List<String> errorDetails = new ArrayList<>(); for (final String wantedHeaderName : wantedHeaders.keySet()) { final List<String> wantedValues = toLowerCaseList(wantedHeaders.get(wantedHeaderName)); final VitamHttpHeader vitamHeader = VitamHttpHeader.get(wantedHeaderName); final List<String> headersValues = toLowerCaseList(headers.getRequestHeader(wantedHeaderName)); List<String> tmpHeadersValues; final List<String> tmpWantedValues = toLowerCaseList(wantedValues); if (headersValues.isEmpty()) { errorDetails.add(String.format("Header %s values are null", wantedHeaderName)); } else { tmpHeadersValues = toLowerCaseList(headersValues); if (vitamHeader != null) { errorDetails .addAll(collectNonMatchingItems(vitamHeader, wantedValues, wantedHeaderName, "Wanted " + "value %s for header %s does not match with define regular %s")); errorDetails .addAll(collectNonMatchingItems(vitamHeader, headersValues, wantedHeaderName, "Found " + "value %s for header %s does not match with define regular %s")); } tmpHeadersValues.removeAll(wantedValues); tmpWantedValues.removeAll(headersValues); if (!tmpHeadersValues.isEmpty()) { LOGGER.warn("Some values ({}) from header {} were not check (not asked)", Joiner.on(", ").join(tmpHeadersValues), wantedHeaderName); } } if (!tmpWantedValues.isEmpty()) { errorDetails.add(String.format("Some WANTED values from header %s were not found (%s)", wantedHeaderName, Joiner.on(", ").join(tmpWantedValues))); } } if (!errorDetails.isEmpty()) { throw new IllegalArgumentException( String.format("There are %d errors %n%s", errorDetails.size(), Joiner.on("\n").join(errorDetails))); } } private  HttpHeaderHelper(); }
HttpHeaderHelper { public static void validateHeaderValue(HttpHeaders headers, MultivaluedHashMap<String, String> wantedHeaders) { ParametersChecker.checkParameter("Headers cannot be null", headers); final List<String> errorDetails = new ArrayList<>(); for (final String wantedHeaderName : wantedHeaders.keySet()) { final List<String> wantedValues = toLowerCaseList(wantedHeaders.get(wantedHeaderName)); final VitamHttpHeader vitamHeader = VitamHttpHeader.get(wantedHeaderName); final List<String> headersValues = toLowerCaseList(headers.getRequestHeader(wantedHeaderName)); List<String> tmpHeadersValues; final List<String> tmpWantedValues = toLowerCaseList(wantedValues); if (headersValues.isEmpty()) { errorDetails.add(String.format("Header %s values are null", wantedHeaderName)); } else { tmpHeadersValues = toLowerCaseList(headersValues); if (vitamHeader != null) { errorDetails .addAll(collectNonMatchingItems(vitamHeader, wantedValues, wantedHeaderName, "Wanted " + "value %s for header %s does not match with define regular %s")); errorDetails .addAll(collectNonMatchingItems(vitamHeader, headersValues, wantedHeaderName, "Found " + "value %s for header %s does not match with define regular %s")); } tmpHeadersValues.removeAll(wantedValues); tmpWantedValues.removeAll(headersValues); if (!tmpHeadersValues.isEmpty()) { LOGGER.warn("Some values ({}) from header {} were not check (not asked)", Joiner.on(", ").join(tmpHeadersValues), wantedHeaderName); } } if (!tmpWantedValues.isEmpty()) { errorDetails.add(String.format("Some WANTED values from header %s were not found (%s)", wantedHeaderName, Joiner.on(", ").join(tmpWantedValues))); } } if (!errorDetails.isEmpty()) { throw new IllegalArgumentException( String.format("There are %d errors %n%s", errorDetails.size(), Joiner.on("\n").join(errorDetails))); } } private  HttpHeaderHelper(); static List<String> getHeaderValues(HttpHeaders headers, VitamHttpHeader name); static List<String> getHeaderValues(HttpHeaders headers, String name); static boolean hasValuesFor(HttpHeaders headers, VitamHttpHeader vitamHeader); static void checkVitamHeaders(HttpHeaders headers); static void checkVitamHeadersMap(MultivaluedMap<String, String> requestHeaders); static void validateHeaderValue(HttpHeaders headers, MultivaluedHashMap<String, String> wantedHeaders); }
HttpHeaderHelper { public static void validateHeaderValue(HttpHeaders headers, MultivaluedHashMap<String, String> wantedHeaders) { ParametersChecker.checkParameter("Headers cannot be null", headers); final List<String> errorDetails = new ArrayList<>(); for (final String wantedHeaderName : wantedHeaders.keySet()) { final List<String> wantedValues = toLowerCaseList(wantedHeaders.get(wantedHeaderName)); final VitamHttpHeader vitamHeader = VitamHttpHeader.get(wantedHeaderName); final List<String> headersValues = toLowerCaseList(headers.getRequestHeader(wantedHeaderName)); List<String> tmpHeadersValues; final List<String> tmpWantedValues = toLowerCaseList(wantedValues); if (headersValues.isEmpty()) { errorDetails.add(String.format("Header %s values are null", wantedHeaderName)); } else { tmpHeadersValues = toLowerCaseList(headersValues); if (vitamHeader != null) { errorDetails .addAll(collectNonMatchingItems(vitamHeader, wantedValues, wantedHeaderName, "Wanted " + "value %s for header %s does not match with define regular %s")); errorDetails .addAll(collectNonMatchingItems(vitamHeader, headersValues, wantedHeaderName, "Found " + "value %s for header %s does not match with define regular %s")); } tmpHeadersValues.removeAll(wantedValues); tmpWantedValues.removeAll(headersValues); if (!tmpHeadersValues.isEmpty()) { LOGGER.warn("Some values ({}) from header {} were not check (not asked)", Joiner.on(", ").join(tmpHeadersValues), wantedHeaderName); } } if (!tmpWantedValues.isEmpty()) { errorDetails.add(String.format("Some WANTED values from header %s were not found (%s)", wantedHeaderName, Joiner.on(", ").join(tmpWantedValues))); } } if (!errorDetails.isEmpty()) { throw new IllegalArgumentException( String.format("There are %d errors %n%s", errorDetails.size(), Joiner.on("\n").join(errorDetails))); } } private  HttpHeaderHelper(); static List<String> getHeaderValues(HttpHeaders headers, VitamHttpHeader name); static List<String> getHeaderValues(HttpHeaders headers, String name); static boolean hasValuesFor(HttpHeaders headers, VitamHttpHeader vitamHeader); static void checkVitamHeaders(HttpHeaders headers); static void checkVitamHeadersMap(MultivaluedMap<String, String> requestHeaders); static void validateHeaderValue(HttpHeaders headers, MultivaluedHashMap<String, String> wantedHeaders);  }