001/* 002 * SPDX-License-Identifier: Apache-2.0 003 * 004 * Copyright 2025-2026 The Enola <https://enola.dev> Authors 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package dev.enola.audio.voice.twilio.relay; 019 020import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; 021 022import static java.util.Objects.requireNonNull; 023 024import com.fasterxml.jackson.databind.ObjectMapper; 025 026import dev.enola.common.jackson.ObjectMappers; 027 028import java.io.IOException; 029 030public class ConversationRelayIO { 031 032 private static final ObjectMapper DEFAULT_OBJECT_MAPPER = 033 ObjectMappers.newJsonObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false); 034 035 private final ObjectMapper objectMapper; 036 037 public ConversationRelayIO() { 038 this(DEFAULT_OBJECT_MAPPER); 039 } 040 041 public ConversationRelayIO(ObjectMapper objectMapper) { 042 this.objectMapper = requireNonNull(objectMapper); 043 } 044 045 public ConversationRelayRequest read(String json) { 046 try { 047 return requireNonNull(objectMapper.readValue(json, ConversationRelayRequest.class)); 048 } catch (IOException e) { 049 // Intentionally not including full json in exception, as it may contain sensitive data 050 // If it's required for debugging in the future, then log it at DEBUG level 051 throw new IllegalArgumentException("Invalid JSON", e); 052 } 053 } 054 055 public String write(ConversationRelayResponse response) { 056 try { 057 return objectMapper.writeValueAsString(response); 058 } catch (IOException e) { 059 // Intentionally not including full response, as it may contain sensitive data 060 // If it's required for debugging in the future, then log it at DEBUG level 061 throw new IllegalArgumentException( 062 "JSON marshalling failed for: " + response.getClass().getName(), e); 063 } 064 } 065}