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 18858cdc219e96839a024007b69ffa70248999cd Author: Rémi Kowalski <rkowal...@linagora.com> AuthorDate: Mon Sep 2 14:32:47 2019 +0200 JAMES-2813 extract ReprocessingOneMailTaskDTO --- .../webadmin/service/ReprocessingOneMailTask.java | 106 ++++--------------- .../service/ReprocessingOneMailTaskDTO.java | 113 +++++++++++++++++++++ .../service/ReprocessingOneMailTaskTest.java | 6 +- 3 files changed, 133 insertions(+), 92 deletions(-) diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingOneMailTask.java b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingOneMailTask.java index dbffedc..b4e260b 100644 --- a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingOneMailTask.java +++ b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingOneMailTask.java @@ -20,21 +20,16 @@ package org.apache.james.webadmin.service; import java.util.Optional; -import java.util.function.Function; + import javax.mail.MessagingException; -import org.apache.james.json.DTOModule; import org.apache.james.mailrepository.api.MailKey; import org.apache.james.mailrepository.api.MailRepositoryPath; import org.apache.james.mailrepository.api.MailRepositoryStore; -import org.apache.james.server.task.json.dto.TaskDTO; -import org.apache.james.server.task.json.dto.TaskDTOModule; import org.apache.james.task.Task; import org.apache.james.task.TaskExecutionDetails; import org.apache.james.task.TaskType; -import com.fasterxml.jackson.annotation.JsonProperty; - public class ReprocessingOneMailTask implements Task { public static final TaskType TYPE = TaskType.of("reprocessingOneTask"); @@ -83,89 +78,6 @@ public class ReprocessingOneMailTask implements Task { } } - private static class ReprocessingOneMailTaskDTO implements TaskDTO { - - public static ReprocessingOneMailTaskDTO toDTO(ReprocessingOneMailTask domainObject, String typeName) { - try { - return new ReprocessingOneMailTaskDTO( - typeName, - domainObject.repositoryPath.urlEncoded(), - domainObject.targetQueue, - domainObject.mailKey.asString(), - domainObject.targetProcessor - ); - } catch (Exception e) { - throw new UrlEncodingFailureSerializationException(domainObject.repositoryPath); - } - } - - private final String type; - private final String repositoryPath; - private final String targetQueue; - private final String mailKey; - private final Optional<String> targetProcessor; - - public ReprocessingOneMailTaskDTO(@JsonProperty("type") String type, - @JsonProperty("repositoryPath") String repositoryPath, - @JsonProperty("targetQueue") String targetQueue, - @JsonProperty("mailKey") String mailKey, - @JsonProperty("targetProcessor") Optional<String> targetProcessor) { - this.type = type; - this.repositoryPath = repositoryPath; - this.mailKey = mailKey; - this.targetQueue = targetQueue; - this.targetProcessor = targetProcessor; - } - - public ReprocessingOneMailTask fromDTO(ReprocessingService reprocessingService) { - return new ReprocessingOneMailTask( - reprocessingService, - getMailRepositoryPath(), - targetQueue, - new MailKey(mailKey), - targetProcessor - ); - } - - private MailRepositoryPath getMailRepositoryPath() { - try { - return MailRepositoryPath.fromEncoded(repositoryPath); - } catch (Exception e) { - throw new InvalidMailRepositoryPathDeserializationException(repositoryPath); - } - } - - @Override - public String getType() { - return type; - } - - public String getRepositoryPath() { - return repositoryPath; - } - - public String getMailKey() { - return mailKey; - } - - public String getTargetQueue() { - return targetQueue; - } - - public Optional<String> getTargetProcessor() { - return targetProcessor; - } - } - - public static final Function<ReprocessingService, TaskDTOModule<ReprocessingOneMailTask, ReprocessingOneMailTaskDTO>> MODULE = (reprocessingService) -> - DTOModule - .forDomainObject(ReprocessingOneMailTask.class) - .convertToDTO(ReprocessingOneMailTaskDTO.class) - .toDomainObjectConverter(dto -> dto.fromDTO(reprocessingService)) - .toDTOConverter(ReprocessingOneMailTaskDTO::toDTO) - .typeName(TYPE.asString()) - .withFactory(TaskDTOModule::new); - private final ReprocessingService reprocessingService; private final MailRepositoryPath repositoryPath; private final String targetQueue; @@ -199,6 +111,22 @@ public class ReprocessingOneMailTask implements Task { return TYPE; } + MailRepositoryPath getRepositoryPath() { + return repositoryPath; + } + + String getTargetQueue() { + return targetQueue; + } + + MailKey getMailKey() { + return mailKey; + } + + Optional<String> getTargetProcessor() { + return targetProcessor; + } + @Override public Optional<TaskExecutionDetails.AdditionalInformation> details() { return Optional.of(additionalInformation); diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingOneMailTaskDTO.java b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingOneMailTaskDTO.java new file mode 100644 index 0000000..364eb8f --- /dev/null +++ b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/ReprocessingOneMailTaskDTO.java @@ -0,0 +1,113 @@ +/**************************************************************** + * 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.webadmin.service; + +import java.util.Optional; +import java.util.function.Function; + +import org.apache.james.json.DTOModule; +import org.apache.james.mailrepository.api.MailKey; +import org.apache.james.mailrepository.api.MailRepositoryPath; +import org.apache.james.server.task.json.dto.TaskDTO; +import org.apache.james.server.task.json.dto.TaskDTOModule; + +import com.fasterxml.jackson.annotation.JsonProperty; + +class ReprocessingOneMailTaskDTO implements TaskDTO { + + public static final Function<ReprocessingService, TaskDTOModule<ReprocessingOneMailTask, ReprocessingOneMailTaskDTO>> MODULE = (reprocessingService) -> + DTOModule + .forDomainObject(ReprocessingOneMailTask.class) + .convertToDTO(ReprocessingOneMailTaskDTO.class) + .toDomainObjectConverter(dto -> dto.fromDTO(reprocessingService)) + .toDTOConverter(ReprocessingOneMailTaskDTO::toDTO) + .typeName(ReprocessingOneMailTask.TYPE.asString()) + .withFactory(TaskDTOModule::new); + + public static ReprocessingOneMailTaskDTO toDTO(ReprocessingOneMailTask domainObject, String typeName) { + try { + return new ReprocessingOneMailTaskDTO( + typeName, + domainObject.getRepositoryPath().urlEncoded(), + domainObject.getTargetQueue(), + domainObject.getMailKey().asString(), + domainObject.getTargetProcessor() + ); + } catch (Exception e) { + throw new ReprocessingOneMailTask.UrlEncodingFailureSerializationException(domainObject.getRepositoryPath()); + } + } + + private final String type; + private final String repositoryPath; + private final String targetQueue; + private final String mailKey; + private final Optional<String> targetProcessor; + + public ReprocessingOneMailTaskDTO(@JsonProperty("type") String type, + @JsonProperty("repositoryPath") String repositoryPath, + @JsonProperty("targetQueue") String targetQueue, + @JsonProperty("mailKey") String mailKey, + @JsonProperty("targetProcessor") Optional<String> targetProcessor) { + this.type = type; + this.repositoryPath = repositoryPath; + this.mailKey = mailKey; + this.targetQueue = targetQueue; + this.targetProcessor = targetProcessor; + } + + public ReprocessingOneMailTask fromDTO(ReprocessingService reprocessingService) { + return new ReprocessingOneMailTask( + reprocessingService, + getMailRepositoryPath(), + targetQueue, + new MailKey(mailKey), + targetProcessor + ); + } + + private MailRepositoryPath getMailRepositoryPath() { + try { + return MailRepositoryPath.fromEncoded(repositoryPath); + } catch (Exception e) { + throw new ReprocessingOneMailTask.InvalidMailRepositoryPathDeserializationException(repositoryPath); + } + } + + @Override + public String getType() { + return type; + } + + public String getRepositoryPath() { + return repositoryPath; + } + + public String getMailKey() { + return mailKey; + } + + public String getTargetQueue() { + return targetQueue; + } + + public Optional<String> getTargetProcessor() { + return targetProcessor; + } +} diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ReprocessingOneMailTaskTest.java b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ReprocessingOneMailTaskTest.java index 45cd75a..af25067 100644 --- a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ReprocessingOneMailTaskTest.java +++ b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/ReprocessingOneMailTaskTest.java @@ -48,7 +48,7 @@ class ReprocessingOneMailTaskTest { MailKey mailKey, Optional<String> targetProcessor, String serialized) throws JsonProcessingException { - JsonTaskSerializer testee = new JsonTaskSerializer(ReprocessingOneMailTask.MODULE.apply(REPROCESSING_SERVICE)); + JsonTaskSerializer testee = new JsonTaskSerializer(ReprocessingOneMailTaskDTO.MODULE.apply(REPROCESSING_SERVICE)); ReprocessingOneMailTask task = new ReprocessingOneMailTask(REPROCESSING_SERVICE, repositoryPath, targetQueue, mailKey, targetProcessor); JsonAssertions.assertThatJson(testee.serialize(task)) .isEqualTo(serialized); @@ -65,7 +65,7 @@ class ReprocessingOneMailTaskTest { MailKey mailKey, Optional<String> targetProcessor, String serialized) throws IOException { - JsonTaskSerializer testee = new JsonTaskSerializer(ReprocessingOneMailTask.MODULE.apply(REPROCESSING_SERVICE)); + JsonTaskSerializer testee = new JsonTaskSerializer(ReprocessingOneMailTaskDTO.MODULE.apply(REPROCESSING_SERVICE)); ReprocessingOneMailTask task = new ReprocessingOneMailTask(REPROCESSING_SERVICE, repositoryPath, targetQueue, mailKey, targetProcessor); assertThat(testee.deserialize(serialized)) @@ -86,7 +86,7 @@ class ReprocessingOneMailTaskTest { @ParameterizedTest @ValueSource(strings = {"{\"type\":\"reprocessingOneTask\",\"repositoryPath\":\"%\",\"targetQueue\":\"queue\",\"mailKey\": \"myMail\",\"targetProcessor\":\"targetProcessor\"}", "{\"type\":\"reprocessingOneTask\",\"repositoryPath\":\"%\",\"targetQueue\":\"queue\",\"mailKey\": \"myMail\"}"}) void taskShouldThrowOnDeserializationUrlDecodingError(String serialized) { - JsonTaskSerializer testee = new JsonTaskSerializer(ReprocessingOneMailTask.MODULE.apply(REPROCESSING_SERVICE)); + JsonTaskSerializer testee = new JsonTaskSerializer(ReprocessingOneMailTaskDTO.MODULE.apply(REPROCESSING_SERVICE)); assertThatThrownBy(() -> testee.deserialize(serialized)) .isInstanceOf(ReprocessingOneMailTask.InvalidMailRepositoryPathDeserializationException.class); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org