Search in sources :

Example 11 with HttpRequest

use of cn.hutool.http.HttpRequest in project Jpom by dromara.

the class WebHookUtil method send.

/**
 * 发送钉钉群自定义机器人消息
 *
 * @param notify  通知对象
 * @param title   描述标签
 * @param context 消息内容
 */
@Override
public void send(MonitorModel.Notify notify, String title, String context) {
    JSONObject text = new JSONObject();
    JSONObject param = new JSONObject();
    // 消息内容
    text.put("content", title + "\n" + context);
    param.put("msgtype", "text");
    param.put("text", text);
    HttpRequest request = HttpUtil.createPost(notify.getValue()).contentType(MediaType.APPLICATION_JSON_VALUE).body(param.toJSONString());
    request.execute();
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) JSONObject(com.alibaba.fastjson.JSONObject)

Example 12 with HttpRequest

use of cn.hutool.http.HttpRequest in project Jpom by dromara.

the class TestGithub method testUser.

@Test
public void testUser() {
    HttpRequest request = HttpUtil.createGet("https://api.github.com/user");
    request.header("Authorization", "token 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");
    request.header("Accept", "application/vnd.github.v3+json");
    String body = request.execute().body();
    System.out.println(body);
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) Test(org.junit.Test)

Example 13 with HttpRequest

use of cn.hutool.http.HttpRequest in project Jpom by dromara.

the class TestGithub method test.

@Test
public void test() {
    HttpRequest post = HttpUtil.createPost("https://api.github.com/repositories?since=824");
    String body = post.execute().body();
    System.out.println(body);
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) Test(org.junit.Test)

Example 14 with HttpRequest

use of cn.hutool.http.HttpRequest in project Jpom by dromara.

the class RepositoryController method githubRepos.

/**
 * github 仓库
 *
 * @param token 个人令牌
 * @param page  分页
 * @return page
 */
private PageResultDto<JSONObject> githubRepos(String token, Page page) {
    String accept = "application/vnd.github.v3+json";
    HttpRequest request = HttpUtil.createGet("https://api.github.com/user");
    request.header("Authorization", StrUtil.format("token {}", token));
    request.header("Accept", accept);
    HttpResponse httpResponse = request.execute();
    String body = httpResponse.body();
    Assert.state(httpResponse.isOk(), "令牌信息错误:" + body);
    JSONObject userBody = JSONObject.parseObject(body);
    String username = userBody.getString("login");
    // 拉取仓库信息
    HttpRequest httpRequestRepos = HttpUtil.createGet("https://api.github.com/user/repos");
    httpRequestRepos.header("Authorization", StrUtil.format("token {}", token));
    httpRequestRepos.header("Accept", accept);
    HttpResponse reposResponse = httpRequestRepos.form("access_token", token).form("sort", "pushed").form("page", page.getPageNumber()).form("per_page", page.getPageSize()).execute();
    body = reposResponse.body();
    Assert.state(reposResponse.isOk(), "拉取仓库信息错误:" + body);
    JSONArray jsonArray = JSONArray.parseArray(body);
    List<JSONObject> objects = jsonArray.stream().map(o -> {
        JSONObject repo = (JSONObject) o;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", repo.getString("name"));
        String cloneUrl = repo.getString("clone_url");
        jsonObject.put("url", cloneUrl);
        jsonObject.put("full_name", repo.getString("full_name"));
        jsonObject.put("description", repo.getString("description"));
        jsonObject.put("private", repo.getBooleanValue("private"));
        // 
        jsonObject.put("username", username);
        jsonObject.put("exists", RepositoryController.this.checkRepositoryUrl(null, cloneUrl));
        return jsonObject;
    }).collect(Collectors.toList());
    // 
    PageResultDto<JSONObject> pageResultDto = new PageResultDto<>(page.getPageNumber(), page.getPageSize(), 1000);
    pageResultDto.setResult(objects);
    return pageResultDto;
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) PageResultDto(io.jpom.model.PageResultDto) BuildUtil(io.jpom.build.BuildUtil) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) io.jpom.plugin(io.jpom.plugin) Page(cn.hutool.db.Page) Feature(io.jpom.permission.Feature) ServletUtil(cn.hutool.extra.servlet.ServletUtil) JpomRuntimeException(io.jpom.system.JpomRuntimeException) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpUtil(cn.hutool.http.HttpUtil) HttpResponse(cn.hutool.http.HttpResponse) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping) URLUtil(cn.hutool.core.util.URLUtil) MethodFeature(io.jpom.permission.MethodFeature) HttpRequest(cn.hutool.http.HttpRequest) PostMapping(org.springframework.web.bind.annotation.PostMapping) RepositoryService(io.jpom.service.dblog.RepositoryService) ClassFeature(io.jpom.permission.ClassFeature) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) File(java.io.File) HttpStatus(org.springframework.http.HttpStatus) Tuple(cn.hutool.core.lang.Tuple) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) ValidatorItem(cn.jiangzeyin.common.validator.ValidatorItem) RepositoryModel(io.jpom.model.data.RepositoryModel) Const(io.jpom.common.Const) BuildInfoService(io.jpom.service.dblog.BuildInfoService) Convert(cn.hutool.core.convert.Convert) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) Entity(cn.hutool.db.Entity) GitProtocolEnum(io.jpom.model.enums.GitProtocolEnum) BaseServerController(io.jpom.common.BaseServerController) Validator(cn.hutool.core.lang.Validator) Assert(org.springframework.util.Assert) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) HttpResponse(cn.hutool.http.HttpResponse) PageResultDto(io.jpom.model.PageResultDto)

Example 15 with HttpRequest

use of cn.hutool.http.HttpRequest in project Jpom by dromara.

the class AbstractTomcatCommander method getStatus.

/**
 * 检查tomcat状态
 *
 * @param tomcatInfoModel tomcat信息
 * @param cmd             操作命令
 * @return 状态结果
 */
protected String getStatus(TomcatInfoModel tomcatInfoModel, String cmd) {
    String strReturn = "start".equals(cmd) ? "stopped" : "started";
    int i = 0;
    while (i < 10) {
        int result = 0;
        String url = String.format("http://127.0.0.1:%d/", tomcatInfoModel.getPort());
        HttpRequest httpRequest = new HttpRequest(url);
        // 设置超时时间为3秒
        httpRequest.setConnectionTimeout(3000);
        try {
            httpRequest.execute();
            result = 1;
        } catch (Exception ignored) {
        }
        i++;
        if ("start".equals(cmd) && result == 1) {
            strReturn = "started";
            break;
        }
        if ("stop".equals(cmd) && result == 0) {
            strReturn = "stopped";
            break;
        }
        ThreadUtil.sleep(1000);
    }
    return strReturn;
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) JpomRuntimeException(io.jpom.system.JpomRuntimeException) IOException(java.io.IOException)

Aggregations

HttpRequest (cn.hutool.http.HttpRequest)21 Test (org.junit.Test)9 JSONObject (com.alibaba.fastjson.JSONObject)6 Ignore (org.junit.Ignore)5 HttpResponse (cn.hutool.http.HttpResponse)4 JpomRuntimeException (io.jpom.system.JpomRuntimeException)4 File (java.io.File)3 IOException (java.io.IOException)3 JsonMessage (cn.jiangzeyin.common.JsonMessage)2 JSONArray (com.alibaba.fastjson.JSONArray)2 Convert (cn.hutool.core.convert.Convert)1 FileUtil (cn.hutool.core.io.FileUtil)1 Tuple (cn.hutool.core.lang.Tuple)1 Validator (cn.hutool.core.lang.Validator)1 StrUtil (cn.hutool.core.util.StrUtil)1 URLUtil (cn.hutool.core.util.URLUtil)1 Entity (cn.hutool.db.Entity)1 Page (cn.hutool.db.Page)1 ServletUtil (cn.hutool.extra.servlet.ServletUtil)1 HttpUtil (cn.hutool.http.HttpUtil)1