SuiteTalk开发:How to getDataCenterUrl

NetSuite SuiteTalk开发:动态获取DataCenterUrl的三种方法

SuiteTalk开发:How to getDataCenterUrl

有3种方法可用于确定正确的URL:

  • 1)在2012.2和更高版本的端点中,可以使用getDataCenterUrls操作。 早期端点不支持此操作。
  • 2)使用NetSuite提供的REST roles service
  • 3)从版本2017.2开始,还可以使用DataCenterUrls REST服务动态发现正确的URL,如下将xxxxxxx替换成公司账户ID
    https://rest.netsuite.com/rest/datacenterurls?account=xxxxxxx

方法1 getDataCenterUrls()

package com.bansi.webservices.myclient;

import java.net.URL;

import com.bansi.webservices.samples.io.Logger;
import com.netsuite.webservices.lists.relationships_2019_1.Customer;
import com.netsuite.webservices.platform.core_2019_1.DataCenterUrls;
import com.netsuite.webservices.platform.core_2019_1.RecordRef;
import com.netsuite.webservices.platform.core_2019_1.types.RecordType;
import com.netsuite.webservices.platform_2019_1.NetSuitePortType;
import com.netsuite.webservices.platform_2019_1.NetSuiteServiceLocator;

public class NLWsClient {
	private static final String ACCOUNT = "your_accountid";
	private NetSuitePortType _port;
	private static final Logger LOG = Logger.getInstance();

	public NLWsClient() throws Exception {
		//1. Locate the NetSuite service.
		DataCenterAwareNetSuiteServiceLocator service = new DataCenterAwareNetSuiteServiceLocator(ACCOUNT);
		//2. Enable support for multiple cookie management.
		service.setMaintainSession(true);
		_port = service.getNetSuitePort();
	}

	// initialize _port - authentication// ...
	public void initPort() {

	}

	public NetSuitePortType getPort() {
		return _port;
	}

	//testDataCenterUrlsPublic
	public static void main(String[] args) throws Exception {
		NLWsClient c = new NLWsClient();
		c.initPort();
		String account = "your_accountid";
		System.out.println("Account: " + account);
		DataCenterUrls urls = c.getPort().getDataCenterUrls(account).getDataCenterUrls();
		System.out.println("\tREST domain: " + urls.getRestDomain());
		System.out.println("\tWeb services domain: " + urls.getWebservicesDomain());
		System.out.println("\tSystem domain: " + urls.getSystemDomain());
		System.out.println();		
	}

	/**
	 * Since 12.2 endpoint accounts are located in multiple data centers with different domain names.
	 * To use the correct one, wrap the Locator and get the correct domain first.
	 *
	 * See getDataCenterUrls WSDL method documentation in the Help Center.
	 */
	private static class DataCenterAwareNetSuiteServiceLocator extends NetSuiteServiceLocator {
		private String account;

		public DataCenterAwareNetSuiteServiceLocator(String account) {
			this.account = account;
		}

		@Override
		public NetSuitePortType getNetSuitePort(URL defaultWsDomainURL) {
			try {
				NetSuitePortType _port = super.getNetSuitePort(defaultWsDomainURL);
				// Get the webservices domain for your account
				DataCenterUrls urls = _port.getDataCenterUrls(account).getDataCenterUrls();
				String wsDomain = urls.getWebservicesDomain();

				// Return URL appropriate for the specific account
				return super.getNetSuitePort(new URL(wsDomain.concat(defaultWsDomainURL.getPath())));
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}

	}
}

方法2 Java Call to the REST roles Service

package com.bansi.webservices.restclient;

import javax.net.ssl.HttpsURLConnection;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

//Sample Java Call to the REST roles Service
public class GetRESTDomain {
	private BufferedReader _br = null;

	public GetRESTDomain() {
		_br = new BufferedReader(new InputStreamReader(System.in));
	}

	static class URLTriplet {
		String restDomain;
		String systemDomain;
		String webservicesDomain;
	}

	/** * Retrieve domains of a NetSuite account through RESTful WS call. ***/

	public URLTriplet getDataCenterUrls(String nsAccount, String nsEmail, String nsPassword) throws IOException, ParseException {
		
		String sysURL = "https://rest.netsuite.com/rest/roles";
		
		URLTriplet urls = new URLTriplet();
		// 'nlauth_account' should NOT be specified for this type of request
		// otherwise an error is returned.//
		String nlAuth = "NLAuth nlauth_email=" + nsEmail + ", nlauth_signature=" + nsPassword;
		URL u = new URL(sysURL);
		HttpsURLConnection uc = (HttpsURLConnection) u.openConnection();
		uc.setAllowUserInteraction(Boolean.FALSE);
		uc.setInstanceFollowRedirects(Boolean.FALSE);
		uc.setRequestMethod("GET");
		uc.setRequestProperty("Authorization", nlAuth);
		BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
		String inputLine = in.readLine();
		in.close();
		if (inputLine.length() > 0) {
			JSONArray jsonArr = (JSONArray) new JSONParser().parse(inputLine);

			for (int i = 0; i < jsonArr.size(); i++) {
				JSONObject json = (JSONObject) jsonArr.get(i);
				// Validate JSON object if (json.containsKey("account") &&
				// json.containsKey("dataCenterURLs"))
				{
					JSONObject jsonAccount = (JSONObject) json.get("account");
					JSONObject jsonDataCenters = (JSONObject) json.get("dataCenterURLs");
					// Validate account and retrieve domains if
					// (jsonAccount.get("internalId").toString().equalsIgnoreCase(nsAccount))
					{
						urls.restDomain = jsonDataCenters.get("restDomain").toString();
						urls.webservicesDomain = jsonDataCenters.get("webservicesDomain").toString();
						urls.systemDomain = jsonDataCenters.get("systemDomain").toString();
						break;
					}
				}
			}
		}

		return urls;
	}
	/*
	public static void main(String args[]) throws IOException {
		GetRESTDomain ns = new GetRESTDomain();
		while (true) {
			System.out.println("GET REST DOMAIN URL");
			System.out.print("Account      : ");
			String account = ns._br.readLine().trim();
	
			if (account.isEmpty()) {
				break;
			}
	
			System.out.print("Email address: ");
			String email = ns._br.readLine().trim();
			System.out.print("Password     : ");
			String password = ns._br.readLine().trim();
			try {
				//方法getDataCenterUrls()是在12.2端点中引入的,自12.2开始,应使用getDataCenterUrls()
				URLTriplet urls = ns.getDataCenterUrls(account, email, password);
				System.out.println("\nREST domain URL        : " + urls.restDomain + "\n");
				System.out.println("\nWeb services domain URL: " + urls.webservicesDomain + "\n");
				System.out.println("\nSystem domain URL      : " + urls.systemDomain + "\n");
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		System.out.print("\nPress any key to exit...");
		ns._br.readLine().trim();
	}
	*/
}

package com.bansi.webservices.restclient;

import com.bansi.webservices.samples.io.Logger;
import com.netsuite.webservices.lists.relationships_2019_1.Customer;
import com.netsuite.webservices.platform.core_2019_1.RecordRef;
import com.netsuite.webservices.platform.core_2019_1.types.RecordType;
import com.netsuite.webservices.platform_2019_1.NetSuitePortType;
import com.netsuite.webservices.platform_2019_1.NetSuiteServiceLocator;

/***
 * Example showing how to get web services domain pointing to the account's data
 * center through RESTful WS call for endpoints older than 12.2. (The method
 * getDataCenterUrls() was introduced in the 12.2 endpoint).
 ***/
public class AXISClient {
	private static final String ENDPOINT_VERSION = "NetSuitePort_2019_1";
	private static final String ACCOUNT = "accountid";
	private static final String PASSWORD = "passworD123";
	private static final String EMAIL = "88888888@qq.com";
	private NetSuitePortType _port;
	private static final Logger LOG = Logger.getInstance();

	public AXISClient(String acct, String email, String passwd) throws Exception {

		//1. Locate the NetSuite service.
		NetSuiteServiceLocator service = new NetSuiteServiceLocator();
		//2. Enable support for multiple cookie management.
		service.setMaintainSession(true);

		GetRESTDomain restHelper = new GetRESTDomain();
		
		String wsDomain = restHelper.getDataCenterUrls(acct, email, passwd).webservicesDomain;
		//注意 wsDomain 在系统中也可以查到,go to 设置 > 公司 > 公司信息 in the NETSUITE UI
		service.setNetSuitePortEndpointAddress(wsDomain + "/services/" + ENDPOINT_VERSION);
		LOG.info(wsDomain + "/services/" + ENDPOINT_VERSION);

		//3. Get the NetSuite port.
		_port = service.getNetSuitePort();
		// initialize _port - authentication// ...

	}

	public static void main(String[] args) throws Exception {
		AXISClient client = new AXISClient(ACCOUNT, EMAIL, PASSWORD);
		// _port points to the correct data center. Perform operations as
		// usual.// ...
		//client.initPort();
		RecordRef rr = new RecordRef();
		//		rr.setInternalId("671");
		//		rr.setType(RecordType.employee);
		//		Employee e = (Employee) client._port.get(rr).getRecord();
		//		System.out.println(e.getFirstName() + " " + e.getLastName());
		rr.setInternalId("1");
		rr.setType(RecordType.customer);
		Customer b = (Customer) client._port.get(rr).getRecord();
		System.out.println(b.getCompanyName());

	}
}

方法3 调用标准WebService获取

在这里插入图片描述
[完]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值