
com.ecfeed.core.webservice.client.GenWebServiceClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ecfeed.junit Show documentation
Show all versions of ecfeed.junit Show documentation
An open library used to connect to the ecFeed service. It can be also used as a standalone testing tool. It is integrated with Junit5 and generates a stream of test cases using a selected algorithm (e.g. Cartesian, N-Wise). There are no limitations associated with the off-line version but the user cannot access the on-line computation servers and the model database.
The newest version!
package com.ecfeed.core.webservice.client;
import com.ecfeed.core.utils.ExceptionHelper;
import com.ecfeed.core.utils.DiskPathHelper;
import javax.net.ssl.SSLContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.Optional;
public class GenWebServiceClient implements IWebServiceClient {
private static final String TAG_CLIENT_VERSION = "clientVersion";
private static final String TAG_CLIENT_TYPE = "clientType";
private static final String TAG_REQUEST_TYPE = "requestType";
private static final String fCommunicationProtocol = "TLSv1.2";
private static final String fClientVersion = "1.0";
private Client fClient;
private String fClientType;
private WebTarget fWebTarget;
public GenWebServiceClient(
String serverUrl,
String endpoint,
String clientType,
Optional keyStorePath) {
fClientType = clientType;
fClient = createClient(fCommunicationProtocol, keyStorePath);
String targetStr = DiskPathHelper.joinSubdirectory(serverUrl, endpoint);
fWebTarget = fClient.target(targetStr);
}
public static String getTestCasesEndPoint() {
return "testCaseService";
}
public static String getGenServiceVersionEndPoint() {
return "genServiceVersion";
}
@Override
public WebServiceResponse sendPostRequest(
String requestType, String requestJson) {
Response response = fWebTarget
.queryParam(TAG_CLIENT_TYPE, fClientType)
.queryParam(TAG_CLIENT_VERSION, fClientVersion)
.queryParam(TAG_REQUEST_TYPE, requestType)
.request()
.post(Entity.entity(requestJson, MediaType.APPLICATION_JSON));
int responseStatus = response.getStatus();
BufferedReader responseBufferedReader =
new BufferedReader(new InputStreamReader(response.readEntity(InputStream.class)));
return new WebServiceResponse(responseStatus, responseBufferedReader);
}
@Override
public WebServiceResponse sendGetRequest() {
Response response = fWebTarget
// .queryParam(TAG_CLIENT_TYPE, fClientType)
// .queryParam(TAG_CLIENT_VERSION, fClientVersion)
//.queryParam(TAG_REQUEST_TYPE, requestType)
.request()
.get();
int responseStatus = response.getStatus();
BufferedReader responseBufferedReader =
new BufferedReader(new InputStreamReader(response.readEntity(InputStream.class)));
return new WebServiceResponse(responseStatus, responseBufferedReader);
}
@Override
public void close() {
if (fClient != null) {
fClient.close();
System.out.println(LocalDateTime.now().toString() + " Remote connection closed");
fClient = null;
}
}
private static Client createClient(String communicationProtocol, Optional keyStorePath) {
ClientBuilder client = ClientBuilder.newBuilder();
client.hostnameVerifier(ServiceWebHostnameVerifier.noSecurity());
client.sslContext(createSslContext(communicationProtocol, keyStorePath));
return client.build();
}
private static SSLContext createSslContext(
String communicationProtocol,
Optional keyStorePath) {
SSLContext securityContext = null;
try {
securityContext = SSLContext.getInstance(communicationProtocol);
securityContext.init(
KeyManagerHelper.useKeyManagerCustom(keyStorePath),
TrustManagerHelper.createTrustManagerCustom(keyStorePath), new SecureRandom());
} catch (KeyManagementException e) {
ExceptionHelper.reportRuntimeException("The secure connection (TLSv1.2) could not be established.", e);
} catch (NoSuchAlgorithmException e) {
ExceptionHelper.reportRuntimeException("The implementation for the protocol specified is not available", e);
}
return securityContext;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy