public class SampleLotteryHttpClient extends Object
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import com.avaya.asm.core.AsmConstants.TRANSPORT;
import com.avaya.asm.datamgr.AssetDM;
import com.avaya.asm.datamgr.DMFactory;
import com.avaya.asm.datamgr.objectapi.AssetLinkInfo;
This class will make an http GET request to "sample.lottery.com"
and get the result if the numbers sent by Collaboration Bus is a winner or loser
public class SampleLotteryHttpClient
{
private final HttpClient httpClient;
private static final int TEN_SECONDS_MS = 10000;
private static final Integer CONNECT_TIMEOUT_MS = Integer.valueOf(TEN_SECONDS_MS);
private static final Integer SOCKET_TIMEOUT_MS = Integer.valueOf(TEN_SECONDS_MS);
public SampleLotteryHttpClient()
{
this.httpClient = new DefaultHttpClient();
}
public String getResultOfPicks(final String numbers) throws IOException, URISyntaxException
{
//Write Code to validate if your request is good or not
// ...
// ...
// HTTP GET is to get the results of Lottery Pics.
//Add dependency in pom.xml for "groupId:org.apache.httpcomponents" "artifactId:httpclient"
//to use HttpComponents
final String configuredUri = "http://sample.lottery.com/";
//Create the URI srting for GET request using string builder
final StringBuilder sb = new StringBuilder(configuredUri.trim());
if (!configuredUri.trim().endsWith("/"))
{
sb.append("/");
}
sb.append("query=")
.append("?numbers=").append(numbers);
// Create the Http GET request using the URL
final HttpGet get = new HttpGet(new URI(sb.toString()));
InetAddress trafficInterface = null;
//Add dependency in pom.xml for "groupId:com.avaya.csp" "artifactId:asm-common"
AssetDM assetDM = (AssetDM) DMFactory.getInstance().getDataMgr(AssetDM.class);
if (assetDM != null)
{
// Set transport as TRANSPORT.TLS for Https connection
final AssetLinkInfo localAsset = assetDM.getAssetLinkInfo(TRANSPORT.TCP);
if (localAsset != null)
{
trafficInterface = InetAddress.getByName(localAsset.getEntityIp());
}
}
// Set the interface for outgoing traffic to Security Module IP Address
// If this piece of code is not used, make sure that the management IP address is not isolated.
if (trafficInterface != null)
{
httpClient.getParams().setParameter(ConnRoutePNames.LOCAL_ADDRESS, trafficInterface);
}
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECT_TIMEOUT_MS);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SOCKET_TIMEOUT_MS);
// Send request and get response. Response can also be formatted or parsed using custom parser
// as per response from the Web server
HttpResponse response = httpClient.execute(get);
// Return the response for the parameter result - Winner or Loser
return response.getParams().getParameter("result").toString();
}
}
Copyright © 2021 Avaya. All rights reserved.