-
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.
Add Groovy Extensions for Servlet Request and Response
- Loading branch information
Showing
5 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/groovy/org/graceframework/plugins/inertia/HttpServletRequestExtension.groovy
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,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)) | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/groovy/org/graceframework/plugins/inertia/HttpServletResponseExtension.groovy
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,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) | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/main/groovy/org/graceframework/plugins/inertia/InertiaRequest.groovy
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,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() | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/main/groovy/org/graceframework/plugins/inertia/InertiaResponse.groovy
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,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()) | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
src/main/resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
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 @@ | ||
moduleName=grace-inertia-module | ||
moduleVersion=1.0 | ||
extensionClasses=org.graceframework.plugins.inertia.HttpServletRequestExtension,org.graceframework.plugins.inertia.HttpServletResponseExtension |