Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ public String user(@PathVariable Long id, @ModelAttribute @Valid UserForm form,

The following annotations can be used on controller methods to set the necessary response headers.

* [@HxLocation](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxLocation.html)
* [@HxPushUrl](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxPushUrl.html)
* [@HxRedirect](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxRedirect.html)
* [@HxRefresh](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxRefresh.html)
* [@HxReplaceUrl](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReplaceUrl.html)
* [@HxReselect](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReselect.html)
* [@HxReswap](https://javadoc.io/doc/io.github.wimdeblauwe/htmx-spring-boot/latest/io/github/wimdeblauwe/htmx/spring/boot/mvc/HxReswap.html)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,14 @@ public void handleMethodArgument(HttpServletRequest request, HttpServletResponse

public void handleMethodAnnotations(Method method, HttpServletRequest request, HttpServletResponse response) {

setHxLocation(request, response, method);
setHxPushUrl(request, response, method);
setHxRedirect(request, response, method);
setHxReplaceUrl(request, response, method);
setHxReswap(response, method);
setHxRetarget(response, method);
setHxReselect(response, method);
setHxTrigger(response, method);
setHxTriggerAfterSettle(response, method);
setHxTriggerAfterSwap(response, method);
setHxRefresh(response, method);
}

private void addHxTriggerHeaders(HttpServletResponse response, HtmxResponseHeader headerName, Collection<HtmxTrigger> triggers) {
Expand All @@ -90,19 +87,6 @@ private void addHxTriggerHeaders(HttpServletResponse response, HtmxResponseHeade
setHeaderJsonValue(response, headerName, triggerMap);
}

private void setHxLocation(HttpServletRequest request, HttpServletResponse response, Method method) {
HxLocation methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxLocation.class);
if (methodAnnotation != null) {
var location = convertToLocation(methodAnnotation);
if (location.hasContextData()) {
location.setPath(RequestContextUtils.createUrl(request, location.getPath(), methodAnnotation.contextRelative()));
setHeaderJsonValue(response, HtmxResponseHeader.HX_LOCATION, location);
} else {
setHeader(response, HtmxResponseHeader.HX_LOCATION, RequestContextUtils.createUrl(request, location.getPath(), methodAnnotation.contextRelative()));
}
}
}

private void setHxPushUrl(HttpServletRequest request, HttpServletResponse response, Method method) {
HxPushUrl methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxPushUrl.class);
if (methodAnnotation != null) {
Expand All @@ -114,13 +98,6 @@ private void setHxPushUrl(HttpServletRequest request, HttpServletResponse respon
}
}

private void setHxRedirect(HttpServletRequest request, HttpServletResponse response, Method method) {
HxRedirect methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxRedirect.class);
if (methodAnnotation != null) {
setHeader(response, HtmxResponseHeader.HX_REDIRECT, RequestContextUtils.createUrl(request, methodAnnotation.value(), methodAnnotation.contextRelative()));
}
}

private void setHxReplaceUrl(HttpServletRequest request, HttpServletResponse response, Method method) {
HxReplaceUrl methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxReplaceUrl.class);
if (methodAnnotation != null) {
Expand Down Expand Up @@ -174,13 +151,6 @@ private void setHxTriggerAfterSwap(HttpServletResponse response, Method method)
}
}

private void setHxRefresh(HttpServletResponse response, Method method) {
HxRefresh methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxRefresh.class);
if (methodAnnotation != null) {
setHeader(response, HtmxResponseHeader.HX_REFRESH, HtmxValue.TRUE);
}
}

private void setHeader(HttpServletResponse response, HtmxResponseHeader header, String value) {
response.setHeader(header.getValue(), value);
}
Expand All @@ -189,30 +159,6 @@ private void setHeader(HttpServletResponse response, HtmxResponseHeader header,
response.setHeader(header.getValue(), String.join(",", values));
}

private HtmxLocation convertToLocation(HxLocation annotation) {
var location = new HtmxLocation();
location.setPath(annotation.path());
if (!annotation.source().isEmpty()) {
location.setSource(annotation.source());
}
if (!annotation.event().isEmpty()) {
location.setEvent(annotation.event());
}
if (!annotation.handler().isEmpty()) {
location.setHandler(annotation.handler());
}
if (!annotation.target().isEmpty()) {
location.setTarget(annotation.target());
}
if (!annotation.target().isEmpty()) {
location.setSwap(annotation.swap());
}
if (!annotation.select().isEmpty()) {
location.setSelect(annotation.select());
}
return location;
}

private String convertToReswap(HxReswap annotation) {

var reswap = new HtmxReswap(annotation.value());
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,41 +111,6 @@ public void testVaryNoHxRequest() throws Exception {
.andExpect(header().stringValues("Vary", not(containsInRelativeOrder("HX-Request"))));
}

@Test
public void testHxRefresh() throws Exception {
mockMvc.perform(get("/hx-refresh"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Refresh", "true"));
}

@Test
public void testHxLocationWithContextData() throws Exception {
mockMvc.perform(get("/hx-location-with-context-data"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Location", "{\"path\":\"/path\",\"source\":\"source\",\"event\":\"event\",\"handler\":\"handler\",\"target\":\"target\",\"swap\":\"swap\",\"select\":\"select\"}"));
}

@Test
public void testHxLocationWithContextDataPathShouldPrependContextPath() throws Exception {
mockMvc.perform(get("/test/hx-location-with-context-data").contextPath("/test"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Location", "{\"path\":\"/test/path\",\"source\":\"source\",\"event\":\"event\",\"handler\":\"handler\",\"target\":\"target\",\"swap\":\"swap\",\"select\":\"select\"}"));
}

@Test
public void testHxLocationWithoutContextData() throws Exception {
mockMvc.perform(get("/test/hx-location-without-context-data").contextPath("/test"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Location", "/test/path"));
}

@Test
public void testHxLocationWithoutContextDataShouldPrependContextPath() throws Exception {
mockMvc.perform(get("/test/hx-location-without-context-data").contextPath("/test"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Location", "/test/path"));
}

@Test
public void testHxPushUrlPath() throws Exception {
mockMvc.perform(get("/hx-push-url-path"))
Expand Down Expand Up @@ -174,20 +139,6 @@ public void testHxPushUrlShouldPrependContextPath() throws Exception {
.andExpect(header().string("HX-Push-Url", "/test/path"));
}

@Test
public void testHxRedirect() throws Exception {
mockMvc.perform(get("/hx-redirect"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Redirect", "/path"));
}

@Test
public void testHxRedirectShouldPrependContextPath() throws Exception {
mockMvc.perform(get("/test/hx-redirect").contextPath("/test"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Redirect", "/test/path"));
}

@Test
public void testHxReplaceUrlPath() throws Exception {
mockMvc.perform(get("/hx-replace-url-path"))
Expand Down Expand Up @@ -312,33 +263,12 @@ public String hxTriggerWithAliasForOverride() {
return "";
}

@GetMapping("/hx-refresh")
@HxRefresh
@ResponseBody
public String hxRefresh() {
return "";
}

@GetMapping("/hx-vary")
@ResponseBody
public String hxVary() {
return "";
}

@GetMapping("/hx-location-without-context-data")
@HxLocation("/path")
@ResponseBody
public String hxLocationWithoutContextData() {
return "";
}

@GetMapping("/hx-location-with-context-data")
@HxLocation(path = "/path", source = "source", event = "event", handler = "handler", target = "target", swap = "swap", select = "select")
@ResponseBody
public String hxLocationWithContextData() {
return "";
}

@GetMapping("/hx-push-url-path")
@HxPushUrl("/path")
@ResponseBody
Expand All @@ -360,13 +290,6 @@ public String hxPushUrlFalse() {
return "";
}

@GetMapping("/hx-redirect")
@HxRedirect("/path")
@ResponseBody
public String hxRedirect() {
return "";
}

@GetMapping("/hx-replace-url-path")
@HxReplaceUrl("/path")
@ResponseBody
Expand Down