0

I'm implementing the ETrade Java SDK on a ColdFusion server.

import com.etrade.etws.account.Account; 
import com.etrade.etws.account.AccountListResponse; 
import com.etrade.etws.oauth.sdk.client.IOAuthClient; 
import com.etrade.etws.oauth.sdk.client.OAuthClientImpl; 
Import com.etrade.etws.oauth.sdk.common.Token; 
import com.etrade.etws.sdk.client.ClientRequest; 

// Variables 
public IOAuthClient client = null; 
public ClientRequest request = null; 
public Token token = null; 
public String oauth_consumer_key = null; // Your consumer key 
public String oauth_consumer_secret = null; // Your consumer secret 
public String oauth_request_token = null; // Request token  
public String oauth_request_token_secret= null; // Request token secret 

client = OAuthClientImpl.getInstance(); // Instantiate IOAUthClient 
request = new ClientRequest(); // Instantiate ClientRequest 
request.setEnv(Environment.SANDBOX); // Use sandbox environment 

request.setConsumerKey(oauth_consumer_key); //Set consumer key 
request.setConsumerSecret(oauth_consumer_secret); // Set consumer secret 
token=client.getRequestToken(request); // Get request-token object         
oauth_request_token  = token.getToken(); // Get token string 
oauth_request_token_secret = token.getSecret(); // Get token secret 

But converting Java to ColdFusion is new to me. I've copied down the JAR files so I don't think the imports are needed.

I've created the first three lines of the variables code section:

<cfset client = CreateObject("java","com.etrade.etws.oauth.sdk.client.IOAuthClient") />
<cfset request = CreateObject("java","com.etrade.etws.sdk.client.ClientRequest") />
<cfset token = CreateObject("java","com.etrade.etws.oauth.sdk.common.Token") />

However, I can't find a good reference for the lines commented with //Instantiate... or //Set...

Very open to thoughts or links to references.

Thanks in advance

user1241463
  • 111
  • 1
  • 1
  • 7
  • The Java functions should be called in the exact same way. `request` is special variable in ColdFusion. Use something else. `client` is also likely to be special. Lastly, use `` rather than tags. You may find that your code is almost identical. – James A Mohler May 31 '17 at 04:48
  • Why was this down voted? Clearly an attempt was made to understand the code. However, the lines in question use a few unfamiliar concepts to those outside java (enums, interfaces, constructors). So this is certainly a fair question. – Leigh May 31 '17 at 05:35

1 Answers1

1

(Too long for comments)

Correct. The import statements are not needed in CF (neither is the The // Variables section). Though as you noticed, the imports are useful for identifying where the different classes are located.

You did a pretty good job figuring out the variable types in the initial code. It is close, but a few tips/corrections:

public IOAuthClient client = null;

  1. Do not let the variable declaration fool you ;-) The class for the client variable should actually be OAuthClientImpl, not the interface "IOAuthClient".

    CreateObject("java","com.etrade.etws.oauth.sdk.client.OAuthClientImpl").getInstance() 
    

request = new ClientRequest(); // Instantiate ClientRequest

  1. In java the syntax new SomeClass(...) is used to instantiate an object. CF does not support the new keyword for java objects. Instead use the psuedo-constructor init():

    cRequest = CreateObject("java","com.etrade.etws.sdk.client.ClientRequest").init()
    

request.setEnv(Environment.SANDBOX); // Use sandbox environment

  1. Unfortunately the example forgot to include the path for the Environment class. However, if you can find it in the downloadable java samples. (Note, Environment is an enum, ie constant, so it does not need to be instantiated).

    createObject("java", "com.etrade.etws.sdk.client.Environment")
    
  2. Some of the java variable names are reserved words in CF (request and client), so best to change them to something else to avoid unexpected results.

NB: Although there is nothing technically wrong with using CFML, it is simpler and more natural to use cfscript for writing java code, as the syntax is so similar. So consider switching before you get too far into things.

Also, you might find this thread helpful. It not about the E-Trade API, but has some general tips about converting java code that should apply to any java library. Once you get the basics down, it is just lather, rinse and repeat for the various classes.

Leigh
  • 28,765
  • 10
  • 55
  • 103