GoogleMapLocationClient.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package com.ruoyi.app.map;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.ruoyi.common.core.domain.entity.SysDictData;
  5. import com.ruoyi.common.utils.DictUtils;
  6. import org.apache.http.client.config.RequestConfig;
  7. import org.apache.http.client.methods.CloseableHttpResponse;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.client.utils.URIBuilder;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClients;
  12. import org.apache.http.util.EntityUtils;
  13. import org.springframework.stereotype.Component;
  14. import java.net.URI;
  15. import java.nio.charset.StandardCharsets;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.Set;
  19. /** Google 地址接口固定主机与路径;参数编码后发送,不跟随第三方重定向。 */
  20. @Component
  21. public class GoogleMapLocationClient {
  22. private static final Set<String> PATHS = Set.of("/maps/api/place/textsearch/json",
  23. "/maps/api/place/nearbysearch/json", "/maps/api/geocode/json", "/maps/api/place/details/json");
  24. private static final RequestConfig TIMEOUTS = RequestConfig.custom().setConnectTimeout(2500)
  25. .setConnectionRequestTimeout(2500).setSocketTimeout(3500).build();
  26. public JSONObject getObject(String path, Map<String, ?> query) {
  27. List<SysDictData> keys = DictUtils.getDictCache("sys_googlemap_key");
  28. if (keys == null || keys.isEmpty() || keys.get(0).getDictValue() == null
  29. || keys.get(0).getDictValue().isBlank()) {
  30. throw new IllegalStateException("google map key unavailable");
  31. }
  32. HttpGet request = new HttpGet(requestUri(path, query, keys.get(0).getDictValue()));
  33. request.setConfig(TIMEOUTS);
  34. try (CloseableHttpClient client = HttpClients.custom().disableRedirectHandling()
  35. .disableAutomaticRetries().build();
  36. CloseableHttpResponse response = client.execute(request)) {
  37. int status = response.getStatusLine().getStatusCode();
  38. if (status < 200 || status >= 300 || response.getEntity() == null) {
  39. throw new IllegalStateException("google map response unavailable");
  40. }
  41. JSONObject result = JSON.parseObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8));
  42. if (result == null) throw new IllegalStateException("google map response invalid");
  43. return result;
  44. } catch (Exception exception) {
  45. // 不保留带有 Key、用户地址或完整 URL 的底层异常。
  46. throw new IllegalStateException("google map request failed");
  47. }
  48. }
  49. static URI requestUri(String path, Map<String, ?> query, String key) {
  50. if (!PATHS.contains(path) || query.containsKey("key")) {
  51. throw new IllegalArgumentException("unsupported google map request");
  52. }
  53. try {
  54. URIBuilder uri = new URIBuilder().setScheme("https").setHost("maps.googleapis.com").setPath(path);
  55. query.forEach((name, value) -> uri.addParameter(name, String.valueOf(value)));
  56. return uri.addParameter("key", key).build();
  57. } catch (java.net.URISyntaxException exception) {
  58. throw new IllegalArgumentException("invalid google map request");
  59. }
  60. }
  61. }