BrightSide Workbench Full Report + Source Code
MailProviderMigration.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.mail.provider;
20 
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import org.turro.collections.KeyValueMap;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.context.HeadlessApplication;
28 import org.turro.elephant.impl.context.ContextFactory;
29 import org.turro.mail.impl.MailMessagePool;
30 import org.turro.mail.impl.MailPool;
31 import org.turro.string.Strings;
32 
37 public class MailProviderMigration {
38 
39  public static MailProviderSet migrate() {
40  Set<String> ids = new HashSet<>();
41  MailProviderSet set = new MailProviderSet();
42  List<String> names = new ArrayList<>();
43  names.add("IMailMessagePool");
44  names.addAll(ContextFactory.getImplementationNamesByType("IMailMessagePool"));
45  names.forEach((s) -> {
47  MailProvider provider = new MailProvider();
48  KeyValueMap attrs = pool.getAttributes();
49  String id = ((String) s).substring(((String) s).indexOf("_") + 1);
50  if("IMailMessagePool".equals(id)) id = MailProviders.GENERIC;
51  if(!ids.contains(id)) {
52  provider.setId(id);
53  provider.setHostname(attrs.get("mailhost"));
54  provider.setPort(attrs.get(Integer.class, "port", null));
55  provider.setTls(attrs.get(Boolean.class, "tls", false));
56  provider.setSsl(attrs.get(Boolean.class, "ssl", false));
57  provider.setUser(attrs.get(String.class, "user", null));
58  provider.setPassword(attrs.get(String.class, "cryptpass", null));
59  if(Strings.isBlank(provider.getPassword())) {
60  provider.setPassword(ElephantContext.encrypt(attrs.get(String.class, "password", null)));
61  }
62  provider.setFrom(MailRecipient.of(attrs.get(String.class, "from", null)));
63  provider.clearEmpties();
64  set.addProvider(provider);
65  ids.add(id);
66  }
67  });
68  ContextFactory.getImplementationNamesByType("IMailPool").forEach((s) -> {
70  MailProvider provider = new MailProvider();
71  KeyValueMap attrs = pool.getAttributes();
72  String id = ((String) s).substring(((String) s).indexOf("_") + 1);
73  if(!ids.contains(id)) {
74  provider.setId(id);
75  provider.setHostname(attrs.get("mailhost"));
76  provider.setPort(attrs.get(Integer.class, "port", null));
77  provider.setTls(attrs.get(Boolean.class, "tls", false));
78  provider.setSsl(attrs.get(Boolean.class, "ssl", false));
79  provider.setUser(attrs.get(String.class, "user", null));
80  provider.setPassword(attrs.get(String.class, "cryptpass", null));
81  if(Strings.isBlank(provider.getPassword())) {
82  provider.setPassword(ElephantContext.encrypt(attrs.get(String.class, "password", null)));
83  }
84  provider.setFrom(MailRecipient.of(attrs.get(String.class, "from", null)));
85  provider.clearEmpties();
86  set.addProvider(provider);
87  ids.add(id);
88  }
89  });
90  return set;
91  }
92 
93 }
IImplementation getImplementation(String name)
void setFrom(MailRecipient from)
static MailRecipient of(String value)