Skip to content

Commit e589add

Browse files
committed
GH-5076: Introduce tools/server-spring6
1 parent 7a43d5e commit e589add

File tree

80 files changed

+10719
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+10719
-1
lines changed

tools/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>federation</module>
1717
<module>server</module>
1818
<module>server-spring</module>
19+
<module>server-spring6</module>
1920
<module>workbench</module>
2021
<module>runtime</module>
2122
<module>runtime-osgi</module>

tools/server-spring/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<version>5.3.0-SNAPSHOT</version>
88
</parent>
99
<artifactId>rdf4j-http-server-spring</artifactId>
10-
<name>RDF4J: HTTP server - core</name>
10+
<name>RDF4J: HTTP server - core (Spring 5)</name>
1111
<description>HTTP server implementing a REST-style protocol</description>
1212
<dependencies>
1313
<dependency>

tools/server-spring6/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.eclipse.rdf4j</groupId>
6+
<artifactId>rdf4j-tools</artifactId>
7+
<version>5.3.0-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>rdf4j-http-server-spring6</artifactId>
10+
<name>RDF4J: HTTP server - core (Spring 6)</name>
11+
<description>HTTP server implementing a REST-style protocol</description>
12+
<dependencies>
13+
<dependency>
14+
<groupId>${project.groupId}</groupId>
15+
<artifactId>rdf4j-client</artifactId>
16+
<version>${project.version}</version>
17+
<type>pom</type>
18+
</dependency>
19+
<dependency>
20+
<groupId>${project.groupId}</groupId>
21+
<artifactId>rdf4j-storage</artifactId>
22+
<version>${project.version}</version>
23+
<type>pom</type>
24+
</dependency>
25+
<dependency>
26+
<groupId>${project.groupId}</groupId>
27+
<artifactId>rdf4j-config</artifactId>
28+
<version>${project.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>${project.groupId}</groupId>
32+
<artifactId>rdf4j-rio-rdfjson</artifactId>
33+
<version>${project.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>javax.servlet</groupId>
37+
<artifactId>javax.servlet-api</artifactId>
38+
<scope>provided</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-aop</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework</groupId>
47+
<artifactId>spring-webmvc</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.slf4j</groupId>
51+
<artifactId>slf4j-api</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.google.guava</groupId>
55+
<artifactId>guava</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.mockito</groupId>
59+
<artifactId>mockito-core</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework</groupId>
64+
<artifactId>spring-test</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Distribution License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/org/documents/edl-v10.php.
8+
*
9+
* SPDX-License-Identifier: BSD-3-Clause
10+
*******************************************************************************/
11+
package org.eclipse.rdf4j.common.webapp;
12+
13+
import java.util.Calendar;
14+
15+
import javax.servlet.http.HttpServletRequest;
16+
import javax.servlet.http.HttpServletResponse;
17+
18+
import org.springframework.web.servlet.HandlerInterceptor;
19+
import org.springframework.web.servlet.ModelAndView;
20+
21+
/**
22+
* Interceptor that inserts some commonly used values into the model. The inserted values are: - path, equal to
23+
* request.getContextPath() (e.g. /context) - basePath, equal to the fully qualified context path (e.g.
24+
* http://www.example.com/context/) - currentYear, equal to the current year
25+
*
26+
* @author Herko ter Horst
27+
*/
28+
public class CommonValuesHandlerInterceptor implements HandlerInterceptor {
29+
30+
@Override
31+
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
32+
Exception ex) {
33+
// nop
34+
}
35+
36+
@Override
37+
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) {
38+
mav.addObject("path", request.getContextPath());
39+
mav.addObject("basePath", request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
40+
+ request.getContextPath() + "/");
41+
mav.addObject("currentYear", Calendar.getInstance().get(Calendar.YEAR));
42+
}
43+
44+
@Override
45+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
46+
throws Exception {
47+
return true;
48+
}
49+
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Distribution License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/org/documents/edl-v10.php.
8+
*
9+
* SPDX-License-Identifier: BSD-3-Clause
10+
*******************************************************************************/
11+
package org.eclipse.rdf4j.common.webapp;
12+
13+
/**
14+
* @author Herko ter Horst
15+
*/
16+
public class Message {
17+
18+
public static final String ATTRIBUTE_KEY = "message";
19+
20+
public enum Type {
21+
ERROR,
22+
WARN,
23+
INFO
24+
}
25+
26+
private final Type type;
27+
28+
private final String i18n;
29+
30+
public Message(Type type, String i18n) {
31+
this.type = type;
32+
this.i18n = i18n;
33+
}
34+
35+
/**
36+
* @return Returns the type.
37+
*/
38+
public Type getType() {
39+
return type;
40+
}
41+
42+
/**
43+
* @return Returns the i18n.
44+
*/
45+
public String getI18n() {
46+
return i18n;
47+
}
48+
49+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Distribution License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/org/documents/edl-v10.php.
8+
*
9+
* SPDX-License-Identifier: BSD-3-Clause
10+
*******************************************************************************/
11+
package org.eclipse.rdf4j.common.webapp;
12+
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
import javax.servlet.http.HttpSession;
16+
17+
import org.springframework.web.servlet.HandlerInterceptor;
18+
import org.springframework.web.servlet.ModelAndView;
19+
import org.springframework.web.servlet.view.RedirectView;
20+
21+
/**
22+
* Interceptor that inserts some commonly used values into the model. The inserted values are: - path, equal to
23+
* request.getContextPath() (e.g. /context) - basePath, equal to the fully qualified context path (e.g.
24+
* http://www.example.com/context/) - currentYear, equal to the current year
25+
*
26+
* @author Herko ter Horst
27+
*/
28+
public class MessageHandlerInterceptor implements HandlerInterceptor {
29+
30+
@Override
31+
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
32+
Exception ex) {
33+
// nop
34+
}
35+
36+
@Override
37+
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) {
38+
HttpSession session = request.getSession();
39+
40+
if (session != null) {
41+
Message message = (Message) session.getAttribute(Message.ATTRIBUTE_KEY);
42+
if (message != null && !mav.getModelMap().containsKey(Message.ATTRIBUTE_KEY)) {
43+
mav.addObject(Message.ATTRIBUTE_KEY, message);
44+
}
45+
46+
boolean shouldRemove = true;
47+
if (mav.hasView() && mav.getView() instanceof RedirectView) {
48+
shouldRemove = false;
49+
}
50+
if (mav.getViewName() != null && mav.getViewName().startsWith("redirect:")) {
51+
shouldRemove = false;
52+
}
53+
54+
if (shouldRemove) {
55+
session.removeAttribute(Message.ATTRIBUTE_KEY);
56+
}
57+
}
58+
}
59+
60+
@Override
61+
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
62+
throws Exception {
63+
return true;
64+
}
65+
66+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Distribution License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/org/documents/edl-v10.php.
8+
*
9+
* SPDX-License-Identifier: BSD-3-Clause
10+
*******************************************************************************/
11+
package org.eclipse.rdf4j.common.webapp.filters;
12+
13+
import java.io.CharArrayWriter;
14+
import java.io.IOException;
15+
import java.io.PrintWriter;
16+
17+
import javax.servlet.Filter;
18+
import javax.servlet.FilterChain;
19+
import javax.servlet.FilterConfig;
20+
import javax.servlet.ServletException;
21+
import javax.servlet.ServletRequest;
22+
import javax.servlet.ServletResponse;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
import javax.servlet.http.HttpServletResponseWrapper;
26+
27+
/**
28+
* @author Herko ter Horst
29+
*/
30+
public class PathFilter implements Filter {
31+
32+
@Override
33+
public void init(FilterConfig filterConf) throws ServletException {
34+
// do nothing
35+
}
36+
37+
@Override
38+
public void destroy() {
39+
// do nothing
40+
}
41+
42+
@Override
43+
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
44+
throws IOException, ServletException {
45+
if (req instanceof HttpServletRequest) {
46+
HttpServletRequest request = (HttpServletRequest) req;
47+
HttpServletResponse response = (HttpServletResponse) res;
48+
String path = request.getContextPath();
49+
50+
PrintWriter out = response.getWriter();
51+
CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) response);
52+
filterChain.doFilter(request, wrapper);
53+
CharArrayWriter caw = new CharArrayWriter();
54+
caw.write(wrapper.toString().replace("${path}", path));
55+
String result = caw.toString();
56+
response.setContentLength(result.length());
57+
out.write(result);
58+
}
59+
}
60+
61+
private static class CharResponseWrapper extends HttpServletResponseWrapper {
62+
63+
private final CharArrayWriter output;
64+
65+
@Override
66+
public String toString() {
67+
return output.toString();
68+
}
69+
70+
public CharResponseWrapper(HttpServletResponse response) {
71+
super(response);
72+
output = new CharArrayWriter();
73+
}
74+
75+
@Override
76+
public PrintWriter getWriter() {
77+
return new PrintWriter(output);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)