Skip to content

Commit

Permalink
Add AutoConfiguration for Inertia Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rainboyan committed Apr 29, 2024
1 parent a925d8d commit 74ebb04
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2024 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
*
* https://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.graceframework.plugins.inertia;

import javax.servlet.DispatcherType;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.ConditionalOnMissingFilterBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.filter.OrderedFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* {@link EnableAutoConfiguration Auto-configuration} for Inertia Plugin.
*
* @author Michael Yan
* @since 0.1
*/
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@Configuration(proxyBeanMethods = false)
public class InertiaAutoConfiguration {

@Bean
@ConditionalOnMissingFilterBean
public FilterRegistrationBean<InertiaRequestFilter> inertiaFilter() {
InertiaRequestFilter filter = new InertiaRequestFilter();
FilterRegistrationBean<InertiaRequestFilter> registration = new FilterRegistrationBean<>(filter);
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);
registration.setOrder(OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER + 100);
return registration;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2024 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
*
* https://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.graceframework.plugins.inertia;

import grails.web.mime.MimeType;

/**
* {@link MimeType} for Inertia
*
* @author Michael Yan
* @since 0.1
*/
public class InertiaMimeType {

public static final String INERTIA_FORMAT = "inertia";

public static final MimeType INERTIA = new MimeType("application/json", "inertia");

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2024 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
*
* https://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.graceframework.plugins.inertia;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

import grails.web.http.HttpHeaders;
import grails.web.mime.MimeType;
import org.grails.web.util.GrailsApplicationAttributes;

/**
* {@link OncePerRequestFilter} to check current request whether from Inertia or not,
* and will set attribute content and response format.
*
* @author Michael Yan
* @since 0.1
*/
public class InertiaRequestFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

request.setAttribute(GrailsApplicationAttributes.CONTENT_FORMAT, InertiaMimeType.INERTIA_FORMAT);
request.setAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT, InertiaMimeType.INERTIA_FORMAT);
request.setAttribute(GrailsApplicationAttributes.RESPONSE_MIME_TYPE, InertiaMimeType.INERTIA);

filterChain.doFilter(request, response);
}

@Override
protected boolean shouldNotFilter(HttpServletRequest request) {
return !(MimeType.ALL.getName().equals(request.getHeader(HttpHeaders.ACCEPT))
&& Boolean.parseBoolean(request.getHeader(InertiaRequest.X_INERTIA)));
}

}
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.graceframework.plugins.inertia.InertiaAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.graceframework.plugins.inertia.InertiaAutoConfiguration

0 comments on commit 74ebb04

Please sign in to comment.