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

ksyun.client.vpc.createvpngatewayroute.v20160304.CreateVpnGatewayRouteClient Maven / Gradle / Ivy

Go to download

The KSYUN Web Services SDK for Java provides Java APIs for building software on KSYUN' cost-effective, scalable, and reliable infrastructure products. The KSYUN Java SDK allows developers to code against APIs for all of KSYUN's infrastructure web services (KSC S3, KSC KEC, KSC RDS etc).

The newest version!
package ksyun.client.vpc.createvpngatewayroute.v20160304;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import common.BaseClient;
import common.Credential;
import common.aws.AWS4EncryptionFactory;
import common.utils.HttpClientUtils;
import common.utils.SignUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname CreateVpnGatewayRouteClient
 * @Description 创建VPN网关下的路由
 */
@Slf4j
public class CreateVpnGatewayRouteClient extends BaseClient {
    private final static String service = "vpc";
    private final static String version = "2016-03-04";
    private final static String action = "CreateVpnGatewayRoute";


    /**
     * 证书
     */
    private final Credential credential;


    public CreateVpnGatewayRouteClient(Credential credential) {
        this.credential = credential;
    }

    private static void enhanceAws4Signature(Map head, Map params, Credential credential, String requestMethod) {
        AWS4EncryptionFactory aws4EncryptionFactory = new AWS4EncryptionFactory(credential.getSecretKey(), credential.getSignStr(), service, credential.getRegion());

        //设置请求参数
        if (params != null) {
            params.entrySet().forEach(entry -> {
                aws4EncryptionFactory.setParamMap(entry.getKey(), entry.getValue());
            });
        }

        //设置请求头
        if (head != null) {
            head.entrySet().forEach(entry -> {
                aws4EncryptionFactory.setHeadMap(entry.getKey(), entry.getValue());
            });
        }

        //aws 加密
        aws4EncryptionFactory.generateSignature(requestMethod);

        //回填aws4 签名
        String authorization = aws4EncryptionFactory.getHead().get(AWS4EncryptionFactory.X_Authorization);
        String xAmzDate = aws4EncryptionFactory.getHead().get(AWS4EncryptionFactory.X_AMZ_DATA);
        head.put(AWS4EncryptionFactory.X_Authorization, authorization);
        head.put(AWS4EncryptionFactory.X_AMZ_DATA, xAmzDate);
    }

