private Map<String, String> getFilteredExternalHeaders(final HttpMessage httpRequest) { final Map<String, String> externalHeaders = StreamSupport.stream(httpRequest.getHeaders().spliterator(), false) .collect(Collectors.toMap(HttpHeader::name, HttpHeader::value)); return headerTranslator.fromExternalHeaders(externalHeaders); } }
private static HeaderTranslator createHeaderTranslator(final ProtocolConfigReader protocolConfigReader) { final HeaderDefinition[] blacklist = protocolConfigReader.blacklist() .stream() .map(Ignored::new) .toArray(HeaderDefinition[]::new); return HeaderTranslator.of(DittoHeaderDefinition.values(), MessageHeaderDefinition.values(), blacklist); } }
private HttpResponse enhanceResponseWithExternalDittoHeaders(final HttpResponse response, final DittoHeaders allDittoHeaders) { final Map<String, String> externalHeaders = headerTranslator.toExternalHeaders(allDittoHeaders); if (externalHeaders.isEmpty()) { logger.debug("No external headers for enhancing the response, returning it as-is."); return response; } logger.debug("Enhancing response with external headers: <{}>.", externalHeaders); final List<HttpHeader> externalHttpHeaders = new LinkedList<>(); externalHeaders.forEach((k, v) -> (externalHttpHeaders).add(RawHeader.create(k, v))); return response.withHeaders(externalHttpHeaders); }
/** * Construct a Ditto header translator that knows about nothing. * * @return the Ditto header translator. */ public static HeaderTranslator empty() { return new HeaderTranslator(Collections.emptyMap()); }
/** * Construct a Ditto header translator that knows about nothing. * * @return the Ditto header translator. */ public static HeaderTranslator empty() { return new HeaderTranslator(Collections.emptyMap()); }
@Override public final Adaptable toAdaptable(final T signal, final TopicPath.Channel channel) { final Adaptable adaptable = constructAdaptable(signal, channel); final Map<String, String> externalHeaders = headerTranslator.toExternalHeaders(adaptable.getDittoHeaders()); return adaptable.setDittoHeaders(DittoHeaders.of(externalHeaders)); }
/** * Creates a {@code ThingErrorResponse} from the given {@code adaptable}. * * @param adaptable the adaptable to convert. * @return the ThingErrorResponse. */ private ThingErrorResponse thingErrorResponseFromAdaptable(final Adaptable adaptable) { final DittoHeaders dittoHeaders = headerTranslator.fromExternalHeaders(adaptable.getHeaders().orElse(DittoHeaders.empty())); final TopicPath topicPath = adaptable.getTopicPath(); final DittoRuntimeException dittoRuntimeException = adaptable.getPayload() .getValue() .map(JsonValue::asObject) .map(jsonObject -> { try { return errorRegistry.parse(jsonObject, dittoHeaders); } catch (final JsonTypeNotParsableException e) { return DittoRuntimeException.fromUnknownErrorJson(jsonObject, dittoHeaders) .orElseThrow(() -> e); } }) .orElseThrow(() -> new JsonMissingFieldException(ThingCommandResponse.JsonFields.PAYLOAD)); final String thingId = topicPath.getNamespace() + ":" + topicPath.getId(); return ThingErrorResponse.of(thingId, dittoRuntimeException, dittoRuntimeException.getDittoHeaders()); }
/** * Create a default header translator for this protocol adapter. * * @return the default header translator. */ public static HeaderTranslator headerTranslator() { return HeaderTranslator.of(DittoHeaderDefinition.values(), MessageHeaderDefinition.values()); }
/** * Construct a Ditto header translator from arrays of header definitions. * * @param headerDefinitions arrays of header definitions. * @return the Ditto header translator that knows about the given definitions. */ public static HeaderTranslator of(final HeaderDefinition[]... headerDefinitions) { return new HeaderTranslator(Arrays.stream(headerDefinitions) .flatMap(Arrays::stream) .collect(Collectors.toMap(HeaderDefinition::getKey, Function.identity()))); }
@Override public final Adaptable toAdaptable(final T signal, final TopicPath.Channel channel) { final Adaptable adaptable = constructAdaptable(signal, channel); final Map<String, String> externalHeaders = headerTranslator.toExternalHeaders(adaptable.getDittoHeaders()); return adaptable.setDittoHeaders(DittoHeaders.of(externalHeaders)); }
/** * Creates a {@code ThingErrorResponse} from the given {@code adaptable}. * * @param adaptable the adaptable to convert. * @return the ThingErrorResponse. */ private ThingErrorResponse thingErrorResponseFromAdaptable(final Adaptable adaptable) { final DittoHeaders dittoHeaders = headerTranslator.fromExternalHeaders(adaptable.getHeaders().orElse(DittoHeaders.empty())); final TopicPath topicPath = adaptable.getTopicPath(); final DittoRuntimeException dittoRuntimeException = adaptable.getPayload() .getValue() .map(JsonValue::asObject) .map(jsonObject -> { try { return errorRegistry.parse(jsonObject, dittoHeaders); } catch (final JsonTypeNotParsableException e) { return DittoRuntimeException.fromUnknownErrorJson(jsonObject, dittoHeaders) .orElseThrow(() -> e); } }) .orElseThrow(() -> new JsonMissingFieldException(ThingCommandResponse.JsonFields.PAYLOAD)); final String thingId = topicPath.getNamespace() + ":" + topicPath.getId(); return ThingErrorResponse.of(thingId, dittoRuntimeException, dittoRuntimeException.getDittoHeaders()); }
/** * Create a default header translator for this protocol adapter. * * @return the default header translator. */ public static HeaderTranslator headerTranslator() { return HeaderTranslator.of(DittoHeaderDefinition.values(), MessageHeaderDefinition.values()); }
/** * Construct a Ditto header translator from arrays of header definitions. * * @param headerDefinitions arrays of header definitions. * @return the Ditto header translator that knows about the given definitions. */ public static HeaderTranslator of(final HeaderDefinition[]... headerDefinitions) { return new HeaderTranslator(Arrays.stream(headerDefinitions) .flatMap(Arrays::stream) .collect(Collectors.toMap(HeaderDefinition::getKey, Function.identity()))); }
final Map<String, String> externalHeaders = headerTranslator.toExternalHeaders(allHeadersBuilder.build());
@Override public final T fromAdaptable(final Adaptable externalAdaptable) { checkNotNull(externalAdaptable, "Adaptable"); // get type from external adaptable before header filtering in case some headers exist for external messages // but not internally in Ditto. final String type = getType(externalAdaptable); // filter headers by header translator, then inject any missing information from topic path final DittoHeaders externalHeaders = externalAdaptable.getHeaders().orElse(DittoHeaders.empty()); final DittoHeaders filteredHeaders = addTopicPathInfo( headerTranslator.fromExternalHeaders(externalHeaders), externalAdaptable.getTopicPath()); final Adaptable adaptable = externalAdaptable.setDittoHeaders(filteredHeaders); final JsonifiableMapper<T> jsonifiableMapper = mappingStrategies.get(type); if (null == jsonifiableMapper) { throw UnknownTopicPathException.newBuilder(adaptable.getTopicPath()).build(); } return DittoJsonException.wrapJsonRuntimeException(() -> jsonifiableMapper.map(adaptable)); }
/** * Build a copy of this header translator without knowledge of certain headers. * * @param headerKeys header keys to forget. * @return a new header translator with less knowledge. */ public HeaderTranslator forgetHeaderKeys(final Collection<String> headerKeys) { final Map<String, HeaderDefinition> newHeaderDefinitionMap = headerDefinitionMap.entrySet() .stream() .filter(entry -> !headerKeys.contains(entry.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); return new HeaderTranslator(newHeaderDefinitionMap); }
final Map<String, String> externalHeaders = headerTranslator.toExternalHeaders(allHeadersBuilder.build());
@Override public final T fromAdaptable(final Adaptable externalAdaptable) { checkNotNull(externalAdaptable, "Adaptable"); // get type from external adaptable before header filtering in case some headers exist for external messages // but not internally in Ditto. final String type = getType(externalAdaptable); // filter headers by header translator, then inject any missing information from topic path final DittoHeaders externalHeaders = externalAdaptable.getHeaders().orElse(DittoHeaders.empty()); final DittoHeaders filteredHeaders = addTopicPathInfo( headerTranslator.fromExternalHeaders(externalHeaders), externalAdaptable.getTopicPath()); final Adaptable adaptable = externalAdaptable.setDittoHeaders(filteredHeaders); final JsonifiableMapper<T> jsonifiableMapper = mappingStrategies.get(type); if (null == jsonifiableMapper) { throw UnknownTopicPathException.newBuilder(adaptable.getTopicPath()).build(); } return DittoJsonException.wrapJsonRuntimeException(() -> jsonifiableMapper.map(adaptable)); }
/** * Build a copy of this header translator without knowledge of certain headers. * * @param headerKeys header keys to forget. * @return a new header translator with less knowledge. */ public HeaderTranslator forgetHeaderKeys(final Collection<String> headerKeys) { final Map<String, HeaderDefinition> newHeaderDefinitionMap = headerDefinitionMap.entrySet() .stream() .filter(entry -> !headerKeys.contains(entry.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); return new HeaderTranslator(newHeaderDefinitionMap); }
@Override public Adaptable toAdaptable(final ThingErrorResponse thingErrorResponse, final TopicPath.Channel channel) { final Payload payload = Payload.newBuilder(thingErrorResponse.getResourcePath()) // .withStatus(thingErrorResponse.getStatusCode()) // .withValue(thingErrorResponse.toJson(thingErrorResponse.getImplementedSchemaVersion()) // .getValue(CommandResponse.JsonFields.PAYLOAD) .orElse(JsonFactory.nullObject())) // only use the error payload .build(); final TopicPathBuilder topicPathBuilder = ProtocolFactory.newTopicPathBuilder(thingErrorResponse.getId()); final TopicPathBuildable topicPathBuildable; if (channel == TopicPath.Channel.TWIN) { topicPathBuildable = topicPathBuilder.twin().errors(); } else if (channel == TopicPath.Channel.LIVE) { topicPathBuildable = topicPathBuilder.live().errors(); } else { throw new IllegalArgumentException("Unknown Channel '" + channel + "'"); } final DittoHeaders responseHeaders = ProtocolFactory.newHeadersWithDittoContentType(thingErrorResponse.getDittoHeaders()); return Adaptable.newBuilder(topicPathBuildable.build()) .withPayload(payload) .withHeaders(DittoHeaders.of(headerTranslator.toExternalHeaders(responseHeaders))) .build(); }