Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit a857478

Browse files
Merge pull request #95 from Venafi/SonarCloud-Duplications
Avoiding duplicated code in Connector classes
2 parents 193c1f9 + c3e5f8f commit a857478

7 files changed

Lines changed: 209 additions & 937 deletions

File tree

src/main/java/com/venafi/vcert/sdk/VCertClient.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,36 @@
2929

3030
public class VCertClient implements Connector {
3131

32-
private Connector connector;
32+
protected Connector connector;
3333

3434
public VCertClient(Config config) throws VCertException {
3535
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
36-
switch (config.connectorType()) {
37-
case TPP:
38-
if (isBlank(config.baseUrl()))
39-
throw new VCertException("TPP client requires a base url");
40-
41-
connector = new TppConnector(Tpp.connect(config));
42-
break;
43-
44-
case CLOUD:
45-
connector = new CloudConnector(Cloud.connect(config));
46-
break;
47-
default:
48-
throw new VCertException("ConnectorType is not defined");
49-
}
36+
37+
this.connector = createConnector(config);
5038

5139
connector.setVendorAndProductName(isBlank(config.appInfo()) ? VCertConstants.DEFAULT_VENDOR_AND_PRODUCT_NAME :
5240
config.appInfo());
5341
}
42+
43+
protected Connector createConnector(Config config) throws VCertException {
44+
Connector connector;
45+
switch (config.connectorType()) {
46+
case TPP:
47+
if (isBlank(config.baseUrl()))
48+
throw new VCertException("TPP client requires a base url");
49+
50+
connector = new TppConnector(Tpp.connect(config));
51+
break;
52+
53+
case CLOUD:
54+
connector = new CloudConnector(Cloud.connect(config));
55+
break;
56+
default:
57+
throw new VCertException("ConnectorType is not defined");
58+
}
59+
60+
return connector;
61+
}
5462

5563
@VisibleForTesting
5664
VCertClient(Connector connector) {
Lines changed: 26 additions & 251 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,47 @@
11
package com.venafi.vcert.sdk;
22

3-
import static org.apache.commons.lang3.StringUtils.isBlank;
4-
5-
import java.security.Security;
6-
73
import com.google.common.annotations.VisibleForTesting;
84

9-
import com.venafi.vcert.sdk.policy.domain.PolicySpecification;
105
import feign.FeignException;
116

12-
import com.venafi.vcert.sdk.certificate.CertificateRequest;
13-
import com.venafi.vcert.sdk.certificate.ImportRequest;
14-
import com.venafi.vcert.sdk.certificate.ImportResponse;
15-
import com.venafi.vcert.sdk.certificate.PEMCollection;
16-
import com.venafi.vcert.sdk.certificate.RenewalRequest;
17-
import com.venafi.vcert.sdk.certificate.RevocationRequest;
18-
import com.venafi.vcert.sdk.certificate.SshCaTemplateRequest;
19-
import com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails;
20-
import com.venafi.vcert.sdk.certificate.SshCertificateRequest;
21-
import com.venafi.vcert.sdk.certificate.SshConfig;
22-
import com.venafi.vcert.sdk.connectors.Policy;
7+
import com.venafi.vcert.sdk.connectors.Connector;
238
import com.venafi.vcert.sdk.connectors.TokenConnector;
24-
import com.venafi.vcert.sdk.connectors.ZoneConfiguration;
259
import com.venafi.vcert.sdk.connectors.tpp.TokenInfo;
2610
import com.venafi.vcert.sdk.connectors.tpp.Tpp;
2711
import com.venafi.vcert.sdk.connectors.tpp.TppTokenConnector;
2812
import com.venafi.vcert.sdk.endpoint.Authentication;
29-
import com.venafi.vcert.sdk.endpoint.ConnectorType;
30-
import com.venafi.vcert.sdk.utils.VCertConstants;
31-
32-
33-
public class VCertTknClient implements TokenConnector {
3413

35-
private TokenConnector connector;
14+
public class VCertTknClient extends VCertClient implements TokenConnector {
3615

3716
public VCertTknClient(Config config) throws VCertException {
38-
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
39-
switch (config.connectorType()) {
40-
case TPP_TOKEN:{
41-
connector = new TppTokenConnector(Tpp.connect(config));
42-
((TppTokenConnector) connector).credentials(config.credentials());
43-
break;
44-
}
45-
default:
46-
throw new VCertException("ConnectorType is not defined");
47-
}
48-
connector.setVendorAndProductName(isBlank(config.appInfo()) ? VCertConstants.DEFAULT_VENDOR_AND_PRODUCT_NAME :
49-
config.appInfo());
50-
}
51-
52-
@VisibleForTesting
53-
VCertTknClient(TokenConnector connector) {
54-
this.connector = connector;
17+
super(config);
5518
}
5619

57-
/**
58-
* {@inheritDoc}
59-
*/
6020
@Override
61-
public ConnectorType getType() {
62-
return connector.getType();
63-
}
64-
65-
/**
66-
* Method not implemented yet.
67-
* Guaranteed to throw an exception.
68-
*
69-
* @throws UnsupportedOperationException always
70-
*/
71-
@Override
72-
public void setBaseUrl(String url) throws VCertException {
73-
connector.setBaseUrl(url);
74-
}
21+
protected Connector createConnector(Config config) throws VCertException {
22+
Connector connector;
23+
switch (config.connectorType()) {
24+
case TPP_TOKEN:{
25+
connector = new TppTokenConnector(Tpp.connect(config));
26+
((TppTokenConnector) connector).credentials(config.credentials());
27+
break;
28+
}
29+
default:
30+
throw new VCertException("ConnectorType is not defined");
31+
}
7532

76-
/**
77-
* {@inheritDoc}
78-
*/
79-
@Override
80-
public void setZone(String zone) {
81-
connector.setZone(zone);
33+
return connector;
8234
}
8335

84-
/**
85-
* {@inheritDoc}
86-
*/
87-
@Override
88-
public void setVendorAndProductName(String vendorAndProductName) {
89-
connector.setVendorAndProductName(vendorAndProductName);
36+
@VisibleForTesting
37+
VCertTknClient(TokenConnector connector) {
38+
super(connector);
9039
}
9140

92-
/**
93-
* {@inheritDoc}
94-
*/
95-
@Override
96-
public String getVendorAndProductName() {
97-
return connector.getVendorAndProductName();
98-
}
41+
@Override
42+
public TokenInfo getTokenInfo() throws VCertException {
43+
return ((TokenConnector)connector).getTokenInfo();
44+
}
9945

10046
//=========================================================================================\\
10147
//=============================== VENAFI 20.2 OAUTH METHODS ===============================\\
@@ -104,7 +50,7 @@ public String getVendorAndProductName() {
10450
@Override
10551
public TokenInfo getAccessToken(Authentication auth) throws VCertException{
10652
try {
107-
return connector.getAccessToken(auth);
53+
return ((TokenConnector)connector).getAccessToken(auth);
10854
} catch (FeignException e) {
10955
throw VCertException.fromFeignException(e);
11056
}
@@ -113,190 +59,19 @@ public TokenInfo getAccessToken(Authentication auth) throws VCertException{
11359
@Override
11460
public TokenInfo getAccessToken() throws VCertException{
11561
try {
116-
return connector.getAccessToken();
62+
return ((TokenConnector)connector).getAccessToken();
11763
} catch (FeignException e) {
11864
throw VCertException.fromFeignException(e);
11965
}
12066
}
12167

12268
@Override
12369
public TokenInfo refreshAccessToken(String applicationId) throws VCertException {
124-
return connector.refreshAccessToken(applicationId);
70+
return ((TokenConnector)connector).refreshAccessToken(applicationId);
12571
}
12672

12773
@Override
12874
public int revokeAccessToken() throws VCertException {
129-
return connector.revokeAccessToken();
130-
}
131-
132-
/**
133-
* {@inheritDoc}
134-
*/
135-
@Override
136-
public void ping() throws VCertException {
137-
try {
138-
connector.ping();
139-
} catch (FeignException e) {
140-
throw VCertException.fromFeignException(e);
141-
}
142-
}
143-
144-
/**
145-
* {@inheritDoc}
146-
*/
147-
@Override
148-
public ZoneConfiguration readZoneConfiguration(String zone) throws VCertException {
149-
try {
150-
return connector.readZoneConfiguration(zone);
151-
} catch (FeignException e) {
152-
throw VCertException.fromFeignException(e);
153-
}
154-
}
155-
156-
/**
157-
* {@inheritDoc}
158-
*/
159-
@Override
160-
public CertificateRequest generateRequest(ZoneConfiguration config, CertificateRequest request)
161-
throws VCertException {
162-
try {
163-
return connector.generateRequest(config, request);
164-
} catch (FeignException e) {
165-
throw VCertException.fromFeignException(e);
166-
}
167-
}
168-
169-
@Override
170-
public String requestCertificate(CertificateRequest request, String zone) throws VCertException {
171-
try {
172-
return connector.requestCertificate(request, zone);
173-
} catch (FeignException e) {
174-
throw VCertException.fromFeignException(e);
175-
}
176-
}
177-
178-
/**
179-
* {@inheritDoc}
180-
*/
181-
@Override
182-
public String requestCertificate(CertificateRequest request, ZoneConfiguration zoneConfiguration)
183-
throws VCertException {
184-
try {
185-
return connector.requestCertificate(request, zoneConfiguration);
186-
} catch (FeignException e) {
187-
throw VCertException.fromFeignException(e);
188-
}
189-
}
190-
191-
/**
192-
* {@inheritDoc}
193-
*/
194-
@Override
195-
public PEMCollection retrieveCertificate(CertificateRequest request) throws VCertException {
196-
try {
197-
return connector.retrieveCertificate(request);
198-
} catch (FeignException e) {
199-
throw VCertException.fromFeignException(e);
200-
}
201-
}
202-
203-
/**
204-
* {@inheritDoc}
205-
*/
206-
@Override
207-
public void revokeCertificate(RevocationRequest request) throws VCertException {
208-
try {
209-
connector.revokeCertificate(request);
210-
} catch (FeignException e) {
211-
throw VCertException.fromFeignException(e);
212-
}
213-
}
214-
215-
/**
216-
* {@inheritDoc}
217-
*/
218-
@Override
219-
public String renewCertificate(RenewalRequest request) throws VCertException {
220-
try {
221-
return connector.renewCertificate(request);
222-
} catch (FeignException e) {
223-
throw VCertException.fromFeignException(e);
224-
}
225-
}
226-
227-
/**
228-
* {@inheritDoc}
229-
*/
230-
@Override
231-
public ImportResponse importCertificate(ImportRequest request) throws VCertException {
232-
try {
233-
return connector.importCertificate(request);
234-
} catch (FeignException e) {
235-
throw VCertException.fromFeignException(e);
236-
}
237-
}
238-
239-
/**
240-
* {@inheritDoc}
241-
*/
242-
@Override
243-
public Policy readPolicyConfiguration(String zone) throws VCertException {
244-
try {
245-
return connector.readPolicyConfiguration(zone);
246-
} catch (FeignException e) {
247-
throw VCertException.fromFeignException(e);
248-
}
249-
}
250-
251-
/**
252-
* {@inheritDoc}
253-
*/
254-
@Override
255-
public void setPolicy(String policyName, PolicySpecification policySpecification) throws VCertException {
256-
try {
257-
connector.setPolicy(policyName, policySpecification);
258-
} catch (FeignException e) {
259-
throw VCertException.fromFeignException(e);
260-
}
261-
}
262-
263-
/**
264-
* {@inheritDoc}
265-
*/
266-
@Override
267-
public PolicySpecification getPolicy(String policyName) throws VCertException {
268-
try {
269-
return connector.getPolicy(policyName);
270-
} catch (FeignException e) {
271-
throw VCertException.fromFeignException(e);
272-
}
273-
}
274-
275-
@Override
276-
public String requestSshCertificate(SshCertificateRequest sshCertificateRequest) throws VCertException {
277-
try {
278-
return connector.requestSshCertificate(sshCertificateRequest);
279-
} catch (FeignException e) {
280-
throw VCertException.fromFeignException(e);
281-
}
282-
}
283-
284-
@Override
285-
public SshCertRetrieveDetails retrieveSshCertificate(SshCertificateRequest sshCertificateRequest)
286-
throws VCertException {
287-
try {
288-
return connector.retrieveSshCertificate(sshCertificateRequest);
289-
} catch (FeignException e) {
290-
throw VCertException.fromFeignException(e);
291-
}
292-
}
293-
294-
@Override
295-
public SshConfig retrieveSshConfig(SshCaTemplateRequest sshCaTemplateRequest) throws VCertException {
296-
try {
297-
return connector.retrieveSshConfig(sshCaTemplateRequest);
298-
} catch (FeignException e) {
299-
throw VCertException.fromFeignException(e);
300-
}
75+
return ((TokenConnector)connector).revokeAccessToken();
30176
}
30277
}

0 commit comments

Comments
 (0)