-
-
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.
Merge pull request #22 from ortus-boxlang/development
1.0.0-Beta19
- Loading branch information
Showing
6 changed files
with
361 additions
and
5 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
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 |
---|---|---|
@@ -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 |
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
256 changes: 256 additions & 0 deletions
256
src/main/java/ortus/boxlang/servlet/BoxPageContext.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,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." ); | ||
} | ||
} |
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,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 ); | ||
} | ||
|
||
} |
Oops, something went wrong.