From a925d8dab394561be3ae14160857c59331a06ab4 Mon Sep 17 00:00:00 2001 From: Michael Yan Date: Mon, 29 Apr 2024 22:46:54 +0800 Subject: [PATCH] Add Groovy Extensions for Servlet Request and Response --- .../HttpServletRequestExtension.groovy | 39 ++++++++++++ .../HttpServletResponseExtension.groovy | 35 +++++++++++ .../plugins/inertia/InertiaRequest.groovy | 63 +++++++++++++++++++ .../plugins/inertia/InertiaResponse.groovy | 53 ++++++++++++++++ ...rg.codehaus.groovy.runtime.ExtensionModule | 3 + 5 files changed, 193 insertions(+) create mode 100644 src/main/groovy/org/graceframework/plugins/inertia/HttpServletRequestExtension.groovy create mode 100755 src/main/groovy/org/graceframework/plugins/inertia/HttpServletResponseExtension.groovy create mode 100644 src/main/groovy/org/graceframework/plugins/inertia/InertiaRequest.groovy create mode 100644 src/main/groovy/org/graceframework/plugins/inertia/InertiaResponse.groovy create mode 100644 src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule diff --git a/src/main/groovy/org/graceframework/plugins/inertia/HttpServletRequestExtension.groovy b/src/main/groovy/org/graceframework/plugins/inertia/HttpServletRequestExtension.groovy new file mode 100644 index 0000000..d1d9066 --- /dev/null +++ b/src/main/groovy/org/graceframework/plugins/inertia/HttpServletRequestExtension.groovy @@ -0,0 +1,39 @@ +/* + * 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.http.HttpServletRequest + +import groovy.transform.CompileStatic + +/** + * Extends the {@link HttpServletRequest} object with new methods for handling Inertia Request + * + * @author Michael Yan + * @since 0.1 + */ +@CompileStatic +class HttpServletRequestExtension { + + static InertiaRequest getInertia(HttpServletRequest request) { + new InertiaRequest(request) + } + + static boolean isInertia(HttpServletRequest request) { + Boolean.parseBoolean(request.getHeader(InertiaRequest.X_INERTIA)) + } + +} diff --git a/src/main/groovy/org/graceframework/plugins/inertia/HttpServletResponseExtension.groovy b/src/main/groovy/org/graceframework/plugins/inertia/HttpServletResponseExtension.groovy new file mode 100755 index 0000000..ecebe59 --- /dev/null +++ b/src/main/groovy/org/graceframework/plugins/inertia/HttpServletResponseExtension.groovy @@ -0,0 +1,35 @@ +/* + * 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.http.HttpServletResponse + +import groovy.transform.CompileStatic + +/** + * Extends the {@link HttpServletResponse} object with new methods for handling Inertia Response + * + * @author Michael Yan + * @since 0.1 + */ +@CompileStatic +class HttpServletResponseExtension { + + static InertiaResponse getInertia(HttpServletResponse response) { + new InertiaResponse(response) + } + +} diff --git a/src/main/groovy/org/graceframework/plugins/inertia/InertiaRequest.groovy b/src/main/groovy/org/graceframework/plugins/inertia/InertiaRequest.groovy new file mode 100644 index 0000000..aa392f8 --- /dev/null +++ b/src/main/groovy/org/graceframework/plugins/inertia/InertiaRequest.groovy @@ -0,0 +1,63 @@ +/* + * 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.http.HttpServletRequest + +import groovy.transform.CompileStatic + +/** + * Inertia Protocol {link https://inertiajs.com/the-protocol} + * + * @author Michael Yan + * @since 0.1 + */ +@CompileStatic +class InertiaRequest { + + public static final String X_INERTIA = 'X-Inertia' + public static final String X_INERTIA_VERSION = 'X-Inertia-Version' + public static final String X_INERTIA_LOCATION = 'X-Inertia-Location' + public static final String X_INERTIA_PARTIAL_DATA = 'X-Inertia-Partial-Data' + public static final String X_INERTIA_PARTIAL_COMPONENT = 'X-Inertia-Partial-Component' + + private final HttpServletRequest request + + InertiaRequest(HttpServletRequest request) { + this.request = request + } + + /** + * Indicates that the request is via an element using X-Inertia + */ + boolean isInertia() { + Boolean.parseBoolean(getHeaderValue(X_INERTIA)) + } + + /** + * Get header value from request + * @param The name of header + * @return value + */ + String getHeaderValue(String name) { + this.request.getHeader(name) + } + + boolean asBoolean() { + isInertia() + } + +} diff --git a/src/main/groovy/org/graceframework/plugins/inertia/InertiaResponse.groovy b/src/main/groovy/org/graceframework/plugins/inertia/InertiaResponse.groovy new file mode 100644 index 0000000..0c18e07 --- /dev/null +++ b/src/main/groovy/org/graceframework/plugins/inertia/InertiaResponse.groovy @@ -0,0 +1,53 @@ +/* + * 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.http.HttpServletResponse + +import groovy.transform.CompileStatic + +import static org.graceframework.plugins.inertia.InertiaRequest.* + +/** + * Inertia Response {link https://inertiajs.com/the-protocol#inertia-responses} + * + * @author Michael Yan + * @since 0.1 + */ +@CompileStatic +class InertiaResponse { + + private final HttpServletResponse response + + InertiaResponse(HttpServletResponse response) { + this.response = response + } + + /** + * The server may immediately returns a 409 Conflict response if the asset versions are different, and includes the URL in a X-Inertia-Location header. + * This header is necessary, since server-side redirects may have occurred. This tells Inertia what the final intended destination URL is. + * + * @param location + */ + void setLocation(String location) { + setHeaderValue(X_INERTIA_LOCATION, location) + } + + void setHeaderValue(String name, Object value) { + this.response.setHeader(name, (value == null) ? "" : value.toString()) + } + +} diff --git a/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule b/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule new file mode 100644 index 0000000..2005bf5 --- /dev/null +++ b/src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule @@ -0,0 +1,3 @@ +moduleName=grace-inertia-module +moduleVersion=1.0 +extensionClasses=org.graceframework.plugins.inertia.HttpServletRequestExtension,org.graceframework.plugins.inertia.HttpServletResponseExtension