mirror of https://gitee.com/makejava/EasyCode.git
添加http工具类,初步添加导入导出按钮
This commit is contained in:
parent
8ed8d06602
commit
7075407bc6
|
@ -1,6 +1,7 @@
|
|||
package com.sjhy.plugin.tool;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 集合工具类
|
||||
|
@ -20,4 +21,14 @@ public class CollectionUtil {
|
|||
public static boolean isEmpty(Collection<?> collection) {
|
||||
return collection == null || collection.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断map是否为空的
|
||||
*
|
||||
* @param map map对象
|
||||
* @return 是否为空
|
||||
*/
|
||||
public static boolean isEmpty(Map<?, ?> map) {
|
||||
return map == null || map.isEmpty();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
package com.sjhy.plugin.tool;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.util.ExceptionUtil;
|
||||
import com.sjhy.plugin.constants.MsgValue;
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.utils.HttpClientUtils;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Http工具类,按需添加方法
|
||||
*
|
||||
* @author makejava
|
||||
* @version 1.0.0
|
||||
* @since 2018/09/03 14:59
|
||||
*/
|
||||
public final class HttpUtils {
|
||||
/**
|
||||
* 用户设备标识
|
||||
*/
|
||||
private static final String USER_AGENT = "EasyCode";
|
||||
/**
|
||||
* 服务器地址
|
||||
*/
|
||||
private static final String HOST_URL = "http://www.shujuhaiyang.com/easycode";
|
||||
/**
|
||||
* http客户端
|
||||
*/
|
||||
private static final CloseableHttpClient HTTP_CLIENT = HttpClients.createDefault();
|
||||
|
||||
/**
|
||||
* 请求超时时间设置(10秒)
|
||||
*/
|
||||
private static final int TIMEOUT = 10 * 1000;
|
||||
|
||||
/**
|
||||
* 状态码
|
||||
*/
|
||||
private static final String STATE_CODE = "code";
|
||||
|
||||
/**
|
||||
* 私有构造方法
|
||||
*/
|
||||
private HttpUtils() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* get请求
|
||||
*
|
||||
* @param uri 请求地址
|
||||
* @return 返回请求结果
|
||||
*/
|
||||
public static String get(String uri) {
|
||||
HttpGet httpGet = new HttpGet(HOST_URL + uri);
|
||||
httpGet.setHeader(HttpHeaders.USER_AGENT, USER_AGENT);
|
||||
httpGet.setConfig(getDefaultConfig());
|
||||
return handlerRequest(httpGet);
|
||||
}
|
||||
|
||||
/**
|
||||
* post json请求
|
||||
*
|
||||
* @param uri 地址
|
||||
* @param param 参数
|
||||
* @return 请求返回结果
|
||||
*/
|
||||
public static String postJson(String uri, Map<String, Object> param) {
|
||||
HttpPost httpPost = new HttpPost(HOST_URL + uri);
|
||||
httpPost.setHeader(HttpHeaders.USER_AGENT, USER_AGENT);
|
||||
httpPost.setConfig(getDefaultConfig());
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
if (!CollectionUtil.isEmpty(param)) {
|
||||
httpPost.setEntity(new StringEntity(objectMapper.writeValueAsString(param), "utf-8"));
|
||||
}
|
||||
return handlerRequest(httpPost);
|
||||
} catch (JsonProcessingException e) {
|
||||
ExceptionUtil.rethrow(e);
|
||||
Messages.showWarningDialog("JSON解析出错!", MsgValue.TITLE_INFO);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static RequestConfig getDefaultConfig() {
|
||||
return RequestConfig.custom().setSocketTimeout(TIMEOUT).setConnectTimeout(TIMEOUT).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一处理请求
|
||||
*
|
||||
* @param request 请求对象
|
||||
* @return 响应字符串
|
||||
*/
|
||||
private static String handlerRequest(HttpUriRequest request) {
|
||||
try {
|
||||
CloseableHttpResponse response = HTTP_CLIENT.execute(request);
|
||||
String body = EntityUtils.toString(response.getEntity());
|
||||
HttpClientUtils.closeQuietly(response);
|
||||
// 解析JSON数据
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode = objectMapper.readTree(body);
|
||||
if (jsonNode.get(STATE_CODE).asInt() == 1) {
|
||||
return jsonNode.get("data").toString();
|
||||
}
|
||||
// 获取错误消息
|
||||
String msg = jsonNode.get("msg").asText();
|
||||
Messages.showWarningDialog(msg, MsgValue.TITLE_INFO);
|
||||
} catch (IOException e) {
|
||||
ExceptionUtil.rethrow(e);
|
||||
Messages.showWarningDialog("无法连接到服务器!", MsgValue.TITLE_INFO);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -42,11 +42,6 @@
|
|||
</model>
|
||||
</properties>
|
||||
</component>
|
||||
<vspacer id="fd955">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="3" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="94649" class="javax.swing.JTextField" binding="authorTextField">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="0" indent="0" use-parent-layout="false">
|
||||
|
@ -70,6 +65,42 @@
|
|||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<grid id="45da" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="3074e" class="javax.swing.JButton" binding="importBtn">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="导入模板"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="f42dd">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<vspacer id="a8dbe">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="4ffdd" class="javax.swing.JButton" binding="exportBtn">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="导出模板"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
||||
|
|
|
@ -1,19 +1,26 @@
|
|||
package com.sjhy.plugin.ui;
|
||||
|
||||
import com.intellij.icons.AllIcons;
|
||||
import com.intellij.openapi.options.Configurable;
|
||||
import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.openapi.options.UnnamedConfigurable;
|
||||
import com.intellij.openapi.ui.InputValidator;
|
||||
import com.intellij.openapi.ui.MessageDialogBuilder;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.sjhy.plugin.config.Settings;
|
||||
import com.sjhy.plugin.constants.MsgValue;
|
||||
import com.sjhy.plugin.tool.CollectionUtil;
|
||||
import com.sjhy.plugin.tool.HttpUtils;
|
||||
import com.sjhy.plugin.tool.StringUtils;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 主设置面板
|
||||
|
@ -39,6 +46,14 @@ public class MainSetting implements Configurable, Configurable.Composite {
|
|||
* 重置默认设置按钮
|
||||
*/
|
||||
private JButton resetBtn;
|
||||
/**
|
||||
* 模板导入按钮
|
||||
*/
|
||||
private JButton importBtn;
|
||||
/**
|
||||
* 模板导出按钮
|
||||
*/
|
||||
private JButton exportBtn;
|
||||
|
||||
/**
|
||||
* 重置列表
|
||||
|
@ -86,6 +101,35 @@ public class MainSetting implements Configurable, Configurable.Composite {
|
|||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 模板导入事件
|
||||
importBtn.addActionListener(e -> {
|
||||
String token = Messages.showInputDialog("Token:", MsgValue.TITLE_INFO, AllIcons.General.PasswordLock, "", new InputValidator() {
|
||||
@Override
|
||||
public boolean checkInput(String inputString) {
|
||||
return !StringUtils.isEmpty(inputString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canClose(String inputString) {
|
||||
return this.checkInput(inputString);
|
||||
}
|
||||
});
|
||||
String result = HttpUtils.get(String.format("/template?token=%s", token));
|
||||
// 解析数据
|
||||
// 覆盖提示
|
||||
Messages.showInfoMessage(result, MsgValue.TITLE_INFO);
|
||||
});
|
||||
|
||||
// 模板导出事件
|
||||
importBtn.addActionListener(e -> {
|
||||
// 选择要分享的数据
|
||||
Map<String, Object> param = new HashMap<>();
|
||||
// 上传数据
|
||||
String result = HttpUtils.postJson("/template", param);
|
||||
// 参数token
|
||||
Messages.showInfoMessage(result, MsgValue.TITLE_INFO);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue