2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
    }
}