1 package org.kuali.common.util.ssh.model;
2
3 import org.kuali.common.util.Assert;
4 import org.kuali.common.util.spring.env.EnvUtils;
5 import org.kuali.common.util.spring.env.EnvironmentService;
6
7 import com.google.common.base.Optional;
8
9 public final class GenerateKeyPairContext {
10
11 private final String name;
12 private final Algorithm algorithm;
13 private final int size;
14
15 public static class Builder {
16
17
18 private final String name;
19
20
21 private Algorithm algorithm = Algorithm.RSA;
22 private int size = 2048;
23
24 private Optional<EnvironmentService> env = EnvUtils.ABSENT;
25 private static final String NAME_KEY = "ssh.keyName";
26 private static final String ALGORITHM_KEY = "ssh.algorithm";
27 private static final String SIZE_KEY = "ssh.keySize";
28
29 public Builder(String name) {
30 this(EnvUtils.ABSENT, name);
31 }
32
33 public Builder(EnvironmentService env, String name) {
34 this(Optional.of(env), name);
35 }
36
37 private Builder(Optional<EnvironmentService> env, String name) {
38 this.env = env;
39 if (env.isPresent()) {
40 this.name = env.get().getString(NAME_KEY, name);
41 } else {
42 this.name = name;
43 }
44 }
45
46 public Builder algorithm(Algorithm algorithm) {
47 this.algorithm = algorithm;
48 return this;
49 }
50
51 public Builder size(int size) {
52 this.size = size;
53 return this;
54 }
55
56 private void validate(GenerateKeyPairContext context) {
57 Assert.noBlanks(context.getName());
58 Assert.positive(context.getSize());
59 }
60
61 private void override() {
62 if (env.isPresent()) {
63 algorithm(env.get().getProperty(ALGORITHM_KEY, Algorithm.class, algorithm));
64 size(env.get().getInteger(SIZE_KEY, size));
65 }
66 }
67
68 public GenerateKeyPairContext build() {
69 override();
70 GenerateKeyPairContext context = new GenerateKeyPairContext(this);
71 validate(context);
72 return context;
73 }
74
75 }
76
77 private GenerateKeyPairContext(Builder builder) {
78 this.name = builder.name;
79 this.algorithm = builder.algorithm;
80 this.size = builder.size;
81 }
82
83 public String getName() {
84 return name;
85 }
86
87 public Algorithm getAlgorithm() {
88 return algorithm;
89 }
90
91 public int getSize() {
92 return size;
93 }
94
95 }