    /**
     * post请求
     *
     * @param path
     * @param requestObj
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doPost(String path, CreateVpnGatewayRouteRequest requestObj) throws Exception {
        Map head = new HashMap<>();
        head.put("Content-Type", "application/x-www-form-urlencoded");
        return doPost(path, requestObj, head);
    }

    /**
     * post请求
     *
     * @param path
     * @param requestObj
     * @param head
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doPost(String path, CreateVpnGatewayRouteRequest requestObj, Map head) throws Exception {
        if (head == null) {
            head = new HashMap<>();
        }
        head.putIfAbsent("Content-Type", "application/x-www-form-urlencoded");
        //参数配置
        JSONObject requestParams = null;
        if (head.get("Content-Type").equalsIgnoreCase("application/json")) {
            requestParams = getPostRawRequestParams(requestObj);
        } else {
            requestParams = getSimpleRequestParams(requestObj);
        }

        //aws4 签名
        enhanceAws4Signature(head, requestParams, credential, "post");

        String response = HttpClientUtils.httpPost(path, requestParams, head);
        log.info("doPost end,path:{},params:{},head:{}", path, requestParams, head);
        return JSON.parseObject(response, CreateVpnGatewayRouteResponse.class);
    }

    /**
     * post 请求
     *
     * @param path
     * @param requestObj
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doPostRaw(String path, CreateVpnGatewayRouteRequest requestObj) throws Exception {
        Map head = new HashMap<>();
        head.put("Content-Type", "application/x-www-form-urlencoded");
        return doPost(path, requestObj, head);
    }

    /**
     * post 请求
     *
     * @param path
     * @param requestObj
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doPostRaw(String path, CreateVpnGatewayRouteRequest requestObj, Map head) throws Exception {
        if (head == null) {
            head = new HashMap<>();
        }
        head.put("Content-Type", "application/x-www-form-urlencoded");
        return doPost(path, requestObj, head);
    }

    /**
     * get 请求
     *
     * @param path
     * @param requestObj
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doGet(String path, CreateVpnGatewayRouteRequest requestObj) throws Exception {
        Map head = new HashMap<>();
        head.putIfAbsent("Content-Type", "application/x-www-form-urlencoded");
        return doGet(path, requestObj, null);
    }

    /**
     * doDelete 请求
     *
     * @param path
     * @param requestObj
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doDelete(String path, CreateVpnGatewayRouteRequest requestObj) throws Exception {
        Map head = new HashMap<>();
        head.put("Content-Type", "application/x-www-form-urlencoded");
        return doDelete(path, requestObj, null);
    }

    /**
     * doDelete 请求
     *
     * @param path
     * @param requestObj
     * @param head
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doDelete(String path, CreateVpnGatewayRouteRequest requestObj, Map head) throws Exception {
        if (head == null) {
            head = new HashMap<>();
        }
        head.putIfAbsent("Content-Type", "application/x-www-form-urlencoded");
        JSONObject requestParams = getRequestParams(requestObj);
        String response = HttpClientUtils.httpDelete(path, requestParams, head);
        log.info("doDelete end,path:{},params:{},head:{}", path, requestParams, head);
        CreateVpnGatewayRouteResponse CreateVpnGatewayRouteResponse = JSON.parseObject(response, CreateVpnGatewayRouteResponse.class);
        return CreateVpnGatewayRouteResponse;
    }

    /**
     * doPut 请求
     *
     * @param path
     * @param requestObj
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doPut(String path, CreateVpnGatewayRouteRequest requestObj) throws Exception {
        Map head = new HashMap<>();
        head.putIfAbsent("Content-Type", "application/x-www-form-urlencoded");
        return doPut(path, requestObj, null);
    }

    /**
     * doPut 请求
     *
     * @param path
     * @param requestObj
     * @param head
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doPut(String path, CreateVpnGatewayRouteRequest requestObj, Map head) throws Exception {
        if (head == null) {
            head = new HashMap<>();
        }
        head.putIfAbsent("Content-Type", "application/x-www-form-urlencoded");
        JSONObject requestParams = getRequestParams(requestObj);
        String response = HttpClientUtils.httpPut(path, requestParams, head);
        log.info("httpPut end,path:{},params:{},head:{}", path, requestParams, head);
        CreateVpnGatewayRouteResponse CreateVpnGatewayRouteResponse = JSON.parseObject(response, CreateVpnGatewayRouteResponse.class);
        return CreateVpnGatewayRouteResponse;
    }

    /**
     * get 请求
     *
     * @param path
     * @param requestObj
     * @param head
     * @return
     * @throws Exception
     */
    public CreateVpnGatewayRouteResponse doGet(String path, CreateVpnGatewayRouteRequest requestObj, Map head) throws Exception {
        if (head == null) {
            head = new HashMap<>();
        }
        head.putIfAbsent("Content-Type", "application/x-www-form-urlencoded");
        //参数配置
        JSONObject requestParams = getSimpleRequestParams(requestObj);

        //aws4 签名
        enhanceAws4Signature(head, requestParams, credential, "get");

        String response = HttpClientUtils.httpGet(path, requestParams, head);
        log.info("doGet end,path:{},params:{},head:{}", path, requestParams, head);
        return JSON.parseObject(response, CreateVpnGatewayRouteResponse.class);
    }

    /**
     * 构造请求参数
     *
     * @param requestObj
     * @return
     */
    private JSONObject getRequestParams(CreateVpnGatewayRouteRequest requestObj) throws Exception {
        JSONObject requestParams = new JSONObject();
        //设置证书
        getCommonParams(credential, requestParams);
        //设置接口属性
        requestParams.put("Service", service);
        requestParams.put("Action", action);
        requestParams.put("Version", version);

        //设置请求体请求参数
        setRequestField(requestObj, requestParams);

        //签名
        String signature = SignUtils.signature(requestParams, credential.getSignStr());
        requestParams.put("Signature", signature);
        return requestParams;
    }

    private JSONObject getSimpleRequestParams(CreateVpnGatewayRouteRequest requestObj) throws Exception {
        JSONObject requestParams = new JSONObject();
        //设置接口属性
        requestParams.put("Action", action);
        requestParams.put("Version", version);

        //设置请求体请求参数
        setRequestField(requestObj, requestParams);
        return requestParams;
    }

    private JSONObject getPostRawRequestParams(CreateVpnGatewayRouteRequest requestObj) throws Exception {
        JSONObject requestParams = new JSONObject();
        //设置接口属性
        requestParams.put("Action", action);
        requestParams.put("Version", version);

        //设置请求体请求参数
        setRequestFieldForPostRaw(requestObj, requestParams);
        return requestParams;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy