|
| 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.sql.PreparedStatement; |
| 20 | +import java.sql.SQLException; |
| 21 | +import java.sql.Types; |
| 22 | + |
| 23 | +import tools.jackson.core.JacksonException; |
| 24 | +import tools.jackson.databind.ObjectMapper; |
| 25 | + |
| 26 | +import org.springframework.integration.support.json.JacksonMessagingUtils; |
| 27 | +import org.springframework.messaging.Message; |
| 28 | +import org.springframework.util.Assert; |
| 29 | + |
| 30 | +/** |
| 31 | + * A {@link ChannelMessageStorePreparedStatementSetter} implementation that uses Jackson |
| 32 | + * to serialize {@link Message} objects to JSON format 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. This makes |
| 36 | + * the stored data human-readable and useful for debugging and troubleshooting. |
| 37 | + * <p> |
| 38 | + * The {@link ObjectMapper} is configured using {@link JacksonMessagingUtils#messagingAwareMapper(String...)} |
| 39 | + * which includes custom serializers/deserializers for Spring Integration message types |
| 40 | + * and embeds class type information for secure deserialization. |
| 41 | + * <p> |
| 42 | + * <b>Usage Example:</b> |
| 43 | + * <pre>{@code |
| 44 | + * @Bean |
| 45 | + * JdbcChannelMessageStore messageStore(DataSource dataSource) { |
| 46 | + * JdbcChannelMessageStore store = new JdbcChannelMessageStore(dataSource); |
| 47 | + * store.setChannelMessageStoreQueryProvider(new PostgresChannelMessageStoreQueryProvider()); |
| 48 | + * |
| 49 | + * // Enable JSON serialization |
| 50 | + * store.setPreparedStatementSetter( |
| 51 | + * new JacksonChannelMessageStorePreparedStatementSetter("com.example")); |
| 52 | + * store.setMessageRowMapper( |
| 53 | + * new JacksonMessageRowMapper("com.example")); |
| 54 | + * |
| 55 | + * return store; |
| 56 | + * } |
| 57 | + * }</pre> |
| 58 | + * <p> |
| 59 | + * <b>Database Column Type:</b> |
| 60 | + * This implementation requires a text-based column type that supports JSON storage: |
| 61 | + * <ul> |
| 62 | + * <li>PostgreSQL: {@code JSONB} (recommended) or {@code JSON}</li> |
| 63 | + * <li>MySQL: {@code JSON} </li> |
| 64 | + * <li>H2: {@code CLOB} </li> |
| 65 | + * </ul> |
| 66 | + * <p> |
| 67 | + * <b>Note:</b> The standard Spring Integration schemas use {@code BLOB}/{@code BYTEA} columns |
| 68 | + * which are not suitable for human-readable JSON storage. Use the provided JSON-specific |
| 69 | + * schemas from {@code schema-*-json.sql} files. |
| 70 | + * |
| 71 | + * @author Yoobin Yoon |
| 72 | + * @since 7.0 |
| 73 | + * |
| 74 | + * @see JacksonMessageRowMapper |
| 75 | + * @see JacksonMessagingUtils#messagingAwareMapper(String...) |
| 76 | + */ |
| 77 | +public class JacksonChannelMessageStorePreparedStatementSetter extends ChannelMessageStorePreparedStatementSetter { |
| 78 | + |
| 79 | + private final ObjectMapper objectMapper; |
| 80 | + |
| 81 | + /** |
| 82 | + * Create a new {@link JacksonChannelMessageStorePreparedStatementSetter} with the |
| 83 | + * default trusted packages from {@link JacksonMessagingUtils#DEFAULT_TRUSTED_PACKAGES}. |
| 84 | + * <p> |
| 85 | + * This constructor is suitable when you only need to serialize/deserialize |
| 86 | + * standard Spring Integration and Java classes. |
| 87 | + */ |
| 88 | + public JacksonChannelMessageStorePreparedStatementSetter() { |
| 89 | + this(new String[0]); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Create a new {@link JacksonChannelMessageStorePreparedStatementSetter} with |
| 94 | + * additional trusted packages for deserialization. |
| 95 | + * <p> |
| 96 | + * The provided packages are appended to the default allow-list from |
| 97 | + * {@link JacksonMessagingUtils#DEFAULT_TRUSTED_PACKAGES}, enabling deserialization |
| 98 | + * of custom payload types. |
| 99 | + * <p> |
| 100 | + * <b>Package Matching:</b> |
| 101 | + * <ul> |
| 102 | + * <li>{@code "com.example"} – trusts {@code com.example} and all subpackages |
| 103 | + * (e.g., {@code com.example.MyClass}, {@code com.example.sub.AnotherClass})</li> |
| 104 | + * <li>{@code "com.example.MyClass"} – trusts only the specific class</li> |
| 105 | + * <li>{@code "*"} – trusts all packages (not recommended for production)</li> |
| 106 | + * </ul> |
| 107 | + * <p> |
| 108 | + * <b>Note:</b> Subpackages are automatically included without wildcards. |
| 109 | + * |
| 110 | + * @param trustedPackages the additional packages to trust for deserialization |
| 111 | + */ |
| 112 | + public JacksonChannelMessageStorePreparedStatementSetter(String... trustedPackages) { |
| 113 | + super(); // Call protected constructor - no serializer needed |
| 114 | + this.objectMapper = JacksonMessagingUtils.messagingAwareMapper(trustedPackages); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Create a new {@link JacksonChannelMessageStorePreparedStatementSetter} with |
| 119 | + * a custom {@link ObjectMapper}. |
| 120 | + * <p> |
| 121 | + * This constructor allows full control over the JSON serialization configuration. |
| 122 | + * The provided mapper should be configured appropriately for Message serialization, |
| 123 | + * typically using {@link JacksonMessagingUtils#messagingAwareMapper(String...)}. |
| 124 | + * @param objectMapper the {@link ObjectMapper} to use for JSON serialization |
| 125 | + */ |
| 126 | + public JacksonChannelMessageStorePreparedStatementSetter(ObjectMapper objectMapper) { |
| 127 | + super(); // Call protected constructor - no serializer needed |
| 128 | + Assert.notNull(objectMapper, "'objectMapper' must not be null"); |
| 129 | + this.objectMapper = objectMapper; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Set the prepared statement values, serializing the message to JSON format. |
| 134 | + * <p> |
| 135 | + * This implementation: |
| 136 | + * <ol> |
| 137 | + * <li>Calls {@code super.setValues()} to populate common fields (parameters 1-5)</li> |
| 138 | + * <li>Serializes the entire {@link Message} (payload and headers) to JSON</li> |
| 139 | + * <li>Sets parameter 6 using a database-specific method: |
| 140 | + * <ul> |
| 141 | + * <li><b>PostgreSQL:</b> {@link PreparedStatement#setObject(int, Object, int)} |
| 142 | + * with {@link Types#OTHER} for automatic JSONB/JSON conversion</li> |
| 143 | + * <li><b>Other databases:</b> {@link PreparedStatement#setString(int, String)} |
| 144 | + * for VARCHAR, CLOB, or JSON columns</li> |
| 145 | + * </ul> |
| 146 | + * </li> |
| 147 | + * </ol> |
| 148 | + * <p> |
| 149 | + * The resulting JSON includes {@code @class} properties for type information, |
| 150 | + * enabling proper deserialization of polymorphic types. |
| 151 | + * |
| 152 | + * @param preparedStatement the {@link PreparedStatement} to populate |
| 153 | + * @param requestMessage the {@link Message} to store |
| 154 | + * @param groupId the group identifier |
| 155 | + * @param region the region in the target table |
| 156 | + * @param priorityEnabled whether message priority should be stored |
| 157 | + * @throws SQLException if statement preparation or JSON serialization fails |
| 158 | + */ |
| 159 | + @Override |
| 160 | + public void setValues(PreparedStatement preparedStatement, Message<?> requestMessage, Object groupId, |
| 161 | + String region, boolean priorityEnabled) throws SQLException { |
| 162 | + |
| 163 | + super.setValues(preparedStatement, requestMessage, groupId, region, priorityEnabled); |
| 164 | + |
| 165 | + try { |
| 166 | + String json = this.objectMapper.writeValueAsString(requestMessage); |
| 167 | + |
| 168 | + String dbProduct = preparedStatement.getConnection().getMetaData().getDatabaseProductName(); |
| 169 | + |
| 170 | + if ("PostgreSQL".equalsIgnoreCase(dbProduct)) { |
| 171 | + preparedStatement.setObject(6, json, Types.OTHER); // NOSONAR magic number |
| 172 | + } |
| 173 | + else { |
| 174 | + preparedStatement.setString(6, json); // NOSONAR magic number |
| 175 | + } |
| 176 | + } |
| 177 | + catch (JacksonException ex) { |
| 178 | + throw new SQLException("Failed to serialize message to JSON. Message ID: " + |
| 179 | + requestMessage.getHeaders().getId(), ex); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments