package com.landtool.lanbase.common.utils;
|
|
import java.io.IOException;
|
|
/**
|
* esri token 帮助类
|
* Xiexx 2018.3.30
|
*/
|
public class EsriTokenUtils {
|
/**
|
* 获取Esri Token
|
* @param requestUri
|
* @param UserName
|
* @param PassWord
|
* @param ClientId
|
* @return Token(String)
|
*/
|
public static String GetEsriServerToken(String requestUri, String UserName, String PassWord, String ClientId) throws IOException {
|
String token = ""; //返回对象
|
String infoUrl = ""; //请求地址
|
|
if(requestUri.toLowerCase().contains("/generatetoken")) {
|
String tokenResponse = HttpOperateUtils.httpGet(requestUri);
|
token = getJsonValue(tokenResponse, "token");
|
return token;
|
} else {
|
if(requestUri.toLowerCase().contains("/rest/")) {
|
infoUrl = requestUri.substring(0, requestUri.toLowerCase().indexOf("/rest/"));
|
}
|
else if (requestUri.toLowerCase().contains("/sharing/")) {
|
infoUrl = requestUri.substring(0, requestUri.toLowerCase().indexOf("/sharing/"));
|
infoUrl = infoUrl + "/sharing";
|
}
|
}
|
if (infoUrl != ""){
|
infoUrl += "/rest/info?f=json";
|
String infoResponse = HttpOperateUtils.httpGet(infoUrl);
|
String tokenServiceUri = getJsonValue(infoResponse, "tokenServicesUrl");
|
if (tokenServiceUri != null && !tokenServiceUri.isEmpty())
|
{
|
String owningSystemUrl = getJsonValue(infoResponse, "owningSystemUrl");
|
if (owningSystemUrl != null && !owningSystemUrl.isEmpty())
|
{
|
tokenServiceUri = owningSystemUrl + "/sharing/generateToken";
|
}
|
}
|
if (tokenServiceUri != null && tokenServiceUri != "")
|
{
|
String tokenExpiration = "3600";//Token有效时间(分钟)
|
String uri = tokenServiceUri + "?f=json&request=getToken&&client=referer&referer=" + ClientId + "&expiration="+ tokenExpiration + "&username=" + UserName + "&password=" + PassWord;
|
String tokenResponse = HttpOperateUtils.httpGet(uri);
|
token = getJsonValue(tokenResponse, "token");
|
}
|
}
|
|
return token;
|
}
|
|
//获取Json指定Key的值
|
private static String getJsonValue(String text, String key)
|
{
|
int i = text.indexOf(key);//获取Key位置
|
String value = "";
|
if (i > -1)
|
{
|
value = text.substring(text.indexOf(':', i) + 1).trim();
|
value = value.length() > 0 && value.startsWith("\"") ?
|
value.substring(1, value.indexOf('"', 1) - 1) :
|
value.substring(0, Math.max(0, Math.min(Math.min(value.indexOf(","), value.indexOf("]")), value.indexOf("}"))));
|
}
|
return value;
|
}
|
}
|