Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@HxPushUrl(true) now retreives the path from the request #129

Merged
merged 2 commits into from
Sep 10, 2024
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
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package io.github.wimdeblauwe.htmx.spring.boot.mvc;

import static io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxResponseHeader.*;

import java.lang.reflect.Method;
import java.time.Duration;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.lang.reflect.Method;
import java.time.Duration;

import static io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxResponseHeader.*;

public class HtmxHandlerInterceptor implements HandlerInterceptor {

Expand All @@ -30,10 +29,10 @@ public HtmxHandlerInterceptor(ObjectMapper objectMapper, HtmxResponseHandlerMeth

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if(modelAndView != null) {
if (modelAndView != null) {
modelAndView.getModel().values().forEach(
value ->{
if(value instanceof HtmxResponse) {
value -> {
if (value instanceof HtmxResponse) {
buildAndRender((HtmxResponse) value, modelAndView, request, response);
} else if (value instanceof HtmxResponse.Builder) {
buildAndRender(((HtmxResponse.Builder) value).build(), modelAndView, request, response);
Expand All @@ -60,9 +59,9 @@ public boolean preHandle(HttpServletRequest request,
if (handler instanceof HandlerMethod) {
Method method = ((HandlerMethod) handler).getMethod();
setHxLocation(response, method);
setHxPushUrl(response, method);
setHxPushUrl(request, response, method);
setHxRedirect(response, method);
setHxReplaceUrl(response, method);
setHxReplaceUrl(request, response, method);
setHxReswap(response, method);
setHxRetarget(response, method);
setHxReselect(response, method);
Expand Down Expand Up @@ -94,10 +93,14 @@ private void setHxLocation(HttpServletResponse response, Method method) {
}
}

private void setHxPushUrl(HttpServletResponse response, Method method) {
private void setHxPushUrl(HttpServletRequest request, HttpServletResponse response, Method method) {
HxPushUrl methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxPushUrl.class);
if (methodAnnotation != null) {
setHeader(response, HX_PUSH_URL, methodAnnotation.value());
if (HtmxValue.TRUE.equals(methodAnnotation.value())) {
setHeader(response, HX_PUSH_URL, getRequestUrl(request));
} else {
setHeader(response, HX_PUSH_URL, methodAnnotation.value());
}
}
}

Expand All @@ -108,10 +111,14 @@ private void setHxRedirect(HttpServletResponse response, Method method) {
}
}

private void setHxReplaceUrl(HttpServletResponse response, Method method) {
private void setHxReplaceUrl(HttpServletRequest request, HttpServletResponse response, Method method) {
HxReplaceUrl methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxReplaceUrl.class);
if (methodAnnotation != null) {
setHeader(response, HX_REPLACE_URL, methodAnnotation.value());
if (HtmxValue.TRUE.equals(methodAnnotation.value())) {
setHeader(response, HX_REPLACE_URL, getRequestUrl(request));
} else {
setHeader(response, HX_REPLACE_URL, methodAnnotation.value());
}
}
}

Expand Down Expand Up @@ -253,4 +260,15 @@ private HtmxReswap.Position convertToPosition(HxReswap.Position position) {
};
}

private String getRequestUrl(HttpServletRequest request) {
String path = request.getRequestURI();
String queryString = request.getQueryString();

if (queryString != null && !queryString.isEmpty()) {
path += "?" + queryString;
}
return path;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
/**
* The value for the {@code HX-Replace-Url} response header.
*/
String value();
String value() default HtmxValue.TRUE;

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,24 @@ public void testHxLocationWithoutContextData() throws Exception {
}

@Test
public void testHxPushUrl() throws Exception {
mockMvc.perform(get("/hx-push-url"))
public void testHxPushUrlPath() throws Exception {
mockMvc.perform(get("/hx-push-url-path"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Push-Url", "/path"));
}
@Test
public void testHxPushUrl() throws Exception {
mockMvc.perform(get("/hx-push-url?test=hello"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Push-Url", "/hx-push-url?test=hello"));
}

@Test
public void testHxPushUrlFalse() throws Exception {
mockMvc.perform(get("/hx-push-url-false?test=hello"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Push-Url", "false"));
}

@Test
public void testHxRedirect() throws Exception {
Expand All @@ -155,12 +168,26 @@ public void testHxRedirect() throws Exception {
}

@Test
public void testHxReplaceUrl() throws Exception {
mockMvc.perform(get("/hx-replace-url"))
public void testHxReplaceUrlPath() throws Exception {
mockMvc.perform(get("/hx-replace-url-path"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Replace-Url", "/path"));
}

@Test
public void testHxReplaceUrl() throws Exception {
mockMvc.perform(get("/hx-replace-url?test=hello&test2=hello2"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Replace-Url", "/hx-replace-url?test=hello&test2=hello2"));
}

@Test
public void testHxReplaceUrlFalse() throws Exception {
mockMvc.perform(get("/hx-replace-url-false?test=hello"))
.andExpect(status().isOk())
.andExpect(header().string("HX-Replace-Url", "false"));
}

@Test
public void testHxReswap() throws Exception {
mockMvc.perform(get("/hx-reswap"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,52 @@ public String hxLocationWithContextData() {
return "";
}

@GetMapping("/hx-push-url")
@GetMapping("/hx-push-url-path")
@HxPushUrl("/path")
@ResponseBody
public String hxPushUrlPath() {
return "";
}

@GetMapping("/hx-push-url")
@HxPushUrl
tschuehly marked this conversation as resolved.
Show resolved Hide resolved
@ResponseBody
public String hxPushUrl() {
return "";
}

@GetMapping("/hx-push-url-false")
@HxPushUrl(HtmxValue.FALSE)
@ResponseBody
public String hxPushUrlFalse() {
return "";
}

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

@GetMapping("/hx-replace-url")
@GetMapping("/hx-replace-url-path")
@HxReplaceUrl("/path")
@ResponseBody
public String hxReplaceUrlPath() {
return "";
}
@GetMapping("/hx-replace-url")
@HxReplaceUrl
@ResponseBody
public String hxReplaceUrl() {
return "";
}
@GetMapping("/hx-replace-url-false")
@HxReplaceUrl(HtmxValue.FALSE)
@ResponseBody
public String hxReplaceUrlFalse() {
return "";
}

@GetMapping("/hx-reswap")
@HxReswap(value = HxSwapType.INNER_HTML, swap = 300)
Expand Down