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

io.github.yedaxia.apidocs.ext.rap.RapSupport Maven / Gradle / Ivy

There is a newer version: 1.4.4
Show newest version
package io.github.yedaxia.apidocs.ext.rap;

import io.github.yedaxia.apidocs.DocContext;
import io.github.yedaxia.apidocs.Docs;
import io.github.yedaxia.apidocs.LogUtils;
import io.github.yedaxia.apidocs.Utils;
import io.github.yedaxia.apidocs.http.DHttpRequest;
import io.github.yedaxia.apidocs.http.DHttpResponse;
import io.github.yedaxia.apidocs.http.DHttpUtils;
import io.github.yedaxia.apidocs.parser.ControllerNode;

import java.io.IOException;
import java.util.*;

/**
 * post request to rap
 *
 * @author yeguozhong yedaxia.github.com
 */
public class RapSupport {

    private String rapHost;
    private Integer projectId; // project id in rap
    private String cookie;

    private List controllerNodeList;

    public RapSupport(List controllerNodeList) {
        this.controllerNodeList = controllerNodeList;
    }

    /**
     * do post
     */
    public void postToRap(){

        Docs.DocsConfig docsConfig = DocContext.getDocsConfig();
        if(controllerNodeList == null
                || controllerNodeList.isEmpty()
                || docsConfig == null
                || docsConfig.getRapHost() == null
                || docsConfig.getRapProjectId() == null){
            LogUtils.warn("docs config properties miss, we don't think you want to post to rap!");
            return;
        }

        this.rapHost = docsConfig.getRapHost();
        this.projectId = Integer.valueOf(docsConfig.getRapProjectId());
        this.cookie = docsConfig.getRapLoginCookie();

        if(!Utils.isNotEmpty(cookie)){
            String account = docsConfig.getRapAccount();
            String password = docsConfig.getRapPassword();
            DHttpResponse response = doLogin(loginUrl(rapHost),account, password);
            this.cookie = response.getHeader("Set-Cookie");
        }

        Set moduleSet = getModuleList();

        ProjectForm projectForm = new ProjectForm();
        projectForm.setId(projectId);

        Set deleteModuleForms = new HashSet<>(moduleSet.size());
        if(moduleSet != null && !moduleSet.isEmpty()){
            for(Module module : moduleSet){
                if(Module.NAME.equalsIgnoreCase(module.getName())){
                    DeleteActionFrom delForm = new DeleteActionFrom();
                    delForm.setClassName("Module");
                    delForm.setId(module.getId());
                    deleteModuleForms.add(delForm);
                }
            }
        }
        projectForm.setDeletedObjectListData(Utils.toJson(deleteModuleForms));

        Project project = Project.valueOf(projectId, controllerNodeList);
        projectForm.setProjectData(Utils.toJson(project));

        postProject(projectForm);
    }

    public DHttpResponse doLogin(String loginUrl, String userName, String password){
        DHttpRequest request = new DHttpRequest();
        request.setAutoRedirect(false);
        request.setUrl(loginUrl);
        request.addParam("account", userName);
        request.addParam("password", password);
        try {
            return DHttpUtils.httpPost(request);
        }catch (IOException ex){
            LogUtils.error("login rap fail , userName : %s, pass : %s", userName, password);
            throw new RuntimeException(ex);
        }
    }

    private Set getModuleList(){
        try{
            DHttpResponse modelResp = DHttpUtils.httpGet(queryModelUrl(rapHost, projectId));
            if(modelResp.getCode() == 200){
                ModelResponse model = Utils.jsonToObject(modelResp.streamAsString(), ModelResponse.class);
                return model.getModel().getModuleList();
            }else{
                LogUtils.error("request module data fail, rapHost : %s, projectId : %s", rapHost, projectId);
                throw new RuntimeException("request module data fail , code : " + modelResp.getCode());
            }
        }catch (IOException e){
            LogUtils.error("get rap models fail", e);
            throw new RuntimeException(e);
        }
    }

    private void postProject(ProjectForm projectForm){
        DHttpRequest request = new DHttpRequest();
        request.setUrl(checkInUrl(rapHost));
        Map params = new HashMap<>();
        params.put("id", String.valueOf(projectForm.getId()));
        params.put("projectData", projectForm.getProjectData());
        if(projectForm.getDeletedObjectListData() != null){
            params.put("deletedObjectListData", projectForm.getDeletedObjectListData());
        }
        if(projectForm.getDescription() != null){
            params.put("description", projectForm.getDescription());
        }
        if(projectForm.getVersionPosition() != null){
            params.put("versionPosition", projectForm.getVersionPosition());
        }
        request.setParams(params);

        Map headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        headers.put("Cookie", cookie);
        request.setHeaders(headers);

        try{
            DHttpResponse  response = DHttpUtils.httpPost(request);
            if(response.getCode() == 200){
                LogUtils.info("post project to rap success, response : %s " , response.streamAsString());
            }else{
                LogUtils.error("post project to rap fail !!! code : %s", response.streamAsString());
            }
        }catch (IOException e){
            LogUtils.error("post project to rap fail", e);
            throw new RuntimeException(e);
        }
    }

    private String queryModelUrl(String host, Integer projectId){
        return String.format("%s/api/queryModel.do?projectId=%s",host, projectId);
    }

    private String checkInUrl(String host){
        return String.format("%s/workspace/checkIn.do", host);
    }

    private String loginUrl(String host){
        return String.format("%s/account/doLogin.do", host);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy