This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 4d351eda81b663973b348630948069988077d645 Author: Rene Cordier <[email protected]> AuthorDate: Thu Aug 29 17:08:04 2019 +0700 JAMES-2868 Add MailAddress serialization --- .../mock/smtp/server/jackson/JsonSimpleModule.java | 26 ++++++++++ .../server/jackson/MailAddressDeserializer.java | 47 ++++++++++++++++++ .../smtp/server/jackson/MailAddressModule.java | 40 ++++++++++++++++ .../smtp/server/jackson/MailAddressSerializer.java | 35 ++++++++++++++ .../smtp/server/jackson/MailAddressModuleTest.java | 55 ++++++++++++++++++++++ 5 files changed, 203 insertions(+) diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/JsonSimpleModule.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/JsonSimpleModule.java new file mode 100644 index 0000000..4e00dc2 --- /dev/null +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/JsonSimpleModule.java @@ -0,0 +1,26 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mock.smtp.server.jackson; + +import com.fasterxml.jackson.databind.Module; + +public interface JsonSimpleModule { + Module asJacksonModule(); +} diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressDeserializer.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressDeserializer.java new file mode 100644 index 0000000..b2bdee7 --- /dev/null +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressDeserializer.java @@ -0,0 +1,47 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mock.smtp.server.jackson; + +import java.io.IOException; + +import javax.mail.internet.AddressException; + +import org.apache.james.core.MailAddress; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +public class MailAddressDeserializer extends JsonDeserializer<MailAddress> { + @Override + public MailAddress deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + String value = jsonParser.getValueAsString(); + try { + return new MailAddress(value); + } catch (AddressException exception) { + throw new JsonParseException( + jsonParser, + String.format("Something went wrong when trying to parse %s into a mail address", value), + exception); + } + } +} diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressModule.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressModule.java new file mode 100644 index 0000000..9ecec8a --- /dev/null +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressModule.java @@ -0,0 +1,40 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mock.smtp.server.jackson; + +import org.apache.james.core.MailAddress; + +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.module.SimpleModule; + +public class MailAddressModule implements JsonSimpleModule { + private final SimpleModule simpleModule; + + public MailAddressModule() { + this.simpleModule = new SimpleModule() + .addSerializer(MailAddress.class, new MailAddressSerializer()) + .addDeserializer(MailAddress.class, new MailAddressDeserializer()); + } + + @Override + public Module asJacksonModule() { + return simpleModule; + } +} diff --git a/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressSerializer.java b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressSerializer.java new file mode 100644 index 0000000..b5b5400 --- /dev/null +++ b/server/mailet/mock-smtp-server/src/main/java/org/apache/james/mock/smtp/server/jackson/MailAddressSerializer.java @@ -0,0 +1,35 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mock.smtp.server.jackson; + +import java.io.IOException; + +import org.apache.james.core.MailAddress; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +public class MailAddressSerializer extends JsonSerializer<MailAddress> { + @Override + public void serialize(MailAddress mailAddress, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + jsonGenerator.writeString(mailAddress.asString()); + } +} diff --git a/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/jackson/MailAddressModuleTest.java b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/jackson/MailAddressModuleTest.java new file mode 100644 index 0000000..8c44504 --- /dev/null +++ b/server/mailet/mock-smtp-server/src/test/java/org/apache/james/mock/smtp/server/jackson/MailAddressModuleTest.java @@ -0,0 +1,55 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mock.smtp.server.jackson; + +import static org.apache.james.mock.smtp.server.Fixture.BOB; +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.james.core.MailAddress; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; + +class MailAddressModuleTest { + private static final String BOB_JSON = "\"[email protected]\""; + + private ObjectMapper mapper; + + @BeforeEach + void setup() { + mapper = new ObjectMapper() + .registerModule(new MailAddressModule().asJacksonModule()); + } + + @Test + void mailAddressShouldBeSerializable() throws Exception { + MailAddress address = new MailAddress(BOB); + String actualJson = mapper.writeValueAsString(address); + assertThat(actualJson).isEqualTo(BOB_JSON); + } + + @Test + void mailAddressShouldBeDeserializable() throws Exception { + MailAddress expectedAddress = new MailAddress(BOB); + MailAddress actualAddress = mapper.readValue(BOB_JSON, MailAddress.class); + assertThat(actualAddress).isEqualToComparingFieldByField(expectedAddress); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
