-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
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
100 changes: 100 additions & 0 deletions
100
src/main/java/ortus/boxlang/runtime/components/web/Header.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,100 @@ | ||
/** | ||
* [BoxLang] | ||
* | ||
* Copyright [2024] [Ortus Solutions, Corp] | ||
* | ||
* 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 | ||
* | ||
* http://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 ortus.boxlang.runtime.components.web; | ||
|
||
import java.util.Set; | ||
|
||
import io.undertow.server.HttpServerExchange; | ||
import io.undertow.util.HttpString; | ||
import ortus.boxlang.runtime.components.Attribute; | ||
import ortus.boxlang.runtime.components.BoxComponent; | ||
import ortus.boxlang.runtime.components.Component; | ||
import ortus.boxlang.runtime.context.IBoxContext; | ||
import ortus.boxlang.runtime.scopes.Key; | ||
import ortus.boxlang.runtime.types.IStruct; | ||
import ortus.boxlang.runtime.types.exceptions.BoxValidationException; | ||
import ortus.boxlang.runtime.validation.Validator; | ||
import ortus.boxlang.web.WebRequestBoxContext; | ||
|
||
@BoxComponent | ||
public class Header extends Component { | ||
|
||
public Header() { | ||
super(); | ||
declaredAttributes = new Attribute[] { | ||
new Attribute( Key._NAME, "string", Set.of( | ||
Validator.NON_EMPTY, | ||
( context, caller, record, records ) -> { | ||
if ( !records.containsKey( Key.statusCode ) && !records.containsKey( Key._NAME ) ) { | ||
throw new BoxValidationException( Key._NAME, record, "is required unless statusCode is provided." ); | ||
} | ||
} ) ), | ||
new Attribute( Key.value, "string", "" ), | ||
new Attribute( Key.charset, "String", "UTF-8" ), | ||
new Attribute( Key.statusCode, "integer", Set.of( Validator.min( 100 ), Validator.max( 599 ) ) ), | ||
new Attribute( Key.statusText, "string", "" ) | ||
}; | ||
} | ||
|
||
/** | ||
* Generates custom HTTP response headers to return to the client. | ||
* | ||
* @param context The context in which the Component is being invoked | ||
* @param attributes The attributes to the Component | ||
* @param body The body of the Component | ||
* @param executionState The execution state of the Component | ||
* | ||
* | ||
* @atribute.name Header name | ||
* | ||
* @atribute.value HTTP header value | ||
* | ||
* @atribute.charset The character encoding in which to encode the header value. | ||
* | ||
* @atribute.statusCode The HTTP status code to return | ||
* | ||
* @atribute.statusText The HTTP status text to return | ||
* | ||
*/ | ||
public BodyResult _invoke( IBoxContext context, IStruct attributes, ComponentBody body, IStruct executionState ) { | ||
String name = attributes.getAsString( Key._NAME ); | ||
String value = attributes.getAsString( Key.value ); | ||
// TODO: Figure out how to use charset, if at all. | ||
String charset = attributes.getAsString( Key.charset ); | ||
Integer statusCode = attributes.getAsInteger( Key.statusCode ); | ||
String statusText = attributes.getAsString( Key.statusText ); | ||
|
||
WebRequestBoxContext requestContext = context.getParentOfType( WebRequestBoxContext.class ); | ||
HttpServerExchange exchange = requestContext.getExchange(); | ||
|
||
if ( statusCode != null ) { | ||
exchange.setStatusCode( statusCode ); | ||
if ( statusText != null ) { | ||
exchange.setReasonPhrase( statusText ); | ||
} | ||
} else { | ||
// TODO: check and see if we remove existing headers, or just append | ||
exchange.getResponseHeaders().add( | ||
new HttpString( name ), | ||
value | ||
); | ||
} | ||
|
||
return DEFAULT_RETURN; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/ortus/boxlang/runtime/components/web/Location.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,75 @@ | ||
/** | ||
* [BoxLang] | ||
* | ||
* Copyright [2024] [Ortus Solutions, Corp] | ||
* | ||
* 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 | ||
* | ||
* http://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 ortus.boxlang.runtime.components.web; | ||
|
||
import java.util.Set; | ||
|
||
import io.undertow.server.HttpServerExchange; | ||
import io.undertow.util.HttpString; | ||
import ortus.boxlang.runtime.components.Attribute; | ||
import ortus.boxlang.runtime.components.BoxComponent; | ||
import ortus.boxlang.runtime.components.Component; | ||
import ortus.boxlang.runtime.context.IBoxContext; | ||
import ortus.boxlang.runtime.scopes.Key; | ||
import ortus.boxlang.runtime.types.IStruct; | ||
import ortus.boxlang.runtime.types.exceptions.AbortException; | ||
import ortus.boxlang.runtime.validation.Validator; | ||
import ortus.boxlang.web.WebRequestBoxContext; | ||
|
||
@BoxComponent | ||
public class Location extends Component { | ||
|
||
public Location() { | ||
super(); | ||
declaredAttributes = new Attribute[] { | ||
new Attribute( Key.URL, "string", Set.of( Validator.NON_EMPTY ) ), | ||
new Attribute( Key.addToken, "boolean" ), | ||
new Attribute( Key.statusCode, "integer", 302, Set.of( Validator.min( 301 ), Validator.max( 399 ) ) ) | ||
}; | ||
} | ||
|
||
/** | ||
* Generates custom HTTP response headers to return to the client. | ||
* | ||
* @param context The context in which the Component is being invoked | ||
* @param attributes The attributes to the Component | ||
* @param body The body of the Component | ||
* @param executionState The execution state of the Component | ||
* | ||
* | ||
* @atribute.URL The URL of web page to open. | ||
* | ||
* @arguments.addToken clientManagement must be enabled. | ||
* | ||
* @argument.statusCode The HTTP status code. | ||
* | ||
*/ | ||
public BodyResult _invoke( IBoxContext context, IStruct attributes, ComponentBody body, IStruct executionState ) { | ||
String URL = attributes.getAsString( Key.URL ); | ||
Boolean addToken = attributes.getAsBoolean( Key.addToken ); | ||
Integer statusCode = attributes.getAsInteger( Key.statusCode ); | ||
|
||
WebRequestBoxContext requestContext = context.getParentOfType( WebRequestBoxContext.class ); | ||
HttpServerExchange exchange = requestContext.getExchange(); | ||
|
||
exchange.setStatusCode( statusCode ); | ||
exchange.getResponseHeaders().put( new HttpString( "location" ), URL ); | ||
|
||
throw new AbortException(); | ||
} | ||
} |