cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2133
Views
1
Helpful
8
Replies

Using Proxy server to integrate Rest API on CVP Studio applicaiton

anil.25.kumar1
Level 1
Level 1

Hi ,

I am working on CVP based IVR development and in this there is requirement to consume Rest API via Proxy and in the Java code, when I am using Proxy by System Properties REST API request is not going via Proxy it is hitting directly ...   Does any one of you have experience using Proxy in CVP based IVR application .   If yes, please help how you have done it?

8 Replies 8

ptindall
Cisco Employee
Cisco Employee

I've had various problems depending on which REST client you're using.   As a starting point, have you tried setting the proxy in your CVP JVM net.properties?

No... I haven't use proxy before on CVP therefore, we don't have any idea how to set Proxy on JVM .net property and where I can found this?  Can you give more detail on this.

We have set sysem property at the beginning of Code and then opening the http connection.

It's just one of the standard Java config files you can edit --  C:\Cisco\CVP\jre\lib\net.properties.    However, if you're already setting the proxy using System.setProperty then you should be overriding what's in net.properties anyway.   Be aware that not every HTTP client uses those settings; there are alternatives so see if you can confirm which HTTP client it is you've coded.

We are using Httpclient api to call Rest URL.

There's many HTTP client implementations out there so will need a bit more clarification.  Suggest you post a code snippet and especially showing the package imports.

Paul

Here is the code snippet with Proxy setting applied on method.

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.audium.server.session.ActionElementData;

public static String getAccessToken(String URL, String client_id ,String client_secret ,String username, String password, String sec_token,ActionElementData data) throws NoSuchAlgorithmException, KeyManagementException
{
   String res   ="";
   String tokenURL = URL;
   SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
         sslContext.init(null, null, new SecureRandom());
         SSLSocketFactory sf = new SSLSocketFactory(sslContext);
         Scheme httpsScheme = new Scheme("https", 443, sf);
         SchemeRegistry schemeRegistry = new SchemeRegistry();
         schemeRegistry.register(httpsScheme);
         ClientConnectionManager cm =  new        SingleClientConnManager(schemeRegistry);
        
         HttpClient httpclient = new DefaultHttpClient(cm);
         final HttpParams httpParameters = httpclient.getParams();

         int connectionTimeOutSec=5;
   HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
         int socketTimeoutSec=5;
   HttpConnectionParams.setSoTimeout        (httpParameters, socketTimeoutSec * 1000);
   System.getProperties().put("http.proxyHost", "XX.XX.XX.XX");
   System.getProperties().put("http.proxyPort", "80");
  
   System.getProperties().put("htttps.proxyHost", "XX.XX.XX.XX");
   System.getProperties().put("https.proxyPort", "80");
   data.addToLog("URL to be use to get access token :", tokenURL);
         HttpPost post = new HttpPost(tokenURL);
   List<NameValuePair> params = new ArrayList<NameValuePair>();
   params.add(new BasicNameValuePair(
     "client_id",
     client_id));
   params.add(new BasicNameValuePair("client_secret",
     client_secret));
   params.add(new BasicNameValuePair("grant_type", "password"));
   params.add(new BasicNameValuePair("username",
     username));
   params.add(new BasicNameValuePair("password",
     password+sec_token));
   try {
    post.setEntity(new UrlEncodedFormEntity(params));
   } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
   }
   HttpResponse response = null;
   try {
    data.addToLog("Just before hitting the URL :", tokenURL);
    response = httpclient.execute(post);
    String body = EntityUtils.toString(response.getEntity());
    JSONObject json = (JSONObject) JSONValue.parse(body);
    System.out.println(json);
    res = (String) json.get("access_token");
    System.out.println(res);
    data.addToLog("Just After hitting the URL :", tokenURL);
    return res;
   } catch (Exception e) {
    data.addToLog("Inside Catch :", e.getMessage());
    e.printStackTrace();
   }
   finally{
    return res;
   }
 
}

Given you're using Apache HTTP client then I'd suggest reworking your code to use HttpClientBuilder as that will inject the proxy settings at the right part of the config build and will probably be a lot simpler.

That said, I see in your example you're overriding the system proxy settings after you've created the HTTP client so I would expect that to be a problem even if the rest of the build is OK.  If I explicitly do a System.setProperty on proxies in the code I would always do it before any of the HTTP client build code.

Thanks Paul,

We will try this option and let you know if this solves our issue.  

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: