mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2025-12-17 21:03:03 +00:00
223
web-service/src/test/java/cn/qaiu/web/test/Test01.java
Normal file
223
web-service/src/test/java/cn/qaiu/web/test/Test01.java
Normal file
@@ -0,0 +1,223 @@
|
||||
package cn.qaiu.web.test;
|
||||
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.beanutils.BeanUtils;
|
||||
import org.apache.commons.beanutils.ConvertUtils;
|
||||
import org.apache.commons.beanutils.Converter;
|
||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||
import org.apache.commons.lang3.time.DateUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <br>Create date 2021/4/29 15:27
|
||||
*
|
||||
* @author <a href="https://qaiu.top">QAIU</a>
|
||||
*/
|
||||
@Slf4j
|
||||
public class Test01 {
|
||||
|
||||
public static class A {
|
||||
String name;
|
||||
String num;
|
||||
String num2;
|
||||
String num3;
|
||||
|
||||
Integer num5;
|
||||
|
||||
public Integer getNum5() {
|
||||
return num5;
|
||||
}
|
||||
|
||||
public void setNum5(Integer num5) {
|
||||
this.num5 = num5;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getNum() {
|
||||
return num;
|
||||
}
|
||||
|
||||
public void setNum(String num) {
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
public String getNum2() {
|
||||
return num2;
|
||||
}
|
||||
|
||||
public void setNum2(String num2) {
|
||||
this.num2 = num2;
|
||||
}
|
||||
|
||||
public String getNum3() {
|
||||
return num3;
|
||||
}
|
||||
|
||||
public void setNum3(String num3) {
|
||||
this.num3 = num3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class B0 {
|
||||
int num;
|
||||
|
||||
public int getNum() {
|
||||
return num;
|
||||
}
|
||||
|
||||
public void setNum(int num) {
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class B extends B0 {
|
||||
String name;
|
||||
|
||||
boolean flag;
|
||||
int num4;
|
||||
Date date;
|
||||
String dateStr;
|
||||
|
||||
Integer num5;
|
||||
|
||||
public Boolean getFlag() {
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void setFlag(Boolean flag) {
|
||||
this.flag = flag;
|
||||
}
|
||||
|
||||
public Integer getNum5() {
|
||||
return num5;
|
||||
}
|
||||
|
||||
public void setNum5(Integer num5) {
|
||||
this.num5 = num5;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getDateStr() {
|
||||
return dateStr;
|
||||
}
|
||||
|
||||
public void setDateStr(String dateStr) {
|
||||
this.dateStr = dateStr;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getNum4() {
|
||||
return num4;
|
||||
}
|
||||
|
||||
public void setNum4(int num4) {
|
||||
this.num4 = num4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "B{" +
|
||||
"num=" + num +
|
||||
", name='" + name + '\'' +
|
||||
", flag=" + flag +
|
||||
", num4=" + num4 +
|
||||
", date=" + date +
|
||||
", dateStr='" + dateStr + '\'' +
|
||||
", num5=" + num5 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static <T> T getParamsToBean(RoutingContext ctx, Class<T> tClass) {
|
||||
// ObjectUtils.identityToString()
|
||||
return null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test01() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
|
||||
|
||||
A a = new A();
|
||||
a.setName("asd");
|
||||
a.setNum("123");
|
||||
a.setNum2("123");
|
||||
a.setNum3("123");
|
||||
a.setNum5(9999);
|
||||
B b = new B();
|
||||
BeanUtils.copyProperties(b, a);
|
||||
System.out.println(b);
|
||||
a.setNum5(233);
|
||||
System.out.println(b);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("name", "小米");
|
||||
map.put("flag", "1");
|
||||
map.put("num", "553454344");
|
||||
map.put("num2", "123");
|
||||
map.put("num4", "q");
|
||||
map.put("dateStr", new Date());
|
||||
map.put("date", "2021-01-01");
|
||||
B b1 = new B();
|
||||
|
||||
ConvertUtils.register(
|
||||
new Converter() {
|
||||
@Override
|
||||
public <T> T convert(Class<T> clazz, Object value) {
|
||||
//字符串转换为日期
|
||||
try {
|
||||
return (T) DateUtils.parseDate(value.toString(), "yyyy-MM-dd");
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}, Date.class);
|
||||
|
||||
|
||||
ConvertUtils.register(
|
||||
new Converter() {
|
||||
@Override
|
||||
public <T> T convert(Class<T> clazz, Object value) {
|
||||
//日期->字符串
|
||||
try {
|
||||
return (T) DateFormatUtils.format((Date) value, "yyyy-MM-dd");
|
||||
} catch (Exception e) {
|
||||
return (T) value;
|
||||
}
|
||||
}
|
||||
}, String.class);
|
||||
|
||||
BeanUtils.populate(b1, map);
|
||||
log.info("---------> {}", b1);
|
||||
}
|
||||
|
||||
}
|
||||
85
web-service/src/test/java/cn/qaiu/web/test/Test02.java
Normal file
85
web-service/src/test/java/cn/qaiu/web/test/Test02.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package cn.qaiu.web.test;
|
||||
|
||||
import javassist.ClassPool;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtMethod;
|
||||
import javassist.NotFoundException;
|
||||
import javassist.bytecode.CodeAttribute;
|
||||
import javassist.bytecode.ExceptionsAttribute;
|
||||
import javassist.bytecode.LocalVariableAttribute;
|
||||
import javassist.bytecode.MethodInfo;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Test02 {
|
||||
|
||||
|
||||
public String[] getParameterName(Class<?> className, String method) {
|
||||
String[] paramNames = null;
|
||||
try {
|
||||
ClassPool pool = ClassPool.getDefault();
|
||||
CtClass ctClass = pool.get(className.getName());
|
||||
CtMethod cm = ctClass.getDeclaredMethod(method);
|
||||
MethodInfo methodInfo = cm.getMethodInfo();
|
||||
CtClass[] parameterTypes = cm.getParameterTypes();
|
||||
|
||||
for (CtClass parameterType : parameterTypes) {
|
||||
System.out.println(parameterType.getDeclaringClass());
|
||||
System.out.println(parameterType.getName() + "----" + parameterType.getSimpleName());
|
||||
}
|
||||
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
|
||||
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
|
||||
.getAttribute(LocalVariableAttribute.tag);
|
||||
paramNames = new String[cm.getParameterTypes().length];
|
||||
CtClass[] exceptionTypes = cm.getExceptionTypes();
|
||||
ExceptionsAttribute exceptionsAttribute = methodInfo.getExceptionsAttribute();
|
||||
|
||||
for (int j = 0; j < paramNames.length; j++) {
|
||||
String s = attr.variableName(attr.tableLength() - paramNames.length + j);
|
||||
paramNames[j] = s;
|
||||
}
|
||||
|
||||
} catch (NotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return paramNames;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test01() throws NoSuchMethodException {
|
||||
|
||||
//
|
||||
// Method[] methods = RealUser.class.getMethods();
|
||||
// for (Method m : methods) {
|
||||
// if (m.getName().equals("setUsername2")) {
|
||||
// Class<?>[] parameterTypes = m.getParameterTypes();
|
||||
// for (Class<?> type : parameterTypes) {
|
||||
// System.out.println(type + "--"+type.getName());
|
||||
// System.out.println(type.isPrimitive());
|
||||
// System.out.println("------------");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
System.out.println(("java.lang.Double".matches("^java\\.lang\\.((Integer)|(Double))$")));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test3() {
|
||||
Map map = new LinkedHashMap();
|
||||
map.put("1", "1");
|
||||
map.put("2", "11");
|
||||
map.put("3", "111");
|
||||
|
||||
System.out.println(map);
|
||||
map.put("1", "12");
|
||||
System.out.println(map);
|
||||
|
||||
}
|
||||
}
|
||||
66
web-service/src/test/java/cn/qaiu/web/test/TestJsoup.java
Normal file
66
web-service/src/test/java/cn/qaiu/web/test/TestJsoup.java
Normal file
File diff suppressed because one or more lines are too long
164
web-service/src/test/java/cn/qaiu/web/test/TestOS.java
Normal file
164
web-service/src/test/java/cn/qaiu/web/test/TestOS.java
Normal file
@@ -0,0 +1,164 @@
|
||||
package cn.qaiu.web.test;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
public class TestOS {
|
||||
//通过截取cmd流方式得到计算机的配置信息(不好)
|
||||
public static List<String> getIpAddress() {
|
||||
Process p = null;
|
||||
List<String> address = new ArrayList<String>();
|
||||
try {
|
||||
p = new ProcessBuilder("ipconfig", "/all").start();
|
||||
} catch (Exception e) {
|
||||
return address;
|
||||
}
|
||||
StringBuffer sb = new StringBuffer();
|
||||
//读取进程输出值
|
||||
InputStream inputStream = p.getInputStream();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
|
||||
String s = "";
|
||||
try {
|
||||
while ((s = br.readLine()) != null) {
|
||||
sb.append(s + "\n");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println(sb);
|
||||
return address;
|
||||
}
|
||||
|
||||
public static void getIpconfig() {
|
||||
Map<String, String> map = System.getenv();
|
||||
System.out.println(map.get("USERNAME"));//获取username
|
||||
System.out.println(map.get("COMPUTERNAME"));//获取计算机名
|
||||
System.out.println(map.get("USERDOMAIN"));//获取计算机域名
|
||||
}
|
||||
|
||||
//得到计算机的ip地址和mac地址
|
||||
public static void getConfig() {
|
||||
try {
|
||||
InetAddress address = InetAddress.getLocalHost();
|
||||
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
|
||||
//ni.getInetAddresses().nextElement().getAddress();
|
||||
byte[] mac = ni.getHardwareAddress();
|
||||
String sIP = address.getHostAddress();
|
||||
String sMAC = "";
|
||||
Formatter formatter = new Formatter();
|
||||
for (int i = 0; i < mac.length; i++) {
|
||||
sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i],
|
||||
(i < mac.length - 1) ?
|
||||
"-" : "").toString();
|
||||
}
|
||||
System.out.println("IP:" + sIP);
|
||||
System.out.println("MAC:" + sMAC);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//得到计算机的ip,名称,操作系统名称,操作系统版本号
|
||||
public static void Config() {
|
||||
try {
|
||||
InetAddress addr = InetAddress.getLocalHost();
|
||||
String ip = addr.getHostAddress().toString(); //获取本机ip
|
||||
String hostName = addr.getHostName().toString(); //获取本机计算机名称
|
||||
System.out.println("本机IP:" + ip + "\n本机名称:" + hostName);
|
||||
Properties props = System.getProperties();
|
||||
System.out.println("操作系统的名称:" + props.getProperty("os.name"));
|
||||
System.out.println("操作系统的版本号:" + props.getProperty("os.version"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//其他的一些东西,会实用到的时候的
|
||||
public static void all() {
|
||||
Properties props = System.getProperties();
|
||||
System.out.println("Java的执行环境版本号:" + props.getProperty("java.version"));
|
||||
System.out.println("Java的执行环境供应商:" + props.getProperty("java.vendor"));
|
||||
System.out.println("Java供应商的URL:" + props.getProperty("java.vendor.url"));
|
||||
System.out.println("Java的安装路径:" + props.getProperty("java.home"));
|
||||
System.out.println("Java的虚拟机规范版本号:" + props.getProperty("java.vm.specification.version"));
|
||||
System.out.println("Java的虚拟机规范供应商:" + props.getProperty("java.vm.specification.vendor"));
|
||||
System.out.println("Java的虚拟机规范名称:" + props.getProperty("java.vm.specification.name"));
|
||||
System.out.println("Java的虚拟机实现版本号:" + props.getProperty("java.vm.version"));
|
||||
System.out.println("Java的虚拟机实现供应商:" + props.getProperty("java.vm.vendor"));
|
||||
System.out.println("Java的虚拟机实现名称:" + props.getProperty("java.vm.name"));
|
||||
System.out.println("Java执行时环境规范版本号:" + props.getProperty("java.specification.version"));
|
||||
System.out.println("Java执行时环境规范供应商:" + props.getProperty("java.specification.vender"));
|
||||
System.out.println("Java执行时环境规范名称:" + props.getProperty("java.specification.name"));
|
||||
System.out.println("Java的类格式版本号号:" + props.getProperty("java.class.version"));
|
||||
System.out.println("Java的类路径:" + props.getProperty("java.class.path"));
|
||||
System.out.println("载入库时搜索的路径列表:" + props.getProperty("java.library.path"));
|
||||
System.out.println("默认的暂时文件路径:" + props.getProperty("java.io.tmpdir"));
|
||||
System.out.println("一个或多个扩展文件夹的路径:" + props.getProperty("java.ext.dirs"));
|
||||
System.out.println("操作系统的名称:" + props.getProperty("os.name"));
|
||||
System.out.println("操作系统的构架:" + props.getProperty("os.arch"));
|
||||
System.out.println("操作系统的版本号:" + props.getProperty("os.version"));
|
||||
System.out.println("文件分隔符:" + props.getProperty("file.separator"));
|
||||
//在 unix 系统中是"/"
|
||||
System.out.println("路径分隔符:" + props.getProperty("path.separator"));
|
||||
//在 unix 系统中是":"
|
||||
System.out.println("行分隔符:" + props.getProperty("line.separator"));
|
||||
//在 unix 系统中是"/n"
|
||||
System.out.println("用户的账户名称:" + props.getProperty("user.name"));
|
||||
System.out.println("用户的主文件夹:" + props.getProperty("user.home"));
|
||||
System.out.println("用户的当前工作文件夹:" + props.getProperty("user.dir"));
|
||||
}
|
||||
|
||||
|
||||
public void showURL() throws IOException {
|
||||
|
||||
// 第一种:获取类加载的根路径 D:\git\daotie\daotie\target\classes
|
||||
File f = new File(this.getClass().getResource("/").getPath());
|
||||
System.out.println(f);
|
||||
|
||||
// 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录 D:\git\daotie\daotie\target\classes\my
|
||||
File f2 = new File(this.getClass().getResource("").getPath());
|
||||
System.out.println(f2);
|
||||
|
||||
// 第二种:获取项目路径 D:\git\daotie\daotie
|
||||
File directory = new File("");// 参数为空
|
||||
String courseFile = directory.getCanonicalPath();
|
||||
System.out.println(courseFile);
|
||||
|
||||
|
||||
// 第三种: file:/D:/git/daotie/daotie/target/classes/
|
||||
URL xmlpath = this.getClass().getClassLoader().getResource("");
|
||||
System.out.println(xmlpath);
|
||||
|
||||
|
||||
// 第四种: D:\git\daotie\daotie
|
||||
System.out.println(System.getProperty("user.dir"));
|
||||
/*
|
||||
* 结果: C:\Documents and Settings\Administrator\workspace\projectName
|
||||
* 获取当前工程路径
|
||||
*/
|
||||
|
||||
// 第五种: 获取所有的类路径 包括jar包的路径
|
||||
System.out.println(System.getProperty("java.class.path"));
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
// getConfig();
|
||||
// Config();
|
||||
// all();
|
||||
// new TestOS().showURL();
|
||||
System.out.println(File.separator);
|
||||
System.out.println(URLEncoder.encode("https://www.ecpan.cn/web/#/yunpanProxy", StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.qaiu.web.test;
|
||||
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.ext.web.client.WebClient;
|
||||
|
||||
public class TestWebClient2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Vertx vertx = Vertx.vertx();
|
||||
WebClient client = WebClient.create(vertx);
|
||||
client.getAbs("https://qaiu.top").send().onSuccess(res -> {
|
||||
System.out.println(res.bodyAsString());
|
||||
client.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package cn.qaiu.web.test;
|
||||
|
||||
import io.vertx.core.MultiMap;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.http.HttpHeaders;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.client.HttpResponse;
|
||||
import io.vertx.ext.web.client.WebClient;
|
||||
import io.vertx.ext.web.multipart.MultipartForm;
|
||||
import io.vertx.ext.web.multipart.impl.MultipartFormImpl;
|
||||
|
||||
public class WebClientExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Vertx vertx = Vertx.vertx();
|
||||
WebClient client = WebClient.create(vertx);
|
||||
|
||||
MultipartForm form = new MultipartFormImpl()
|
||||
.attribute("email", "736226400@qq.com")
|
||||
.attribute("password", "");
|
||||
|
||||
client.postAbs("https://cowtransfer.com/api/user/emaillogin")
|
||||
.putHeader(HttpHeaders.CONTENT_TYPE.toString(), "multipart/form-data; boundary=WebAppBoundary")
|
||||
.sendMultipartForm(form, ar -> {
|
||||
if (ar.succeeded()) {
|
||||
HttpResponse<Buffer> response = ar.result();
|
||||
System.out.println("Response status code: " + response.statusCode());
|
||||
|
||||
// Print all response headers
|
||||
MultiMap headers = response.headers();
|
||||
headers.names().forEach(name -> {
|
||||
System.out.println(name + ": " + headers.getAll(name));
|
||||
});
|
||||
|
||||
JsonObject responseBody = response.bodyAsJsonObject();
|
||||
System.out.println("Response body: " + responseBody.encodePrettily());
|
||||
} else {
|
||||
System.out.println("Something went wrong: " + ar.cause().getMessage());
|
||||
}
|
||||
vertx.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
169
web-service/src/test/java/cn/qaiu/web/test/WebProxyExamples.java
Normal file
169
web-service/src/test/java/cn/qaiu/web/test/WebProxyExamples.java
Normal file
@@ -0,0 +1,169 @@
|
||||
package cn.qaiu.web.test;
|
||||
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.http.HttpMethod;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.handler.StaticHandler;
|
||||
import io.vertx.ext.web.proxy.handler.ProxyHandler;
|
||||
import io.vertx.httpproxy.HttpProxy;
|
||||
import io.vertx.httpproxy.ProxyRequest;
|
||||
import io.vertx.httpproxy.ProxyResponse;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:emad.albloushi@gmail.com">Emad Alblueshi</a>
|
||||
*/
|
||||
|
||||
public class WebProxyExamples {
|
||||
|
||||
public void origin() {
|
||||
HttpServer backendServer = vertx.createHttpServer();
|
||||
|
||||
Router backendRouter = Router.router(vertx);
|
||||
backendRouter.route().handler(ctx -> {
|
||||
System.out.println(ctx.request().path());
|
||||
ctx.next();
|
||||
});
|
||||
|
||||
backendRouter.route(HttpMethod.GET, "/demo/foo").handler(rc -> rc.response()
|
||||
.putHeader("content-type", "text/html")
|
||||
.end("<html><body><h1>I'm the target resource111!</h1></body></html>"));
|
||||
backendRouter.route(HttpMethod.GET, "/demo/a")
|
||||
.handler(rc -> rc.response().putHeader("content-type", "text/html").end("AAA"));
|
||||
backendRouter.route(HttpMethod.GET, "/demo/b")
|
||||
.handler(rc -> rc.response().putHeader("content-type", "text/html").end("BBB"));
|
||||
|
||||
backendServer.requestHandler(backendRouter).listen(7070);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
/a -> 7070/foo/a
|
||||
/aaa/b -> '7070/foo/' -> 7070/foo/b
|
||||
/aaa/b -> /foo/b -> 7070/foo/b
|
||||
|
||||
/aaa/b -> '7070/foo' -> 7070/foob
|
||||
/aaa/a -> '7070/' -> 7070/aaa/a
|
||||
/aaa/a -> '7070/aaa/' -> 7070/aaa/a
|
||||
|
||||
|
||||
*/
|
||||
|
||||
public Vertx vertx = Vertx.vertx();
|
||||
public HttpClient proxyClient = vertx.createHttpClient();
|
||||
// 创建 http代理处理器
|
||||
HttpProxy httpProxy = HttpProxy.reverseProxy(proxyClient);
|
||||
// 代理处理器绑定到路由
|
||||
Router proxyRouter = Router.router(vertx);
|
||||
|
||||
public void route() {
|
||||
httpProxy.origin(7070, "localhost");
|
||||
|
||||
proxyRouter.route("/demo/*").handler(ProxyHandler.create(httpProxy));
|
||||
proxyRouter.route("/api/*").handler(ctx -> ctx.reroute(ctx.request().path().replaceAll("^/api/", "/demo/")));
|
||||
|
||||
// Router r1 = Router.router(vertx);
|
||||
// r1.route().handler(ctx -> {
|
||||
// int statusCode = ctx.response().getStatusCode();
|
||||
// if (statusCode == 404) {
|
||||
// ctx.response().write("subRouter ---------------> 404");
|
||||
// ctx.end();
|
||||
// }
|
||||
// });
|
||||
|
||||
proxyRouter.route("/*").handler(StaticHandler.create("webroot/test"));
|
||||
|
||||
proxyRouter.errorHandler(404, this::handle404);
|
||||
|
||||
// proxyRouter.route("/api/*").handler(ctx -> ctx.end("123123"));
|
||||
// 路由绑定到代理服务器
|
||||
HttpServer proxyServer = vertx.createHttpServer();
|
||||
proxyServer.requestHandler(proxyRouter);
|
||||
proxyServer.listen(1080);
|
||||
}
|
||||
|
||||
private void handle404(RoutingContext routingContext) {
|
||||
routingContext.end(routingContext.request().path() + "-------> 404");
|
||||
}
|
||||
|
||||
public void routeShort(Vertx vertx, Router proxyRouter) {
|
||||
HttpClient proxyClient = vertx.createHttpClient();
|
||||
|
||||
HttpProxy httpProxy = HttpProxy.reverseProxy(proxyClient);
|
||||
|
||||
proxyRouter
|
||||
.route(HttpMethod.GET, "/*")
|
||||
.handler(ProxyHandler.create(httpProxy, 7070, "localhost"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void lowLevel() {
|
||||
HttpServer proxyServer = vertx.createHttpServer();
|
||||
proxyServer.requestHandler(outboundRequest -> {
|
||||
ProxyRequest proxyRequest = ProxyRequest.reverseProxy(outboundRequest);
|
||||
|
||||
proxyClient.request(proxyRequest.getMethod(), 443, "qaiu.top", proxyRequest.getURI())
|
||||
.compose(proxyRequest::send)
|
||||
// Send the proxy response
|
||||
.onSuccess(ProxyResponse::send)
|
||||
.onFailure(err -> {
|
||||
// Release the request
|
||||
proxyRequest.release();
|
||||
|
||||
// Send error
|
||||
outboundRequest.response().setStatusCode(500)
|
||||
.send();
|
||||
});
|
||||
}).listen(8181);
|
||||
}
|
||||
|
||||
|
||||
public void multi(Vertx vertx, Router proxyRouter) {
|
||||
HttpClient proxyClient = vertx.createHttpClient();
|
||||
|
||||
HttpProxy httpProxy1 = HttpProxy.reverseProxy(proxyClient);
|
||||
httpProxy1.origin(7070, "localhost");
|
||||
|
||||
HttpProxy httpProxy2 = HttpProxy.reverseProxy(proxyClient);
|
||||
httpProxy2.origin(6060, "localhost");
|
||||
|
||||
proxyRouter
|
||||
.route(HttpMethod.GET, "/foo").handler(ProxyHandler.create(httpProxy1));
|
||||
|
||||
proxyRouter
|
||||
.route(HttpMethod.GET, "/bar").handler(ProxyHandler.create(httpProxy2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() throws IOException, URISyntaxException {
|
||||
// URL url = new URL("www.runoob.com/html/html-tutorial.html");
|
||||
URI uri = new URI("http://www.runoob.com");
|
||||
|
||||
System.out.println(StringUtils.isEmpty(uri.getPath()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final WebProxyExamples examples = new WebProxyExamples();
|
||||
examples.vertx.executeBlocking(rs -> {
|
||||
rs.complete();
|
||||
examples.origin();
|
||||
});
|
||||
examples.vertx.executeBlocking(rs -> {
|
||||
rs.complete();
|
||||
examples.route();
|
||||
});
|
||||
System.out.println("ok");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user