All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.toolbox.utils.ServerApiTemplate Maven / Gradle / Ivy

There is a newer version: 2.7.44
Show newest version
/*
 * @(#)ServerApiTemplate.java	2021-11-17
 *
 * Copyright (c) 2021. All Rights Reserved.
 *
 */

package com.github.javaclub.toolbox.utils;

import static com.github.javaclub.toolbox.ToolBox.Strings.escapeHTML;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.github.javaclub.Constants.WebMvc;
import com.github.javaclub.toolbox.ToolBox.JSONS;
import com.github.javaclub.toolbox.ToolBox.Web;
import com.github.javaclub.toolbox.conf.CompositeAppConfigProperties;
import com.github.javaclub.toolbox.model.HttpInvokeResult;
import com.google.common.collect.Maps;

/**
 * ServerApiTemplate
 *
 * @author Gerald Chen
 * @version $Id: ServerApiTemplate.java 2021-11-17 14:06:04 Exp $
 */
public class ServerApiTemplate {
	
	private static final Logger log = LoggerFactory.getLogger(ServerApiTemplate.class);
	
	static String INVOKE_STATUS_FLAG = "status";
	static String INVOKE_RETURN_MESSAGE = "message";
	static String INVOKE_RETURN_DATA = "entry";
	
	static {
		INVOKE_STATUS_FLAG = CompositeAppConfigProperties.getInstance().getValueOrDefault(WebMvc.API_STATUS_FLAG, "status");
		INVOKE_RETURN_MESSAGE = CompositeAppConfigProperties.getInstance().getValueOrDefault(WebMvc.API_RETURN_MSG, "message");;
		INVOKE_RETURN_DATA = CompositeAppConfigProperties.getInstance().getValueOrDefault(WebMvc.API_RETURN_DATA, "entry");;
	}

	private ServerApiTemplate() {
	}
	
	public static  T invoke(Class clazz, String apiUrl, String apiAk, 
			Map paramMap, Map headerMap) {
		HttpInvokeResult resp = null;
		try {
			resp = doApiInvoke(apiUrl, apiAk, paramMap, headerMap);
		} catch (Exception e) {
			log.error("Invoke ssoApi error apiPath=" + apiUrl, e);
		}
		if (null != resp) {
			boolean httpOk = resp.isHttpOK();
			JSONObject jsonMap = resp.parseResponse(JSONObject.class);
			boolean status = null == jsonMap ? false : jsonMap.getBooleanValue(INVOKE_STATUS_FLAG);
			if (!httpOk || !status) {
				log.error("Invoke serverApi failed, apiUrl={}", apiUrl);
				String error = !httpOk ? escapeHTML(resp.getResponseText()) : jsonMap.getString(INVOKE_RETURN_MESSAGE);
				Map map = Maps.newLinkedHashMap();
				map.put("请求地址", resp.getRequestUrl());
				map.put("异常明细", error);
				AlarmUtils.sendDingTalk("调用serverApi失败", new Date(), map);
				return null;
			}
			log.info("reponseText = {}", resp.getResponseText());
			
			T result = JSONS.parseNode(resp.getResponseText(), INVOKE_RETURN_DATA, clazz);
			return result;
		}
		
		return null;
	}
	
	public static HttpInvokeResult doApiInvoke(String apiUrl, String apiAk, 
			Map paramMap, Map headerMap) throws Exception {
		Map headerParams = Maps.newHashMap();
		if (null != headerMap && 0 < headerMap.size()) {
			for (Map.Entry elem : headerMap.entrySet()) {
				headerParams.put(elem.getKey(), elem.getValue());
			}
		}
		headerParams.put("User-Agent", "Javaclub Toolbox ServerAPI ClientInvoker 1/1.5.8");
		headerParams.put("X-Requested-With", "XMLHttpRequest");
		
		Map reqParams = Maps.newHashMap();
		if(null != paramMap) {
			paramMap.remove("timestamp");
			reqParams.putAll(paramMap);
		}
		reqParams.put("timestamp", String.valueOf(System.currentTimeMillis()));

		String sign = Web.sign(reqParams, Arrays.asList(new String[] { "sign" }), apiAk);
		reqParams.put("sign", sign);
		
		HttpInvokeResult r = Web.post(apiUrl, reqParams, headerParams);
		return r;
	}

	public static void main(String[] args) {

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy