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

com.hikvision.artemis.sdk.ArtemisHttpUtil Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.hikvision.artemis.sdk;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.hikvision.artemis.sdk.constant.*;
import com.hikvision.artemis.sdk.enums.Method;
import com.hikvision.artemis.sdk.util.MessageDigestUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;

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

public class ArtemisHttpUtil {
    private static final Logger logger = LoggerFactory.getLogger(ArtemisHttpUtil.class);

    //自定义参与签名Header前缀(可选,默认只有"X-Ca-"开头的参与到Header签名)
    private final static List CUSTOM_HEADERS_TO_SIGN_PREFIX = new ArrayList();

    //调用网关成功的标志,标志位
    private final static String SUCC_PRE = "2";

    //调用网关重定向的标志,标志位
    private final static String REDIRECT_PRE = "3";

    /**
     * get请求
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param querys      map类型  get请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值
     * @param header
     */

    public static String doGetArtemis(Map path, Map querys, String accept, String contentType, Map header) {

        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);

        String responseStr = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
           if(header!=null){
              headers.putAll(header);
           }
            logger.info(path.get(httpSchema));
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.GET, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);

            request.setQuerys(querys);
            //调用服务端
            Response response = Client.execute(request);
            responseStr =response.getBody();
        } catch (Exception e) {
            logger.error("the Artemis GET Request is failed[doGetArtemis]", e);
        }
        return responseStr;
    }


    /**
     * get请求
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param querys      map类型  get请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值
     */

    public static String doGetArtemis(Map path, Map querys, String accept, String contentType) {

        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);

        String responseStr = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
            logger.info(path.get(httpSchema));
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.GET, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);

            request.setQuerys(querys);
            //调用服务端
            Response response = Client.execute(request);
            responseStr =response.getBody();
        } catch (Exception e) {
            logger.error("the Artemis GET Request is failed[doGetArtemis]", e);
        }
        return responseStr;
    }

    /**
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param querys      map类型  get请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值
     * @param header 请求参数有header以map的方式,没有则为null
     * @return  GET图片下载类型 HttpResponse类型
     */
    public static HttpResponse doGetResponse(Map path, Map querys, String accept, String contentType, Map header) {


        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);

        HttpResponse httpResponse = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
            if(header!=null){
                headers.putAll(header);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.GET_RESPONSE, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);

            request.setQuerys(querys);
            //调用服务端
            Response response = Client.execute(request);

            httpResponse = response.getResponse();

        } catch (Exception e) {
            logger.error("the Artemis GET Request is failed[doGetArtemis]", e);
        }

        return httpResponse;

    }




    /**
     * postForm请求,postForm请求包含query参数和form表单参数
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param paramMap    Form表单请求的参数,键值对形式的map
     * @param querys      map类型  post请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/x-www-form-urlencoded; charset=UTF-8")
     * @param header 请求参数有header以map的方式,没有则为null
     * @return  返回表单post请求,返回字符串类型
     *
     */

    public static String doPostFormArtemis(Map path, Map paramMap, Map querys,String accept, String contentType, Map header) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);
        String responseStr = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_FORM);
            }
            if(header!=null){
                headers.putAll(header);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.POST_FORM, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            //postForm请求的query参数
            request.setQuerys(querys);
            //postForm请求的表单参数
            request.setBodys(paramMap);

            //调用服务端
            Response response = Client.execute(request);

            responseStr = getResponseResult(response);

        } catch (Exception e) {
            logger.error("the Artemis PostForm Request is failed[doPostFormArtemis]", e);
        }
        return responseStr;
    }


    /**
     * postForm请求,postForm请求包含query参数和form表单参数
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param paramMap    Form表单请求的参数,键值对形式的map
     * @param querys      map类型  post请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/x-www-form-urlencoded; charset=UTF-8")
     * @return  返回表单post请求,返回字符串类型
     *
     */

    public static String doPostFormArtemis(Map path, Map paramMap, Map querys,String accept, String contentType) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);
        String responseStr = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_FORM);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.POST_FORM, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            //postForm请求的query参数
            request.setQuerys(querys);
            //postForm请求的表单参数
            request.setBodys(paramMap);

            //调用服务端
            Response response = Client.execute(request);

            responseStr = getResponseResult(response);

        } catch (Exception e) {
            logger.error("the Artemis PostForm Request is failed[doPostFormArtemis]", e);
        }
        return responseStr;
    }

    /**
     * postformImg请求
     */

    /**
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param paramMap    Form表单请求的参数,键值对形式的map
     * @param querys      map类型  post请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/x-www-form-urlencoded; charset=UTF-8")
     * @param header 请求参数有header以map的方式,没有则为null
     * @return  POST表单类型图片下载接口  HttpResponse类型
     */
    public static HttpResponse doPostFormImgArtemis(Map path, Map paramMap, Map querys,String accept, String contentType, Map header) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);
        HttpResponse response = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_FORM);
            }
            if(header!=null){
                headers.putAll(header);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.POST_FORM_RESPONSE, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            //postForm请求的query参数
            request.setQuerys(querys);
            //postForm请求的表单参数
            request.setBodys(paramMap);

            //调用服务端
            Response response1 = Client.execute(request);

            response = response1.getResponse();

        } catch (Exception e) {
            logger.error("the Artemis PostForm Request is failed[doPostFormImgArtemis]", e);
        }
        return response;
    }


    /**
     * postString请求
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param body        postString String请求体
     * @param querys      map类型  post请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/text; charset=UTF-8")
     * @param header header参数,无过没有值为null
     * @return  POST json类型接口  返回字符串类型
     */


    public static String doPostStringArtemis(Map path, String body,
                                 Map querys, String accept, String contentType, Map header) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);
        String responseStr = null;
        try {

            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //(POST/PUT请求必选)请求Body内容格式请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
            if(header!=null){
                headers.putAll(header);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.POST_STRING, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            //请求的query
            request.setQuerys(querys);
            //请求的bodyString
            request.setStringBody(body);
            //调用服务端
            Response response = Client.execute(request);

            responseStr = getResponseResult(response);

        } catch (Exception e) {
            logger.error("the Artemis PostString Request is failed[doPostStringArtemis]", e);
        }
        return responseStr;
    }


    /**
     * postString请求
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param body        postString String请求体
     * @param querys      map类型  post请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/text; charset=UTF-8")
     * @return  POST json类型接口  返回字符串类型
     */


    public static String doPostStringArtemis(Map path, String body,
                                             Map querys, String accept, String contentType) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);
        String responseStr = null;
        try {

            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //(POST/PUT请求必选)请求Body内容格式请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.POST_STRING, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            //请求的query
            request.setQuerys(querys);
            //请求的bodyString
            request.setStringBody(body);
            //调用服务端
            Response response = Client.execute(request);

            responseStr = getResponseResult(response);

        } catch (Exception e) {
            logger.error("the Artemis PostString Request is failed[doPostStringArtemis]", e);
        }
        return responseStr;
    }

    /**
     * postString请求
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param body        postString String请求体
     * @param querys      map类型  post请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/x-www-form-urlencoded; charset=UTF-8")
     * @param header 请求参数有header以map的方式,没有则为null
     * @return  POST json请求类型图片下载接口  HttpResponse类型
     */
    public static HttpResponse doPostStringImgArtemis(Map path, String body,
                                             Map querys, String accept, String contentType, Map header) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);
        HttpResponse responseStr = null;
        try {

            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //(POST/PUT请求必选)请求Body内容格式请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
            if(header!=null){
                headers.putAll(header);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.POST_STRING_RESPONSE, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            //请求的query
            request.setQuerys(querys);
            //请求的bodyString
            request.setStringBody(body);
            //调用服务端
            Response response = Client.execute(request);

            responseStr = response.getResponse();

        } catch (Exception e) {
            logger.error("the Artemis PostString Request is failed[doPostStringArtemis]", e);
        }
        return responseStr;
    }

    /**
     * postBytes请求
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param bytesBody   请求体,byte字节
     * @param querys      map类型  post请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/text; charset=UTF-8")
     */
    public static String doPostBytesArtemis(Map path, byte[] bytesBody, Map querys, String accept, String contentType, Map header) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);

        String responseStr = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //(可选)Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header
            if (bytesBody != null) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(bytesBody));
            }
            //(POST/PUT请求必选)请求Body内容格式
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
            if(header!=null){
                headers.putAll(header);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.POST_BYTES,httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            //请求的query
            request.setQuerys(querys);
            request.setBytesBody(bytesBody);
            //调用服务端
            Response response = Client.execute(request);

            responseStr = getResponseResult(response);
        } catch (Exception e) {
            logger.error("the Artemis PostBytes Request is failed[doPostBytesArtemis]", e);
        }
        return responseStr;
    }


    /**
     * postBytes请求
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param bytesBody   请求体,byte字节
     * @param querys      map类型  post请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/text; charset=UTF-8")
     */
    public static String doPostBytesArtemis(Map path, byte[] bytesBody, Map querys, String accept, String contentType) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);

        String responseStr = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //(可选)Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header
            if (bytesBody != null) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(bytesBody));
            }
            //(POST/PUT请求必选)请求Body内容格式
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.POST_BYTES,httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            //请求的query
            request.setQuerys(querys);
            request.setBytesBody(bytesBody);
            //调用服务端
            Response response = Client.execute(request);

            responseStr = getResponseResult(response);
        } catch (Exception e) {
            logger.error("the Artemis PostBytes Request is failed[doPostBytesArtemis]", e);
        }
        return responseStr;
    }


    /**
     * putString请求
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param body        putString String请求体
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/text; charset=UTF-8")
     */
    public static String doPutStringArtemis(Map path, String body, String accept, String contentType) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);

        String responseStr = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //(可选)Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header
            if (StringUtils.isNotBlank(body)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(body));
            }
            //(POST/PUT请求必选)请求Body内容格式
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.PUT_STRING, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            request.setStringBody(body);

            //调用服务端
            Response response = Client.execute(request);

            responseStr = getResponseResult(response);

        } catch (Exception e) {
            logger.error("the Artemis PutString Request is failed[doPutStringArtemis]", e);
        }
        return responseStr;
    }

    /**
     * putBytes请求
     *
     * @param path        artemis配置的putBytes请求的路径
     * @param bytesBody   请求体,byte字节
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值("application/text; charset=UTF-8")
     */
    public static String doPutBytesArtemis(Map path, byte[] bytesBody, String accept, String contentType) {

        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);

        String responseStr = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //(可选)Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header
            if (bytesBody != null) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(bytesBody));
            }
            //(POST/PUT请求必选)请求Body内容格式
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
            }
            CUSTOM_HEADERS_TO_SIGN_PREFIX.clear();
            Request request = new Request(Method.PUT_BYTES, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            request.setBytesBody(bytesBody);
            //调用服务端
            Response response = Client.execute(request);

            responseStr = getResponseResult(response);
        } catch (Exception e) {
            logger.error("the Artemis PutBytes Request is failed[doPutBytesArtemis]", e);
        }
        return responseStr;
    }

    /**
     * delete请求
     *
     * @param path        artemis配置的get请求的路径 是一个数组长度为1的Hashmap集合,只存一组数据,key为http的请求方式,value为host后面的path路径。
     * @param querys      map类型  delete请求的url查询参数(url中的query参数,没有就是为空)形如 "?aa=1&&bb=2"形式参数变成map键值对 query.put("aa","1");query.put("bb","2")
     * @param accept      指定客户端能够接收的内容类型,该参数传空时的默认全部类型接受
     * @param contentType 请求的与实体对应的MIME信息,该参数传空时的取默认值
     */
    public static String doDeleteArtemis(Map path, Map querys, String accept, String contentType) {
        /**
         * 根据传入的path获取是请求是http还是https
         */
        String httpSchema = (String) path.keySet().toArray()[0];

        if (httpSchema == null || StringUtils.isEmpty(httpSchema))
            throw new RuntimeException(MsgConstants.HTTP_SCHEMA_ERROR + "httpSchema: " + httpSchema);

        String responseStr = null;
        try {
            Map headers = new HashMap();
            //(必填)根据期望的Response内容类型设置
            if (StringUtils.isNotBlank(accept)) {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, accept);
            } else {
                headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "*/*");
            }
            //请求的与实体对应的MIME信息
            if (StringUtils.isNotBlank(contentType)) {
                headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, contentType);
            }
            Request request = new Request(Method.DELETE, httpSchema + ArtemisConfig.host,
                    path.get(httpSchema), ArtemisConfig.appKey, ArtemisConfig.appSecret, Constants.DEFAULT_TIMEOUT);
            request.setHeaders(headers);
            request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
            request.setQuerys(querys);
            //调用服务端
            Response response = Client.execute(request);

            responseStr = getResponseResult(response);
        } catch (Exception e) {
            logger.error("the Artemis DELETE Request is failed[doDeleteArtemis]", e);
        }
        return responseStr;
    }

    /**
     * response 获取body内容
     *
     * @param response
     */
    private static String getResponseResult(Response response) {
        String responseStr = null;

        int statusCode = response.getStatusCode();

        if (String.valueOf(statusCode).startsWith(SUCC_PRE) || String.valueOf(statusCode).startsWith(REDIRECT_PRE)) {//调用Artemis网关成功
            responseStr = response.getBody();
            logger.info("the Artemis Request is Success,statusCode:" + statusCode + " SuccessMsg:" + response.getBody());

        } else {
            String msg = response.getErrorMessage();
            responseStr = response.getBody();
            logger.error("the Artemis Request is Failed,statusCode:" + statusCode + " errorMsg:" + msg);
        }
        return responseStr;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy