Skip to content

Commit

Permalink
Add Groovy Extensions for Servlet Request and Response
Browse files Browse the repository at this point in the history
  • Loading branch information
rainboyan committed Apr 29, 2024
1 parent 4e7519d commit a925d8d
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
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))
}

}
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)
}

}
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()
}

}
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())
}

}
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

0 comments on commit a925d8d

Please sign in to comment.