@Test public void shouldMapValuesFromMessageHeaders() throws Exception { String schemaStr = JsonUtils.writer().forType(JsonSchema.class).writeValueAsString(schema); JsonNode schemaNode = JsonUtils.reader().forType(JsonNode.class).readTree(schemaStr); final HttpRequestWrapperProcessor processor = new HttpRequestWrapperProcessor(schemaNode); final Exchange exchange = mock(Exchange.class); final Message message = mock(Message.class); final ExtendedCamelContext camelContext = mock(ExtendedCamelContext.class); when(camelContext.adapt(ExtendedCamelContext.class)).thenReturn(camelContext); when(camelContext.getHeadersMapFactory()).thenReturn(mock(HeadersMapFactory.class)); when(exchange.getIn()).thenReturn(message); when(exchange.getContext()).thenReturn(camelContext); when(message.getBody()).thenReturn(givenBody); when(message.getHeader("param1", String[].class)).thenReturn(new String[] {"param_value1"}); when(message.getHeader("param2", String[].class)).thenReturn(new String[] {"param_value2_1", "param_value2_2"}); processor.process(exchange); final ArgumentCaptor<Message> replacement = ArgumentCaptor.forClass(Message.class); verify(exchange).setIn(replacement.capture()); assertThat(replacement.getValue().getBody()).isEqualTo(replacedBody); }
@Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") public void process(Exchange exchange) throws Exception { final Message message = exchange.getIn(); final Object body = message.getBody(); final ObjectNode rootNode = JsonUtils.copyObjectMapperConfiguration().createObjectNode(); if (!parameters.isEmpty()) { final ObjectNode parametersNode = rootNode.putObject("parameters"); final HttpServletRequest servletRequest = message.getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class); for (String parameter : parameters) { final String[] values; final String[] headerValues = message.getHeader(parameter, String[].class); if (servletRequest != null && headerValues == null) { values = servletRequest.getParameterValues(parameter); } else { values = headerValues; } putPrameterValueTo(parametersNode, parameter, values); } } if (body instanceof String) { final String string = (String) body; if (ObjectHelper.isNotEmpty(string)) { rootNode.set("body", READER.readValue(string)); } } else if (body instanceof InputStream) { try (InputStream stream = (InputStream) body) { if (stream.available() > 0) { rootNode.set("body", READER.readValue(stream)); } } } else if (body != null) { rootNode.putPOJO("body", body); } final String newBody = JsonUtils.toString(rootNode); final Message replacement = new DefaultMessage(exchange.getContext()); replacement.copyFromWithNewBody(message, newBody); replacement.setHeader(Exchange.CONTENT_TYPE, "application/json"); ExchangeHelper.replaceMessage(exchange, replacement, false); }
HttpRequestWrapperProcessor implements Processor { @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") public void process(Exchange exchange) throws Exception { final Message message = exchange.getIn(); final Object body = message.getBody(); final ObjectNode rootNode = JsonUtils.copyObjectMapperConfiguration().createObjectNode(); if (!parameters.isEmpty()) { final ObjectNode parametersNode = rootNode.putObject("parameters"); final HttpServletRequest servletRequest = message.getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class); for (String parameter : parameters) { final String[] values; final String[] headerValues = message.getHeader(parameter, String[].class); if (servletRequest != null && headerValues == null) { values = servletRequest.getParameterValues(parameter); } else { values = headerValues; } putPrameterValueTo(parametersNode, parameter, values); } } if (body instanceof String) { final String string = (String) body; if (ObjectHelper.isNotEmpty(string)) { rootNode.set("body", READER.readValue(string)); } } else if (body instanceof InputStream) { try (InputStream stream = (InputStream) body) { if (stream.available() > 0) { rootNode.set("body", READER.readValue(stream)); } } } else if (body != null) { rootNode.putPOJO("body", body); } final String newBody = JsonUtils.toString(rootNode); final Message replacement = new DefaultMessage(exchange.getContext()); replacement.copyFromWithNewBody(message, newBody); replacement.setHeader(Exchange.CONTENT_TYPE, "application/json"); ExchangeHelper.replaceMessage(exchange, replacement, false); } }
HttpRequestWrapperProcessor implements Processor { @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") public void process(Exchange exchange) throws Exception { final Message message = exchange.getIn(); final Object body = message.getBody(); final ObjectNode rootNode = JsonUtils.copyObjectMapperConfiguration().createObjectNode(); if (!parameters.isEmpty()) { final ObjectNode parametersNode = rootNode.putObject("parameters"); final HttpServletRequest servletRequest = message.getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class); for (String parameter : parameters) { final String[] values; final String[] headerValues = message.getHeader(parameter, String[].class); if (servletRequest != null && headerValues == null) { values = servletRequest.getParameterValues(parameter); } else { values = headerValues; } putPrameterValueTo(parametersNode, parameter, values); } } if (body instanceof String) { final String string = (String) body; if (ObjectHelper.isNotEmpty(string)) { rootNode.set("body", READER.readValue(string)); } } else if (body instanceof InputStream) { try (InputStream stream = (InputStream) body) { if (stream.available() > 0) { rootNode.set("body", READER.readValue(stream)); } } } else if (body != null) { rootNode.putPOJO("body", body); } final String newBody = JsonUtils.toString(rootNode); final Message replacement = new DefaultMessage(exchange.getContext()); replacement.copyFromWithNewBody(message, newBody); replacement.setHeader(Exchange.CONTENT_TYPE, "application/json"); ExchangeHelper.replaceMessage(exchange, replacement, false); } HttpRequestWrapperProcessor(JsonNode schema); }
HttpRequestWrapperProcessor implements Processor { @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") public void process(Exchange exchange) throws Exception { final Message message = exchange.getIn(); final Object body = message.getBody(); final ObjectNode rootNode = JsonUtils.copyObjectMapperConfiguration().createObjectNode(); if (!parameters.isEmpty()) { final ObjectNode parametersNode = rootNode.putObject("parameters"); final HttpServletRequest servletRequest = message.getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class); for (String parameter : parameters) { final String[] values; final String[] headerValues = message.getHeader(parameter, String[].class); if (servletRequest != null && headerValues == null) { values = servletRequest.getParameterValues(parameter); } else { values = headerValues; } putPrameterValueTo(parametersNode, parameter, values); } } if (body instanceof String) { final String string = (String) body; if (ObjectHelper.isNotEmpty(string)) { rootNode.set("body", READER.readValue(string)); } } else if (body instanceof InputStream) { try (InputStream stream = (InputStream) body) { if (stream.available() > 0) { rootNode.set("body", READER.readValue(stream)); } } } else if (body != null) { rootNode.putPOJO("body", body); } final String newBody = JsonUtils.toString(rootNode); final Message replacement = new DefaultMessage(exchange.getContext()); replacement.copyFromWithNewBody(message, newBody); replacement.setHeader(Exchange.CONTENT_TYPE, "application/json"); ExchangeHelper.replaceMessage(exchange, replacement, false); } HttpRequestWrapperProcessor(JsonNode schema); @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") // https://github.com/spotbugs/spotbugs/issues/259 void process(Exchange exchange); Set<String> getParameters(); }
HttpRequestWrapperProcessor implements Processor { @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") public void process(Exchange exchange) throws Exception { final Message message = exchange.getIn(); final Object body = message.getBody(); final ObjectNode rootNode = JsonUtils.copyObjectMapperConfiguration().createObjectNode(); if (!parameters.isEmpty()) { final ObjectNode parametersNode = rootNode.putObject("parameters"); final HttpServletRequest servletRequest = message.getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class); for (String parameter : parameters) { final String[] values; final String[] headerValues = message.getHeader(parameter, String[].class); if (servletRequest != null && headerValues == null) { values = servletRequest.getParameterValues(parameter); } else { values = headerValues; } putPrameterValueTo(parametersNode, parameter, values); } } if (body instanceof String) { final String string = (String) body; if (ObjectHelper.isNotEmpty(string)) { rootNode.set("body", READER.readValue(string)); } } else if (body instanceof InputStream) { try (InputStream stream = (InputStream) body) { if (stream.available() > 0) { rootNode.set("body", READER.readValue(stream)); } } } else if (body != null) { rootNode.putPOJO("body", body); } final String newBody = JsonUtils.toString(rootNode); final Message replacement = new DefaultMessage(exchange.getContext()); replacement.copyFromWithNewBody(message, newBody); replacement.setHeader(Exchange.CONTENT_TYPE, "application/json"); ExchangeHelper.replaceMessage(exchange, replacement, false); } HttpRequestWrapperProcessor(JsonNode schema); @Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") // https://github.com/spotbugs/spotbugs/issues/259 void process(Exchange exchange); Set<String> getParameters();  }