package com.stylefeng.guns.modular.system.utils.PortUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.stylefeng.guns.core.common.exception.BizExceptionEnum; import com.stylefeng.guns.core.exception.GunsException; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.system.warpper.TerminaleDataWarpper; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class APMStrategy implements WharfStrategy{ //Sandbox Interface private static final String url = "https://api-sandbox.apmterminals.com/import-availability"; //Production Interface // private static final String url = "https://api.apmterminals.com/import-availability"; private static final String auth_url = "https://api.apmterminals.com/oauth/client_credential/accesstoken"; private static final String KEY = "di3RgCcM9zlgb5BG1UiYSIxwYdmYUxTo"; private static final String SECRET = "AIJczPxBeocsEjtI"; private static String bearerToken = "5dDHq3LwPW3EYZ6rtPGK0zqWsRA5"; public static void main(String[] args) { getAuth(); String us = auth_url+ "?assetId="+"EITU1509974"+"&facilityCode="+"USLAX"; HttpResponse execute = HttpRequest.get(us).header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36") .header("cookie", "_ga=GA1.2.1226281326.1675309614; _gid=GA1.2.1467106222.1675309614; _ga=GA1.4.1226281326.1675309614; _gid=GA1.4.1467106222.1675309614; AWSALB=1bWqINVI+LJP87FTEXfEw1Ob1nkbr+I4baSbUGUmu5+/LdiqL9ic04Nj7F0Vz3rvharAG7a8dVe3MX6YMNEbUINVr++CCv/UBw6JeCRS0PcbRLxK7wVHb1lPT8Jl; AWSALBCORS=1bWqINVI+LJP87FTEXfEw1Ob1nkbr+I4baSbUGUmu5+/LdiqL9ic04Nj7F0Vz3rvharAG7a8dVe3MX6YMNEbUINVr++CCv/UBw6JeCRS0PcbRLxK7wVHb1lPT8Jl") .header("path", "?assetId=" + "EITU1509974" + "&facilityCode=" + "USLAX") .header("Authorization", "Bearer " + bearerToken) .execute(); int status = execute.getStatus(); System.out.println(status); System.out.println(execute.body()); } @Override public TerminaleDataWarpper executeLogic(String... params) { String containerId = ""; String enums = ""; if (params.length == 0) { throw new GunsException(BizExceptionEnum.REQUEST_EMPTY_ERROR); } else { containerId = params[0]; enums = params[1]; } String us = url+ "?assetId="+containerId+"&facilityCode="+enums; HttpResponse execute = HttpRequest.get(us).header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36") .header("cookie", "_ga=GA1.2.1226281326.1675309614; _gid=GA1.2.1467106222.1675309614; _ga=GA1.4.1226281326.1675309614; _gid=GA1.4.1467106222.1675309614; AWSALB=1bWqINVI+LJP87FTEXfEw1Ob1nkbr+I4baSbUGUmu5+/LdiqL9ic04Nj7F0Vz3rvharAG7a8dVe3MX6YMNEbUINVr++CCv/UBw6JeCRS0PcbRLxK7wVHb1lPT8Jl; AWSALBCORS=1bWqINVI+LJP87FTEXfEw1Ob1nkbr+I4baSbUGUmu5+/LdiqL9ic04Nj7F0Vz3rvharAG7a8dVe3MX6YMNEbUINVr++CCv/UBw6JeCRS0PcbRLxK7wVHb1lPT8Jl") .header("path", "?assetId=" + containerId + "&facilityCode=" + enums) .header("Authorization", "Bearer " + bearerToken) .execute(); String body = execute.body(); int status = execute.getStatus(); if (status != 200){ getAuth(); return executeLogic(); } ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonArray = null; TerminaleDataWarpper warpper = new TerminaleDataWarpper(); try { jsonArray = objectMapper.readTree(body); for (JsonNode jsonNode : jsonArray) { String appointmentDateTimeLocal = jsonNode.get("appointmentDateTimeLocal").asText(); warpper.setLfd(appointmentDateTimeLocal); String appointmentNumber = jsonNode.get("containerId").asText(); warpper.setAppointmentNumber(appointmentNumber); String readyForDelivery = jsonNode.get("readyForDelivery").asText(); if (ToolUtil.isNotEmpty(readyForDelivery)) { warpper.setAvailability(readyForDelivery); } String containerHolds = jsonNode.get("containerHolds").asText(); String location = jsonNode.get("location").asText(); if (ToolUtil.isNotEmpty(location)){ warpper.setClosedArea(location); } List collect = new ArrayList<>(); if (ToolUtil.isEmpty(containerHolds)){ return warpper; } if (containerHolds.contains(",")){ String[] split = containerHolds.split(","); collect = Arrays.stream(split).collect(Collectors.toList()); if (collect.contains("LINE")){ warpper.setLineHold("1"); } if (collect.contains("TMF")){ warpper.setCustomHold("1"); } if (collect.contains("PIER")){ warpper.setPierpass("1"); } if (collect.contains("CTF")){ warpper.setCtf("1"); } }else { if (containerHolds.equals("LINE")){ warpper.setLineHold("1"); } if (containerHolds.equals("TMF")){ warpper.setCustomHold("1"); } if (containerHolds.equals("PIER")){ warpper.setPierpass("1"); } if (containerHolds.equals("CTF")){ warpper.setCtf("1"); } if (containerHolds.equals("AREA")){ warpper.setClosedArea("1"); } } break; } } catch (IOException e) { throw new RuntimeException(e); } return warpper; } public static void getAuth(){ String us = auth_url+ "?grant_type=client_credentials"; String body = HttpRequest.post(us) .form("client_id", KEY) .form("client_id", SECRET) .execute().body(); JSONObject object = JSONObject.parseObject(body); Object o = object.get("access_token"); if (ToolUtil.isNotEmpty(o)){ bearerToken = (String) o; } } }