|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.dubbo.spring.security.jackson; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 22 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 23 | +import org.apache.dubbo.common.utils.ClassUtils; |
| 24 | +import org.apache.dubbo.common.utils.StringUtils; |
| 25 | +import org.springframework.security.jackson2.CoreJackson2Module; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.function.Consumer; |
| 30 | + |
| 31 | +public class ObjectMapperCodec { |
| 32 | + |
| 33 | + private ObjectMapper mapper = new ObjectMapper(); |
| 34 | + |
| 35 | + public ObjectMapperCodec() { |
| 36 | + registerDefaultModule(); |
| 37 | + } |
| 38 | + |
| 39 | + public <T> T deserialize(byte[] bytes, Class<T> clazz) { |
| 40 | + try { |
| 41 | + |
| 42 | + if (bytes == null || bytes.length == 0) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + return mapper.readValue(bytes, clazz); |
| 47 | + |
| 48 | + } catch (Exception exception) { |
| 49 | + throw new RuntimeException( |
| 50 | + String.format("objectMapper! deserialize error %s", exception)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public <T> T deserialize(String content, Class<T> clazz) { |
| 55 | + if (StringUtils.isBlank(content)) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + return deserialize(content.getBytes(), clazz); |
| 59 | + } |
| 60 | + |
| 61 | + public String serialize(Object object) { |
| 62 | + try { |
| 63 | + |
| 64 | + if (object == null) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + return mapper.writeValueAsString(object); |
| 69 | + |
| 70 | + } catch (Exception ex) { |
| 71 | + throw new RuntimeException(String.format("objectMapper! serialize error %s", ex)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public ObjectMapperCodec addModule(SimpleModule simpleModule) { |
| 76 | + mapper.registerModule(simpleModule); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + public ObjectMapperCodec configureMapper(Consumer<ObjectMapper> objectMapperConfigure) { |
| 81 | + objectMapperConfigure.accept(this.mapper); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + private void registerDefaultModule() { |
| 86 | + mapper.registerModule(new CoreJackson2Module()); |
| 87 | + mapper.registerModule(new JavaTimeModule()); |
| 88 | + |
| 89 | + List<String> jacksonModuleClassNameList = new ArrayList<>(); |
| 90 | + jacksonModuleClassNameList.add("org.springframework.security.oauth2.server.authorization.jackson2.OAuth2AuthorizationServerJackson2Module"); |
| 91 | + jacksonModuleClassNameList.add("org.springframework.security.oauth2.client.jackson2.OAuth2ClientJackson2Module"); |
| 92 | + jacksonModuleClassNameList.add("org.springframework.security.web.server.jackson2.WebServerJackson2Module"); |
| 93 | + jacksonModuleClassNameList.add("com.fasterxml.jackson.module.paramnames.ParameterNamesModule"); |
| 94 | + jacksonModuleClassNameList.add("org.springframework.security.web.jackson2.WebServletJackson2Module"); |
| 95 | + jacksonModuleClassNameList.add("org.springframework.security.web.jackson2.WebJackson2Module"); |
| 96 | + jacksonModuleClassNameList.add("org.springframework.boot.jackson.JsonMixinModule"); |
| 97 | + jacksonModuleClassNameList.add("org.springframework.security.ldap.jackson2.LdapJackson2Module"); |
| 98 | + loadModuleIfPresent(jacksonModuleClassNameList); |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + private void loadModuleIfPresent(List<String> jacksonModuleClassNameList) { |
| 103 | + for (String moduleClassName : jacksonModuleClassNameList) { |
| 104 | + try { |
| 105 | + SimpleModule objectMapperModule = (SimpleModule) ClassUtils.forName(moduleClassName, |
| 106 | + ObjectMapperCodec.class.getClassLoader()).newInstance(); |
| 107 | + mapper.registerModule(objectMapperModule); |
| 108 | + |
| 109 | + } catch (Throwable ex) { |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments