|
| 1 | +/* |
| 2 | + * Copyright 2025-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.jdbc.store.channel; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.sql.PreparedStatement; |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.sql.Types; |
| 23 | + |
| 24 | +import org.springframework.integration.support.json.JacksonJsonObjectMapper; |
| 25 | +import org.springframework.integration.support.json.JacksonMessagingUtils; |
| 26 | +import org.springframework.integration.support.json.JsonObjectMapper; |
| 27 | +import org.springframework.messaging.Message; |
| 28 | +import org.springframework.util.Assert; |
| 29 | + |
| 30 | +/** |
| 31 | + * A {@link ChannelMessageStorePreparedStatementSetter} implementation that uses JSON |
| 32 | + * serialization for {@link Message} objects instead of Java serialization. |
| 33 | + * <p> |
| 34 | + * This implementation stores the entire message (including headers and payload) as JSON, |
| 35 | + * with type information embedded using Jackson's {@code @class} property for proper deserialization. |
| 36 | + * <p> |
| 37 | + * <b>IMPORTANT:</b> JSON serialization exposes message content in text format in the database. |
| 38 | + * Ensure proper database access controls and encryption for sensitive data. |
| 39 | + * Consider the security implications before using this in production with sensitive information. |
| 40 | + * <p> |
| 41 | + * <b>Database Requirements:</b> |
| 42 | + * This implementation requires modifying the MESSAGE_CONTENT column to a text-based type: |
| 43 | + * <ul> |
| 44 | + * <li>PostgreSQL: Change from {@code BYTEA} to {@code JSONB}</li> |
| 45 | + * <li>MySQL: Change from {@code BLOB} to {@code JSON}</li> |
| 46 | + * <li>H2: Change from {@code LONGVARBINARY} to {@code CLOB}</li> |
| 47 | + * </ul> |
| 48 | + * See the reference documentation for schema migration instructions. |
| 49 | + * <p> |
| 50 | + * <b>Usage Example:</b> |
| 51 | + * <pre>{@code |
| 52 | + * @Bean |
| 53 | + * JdbcChannelMessageStore messageStore(DataSource dataSource) { |
| 54 | + * JdbcChannelMessageStore store = new JdbcChannelMessageStore(dataSource); |
| 55 | + * store.setChannelMessageStoreQueryProvider(new PostgresChannelMessageStoreQueryProvider()); |
| 56 | + * |
| 57 | + * // Enable JSON serialization (requires schema modification) |
| 58 | + * store.setPreparedStatementSetter( |
| 59 | + * new JsonChannelMessageStorePreparedStatementSetter()); |
| 60 | + * store.setMessageRowMapper( |
| 61 | + * new JsonMessageRowMapper("com.example")); |
| 62 | + * |
| 63 | + * return store; |
| 64 | + * } |
| 65 | + * }</pre> |
| 66 | + * |
| 67 | + * @author Yoobin Yoon |
| 68 | + * |
| 69 | + * @since 7.0 |
| 70 | + */ |
| 71 | +public class JsonChannelMessageStorePreparedStatementSetter extends ChannelMessageStorePreparedStatementSetter { |
| 72 | + |
| 73 | + private final JsonObjectMapper<?, ?> jsonObjectMapper; |
| 74 | + |
| 75 | + /** |
| 76 | + * Create a new {@link JsonChannelMessageStorePreparedStatementSetter} with the |
| 77 | + * default {@link JsonObjectMapper} configured for Spring Integration message serialization. |
| 78 | + * <p> |
| 79 | + * This constructor is suitable when serializing standard Spring Integration |
| 80 | + * and Java classes. Custom payload types will require their package to be added to the |
| 81 | + * corresponding {@link JsonMessageRowMapper}. |
| 82 | + */ |
| 83 | + public JsonChannelMessageStorePreparedStatementSetter() { |
| 84 | + this(new JacksonJsonObjectMapper(JacksonMessagingUtils.messagingAwareMapper())); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Create a new {@link JsonChannelMessageStorePreparedStatementSetter} with a |
| 89 | + * custom {@link JsonObjectMapper}. |
| 90 | + * <p> |
| 91 | + * This constructor allows full control over the JSON serialization configuration. |
| 92 | + * <p> |
| 93 | + * <b>Note:</b> The same JsonObjectMapper configuration should be used in the corresponding |
| 94 | + * {@link JsonMessageRowMapper} for consistent serialization and deserialization. |
| 95 | + * @param jsonObjectMapper the {@link JsonObjectMapper} to use for JSON serialization |
| 96 | + */ |
| 97 | + public JsonChannelMessageStorePreparedStatementSetter(JsonObjectMapper<?, ?> jsonObjectMapper) { |
| 98 | + super(); |
| 99 | + Assert.notNull(jsonObjectMapper, "'jsonObjectMapper' must not be null"); |
| 100 | + this.jsonObjectMapper = jsonObjectMapper; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void setValues(PreparedStatement preparedStatement, Message<?> requestMessage, |
| 105 | + Object groupId, String region, boolean priorityEnabled) throws SQLException { |
| 106 | + |
| 107 | + super.setValues(preparedStatement, requestMessage, groupId, region, priorityEnabled); |
| 108 | + |
| 109 | + try { |
| 110 | + String json = this.jsonObjectMapper.toJson(requestMessage); |
| 111 | + |
| 112 | + String dbProduct = preparedStatement.getConnection().getMetaData().getDatabaseProductName(); |
| 113 | + |
| 114 | + if ("PostgreSQL".equalsIgnoreCase(dbProduct)) { |
| 115 | + preparedStatement.setObject(6, json, Types.OTHER); // NOSONAR magic number |
| 116 | + } |
| 117 | + else { |
| 118 | + preparedStatement.setString(6, json); // NOSONAR magic number |
| 119 | + } |
| 120 | + } |
| 121 | + catch (IOException ex) { |
| 122 | + throw new SQLException("Failed to serialize message to JSON: " + requestMessage, ex); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments