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

Add all constants to HtmxResponseHeader #23

Merged
merged 1 commit into from
May 20, 2022
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
Expand Up @@ -34,7 +34,7 @@ private void setHxTrigger(HttpServletResponse response, Method method) {
private void setHxRefresh(HttpServletResponse response, Method method) {
HxRefresh methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, HxRefresh.class);
if (methodAnnotation != null) {
response.setHeader("HX-Refresh", "true");
response.setHeader(HX_REFRESH.getValue(), "true");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.github.wimdeblauwe.hsbt.mvc;

public enum HtmxResponseHeader {
HX_PUSH("HX-Push"),
HX_REDIRECT("HX-Redirect"),
HX_REFRESH("HX-Refresh"),
HX_RETARGET("HX-Retarget"),
HX_TRIGGER("HX-Trigger"),
HX_TRIGGER_AFTER_SETTLE("HX-Trigger-After-Settle"),
HX_TRIGGER_AFTER_SWAP("HX-Trigger-After-Swap");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,18 @@
*/
package io.github.wimdeblauwe.hsbt.mvc;

import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.Assert;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.*;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any concerns with star imports?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I always let IntelliJ handle my imports and optimize them.


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
import java.util.Map;

/**
* A {@link HandlerInterceptor} that turns {@link HtmxResponse} instances returned from controller methods into a
Expand Down Expand Up @@ -92,27 +86,27 @@ private void addHxHeaders(HtmxResponse htmxResponse, HttpServletResponse respons
setTriggerHeader(HxTriggerLifecycle.SETTLE, htmxResponse.getTriggersAfterSettle(), response);
setTriggerHeader(HxTriggerLifecycle.SWAP, htmxResponse.getTriggersAfterSwap(), response);

if(htmxResponse.getHeaderPushHistory() != null) {
response.setHeader("HX-Push", htmxResponse.getHeaderPushHistory());
if (htmxResponse.getHeaderPushHistory() != null) {
response.setHeader(HtmxResponseHeader.HX_PUSH.getValue(), htmxResponse.getHeaderPushHistory());
}
if(htmxResponse.getHeaderRedirect() != null) {
response.setHeader("HX-Redirect", htmxResponse.getHeaderRedirect());
if (htmxResponse.getHeaderRedirect() != null) {
response.setHeader(HtmxResponseHeader.HX_REDIRECT.getValue(), htmxResponse.getHeaderRedirect());
}
if(htmxResponse.getHeaderRefresh()) {
response.setHeader("HX-Refresh", "true");
if (htmxResponse.getHeaderRefresh()) {
response.setHeader(HtmxResponseHeader.HX_REFRESH.getValue(), "true");
}
if(htmxResponse.getHeaderRetarget() != null) {
response.setHeader("HX-Retarget", htmxResponse.getHeaderRetarget());
if (htmxResponse.getHeaderRetarget() != null) {
response.setHeader(HtmxResponseHeader.HX_RETARGET.getValue(), htmxResponse.getHeaderRetarget());
}
}

private void setTriggerHeader(HxTriggerLifecycle triggerHeader, Map<String, String> triggers, HttpServletResponse response) {
if(triggers.isEmpty()) {
if (triggers.isEmpty()) {
return;
}
if(triggers.size() == 1) {
if (triggers.size() == 1) {
Map.Entry<String, String> singleHeader = triggers.entrySet().stream().findFirst().orElseThrow();
if(singleHeader.getValue() == null || singleHeader.getValue().isBlank()) {
if (singleHeader.getValue() == null || singleHeader.getValue().isBlank()) {
response.setHeader(triggerHeader.getHeaderName(), singleHeader.getKey());
} else {
try {
Expand All @@ -137,9 +131,9 @@ private View toView(HtmxResponse partials) {
return (model, request, response) -> {
Locale locale = locales.getObject().resolveLocale(request);
for (String template : partials.getTemplates()) {
views.resolveViewName(template, locale)
.render(model, request, response);

View view = views.resolveViewName(template, locale);
Assert.notNull(view, "Template '" + template + "' could not be resolved");
view.render(model, request, response);
}
};
}
Expand Down