-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure MimeType and Response format for Turbo
- Loading branch information
Showing
6 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
plugins/turbo/src/main/groovy/org/graceframework/plugins/turbo/TurboAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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.turbo; | ||
|
||
import javax.servlet.DispatcherType; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigureOrder; | ||
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; | ||
|
||
import grails.web.mime.MimeTypeProvider; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for Turbo Plugin. | ||
* | ||
* @author Michael Yan | ||
* @since 0.1 | ||
*/ | ||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) | ||
@Configuration(proxyBeanMethods = false) | ||
@AutoConfigureOrder(-100) | ||
public class TurboAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingFilterBean | ||
public FilterRegistrationBean<TurboRequestFilter> turboFilter() { | ||
TurboRequestFilter filter = new TurboRequestFilter(); | ||
FilterRegistrationBean<TurboRequestFilter> registration = new FilterRegistrationBean<>(filter); | ||
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC); | ||
registration.setOrder(OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER + 100); | ||
return registration; | ||
} | ||
|
||
@Bean | ||
public MimeTypeProvider turboMimeTypeProvider() { | ||
return new TurboMimeTypeProvider(); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
plugins/turbo/src/main/groovy/org/graceframework/plugins/turbo/TurboMimeType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.turbo; | ||
|
||
import grails.web.mime.MimeType; | ||
|
||
/** | ||
* {@link MimeType} for Turbo | ||
* | ||
* @author Michael Yan | ||
* @since 0.1 | ||
*/ | ||
public class TurboMimeType { | ||
|
||
public static final String TURBO_STREAM_FORMAT = "turbo_stream"; | ||
|
||
public static final MimeType TURBO_STREAM = new MimeType("text/vnd.turbo-stream.html", "turbo_stream"); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
plugins/turbo/src/main/groovy/org/graceframework/plugins/turbo/TurboMimeTypeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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.turbo; | ||
|
||
import grails.web.mime.MimeType; | ||
import grails.web.mime.MimeTypeProvider; | ||
|
||
/** | ||
* {@link MimeTypeProvider} for Turbo | ||
* | ||
* @author Michael Yan | ||
* @since 0.1 | ||
*/ | ||
public class TurboMimeTypeProvider implements MimeTypeProvider { | ||
|
||
@Override | ||
public MimeType[] getMimeTypes() { | ||
return new MimeType[] { TurboMimeType.TURBO_STREAM }; | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
plugins/turbo/src/main/groovy/org/graceframework/plugins/turbo/TurboRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.turbo; | ||
|
||
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 Turbo Stream or not, | ||
* and will set attribute content and response format. | ||
* | ||
* @author Michael Yan | ||
* @since 0.1 | ||
*/ | ||
public class TurboRequestFilter extends OncePerRequestFilter { | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
request.setAttribute(GrailsApplicationAttributes.CONTENT_FORMAT, TurboMimeType.TURBO_STREAM_FORMAT); | ||
request.setAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT, TurboMimeType.TURBO_STREAM_FORMAT); | ||
request.setAttribute(GrailsApplicationAttributes.RESPONSE_MIME_TYPE, TurboMimeType.TURBO_STREAM); | ||
response.setContentType(TurboMimeType.TURBO_STREAM.getName()); | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
@Override | ||
protected boolean shouldNotFilter(HttpServletRequest request) { | ||
return !HttpServletRequestExtension.isTurboStream(request); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Auto Configure | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
org.graceframework.plugins.turbo.TurboAutoConfiguration |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.graceframework.plugins.turbo.TurboAutoConfiguration |