DATAREST-1386 - Add HAL Explorer module
We now ship a module containing the HAL Explorer [0] as an alternative to the HAL Browser which seems to be mostly out of maintenance and the WebJar refers to a jQuery version with a CVE. The new module still contains some customization code that we had in place for the browser to customize the POST/PUT forms to use the JSON Schema exposed for the aggregates. The HAL Browser extension is now deprecated in the form that its inclusion causes a warn log to recommend switching to the explorer. A couple of general coding improvements: package scope for controllers and handler methods. Better use of constants and newer request mapping annotations. Tweaked the configuration of static resource handlers to resolve the implicit cyclic dependency between the browser/explorer and the WebMVC module by introducing a Spring Factories backed extension SPI. [0] https://github.com/toedter/hal-explorer
This commit is contained in:
@@ -15,12 +15,13 @@
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.halbrowser;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.data.rest.webmvc.BasePathAwareController;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
@@ -32,19 +33,27 @@ import org.springframework.web.util.UriComponents;
|
||||
* @author Oliver Gierke
|
||||
* @soundtrack Miles Davis - So what (Kind of blue)
|
||||
*/
|
||||
@Slf4j
|
||||
@BasePathAwareController
|
||||
public class HalBrowser {
|
||||
class HalBrowser {
|
||||
|
||||
private static String BROWSER = "/browser";
|
||||
private static String INDEX = "/index.html";
|
||||
static final String BROWSER = "/browser";
|
||||
static final String INDEX = "/index.html";
|
||||
|
||||
HalBrowser() {
|
||||
LOG.warn("---");
|
||||
LOG.warn(
|
||||
"Spring Data REST HAL Browser is deprecated! Prefer the HAL Explorer (artifactId: spring-data-rest-hal-explorer)!");
|
||||
LOG.warn("---");
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirects requests to the API root asking for HTML to the HAL browser.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value = { "/", "" }, method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
|
||||
public View index(HttpServletRequest request) {
|
||||
@GetMapping(path = { "/", "" }, produces = MediaType.TEXT_HTML_VALUE)
|
||||
View index(HttpServletRequest request) {
|
||||
return getRedirectView(request, false);
|
||||
}
|
||||
|
||||
@@ -53,9 +62,9 @@ public class HalBrowser {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value = "/browser", method = RequestMethod.GET)
|
||||
public View browser(HttpServletRequest request) {
|
||||
return getRedirectView(request, request.getRequestURI().endsWith("/browser"));
|
||||
@GetMapping(path = BROWSER)
|
||||
View browser(HttpServletRequest request) {
|
||||
return getRedirectView(request, request.getRequestURI().endsWith(BROWSER));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +86,7 @@ public class HalBrowser {
|
||||
}
|
||||
|
||||
builder.path(INDEX);
|
||||
builder.fragment(browserRelative ? path.substring(0, path.lastIndexOf("/browser")) : path);
|
||||
builder.fragment(browserRelative ? path.substring(0, path.lastIndexOf(BROWSER)) : path);
|
||||
|
||||
return new RedirectView(builder.build().toUriString());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.halbrowser;
|
||||
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.webmvc.config.StaticResourceProvider;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
|
||||
/**
|
||||
* {@link StaticResourceProvider} to expose the HAL Browser WebJar content via a static resource route.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
* @since 3.2
|
||||
* @soundtrack Tedeschi Trucks Band - Signs, High Times (Signs)
|
||||
*/
|
||||
class HalBrowserConfiguration implements StaticResourceProvider {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.rest.webmvc.config.StaticResourceProvider#customizeResources(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry, org.springframework.data.rest.core.config.RepositoryRestConfiguration)
|
||||
*/
|
||||
@Override
|
||||
public void customizeResources(ResourceHandlerRegistry registry, RepositoryRestConfiguration configuration) {
|
||||
|
||||
String basePath = configuration.getBasePath().toString().concat(HalBrowser.BROWSER);
|
||||
String rootLocation = "classpath:META-INF/spring-data-rest/hal-browser/";
|
||||
|
||||
registry.addResourceHandler(basePath.concat("/**")).addResourceLocations(rootLocation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.data.rest.webmvc.config.StaticResourceProvider=org.springframework.data.rest.webmvc.halbrowser.HalBrowserConfiguration
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
<logger name="org.springframework" level="warn" />
|
||||
|
||||
<root level="error">
|
||||
<root level="warn">
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user