WANG 6 years ago
parent
commit
e6e4b895fd

+ 5 - 2
pom.xml

@@ -1,6 +1,5 @@
 <?xml version="1.0"?>
 <?xml version="1.0"?>
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
-	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 	<modelVersion>4.0.0</modelVersion>
 	<modelVersion>4.0.0</modelVersion>
 	<parent>
 	<parent>
 		<groupId>cn.com.qmth.examcloud</groupId>
 		<groupId>cn.com.qmth.examcloud</groupId>
@@ -122,6 +121,10 @@
 			<groupId>org.freemarker</groupId>
 			<groupId>org.freemarker</groupId>
 			<artifactId>freemarker</artifactId>
 			<artifactId>freemarker</artifactId>
 		</dependency>
 		</dependency>
+		<dependency>
+			<groupId>com.squareup.okhttp3</groupId>
+			<artifactId>okhttp</artifactId>
+		</dependency>
 	</dependencies>
 	</dependencies>
 
 
 </project>
 </project>

+ 115 - 0
src/main/java/cn/com/qmth/examcloud/commons/util/HttpMethod.java

@@ -0,0 +1,115 @@
+package cn.com.qmth.examcloud.commons.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * HTTP method
+ *
+ * @author WANGWEI
+ * @date 2019年4月10日
+ * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
+ */
+public class HttpMethod {
+
+	public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS");
+
+	public static final HttpMethod GET = new HttpMethod("GET");
+
+	public static final HttpMethod HEAD = new HttpMethod("HEAD");
+
+	public static final HttpMethod POST = new HttpMethod("POST");
+
+	public static final HttpMethod PUT = new HttpMethod("PUT");
+
+	public static final HttpMethod PATCH = new HttpMethod("PATCH");
+
+	public static final HttpMethod DELETE = new HttpMethod("DELETE");
+
+	public static final HttpMethod TRACE = new HttpMethod("TRACE");
+
+	public static final HttpMethod CONNECT = new HttpMethod("CONNECT");
+
+	private static final Map<String, HttpMethod> METHOD_MAP = new HashMap<String, HttpMethod>();
+
+	static {
+		METHOD_MAP.put(OPTIONS.toString(), OPTIONS);
+		METHOD_MAP.put(GET.toString(), GET);
+		METHOD_MAP.put(HEAD.toString(), HEAD);
+		METHOD_MAP.put(POST.toString(), POST);
+		METHOD_MAP.put(PUT.toString(), PUT);
+		METHOD_MAP.put(PATCH.toString(), PATCH);
+		METHOD_MAP.put(DELETE.toString(), DELETE);
+		METHOD_MAP.put(TRACE.toString(), TRACE);
+		METHOD_MAP.put(CONNECT.toString(), CONNECT);
+	}
+
+	private final String name;
+
+	/**
+	 * 构造函数
+	 *
+	 * @param name
+	 */
+	private HttpMethod(String name) {
+		if (name == null) {
+			throw new NullPointerException("name");
+		}
+
+		name = name.trim();
+		if (name.length() == 0) {
+			throw new IllegalArgumentException("empty name");
+		}
+
+		for (int i = 0; i < name.length(); i++) {
+			if (Character.isISOControl(name.charAt(i)) || Character.isWhitespace(name.charAt(i))) {
+				throw new IllegalArgumentException("invalid character in name");
+			}
+		}
+
+		this.name = name;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public static HttpMethod valueOf(String name) {
+		if (name == null) {
+			throw new NullPointerException("name");
+		}
+
+		name = name.trim();
+		if (name.length() == 0) {
+			throw new IllegalArgumentException("empty name");
+		}
+
+		HttpMethod result = METHOD_MAP.get(name);
+		if (result != null) {
+			return result;
+		} else {
+			throw new IllegalArgumentException("undefined name");
+		}
+	}
+
+	@Override
+	public int hashCode() {
+		return getName().hashCode();
+	}
+
+	@Override
+	public boolean equals(Object o) {
+		if (!(o instanceof HttpMethod)) {
+			return false;
+		}
+
+		HttpMethod that = (HttpMethod) o;
+		return getName().equals(that.getName());
+	}
+
+	@Override
+	public String toString() {
+		return getName();
+	}
+
+}

+ 341 - 0
src/main/java/cn/com/qmth/examcloud/commons/util/OKHttpUtil.java

@@ -0,0 +1,341 @@
+package cn.com.qmth.examcloud.commons.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.collections.CollectionUtils;
+
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
+import okhttp3.FormBody;
+import okhttp3.MediaType;
+import okhttp3.MultipartBody;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Request.Builder;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+
+/**
+ * OKHttp
+ *
+ * @author WANGWEI
+ * @date 2018年9月6日
+ * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
+ */
+public class OKHttpUtil {
+
+	private static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(OKHttpUtil.class);
+
+	public static final class MediaTypes {
+
+		public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
+	}
+
+	/**
+	 * 请求体构建器
+	 *
+	 * @author WANGWEI
+	 * @date 2019年4月10日
+	 * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
+	 */
+	public static interface RequestBodyBuilder {
+		RequestBody build();
+	}
+
+	/**
+	 * json请求体构建器
+	 *
+	 * @author WANGWEI
+	 * @date 2019年4月10日
+	 * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
+	 */
+	public static final class JsonBodyBuilder implements RequestBodyBuilder {
+
+		private String json;
+
+		public JsonBodyBuilder(String json) {
+			super();
+			this.json = json;
+		}
+
+		@Override
+		public RequestBody build() {
+			return RequestBody.create(MediaTypes.JSON, json);
+		}
+
+		@Override
+		public String toString() {
+			return json;
+		}
+	}
+
+	private static OkHttpClient okHttpClient;
+
+	static {
+		okHttpClient = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)
+				.readTimeout(20, TimeUnit.SECONDS).build();
+	}
+
+	public static OkHttpClient getOkHttpClient() {
+		return okHttpClient;
+	}
+
+	/**
+	 * 发送请求 (带json请求体)
+	 *
+	 * @author WANGWEI
+	 * @param httpMethod
+	 * @param url
+	 * @param headers
+	 * @param jsonBody
+	 * @return
+	 */
+	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
+			String jsonBody) {
+		return call(httpMethod, url, headers, new JsonBodyBuilder(jsonBody));
+	}
+
+	/**
+	 * 发送请求 (带请求体)
+	 *
+	 * @author WANGWEI
+	 * @param httpMethod
+	 * @param url
+	 * @param headers
+	 * @param requestBodyBuilder
+	 * @return
+	 */
+	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
+			RequestBodyBuilder requestBodyBuilder) {
+
+		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
+		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
+		LOG.info("[okhttp3] body: " + requestBodyBuilder);
+
+		Builder builder = null;
+		if (httpMethod.equals(HttpMethod.POST)) {
+			builder = new Request.Builder().url(url).post(requestBodyBuilder.build());
+		} else if (httpMethod.equals(HttpMethod.PUT)) {
+			builder = new Request.Builder().url(url).put(requestBodyBuilder.build());
+		} else if (httpMethod.equals(HttpMethod.DELETE)) {
+			builder = new Request.Builder().url(url).delete(requestBodyBuilder.build());
+		}
+
+		if (null != headers && 0 != headers.size()) {
+			for (Entry<String, String> entry : headers.entrySet()) {
+				builder.addHeader(entry.getKey(), entry.getValue());
+			}
+		}
+
+		Request request = builder.build();
+
+		Response response = null;
+		try {
+			response = okHttpClient.newCall(request).execute();
+			return response;
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	/**
+	 * 发送请求
+	 *
+	 * @author WANGWEI
+	 * @param httpMethod
+	 * @param url
+	 * @return
+	 */
+	public static Response call(HttpMethod httpMethod, String url) {
+		return call(httpMethod, url, null);
+	}
+
+	/**
+	 * 发送请求
+	 *
+	 * @author WANGWEI
+	 * @param httpMethod
+	 * @param url
+	 * @param headers
+	 * @return
+	 */
+	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers) {
+
+		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
+		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
+
+		Builder builder = null;
+		if (httpMethod.equals(HttpMethod.GET)) {
+			builder = new Request.Builder().url(url);
+		} else if (httpMethod.equals(HttpMethod.POST)) {
+			builder = new Request.Builder().url(url).post(new FormBody.Builder().build());
+		} else if (httpMethod.equals(HttpMethod.PUT)) {
+			builder = new Request.Builder().url(url).put(new FormBody.Builder().build());
+		} else if (httpMethod.equals(HttpMethod.DELETE)) {
+			builder = new Request.Builder().url(url).delete(new FormBody.Builder().build());
+		}
+
+		if (null != headers && 0 != headers.size()) {
+			for (Entry<String, String> entry : headers.entrySet()) {
+				builder.addHeader(entry.getKey(), entry.getValue());
+			}
+		}
+
+		Request request = builder.build();
+
+		Response response = null;
+		try {
+			response = okHttpClient.newCall(request).execute();
+			return response;
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	/**
+	 * 发送请求 (表单)
+	 *
+	 * @author WANGWEI
+	 * @param httpMethod
+	 * @param url
+	 * @param headers
+	 * @param params
+	 * @return
+	 */
+	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
+			Map<String, String> params) {
+
+		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
+		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
+		LOG.info("[okhttp3] params: " + JsonUtil.toJson(params));
+
+		okhttp3.FormBody.Builder formBody = new FormBody.Builder();
+
+		if (null != params && 0 != params.size()) {
+			for (Entry<String, String> entry : params.entrySet()) {
+				formBody.add(entry.getKey(), entry.getValue());
+			}
+		}
+
+		Builder builder = null;
+		if (httpMethod.equals(HttpMethod.POST)) {
+			builder = new Request.Builder().url(url).post(formBody.build());
+		} else if (httpMethod.equals(HttpMethod.PUT)) {
+			builder = new Request.Builder().url(url).put(formBody.build());
+		} else if (httpMethod.equals(HttpMethod.DELETE)) {
+			builder = new Request.Builder().url(url).delete(formBody.build());
+		}
+
+		if (null != headers && 0 != headers.size()) {
+			for (Entry<String, String> entry : headers.entrySet()) {
+				builder.addHeader(entry.getKey(), entry.getValue());
+			}
+		}
+
+		Request request = builder.build();
+
+		Response response = null;
+		try {
+			response = okHttpClient.newCall(request).execute();
+			return response;
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	/**
+	 * 表单文件参数
+	 *
+	 * @author WANGWEI
+	 * @date 2019年4月10日
+	 * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
+	 */
+	public static class FileDataPart {
+
+		private String name;
+
+		private String filename;
+
+		private File file;
+
+		public FileDataPart(String name, String filename, File file) {
+			super();
+			this.name = name;
+			this.filename = filename;
+			this.file = file;
+		}
+
+		public String getName() {
+			return name;
+		}
+
+		public String getFilename() {
+			return filename;
+		}
+
+		public File getFile() {
+			return file;
+		}
+
+	}
+
+	/**
+	 * 发送请求 (包含文件表单)
+	 *
+	 * @author WANGWEI
+	 * @param httpMethod
+	 * @param url
+	 * @param headers
+	 * @param params
+	 * @param fileDataPartList
+	 * @return
+	 */
+	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
+			Map<String, String> params, List<FileDataPart> fileDataPartList) {
+
+		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
+		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
+		LOG.info("[okhttp3] params: " + JsonUtil.toJson(params));
+
+		okhttp3.MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder()
+				.setType(MultipartBody.ALTERNATIVE);
+
+		if (null != params) {
+			for (Entry<String, String> entry : params.entrySet()) {
+				multipartBodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
+			}
+		}
+
+		if (CollectionUtils.isNotEmpty(fileDataPartList)) {
+			MediaType type = MediaType.parse("application/octet-stream");
+			for (FileDataPart fileDataPart : fileDataPartList) {
+				RequestBody fileBody = RequestBody.create(type, fileDataPart.getFile());
+				multipartBodyBuilder.addFormDataPart(fileDataPart.getName(),
+						fileDataPart.getFilename(), fileBody);
+			}
+		}
+
+		Builder builder = new Request.Builder().url(url).post(multipartBodyBuilder.build());
+		if (null != headers && 0 != headers.size()) {
+			for (Entry<String, String> entry : headers.entrySet()) {
+				builder.addHeader(entry.getKey(), entry.getValue());
+			}
+		}
+
+		Request request = builder.build();
+
+		Response response = null;
+		try {
+			response = okHttpClient.newCall(request).execute();
+			return response;
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+}