Skip to content

Commit b29f0ce

Browse files
committed
尝试为undertow适配Jsp[仍有问题]
1 parent 10be940 commit b29f0ce

File tree

24 files changed

+947
-3
lines changed

24 files changed

+947
-3
lines changed

solon-jakarta-parent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<jetty.jakarta.version>12.1.1</jetty.jakarta.version>
3333

34-
<undertow.jakarta.version>2.3.19.Final</undertow.jakarta.version>
34+
<undertow.jakarta.version>2.3.20.Final</undertow.jakarta.version>
3535
<undertow.jastow.jakarta.version>2.2.8.Final</undertow.jastow.jakarta.version>
3636
<undertow.jboss-metadata.jakarta.version>16.1.0.Final</undertow.jboss-metadata.jakarta.version>
3737

solon-jakarta-projects/solon-server/solon-server-jetty-jakarta/src/main/java/org/noear/solon/server/jetty/jsp/JspTldLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @since 2.6
4141
*/
4242
public class JspTldLocator {
43-
static final Logger log = LoggerFactory.getLogger(JspTldLocator.class);
43+
private static final Logger log = LoggerFactory.getLogger(JspTldLocator.class);
4444

4545
public static Map<String, TaglibDescriptor> createTldInfos(String... dltDirs) throws IOException {
4646
HashMap<String, TaglibDescriptor> tagLibInfos = new HashMap<>();

solon-jakarta-projects/solon-server/solon-server-tomcat-jakarta/src/test/java/demo3013/WebApp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@SolonMain
1515
public class WebApp {
1616
public static void main(String[] args) {
17+
// AntCompiler
1718
Solon.start(WebApp.class, args);
1819
}
1920
}

solon-jakarta-projects/solon-server/solon-server-undertow-jakarta/pom.xml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,92 @@
5151
</dependency>
5252

5353
<!-- server core end -->
54+
<!--jsp-->
55+
<!--<dependency>
56+
<groupId>org.mortbay.jasper</groupId>
57+
<artifactId>mortbay-apache-jsp</artifactId>
58+
<version>11.0.10.1</version>
59+
<scope>provided</scope>
60+
</dependency>-->
61+
<!--<dependency>
62+
<groupId>org.jboss.spec.javax.servlet</groupId>
63+
<artifactId>jboss-servlet-api_4.0_spec</artifactId>
64+
<version>2.0.0.Final</version>
65+
</dependency>-->
66+
<dependency>
67+
<groupId>io.undertow.jastow</groupId>
68+
<artifactId>jastow</artifactId>
69+
<version>${undertow.jastow.jakarta.version}</version>
70+
<exclusions>
71+
<exclusion>
72+
<groupId>log4j</groupId>
73+
<artifactId>log4j</artifactId>
74+
</exclusion>
75+
<exclusion>
76+
<groupId>io.undertow</groupId>
77+
<artifactId>undertow-core</artifactId>
78+
</exclusion>
79+
<exclusion>
80+
<groupId>io.undertow</groupId>
81+
<artifactId>undertow-servlet</artifactId>
82+
</exclusion>
83+
</exclusions>
84+
<scope>provided</scope>
85+
</dependency>
86+
<!--<dependency>
87+
<groupId>org.jboss.metadata</groupId>
88+
<artifactId>jboss-metadata-common</artifactId>
89+
<version>${undertow.jboss-metadata.jakarta.version}</version>
90+
<scope>provided</scope>
91+
</dependency>-->
92+
<dependency>
93+
<groupId>org.jboss.metadata</groupId>
94+
<artifactId>jboss-metadata-web</artifactId>
95+
<version>${undertow.jboss-metadata.jakarta.version}</version>
96+
<scope>provided</scope>
97+
</dependency>
98+
<dependency>
99+
<groupId>org.eclipse.jdt</groupId>
100+
<artifactId>ecj</artifactId>
101+
<version>3.43.0</version>
102+
<scope>provided</scope>
103+
</dependency>
104+
<!--<dependency>
105+
<groupId>jakarta.el</groupId>
106+
<artifactId>jakarta.el-api</artifactId>
107+
<version>6.0.1</version>
108+
<scope>provided</scope>
109+
</dependency>-->
110+
<dependency>
111+
<groupId>org.glassfish.expressly</groupId>
112+
<artifactId>expressly</artifactId>
113+
<version>6.0.0</version>
114+
<scope>provided</scope>
115+
</dependency>
116+
<dependency>
117+
<groupId>jakarta.servlet.jsp.jstl</groupId>
118+
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
119+
<version>3.0.2</version>
120+
<scope>provided</scope>
121+
</dependency>
122+
<dependency>
123+
<groupId>com.sun.el</groupId>
124+
<artifactId>el-ri</artifactId>
125+
<version>3.0.4</version>
126+
<scope>provided</scope>
127+
</dependency>
128+
<dependency>
129+
<groupId>jakarta.servlet.jsp</groupId>
130+
<artifactId>jakarta.servlet.jsp-api</artifactId>
131+
<version>3.1.1</version>
132+
<scope>provided</scope>
133+
</dependency>
54134

135+
<dependency>
136+
<groupId>org.projectlombok</groupId>
137+
<artifactId>lombok</artifactId>
138+
<scope>provided</scope>
139+
</dependency>
55140
<dependency>
56141
<groupId>org.noear</groupId>
57142
<artifactId>solon-test</artifactId>
@@ -63,6 +148,11 @@
63148
<artifactId>solon-logging-simple</artifactId>
64149
<scope>test</scope>
65150
</dependency>
151+
<dependency>
152+
<groupId>org.noear</groupId>
153+
<artifactId>solon-view-jsp-jakarta</artifactId>
154+
<scope>test</scope>
155+
</dependency>
66156
</dependencies>
67157

68158
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2017-2025 noear.org and authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.noear.solon.server.undertow;
17+
18+
import io.undertow.jsp.HackInstanceManager;
19+
import io.undertow.jsp.JspServletBuilder;
20+
import io.undertow.server.HttpHandler;
21+
import io.undertow.servlet.api.*;
22+
import org.apache.jasper.deploy.JspPropertyGroup;
23+
import org.apache.jasper.deploy.TagLibraryInfo;
24+
import org.noear.solon.server.prop.impl.HttpServerProps;
25+
import org.noear.solon.server.undertow.http.UtHttpContextServletHandler;
26+
import org.noear.solon.server.undertow.jsp.JspResourceManager;
27+
import org.noear.solon.server.undertow.jsp.JspServletEx;
28+
import org.noear.solon.server.undertow.jsp.JspTldLocator;
29+
import org.noear.solon.core.AppClassLoader;
30+
31+
import java.util.HashMap;
32+
import java.util.Map;
33+
34+
/**
35+
* @author by: Yukai
36+
* @since: 2019/3/28 15:50
37+
*/
38+
public class UndertowServerAddJsp extends UndertowServer {
39+
40+
public UndertowServerAddJsp(HttpServerProps props) {
41+
super(props);
42+
}
43+
44+
@Override
45+
protected HttpHandler buildHandler() throws Exception {
46+
DeploymentInfo builder = initDeploymentInfo();
47+
48+
//添加jsp处理
49+
String fileRoot = getResourceRoot();
50+
ServletInfo jspServlet = JspServletEx.createServlet("JSPServlet", "*.jsp");
51+
jspServlet.addInitParam("fork", "false");
52+
jspServlet.addInitParam("xpoweredBy", "false");
53+
jspServlet.addInitParam("development", "true");
54+
55+
builder.setResourceManager(new JspResourceManager(AppClassLoader.global(), fileRoot))
56+
.addServlet(new ServletInfo("ACTServlet", UtHttpContextServletHandler.class).addMapping("/").setAsyncSupported(true))
57+
.addServlet(jspServlet);
58+
59+
60+
//添加taglib支持
61+
Map<String, TagLibraryInfo> tagLibraryMap = JspTldLocator.createTldInfos("WEB-INF", "templates");
62+
JspServletBuilder.setupDeployment(builder, new HashMap<String, JspPropertyGroup>(), tagLibraryMap, new HackInstanceManager());
63+
64+
65+
//开始部署
66+
final ServletContainer container = ServletContainer.Factory.newInstance();
67+
DeploymentManager manager = container.addDeployment(builder);
68+
manager.deploy();
69+
70+
return manager.start();
71+
}
72+
}

solon-jakarta-projects/solon-server/solon-server-undertow-jakarta/src/main/java/org/noear/solon/server/undertow/integration/UndertowPlugin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.noear.solon.server.prop.impl.HttpServerProps;
2323
import org.noear.solon.server.prop.impl.WebSocketServerProps;
2424
import org.noear.solon.server.undertow.UndertowServer;
25+
import org.noear.solon.server.undertow.UndertowServerAddJsp;
2526
import org.noear.solon.core.*;
2627
import org.noear.solon.core.bean.LifecycleBean;
2728
import org.noear.solon.core.event.EventBus;
@@ -30,6 +31,7 @@
3031
import org.slf4j.Logger;
3132
import org.slf4j.LoggerFactory;
3233

34+
import io.undertow.jsp.JspServletBuilder;
3335
import jakarta.servlet.annotation.WebFilter;
3436
import jakarta.servlet.annotation.WebListener;
3537
import jakarta.servlet.annotation.WebServlet;
@@ -85,7 +87,11 @@ private void start0(AppContext context) throws Throwable {
8587
final int _port = props.getPort();
8688
final String _name = props.getName();
8789

88-
_server = new UndertowServer(props);
90+
if (ClassUtil.hasClass(() -> JspServletBuilder.class)) {
91+
_server = new UndertowServerAddJsp(props);
92+
} else {
93+
_server = new UndertowServer(props);
94+
}
8995
_server.enableWebSocket(context.app().enableWebSocket());
9096

9197
EventBus.publish(_server);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2017-2025 noear.org and authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.noear.solon.server.undertow.jsp;
17+
18+
import io.undertow.UndertowMessages;
19+
import io.undertow.server.handlers.resource.Resource;
20+
import io.undertow.server.handlers.resource.ResourceChangeListener;
21+
import io.undertow.server.handlers.resource.ResourceManager;
22+
import io.undertow.server.handlers.resource.URLResource;
23+
import org.noear.solon.core.handle.Context;
24+
25+
import java.io.IOException;
26+
import java.net.URI;
27+
import java.net.URL;
28+
29+
/**
30+
* JSP资源管理器
31+
*/
32+
public class JspResourceManager implements ResourceManager {
33+
private final ClassLoader classLoader;
34+
private final String prefix;
35+
36+
public JspResourceManager(ClassLoader classLoader, String prefix) {
37+
this.classLoader = classLoader;
38+
if (prefix.isEmpty()) {
39+
this.prefix = "";
40+
} else if (prefix.endsWith("/")) {
41+
this.prefix = prefix;
42+
} else {
43+
this.prefix = prefix + "/";
44+
}
45+
46+
}
47+
48+
@Override
49+
public Resource getResource(String path) throws IOException {
50+
if (path == null || path.endsWith(".jsp") == false) {
51+
return null;
52+
}
53+
54+
if(Context.current() == null){
55+
//说明先走的是jsp请求 //禁止
56+
return null;
57+
}
58+
59+
String modPath = path;
60+
if (path.startsWith("/")) {
61+
modPath = path.substring(1);
62+
}
63+
64+
String realPath = this.prefix + modPath;
65+
URL resource = null;
66+
if (realPath.startsWith("file:")) {
67+
resource = URI.create(realPath).toURL();
68+
} else {
69+
resource = this.classLoader.getResource(realPath);
70+
}
71+
72+
return resource == null ? null : new URLResource(resource, path);
73+
}
74+
75+
@Override
76+
public boolean isResourceChangeListenerSupported() {
77+
return false;
78+
}
79+
80+
@Override
81+
public void registerResourceChangeListener(ResourceChangeListener listener) {
82+
throw UndertowMessages.MESSAGES.resourceChangeListenerNotSupported();
83+
}
84+
85+
@Override
86+
public void removeResourceChangeListener(ResourceChangeListener listener) {
87+
throw UndertowMessages.MESSAGES.resourceChangeListenerNotSupported();
88+
}
89+
90+
@Override
91+
public void close() throws IOException {
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2017-2025 noear.org and authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.noear.solon.server.undertow.jsp;
17+
18+
import java.io.IOException;
19+
20+
import org.apache.jasper.servlet.JspServlet;
21+
import org.noear.solon.core.handle.Context;
22+
23+
import io.undertow.servlet.api.ServletInfo;
24+
import jakarta.servlet.ServletException;
25+
import jakarta.servlet.ServletRequest;
26+
import jakarta.servlet.ServletResponse;
27+
28+
public class JspServletEx extends JspServlet {
29+
30+
public static ServletInfo createServlet(String name, String path) {
31+
ServletInfo servlet = new ServletInfo(name, JspServletEx.class);
32+
servlet.addMapping(path);
33+
servlet.setRequireWelcomeFileMapping(true);
34+
return servlet;
35+
}
36+
37+
38+
@Override
39+
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
40+
if(Context.current() == null){
41+
return;
42+
}
43+
44+
super.service(req, res);
45+
}
46+
}

0 commit comments

Comments
 (0)