Skip to content

Commit 7575edf

Browse files
committed
Add support for making endpoints accessible via HTTP
1 parent f3388ca commit 7575edf

24 files changed

+3156
-2
lines changed

spring-boot-parent/src/checkstyle/import-control.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@
2929
</subpackage>
3030
</subpackage>
3131

32+
<!-- Endpoint infrastructure -->
33+
<subpackage name="endpoint">
34+
<disallow pkg="org.springframework.http" />
35+
<disallow pkg="org.springframework.web" />
36+
<subpackage name="web">
37+
<allow pkg="org.springframework.http" />
38+
<allow pkg="org.springframework.web" />
39+
<subpackage name="mvc">
40+
<disallow pkg="org.springframework.web.reactive" />
41+
</subpackage>
42+
<subpackage name="reactive">
43+
<disallow pkg="org.springframework.web.servlet" />
44+
</subpackage>
45+
</subpackage>
46+
</subpackage>
47+
3248
<!-- Logging -->
3349
<subpackage name="logging">
3450
<disallow pkg="org.springframework.context" />
@@ -109,4 +125,4 @@
109125
</subpackage>
110126

111127
</subpackage>
112-
</import-control>
128+
</import-control>

spring-boot/pom.xml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@
159159
<artifactId>jetty-webapp</artifactId>
160160
<optional>true</optional>
161161
</dependency>
162+
<dependency>
163+
<groupId>org.glassfish.jersey.core</groupId>
164+
<artifactId>jersey-server</artifactId>
165+
<optional>true</optional>
166+
</dependency>
162167
<dependency>
163168
<groupId>org.hamcrest</groupId>
164169
<artifactId>hamcrest-library</artifactId>
@@ -271,6 +276,11 @@
271276
<artifactId>h2</artifactId>
272277
<scope>test</scope>
273278
</dependency>
279+
<dependency>
280+
<groupId>com.jayway.jsonpath</groupId>
281+
<artifactId>json-path</artifactId>
282+
<scope>test</scope>
283+
</dependency>
274284
<dependency>
275285
<groupId>com.microsoft.sqlserver</groupId>
276286
<artifactId>mssql-jdbc</artifactId>
@@ -316,6 +326,16 @@
316326
<artifactId>jaybird-jdk18</artifactId>
317327
<scope>test</scope>
318328
</dependency>
329+
<dependency>
330+
<groupId>org.glassfish.jersey.containers</groupId>
331+
<artifactId>jersey-container-servlet-core</artifactId>
332+
<scope>test</scope>
333+
</dependency>
334+
<dependency>
335+
<groupId>org.glassfish.jersey.media</groupId>
336+
<artifactId>jersey-media-json-jackson</artifactId>
337+
<scope>test</scope>
338+
</dependency>
319339
<dependency>
320340
<groupId>org.hsqldb</groupId>
321341
<artifactId>hsqldb</artifactId>
@@ -352,4 +372,17 @@
352372
<scope>test</scope>
353373
</dependency>
354374
</dependencies>
355-
</project>
375+
<build>
376+
<plugins>
377+
<plugin>
378+
<groupId>org.apache.maven.plugins</groupId>
379+
<artifactId>maven-compiler-plugin</artifactId>
380+
<configuration>
381+
<compilerArgs>
382+
<arg>-parameters</arg>
383+
</compilerArgs>
384+
</configuration>
385+
</plugin>
386+
</plugins>
387+
</build>
388+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2017 the original author or 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+
* http://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+
17+
package org.springframework.boot.endpoint.web;
18+
19+
import java.util.Collection;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
23+
import org.springframework.boot.endpoint.EndpointInfo;
24+
25+
/**
26+
* A resolver for {@link Link links} to web endpoints.
27+
*
28+
* @author Andy Wilkinson
29+
* @since 2.0.0
30+
*/
31+
public class EndpointLinksResolver {
32+
33+
/**
34+
* Resolves links to the operations of the given {code webEndpoints} based on a
35+
* request with the given {@code requestUrl}.
36+
*
37+
* @param webEndpoints the web endpoints
38+
* @param requestUrl the url of the request for the endpoint links
39+
* @return the links
40+
*/
41+
public Map<String, Link> resolveLinks(
42+
Collection<EndpointInfo<WebEndpointOperation>> webEndpoints,
43+
String requestUrl) {
44+
String normalizedUrl = normalizeRequestUrl(requestUrl);
45+
Map<String, Link> links = new LinkedHashMap<String, Link>();
46+
links.put("self", new Link(normalizedUrl));
47+
for (EndpointInfo<WebEndpointOperation> endpoint : webEndpoints) {
48+
for (WebEndpointOperation operation : endpoint.getOperations()) {
49+
webEndpoints.stream().map(EndpointInfo::getId).forEach((id) -> links
50+
.put(operation.getId(), createLink(normalizedUrl, operation)));
51+
}
52+
}
53+
return links;
54+
}
55+
56+
private String normalizeRequestUrl(String requestUrl) {
57+
if (requestUrl.endsWith("/")) {
58+
return requestUrl.substring(0, requestUrl.length() - 1);
59+
}
60+
return requestUrl;
61+
}
62+
63+
private Link createLink(String requestUrl, WebEndpointOperation operation) {
64+
String path = operation.getRequestPredicate().getPath();
65+
return new Link(requestUrl + (path.startsWith("/") ? path : "/" + path));
66+
}
67+
68+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2017 the original author or 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+
* http://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+
17+
package org.springframework.boot.endpoint.web;
18+
19+
import org.springframework.core.style.ToStringCreator;
20+
21+
/**
22+
* Details for a link in a
23+
* <a href="https://tools.ietf.org/html/draft-kelly-json-hal-08">HAL</a>-formatted
24+
* response.
25+
*
26+
* @author Andy Wilkinson
27+
* @since 2.0.0
28+
*/
29+
public class Link {
30+
31+
private final String href;
32+
33+
private final boolean templated;
34+
35+
/**
36+
* Creates a new {@link Link} with the given href.
37+
* @param href the href
38+
*/
39+
public Link(String href) {
40+
this.href = href;
41+
this.templated = href.contains("{");
42+
43+
}
44+
45+
/**
46+
* Returns the href of the link.
47+
* @return the href
48+
*/
49+
public String getHref() {
50+
return this.href;
51+
}
52+
53+
/**
54+
* Returns whether or not the {@link #getHref() href} is templated.
55+
* @return {@code true} if the href is templated, otherwise {@code false}
56+
*/
57+
public boolean isTemplated() {
58+
return this.templated;
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return new ToStringCreator(this).append("href", this.href).toString();
64+
}
65+
66+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2012-2017 the original author or 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+
* http://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+
17+
package org.springframework.boot.endpoint.web;
18+
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
22+
import org.springframework.core.style.ToStringCreator;
23+
24+
/**
25+
* A predicate for a request to an operation on a web endpoint.
26+
*
27+
* @author Andy Wilkinson
28+
* @since 2.0.0
29+
*/
30+
public class OperationRequestPredicate {
31+
32+
private final String path;
33+
34+
private final String canonicalPath;
35+
36+
private final WebEndpointHttpMethod httpMethod;
37+
38+
private final Collection<String> consumes;
39+
40+
private final Collection<String> produces;
41+
42+
/**
43+
* Creates a new {@code WebEndpointRequestPredict}.
44+
*
45+
* @param path the path for the operation
46+
* @param httpMethod the HTTP method that the operation supports
47+
* @param produces the media types that the operation produces
48+
* @param consumes the media types that the operation consumes
49+
*/
50+
public OperationRequestPredicate(String path, WebEndpointHttpMethod httpMethod,
51+
Collection<String> consumes, Collection<String> produces) {
52+
this.path = path;
53+
this.canonicalPath = path.replaceAll("\\{.*?}", "{*}");
54+
this.httpMethod = httpMethod;
55+
this.consumes = consumes;
56+
this.produces = produces;
57+
}
58+
59+
/**
60+
* Returns the path for the operation.
61+
* @return the path
62+
*/
63+
public String getPath() {
64+
return this.path;
65+
}
66+
67+
/**
68+
* Returns the HTTP method for the operation.
69+
* @return the HTTP method
70+
*/
71+
public WebEndpointHttpMethod getHttpMethod() {
72+
return this.httpMethod;
73+
}
74+
75+
/**
76+
* Returns the media types that the operation consumes.
77+
* @return the consumed media types
78+
*/
79+
public Collection<String> getConsumes() {
80+
return Collections.unmodifiableCollection(this.consumes);
81+
}
82+
83+
/**
84+
* Returns the media types that the operation produces.
85+
* @return the produced media types
86+
*/
87+
public Collection<String> getProduces() {
88+
return Collections.unmodifiableCollection(this.produces);
89+
}
90+
91+
@Override
92+
public String toString() {
93+
return new ToStringCreator(this).append("httpMethod", this.httpMethod)
94+
.append("path", this.path).append("consumes", this.consumes)
95+
.append("produces", this.produces).toString();
96+
}
97+
98+
@Override
99+
public int hashCode() {
100+
final int prime = 31;
101+
int result = 1;
102+
result = prime * result + this.consumes.hashCode();
103+
result = prime * result + this.httpMethod.hashCode();
104+
result = prime * result + this.canonicalPath.hashCode();
105+
result = prime * result + this.produces.hashCode();
106+
return result;
107+
}
108+
109+
@Override
110+
public boolean equals(Object obj) {
111+
if (this == obj) {
112+
return true;
113+
}
114+
if (obj == null) {
115+
return false;
116+
}
117+
if (getClass() != obj.getClass()) {
118+
return false;
119+
}
120+
OperationRequestPredicate other = (OperationRequestPredicate) obj;
121+
if (!this.consumes.equals(other.consumes)) {
122+
return false;
123+
}
124+
if (this.httpMethod != other.httpMethod) {
125+
return false;
126+
}
127+
if (!this.canonicalPath.equals(other.canonicalPath)) {
128+
return false;
129+
}
130+
if (!this.produces.equals(other.produces)) {
131+
return false;
132+
}
133+
return true;
134+
}
135+
136+
}

0 commit comments

Comments
 (0)