Skip to content

Commit

Permalink
Merge pull request #22 from ortus-boxlang/development
Browse files Browse the repository at this point in the history
1.0.0-Beta19
  • Loading branch information
lmajano authored Oct 18, 2024
2 parents 667b0c8 + 97a9c18 commit adbd3dc
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 5 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ dependencies {

// Servlet API
implementation 'javax.servlet:javax.servlet-api:4.0.1'
// We only need these for the PageContext class
implementation 'javax.servlet.jsp:javax.servlet.jsp-api:2.3.3'
implementation 'javax.el:javax.el-api:3.0.0'



implementation 'commons-fileupload:commons-fileupload:1.5'

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Fri Oct 04 19:25:39 UTC 2024
boxlangVersion=1.0.0-beta18
#Fri Oct 11 15:36:38 UTC 2024
boxlangVersion=1.0.0-beta19
jdkVersion=21
version=1.0.0-beta18
version=1.0.0-beta19
group=ortus.boxlang
3 changes: 2 additions & 1 deletion src/main/java/ortus/boxlang/servlet/BoxLangServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void init( ServletConfig config ) throws ServletException {
System.out.println(
"Ortus BoxLang Version: " + versionInfo.getAsString( Key.of( "version" ) ) + " (Built On: " + versionInfo.getAsString( Key.of( "buildDate" ) )
+ ")" );

}

/**
Expand All @@ -108,7 +109,7 @@ public void init( ServletConfig config ) throws ServletException {
public void service( ServletRequest req, ServletResponse res ) throws ServletException, IOException {
// FusionReactor automatically tracks servlets
// Note: web root can be different every request if this is a multi-site server or using ModCFML
var exchange = new BoxHTTPServletExchange( ( HttpServletRequest ) req, ( HttpServletResponse ) res );
var exchange = new BoxHTTPServletExchange( ( HttpServletRequest ) req, ( HttpServletResponse ) res, this );
try {
WebRequestExecutor.execute( exchange, config.getServletContext().getRealPath( "/" ), false );
} finally {
Expand Down
256 changes: 256 additions & 0 deletions src/main/java/ortus/boxlang/servlet/BoxPageContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
package ortus.boxlang.servlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.el.ELContext;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;

public class BoxPageContext extends PageContext {

private final Map<String, Object> pageAttributes = new HashMap<>();
private ServletRequest request;
private ServletResponse response;
private HttpSession session;
private ServletContext application;
private Object page;
private JspWriter out;
private Exception exception;
private ServletConfig servletConfig;

@Override
public ELContext getELContext() {
throw new UnsupportedOperationException( "Not supported yet." );
}

@Override
public JspWriter getOut() {
return out;
}

@Override
public HttpSession getSession() {
return session;
}

@Override
public Object getAttribute( String name, int scope ) {
switch ( scope ) {
case PAGE_SCOPE :
return pageAttributes.get( name );
case REQUEST_SCOPE :
return request.getAttribute( name );
case SESSION_SCOPE :
return session.getAttribute( name );
case APPLICATION_SCOPE :
return application.getAttribute( name );
default :
throw new IllegalArgumentException( "Invalid scope: " + scope );
}
}

@Override
public Enumeration<String> getAttributeNamesInScope( int scope ) {
switch ( scope ) {
case PAGE_SCOPE :
return java.util.Collections.enumeration( pageAttributes.keySet() );
case REQUEST_SCOPE :
return request.getAttributeNames();
case SESSION_SCOPE :
return session.getAttributeNames();
case APPLICATION_SCOPE :
return application.getAttributeNames();
default :
throw new IllegalArgumentException( "Invalid scope: " + scope );
}
}

@Override
public int getAttributesScope( String name ) {
if ( pageAttributes.containsKey( name ) ) {
return PAGE_SCOPE;
} else if ( request.getAttribute( name ) != null ) {
return REQUEST_SCOPE;
} else if ( session != null && session.getAttribute( name ) != null ) {
return SESSION_SCOPE;
} else if ( application.getAttribute( name ) != null ) {
return APPLICATION_SCOPE;
} else {
return 0;
}
}

@Override
public void setAttribute( String name, Object value ) {
pageAttributes.put( name, value );
}

@Override
public void setAttribute( String name, Object value, int scope ) {
switch ( scope ) {
case PAGE_SCOPE :
pageAttributes.put( name, value );
break;
case REQUEST_SCOPE :
request.setAttribute( name, value );
break;
case SESSION_SCOPE :
session.setAttribute( name, value );
break;
case APPLICATION_SCOPE :
application.setAttribute( name, value );
break;
default :
throw new IllegalArgumentException( "Invalid scope: " + scope );
}
}

@Override
public Object getAttribute( String name ) {
return pageAttributes.get( name );
}

@Override
public Object findAttribute( String name ) {
Object value = pageAttributes.get( name );
if ( value == null ) {
value = request.getAttribute( name );
}
if ( value == null && session != null ) {
value = session.getAttribute( name );
}
if ( value == null ) {
value = application.getAttribute( name );
}
return value;
}

@Override
public void removeAttribute( String name ) {
pageAttributes.remove( name );
}

@Override
public void removeAttribute( String name, int scope ) {
switch ( scope ) {
case PAGE_SCOPE :
pageAttributes.remove( name );
break;
case REQUEST_SCOPE :
request.removeAttribute( name );
break;
case SESSION_SCOPE :
session.removeAttribute( name );
break;
case APPLICATION_SCOPE :
application.removeAttribute( name );
break;
default :
throw new IllegalArgumentException( "Invalid scope: " + scope );
}
}

@Override
public void initialize( Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize,
boolean autoFlush ) {
this.request = request;
this.response = response;
this.application = request.getServletContext();
this.session = null;
}

@Override
public void release() {
pageAttributes.clear();
request = null;
response = null;
session = null;
application = null;
page = null;
out = null;
exception = null;
servletConfig = null;
}

@Override
public Object getPage() {
return page;
}

@Override
public ServletRequest getRequest() {
return request;
}

@Override
public ServletResponse getResponse() {
return response;
}

@Override
public Exception getException() {
return exception;
}

@Override
public ServletConfig getServletConfig() {
return servletConfig;
}

@Override
public ServletContext getServletContext() {
return application;
}

@Override
public void forward( String path ) throws ServletException, IOException {
request.getRequestDispatcher( path ).forward( request, response );
}

@Override
public void include( String path ) throws ServletException, IOException {
request.getRequestDispatcher( path ).include( request, response );
}

@Override
public void include( String path, boolean flush ) throws ServletException, IOException {
if ( flush ) {
// out.flush();
}
request.getRequestDispatcher( path ).include( request, response );
}

@SuppressWarnings( "deprecation" )
@Override
public ExpressionEvaluator getExpressionEvaluator() {
throw new UnsupportedOperationException( "Not supported yet." );
}

@SuppressWarnings( "deprecation" )
@Override
public VariableResolver getVariableResolver() {
throw new UnsupportedOperationException( "Not supported yet." );
}

@Override
public void handlePageException( Exception e ) throws ServletException, IOException {
throw new UnsupportedOperationException( "Not supported yet." );
}

@Override
public void handlePageException( Throwable t ) throws ServletException, IOException {
throw new UnsupportedOperationException( "Not supported yet." );
}
}
78 changes: 78 additions & 0 deletions src/main/java/ortus/boxlang/web/bifs/GetPageContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* [BoxLang]
*
* Copyright [2023] [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.web.bifs;

import javax.servlet.jsp.JspWriter;

import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.scopes.ArgumentsScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.servlet.BoxPageContext;
import ortus.boxlang.web.context.WebRequestBoxContext;
import ortus.boxlang.web.exchange.BoxHTTPServletExchange;

@BoxBIF
public class GetPageContext extends BIF {

private static final Key page_context_attachment = Key.of( "page_context_attachment" );

/**
* Constructor
*/
public GetPageContext() {
super();
}

/**
*
* Gets the current java PageContext object that provides access to page attributes and configuration, request and response objects.
* If not running in a servlet, this will be a fake class attempting to provide most of the common methods.
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
WebRequestBoxContext requestContext = context.getParentOfType( WebRequestBoxContext.class );
// Create if neccessary
if ( !requestContext.hasAttachment( page_context_attachment ) ) {
synchronized ( requestContext ) {
// Double check lock pattern
if ( !requestContext.hasAttachment( page_context_attachment ) ) {
// Create a PageContext object
BoxHTTPServletExchange exchange = ( BoxHTTPServletExchange ) requestContext.getHTTPExchange();

BoxPageContext pageContext = new BoxPageContext();
pageContext.initialize(
exchange.getServlet(),
exchange.getServletRequest(),
exchange.getServletResponse(),
null,
true,
JspWriter.DEFAULT_BUFFER,
true
);
// Attach the PageContext to the request context so it's available for the duration of the request
requestContext.putAttachment( page_context_attachment, pageContext );
}
}
}
// Return the PageContext object which is now attached to the request context
return requestContext.getAttachment( page_context_attachment );
}

}
Loading

0 comments on commit adbd3dc

Please sign in to comment.