Skip to content

Commit aea941c

Browse files
committed
支持 http 安装插件
1 parent 52b0444 commit aea941c

File tree

18 files changed

+787
-1
lines changed

18 files changed

+787
-1
lines changed

bom-manager/exp-one-bom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
<artifactId>example-extension-define</artifactId>
7171
<version>${project.version}</version>
7272
</dependency>
73+
<dependency>
74+
<groupId>cn.think.in.java</groupId>
75+
<artifactId>open-exp-http-server</artifactId>
76+
<version>${project.version}</version>
77+
</dependency>
7378
<dependency>
7479
<groupId>cn.think.in.java</groupId>
7580
<artifactId>open-exp-document-api</artifactId>

bom-manager/exp-third-bom/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@
6666
<version>4.13.2</version>
6767
<scope>test</scope>
6868
</dependency>
69+
<dependency>
70+
<groupId>ch.qos.logback</groupId>
71+
<artifactId>logback-classic</artifactId>
72+
<version>1.2.9</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.apache.httpcomponents</groupId>
77+
<artifactId>httpmime</artifactId>
78+
<version>4.5.14</version>
79+
</dependency>
6980
</dependencies>
7081
</dependencyManagement>
7182

example/example-springboot2/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@
6363
<dependency>
6464
<groupId>cn.think.in.java</groupId>
6565
<artifactId>example-extension-define</artifactId>
66-
<version>1.0-SNAPSHOT</version>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>cn.think.in.java</groupId>
70+
<artifactId>open-exp-http-server</artifactId>
6771
</dependency>
6872
</dependencies>
6973

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.think.in.java.open.exp.client;
2+
3+
/**
4+
* @Author cxs
5+
* @Description
6+
* @date 2023/8/28
7+
* @version 1.0
8+
**/
9+
public interface ConfigSpi {
10+
11+
String getProperty(String key, String def);
12+
13+
class MockConfigSpi implements ConfigSpi {
14+
15+
@Override
16+
public String getProperty(String key, String def) {
17+
return def;
18+
}
19+
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.think.in.java.open.exp.client;
2+
3+
/**
4+
* @Author cxs
5+
* @Description
6+
* @date 2023/8/28
7+
* @version 1.0
8+
**/
9+
public interface PluginManager {
10+
11+
void start();
12+
13+
void stop();
14+
15+
class PluginManagerMock implements PluginManager {
16+
17+
@Override
18+
public void start() {
19+
20+
}
21+
22+
@Override
23+
public void stop() {
24+
25+
}
26+
}
27+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>cn.think.in.java</groupId>
8+
<artifactId>open-exp-code</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>open-exp-http-server</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>8</maven.compiler.source>
16+
<maven.compiler.target>8</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
<dependency>
23+
<groupId>cn.think.in.java</groupId>
24+
<artifactId>exp-one-bom</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
<type>pom</type>
27+
<scope>import</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>cn.think.in.java</groupId>
31+
<artifactId>exp-third-bom</artifactId>
32+
<version>1.0-SNAPSHOT</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>cn.think.in.java</groupId>
42+
<artifactId>open-exp-client-api</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.apache.httpcomponents</groupId>
46+
<artifactId>httpmime</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>ch.qos.logback</groupId>
55+
<artifactId>logback-classic</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.mapstruct</groupId>
60+
<artifactId>mapstruct-processor</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>cn.think.in.java</groupId>
64+
<artifactId>open-exp-json-api</artifactId>
65+
</dependency>
66+
67+
</dependencies>
68+
69+
</project>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package cn.think.in.java.open.exp.http.server;
2+
3+
import cn.think.in.java.open.exp.client.ConfigSpi;
4+
import cn.think.in.java.open.exp.client.SpiFactory;
5+
import cn.think.in.java.open.exp.client.StringUtil;
6+
import cn.think.in.java.open.exp.http.server.han.GetAllHandler;
7+
import cn.think.in.java.open.exp.http.server.han.HttpFileHandler;
8+
import cn.think.in.java.open.exp.http.server.han.InstallHandler;
9+
import cn.think.in.java.open.exp.http.server.han.UnInstallHandler;
10+
import lombok.extern.slf4j.Slf4j;
11+
import org.apache.http.ConnectionClosedException;
12+
import org.apache.http.HttpServerConnection;
13+
import org.apache.http.impl.DefaultBHttpServerConnection;
14+
import org.apache.http.impl.DefaultConnectionReuseStrategy;
15+
import org.apache.http.impl.DefaultHttpResponseFactory;
16+
import org.apache.http.protocol.*;
17+
18+
import java.io.IOException;
19+
import java.net.ServerSocket;
20+
import java.net.Socket;
21+
import java.util.concurrent.Executors;
22+
import java.util.concurrent.TimeUnit;
23+
24+
@Slf4j
25+
public class ApacheHttpSimpleServer {
26+
private static ConfigSpi configSpi = SpiFactory.get(ConfigSpi.class, new ConfigSpi.MockConfigSpi());
27+
volatile static boolean running;
28+
static ServerSocket serversocket;
29+
30+
public static void stop() {
31+
running = false;
32+
try {
33+
serversocket.close();
34+
} catch (IOException e) {
35+
throw new RuntimeException(e);
36+
}
37+
}
38+
39+
public static void start(String contextPath, int port) throws Exception {
40+
if (running) {
41+
log.warn("ApacheHttpSimpleServer is running....");
42+
return;
43+
}
44+
HttpProcessor httpProcessor = HttpProcessorBuilder.create()
45+
.add(new ResponseDate())
46+
.add(new ResponseServer("EEP-Server/1.1"))
47+
.add(new ResponseContent())
48+
.add(new ResponseConnControl()).build();
49+
50+
UriHttpRequestHandlerMapper handlerMapper = new UriHttpRequestHandlerMapper();
51+
52+
String uploadAndInstall;
53+
String install;
54+
String uninstall;
55+
String getAll;
56+
57+
if (!StringUtil.isEmpty(contextPath)) {
58+
uploadAndInstall = contextPath + "-uploadAndInstall";
59+
install = contextPath + "-install";
60+
uninstall = contextPath + "-uninstall";
61+
getAll = contextPath + "-getAll";
62+
} else {
63+
uploadAndInstall = "/uploadAndInstall";
64+
install = "/install";
65+
uninstall = "/uninstall";
66+
getAll = "/getAll";
67+
}
68+
69+
handlerMapper.register(uploadAndInstall, new HttpFileHandler());
70+
handlerMapper.register(install, new InstallHandler());
71+
handlerMapper.register(uninstall, new UnInstallHandler());
72+
handlerMapper.register(getAll, new GetAllHandler());
73+
74+
Executors.newScheduledThreadPool(1).schedule(() -> {
75+
String url = "http://localhost:" + Integer.parseInt(configSpi.getProperty("exp.plugin.manager.port", "8888"));
76+
String uploadAndInstall2 = (url + uploadAndInstall);
77+
String install2 = url + install + "?path=xxxx";
78+
String uninstall2 = url + uninstall + "?pluginId=xxxx";
79+
String getAll2 = url + getAll;
80+
log.info("插件管理模块:先卸载后安装 URL : {}", uploadAndInstall2);
81+
log.info("插件管理模块: 安装 URL : {}", install2);
82+
log.info("插件管理模块: 卸载 URL : {}", uninstall2);
83+
log.info("插件管理模块: 获取所有 URL : {}", getAll2);
84+
}, 1, TimeUnit.SECONDS);
85+
86+
HttpService httpService = new HttpService(
87+
httpProcessor,
88+
new DefaultConnectionReuseStrategy(),
89+
new DefaultHttpResponseFactory(),
90+
handlerMapper,
91+
null);
92+
93+
serversocket = new ServerSocket(port);
94+
log.info("server start");
95+
running = true;
96+
try {
97+
while (running) {
98+
Socket socket = serversocket.accept();
99+
socket.setSoTimeout(10000);
100+
DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(8 * 1024);
101+
conn.bind(socket);
102+
103+
Thread t = new RequestListenerThread(httpService, conn);
104+
t.setName("eep-http-server-" + conn.getRemoteAddress() + ":" + conn.getRemotePort());
105+
t.setDaemon(true);
106+
t.start();
107+
}
108+
log.info("server stop end");
109+
} catch (Exception e) {
110+
log.warn("server stop end");
111+
}
112+
}
113+
114+
static class RequestListenerThread extends Thread {
115+
private final HttpService httpService;
116+
private final HttpServerConnection conn;
117+
118+
public RequestListenerThread(final HttpService httpService, final HttpServerConnection conn) {
119+
super();
120+
this.httpService = httpService;
121+
this.conn = conn;
122+
}
123+
124+
@Override
125+
public void run() {
126+
try {
127+
HttpContext context = new BasicHttpContext();
128+
this.httpService.handleRequest(this.conn, context);
129+
} catch (ConnectionClosedException ignore) {
130+
} catch (Exception ex) {
131+
log.error(ex.getMessage(), ex);
132+
} finally {
133+
try {
134+
this.conn.shutdown();
135+
} catch (IOException ignore) {
136+
}
137+
}
138+
}
139+
}
140+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.think.in.java.open.exp.http.server;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.InputStream;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
import java.net.URLDecoder;
8+
9+
public class HttpFileDownloader {
10+
11+
public static void download(String fileURL, String savePath) {
12+
if (!savePath.startsWith("/") && !fileURL.startsWith("http")) {
13+
throw new RuntimeException("path 错误" + savePath);
14+
}
15+
16+
try {
17+
if (fileURL.contains("%")) {
18+
fileURL = URLDecoder.decode(fileURL, "UTF-8");
19+
}
20+
URL url = new URL(fileURL);
21+
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
22+
int responseCode = httpConn.getResponseCode();
23+
24+
if (responseCode == HttpURLConnection.HTTP_OK) {
25+
InputStream inputStream = httpConn.getInputStream();
26+
FileOutputStream outputStream = new FileOutputStream(savePath);
27+
28+
int bytesRead;
29+
byte[] buffer = new byte[4096];
30+
31+
while ((bytesRead = inputStream.read(buffer)) != -1) {
32+
outputStream.write(buffer, 0, bytesRead);
33+
}
34+
35+
outputStream.close();
36+
inputStream.close();
37+
}
38+
httpConn.disconnect();
39+
} catch (Exception e) {
40+
throw new RuntimeException(e);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)