telosys
Blame | Last modification | View Log | RSS feed
package org.objectweb.telosys.uil.taglib;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import org.objectweb.telosys.common.Telosys;
import org.objectweb.telosys.common.TelosysClassLogger;
import org.objectweb.telosys.common.TelosysConst;
import org.objectweb.telosys.common.TelosysProperties;
import org.objectweb.telosys.screen.env.ScreenSession;
import org.objectweb.telosys.screen.env.ScreenSessionManager;
import org.objectweb.telosys.uil.TelosysUIL;
import org.objectweb.telosys.uil.i18n.Translator;
import org.objectweb.telosys.uil.renderer.Renderers;
import org.objectweb.telosys.uil.screenmap.ScreenDefConfig;
import org.objectweb.telosys.uil.screenmap.ScreenDefRootDir;
import org.objectweb.telosys.uil.screenmap.ScreenDefinitions;
import org.objectweb.telosys.uil.screenmap.ScreenMap;
import org.objectweb.telosys.uil.screenmap.ScreenMapConst;
import org.objectweb.telosys.uil.taglib.widget.TagLocalDefaultValues;
import org.objectweb.telosys.uil.taglib.widget.Variables;
/**
* @author Laurent GUERIN
*
*/
public class TagCommons
{
private final static String TELOSYS_SCREEN_ENV = "TELOSYS_SCREEN_ENV";
private final static String DEFAULT_UPLOAD_PATH = "/upload";
private static final TelosysClassLogger log = new TelosysClassLogger(TagCommons.class);
// removed in v 1.0.4
// /**
// * Creates a screen environment for the current page context
// *
// * @param pageContext
// * @return
// */
// public static TagScreenEnv initScreenEnv(PageContext pageContext)
// {
// //$log.trace("TagCommons : initScreenEnv " );
// //TagScreenEnv screenEnv = new TagScreenEnv(pageContext);
// TagScreenEnv screenEnv = new TagScreenEnv();
// pageContext.setAttribute(TELOSYS_SCREEN_ENV, screenEnv);
// return screenEnv;
// }
/**
* Returns the screen map instance associated with the current page <br>
* The screen map can be null if the JSP is invoked directly (without the screenmap servlet)
*
* @param pageContext
* @return the screen map (or null)
*/
//private static ScreenMap getScreenMap(PageContext pageContext)
/* package */ static ScreenMap getScreenMap(PageContext pageContext) // v 0.9.9
{
ScreenMap screenMap = null;
ServletRequest request = pageContext.getRequest();
if (request != null)
{
screenMap = (ScreenMap) request.getAttribute(ScreenMapConst.SCREEN_MAP);
// if (screenMap != null)
// {
// return screenMap;
// }
// ScreenMap not found : It's not an error => do not log
// else
// {
// log.error("getScreenMap() : Screen map attribute ('" + ScreenMapConst.SCREEN_MAP + "') not found in request ");
// }
return screenMap; // can be NULL
}
else
{
log.error("getScreenMap() : Cannot get request object from 'pageContext' ");
}
return null;
}
private static String getCurrentRenderer(PageContext pageContext)
{
ServletRequest request = pageContext.getRequest();
if (request != null)
{
Object obj = request.getAttribute(TelosysConst.CURRENT_RENDERER);
if (obj != null)
{
if (obj instanceof String )
{
return (String) obj ;
}
}
}
else
{
log.error("getCurrentRenderer() : Cannot get request object from 'pageContext' ");
}
return null;
}
public static Translator getTranslator(PageContext pageContext)
{
//--- Try to find a renderer from the most nested page fragment
Translator t = null ;
//--- 1) Renderer
t = getRendererTranslator(pageContext);
if ( t != null ) return t ;
//--- 2) Screen Map Body
t = getScreenMapTranslator(pageContext);
if ( t != null ) return t ;
//--- 3) Screen Map Template
// TODO
//--- Not found
return null ;
}
/**
* Returns the translator of the current renderer (if any)
* @param pageContext
* @return
*/
private static Translator getRendererTranslator(PageContext pageContext)
{
//--- Is there a renderer associated with this page context ?
String sRenderer = getCurrentRenderer(pageContext);
if (sRenderer != null)
{
// We are in a renderer processing ...
return Renderers.getTranslator(sRenderer);
}
return null ;
}
/**
* Returns the translator of the current screen map (if any)
* @param pageContext
* @return
*/
private static Translator getScreenMapTranslator(PageContext pageContext)
{
//ScreenMapTranslator translator = null;
//--- Is there a screen map associated with this page context ?
ScreenMap screenMap = getScreenMap(pageContext);
if (screenMap != null)
{
return screenMap.getTranslator();
}
return null ;
}
/**
* Returns the screen environment for the current page context
*
* @param pageContext
* @return
*/
//public static TagLocalDefaultValues getScreenEnv(PageContext pageContext)
public static TagLocalDefaultValues getLocalDefaultValues(PageContext pageContext)
{
TagLocalDefaultValues localDefaults = (TagLocalDefaultValues) pageContext.getAttribute(TELOSYS_SCREEN_ENV);
if (localDefaults != null)
{
return localDefaults;
}
else
{
//--- Creates a new ScreenEnv if it doesn't exist
//screenEnv = initScreenEnv(pageContext); // v 1.0.4
localDefaults = new TagLocalDefaultValues();// v 1.0.4
pageContext.setAttribute(TELOSYS_SCREEN_ENV, localDefaults);// v 1.0.4
return localDefaults;// v 1.0.4
}
}
/**
* Returns the screen type associated with the given page context ( "html", "xul", null )
*
* @param pageContext
* @return
*/
public static String getScreenType(PageContext pageContext)
{
//--- Is there a screen map associated with this page context ?
ScreenMap screenMap = getScreenMap(pageContext);
if (screenMap != null)
{
return screenMap.getType();
}
else
{
//--- There's no screen map => is there a specific screen type ?
Variables variables = Variables.getVariables(pageContext, false);
if (variables != null)
{
return variables.getScreenType();
}
}
//--- Unknown type
return null;
}
/**
* Returns true if the given string can be considered as "html"
*
* @param sScreenType
* @return
*/
public static boolean isHTML(String sScreenType)
{
if (sScreenType != null)
{
String s = sScreenType.trim().toLowerCase();
return (s.equals("html") ? true : false);
}
return false;
}
/**
* Returns true if the given string can be considered as "xul"
*
* @param sScreenType
* @return
*/
public static boolean isXUL(String sScreenType)
{
if (sScreenType != null)
{
String s = sScreenType.trim().toLowerCase();
return (s.equals("xul") ? true : false);
}
return false;
}
/**
* Returns the full URL for the context root <br>( ie : "http://server:port/contextname" )
*
* @param pageContext
* @return
*/
public static String getRootContextURL(PageContext pageContext)
{
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
if (request == null)
{
log.error("getRootContextURL() : request is null ");
return "xxx(error_1)xxx";
}
String sProtocol = request.getScheme();
if (sProtocol == null)
{
log.error("getRootContextURL() : cannot get protocol ");
return "xxx(error_2)xxx";
}
String sHostName = request.getServerName();
if (sHostName == null)
{
log.error("getRootContextURL() : cannot get server name ");
return "xxx(error_3)xxx";
}
String sContexPath = request.getContextPath();
if (sContexPath == null)
{
log.error("getRootContextURL() : cannot get context path");
return "xxx(error_4)xxx";
}
int iHostPort = request.getServerPort();
//--- Build the full http URL
return sProtocol + "://" + sHostName + ":" + iHostPort + sContexPath;
}
//----------------------------------------------------------------
/**
* Returns the full "client side" http URL for the given resource path
*
* @param pageContext
* @param sResourcePath :
* the server side resource path ( ie : "/folder/file" )
* @return : the full URL ( ie : "http://myhost:8080/contextname/folder/file")
*/
public static String getFullHttpURL(PageContext pageContext, String sResourcePath)
{
String sRootContextURL = getRootContextURL(pageContext);
if (sRootContextURL == null)
{
log.error("getFullHttpURL() : cannot get root context URL ");
return null;
}
String sRoot2 = sRootContextURL;
if (sRootContextURL.endsWith("/"))
{
//--- Cut the last char
sRoot2 = sRootContextURL.substring(0, sRootContextURL.length() - 1);
}
if (sResourcePath != null)
{
String sPath2 = sResourcePath;
if (sResourcePath.startsWith("/"))
{
//--- Cut the first char
sPath2 = sResourcePath.substring(1);
}
return sRoot2 + "/" + sPath2;
}
else
{
return sRoot2 + "/";
}
}
//----------------------------------------------------------------
/**
* Returns the global configuration of the screens definitions <br>( cf "config" tag in the screens XML file )
*
* @return : the screens definition congig object
*/
public static ScreenDefConfig getScreenDefConfig()
{
ScreenDefinitions screenDefinitions = TelosysUIL.getScreenDefinitions();
if (screenDefinitions == null)
{
log.error("getConfig() : Cannot get 'ScreenDefinitions' from 'TelosysUIL' ");
return null;
}
return screenDefinitions.getConfig();
}
//----------------------------------------------------------------
/**
* Returns the "server side" screen map servlet path <br>
* defined in the "config" tag of the screens XML file<br>
* ( e.g. : "/screenmap" )
*
* @return
*/
public static String getScreenMapResourcePath()
{
ScreenDefConfig config = getScreenDefConfig();
if (config != null)
{
return config.getScreenMapUrl();
}
else
{
return null;
}
}
/**
* Returns the "server side" screen action servlet path <br>
* defined in the "config" tag of the screens XML file
*
* @return
*/
public static String getScreenActionResourcePath()
{
ScreenDefConfig config = getScreenDefConfig();
if (config != null)
{
return config.getAjaxRequestUrl();
}
else
{
return null;
}
}
/**
* Returns the full "client side" URL of the screen map servlet
*
* @param pageContext
* @return : the URL ( ie : "http://myhost:8080/contextname/screenmap" )
*/
public static String getScreenMapURL(PageContext pageContext)
{
String sServerSideScreenMapPath = TagCommons.getScreenMapResourcePath(); // "/screenmap"
return getFullHttpURL(pageContext, sServerSideScreenMapPath);
}
// /**
// * Returns the full "client side" URL of the given screen map informations <br>
// * ( e.g. : "http://server:port/webapp/screenmap/name-type/id-ctx:action?params" )
// * @param pageContext
// * @param sScreenName
// * @param sScreenType
// * @param sContextId
// * @param sContextName
// * @param sContextAction
// * @param sParams
// * @return
// */
// public static String getScreenMapURL(PageContext pageContext,
// String sScreenName, String sScreenType,
// String sContextId, String sContextName, String sContextAction, String sParams )
// {
// String sScreenMapRootURL = getScreenMapURL(pageContext);
//
// String s1 = "" ;
// String s2 = "" ;
// String s3 = "" ;
// String s4 = "" ;
//
// //--- Part 1 : "/name" or "/name-html" or "name-xul"
// if ( sScreenName != null )
// {
// s1 = "/" + sScreenName + ( sScreenType != null ? "-" + sScreenType : "" );
// }
// else
// {
// // s1 = "/ERROR_NO_SCREEN_NAME" ;
// return sScreenMapRootURL ;
// }
//
// //--- Part 2 : "/id" or "/id-contextname" or "/contextname"
// if ( sContextId != null )
// {
// s2 = "/" + sContextId + ( sContextName != null ? "-" + sContextName : "" ) ;
// }
// else
// {
// if ( sContextName != null )
// {
// s2 = "/" + sContextName ;
// }
// }
//
// //--- Part 3 : ":action"
// if (sContextAction != null)
// {
// s3 = ":" + sContextAction;
// }
//
// //--- Part 4 : "?param1=xxx¶m2=bbbb"
// if (sParams != null)
// {
// s4 = "?" + sParams ;
// }
//
// return sScreenMapRootURL + s1 + s2 + s3 + s4 ;
// }
/**
* Returns the full "client side" URL of the AJAX screen actions servlet
*
* @param pageContext
* @return : the URL ( ie : "http://myhost:8080/contextname/screenaction" )
*/
public static String getScreenActionURL(PageContext pageContext)
{
String sServerSideScreenActionPath = TagCommons.getScreenActionResourcePath();
return getFullHttpURL(pageContext, sServerSideScreenActionPath);
}
/**
* Returns the full "client side" URL for an upload ( URL of the upload servlet )
* @param pageContext
* @return : the URL ( ie : "http://myhost:8080/contextname/upload" )
*/
public static String getUploadURL(PageContext pageContext)
{
String sUploadPath = DEFAULT_UPLOAD_PATH;
Properties prop = Telosys.getProperties();
if (prop != null)
{
//--- Fichier de configuration des bases de données
//String s = prop.getProperty("UploadURL");
String s = prop.getProperty(TelosysProperties.UPLOAD_URL); // v 1.0.5
if (s != null)
{
sUploadPath = s.trim();
}
}
String sFullUploadURL = getFullHttpURL(pageContext, sUploadPath);
return sFullUploadURL;
}
/**
* Returns the full URL for the images directory <br>
* ie : "http://server:port/contextname/images_dir/"
*
* @param pageContext
* @return
*/
public static String getImgDirURL(PageContext pageContext)
{
return getRootContextURL(pageContext) + getImgDir(pageContext) + "/";
}
/**
* Returns the images directory for the current type of screen map <br>
* or for the default screen type if there's no screen map (direct call to JSP)
*
* @param p
* @return
*/
public static String getImgDir(PageContext p)
{
ScreenMap screenMap = getScreenMap(p);
if (screenMap != null)
{
//--- There's a ScreenMap object associated with the request => use it
String sImgRootDir = screenMap.getImagesRootDir();
if (sImgRootDir != null)
{
return sImgRootDir;
}
else
{
log.error("Images root dir not set for type " + screenMap.getType());
return "xxx(error_1)xxx";
}
}
else
{
//--- No ScreenMap object associated with the request => use default type image directory
ScreenDefinitions screenDefinitions = TelosysUIL.getScreenDefinitions();
if (screenDefinitions != null)
{
//--- Get the "root dir" definitions for the default type
String sScreenType = TelosysUIL.getDefaultScreenType();
ScreenDefRootDir rootDir = screenDefinitions.getRootDir(sScreenType);
String sImgRootDir = rootDir.getImagesRootDir();
if (sImgRootDir != null)
{
return sImgRootDir;
}
else
{
log.error("Images root dir not set for the default type " + sScreenType);
return "xxx(error_2)xxx";
}
}
else
{
log.error("ScreenDefinitions not initialized !");
return "xxx(error_3)xxx";
}
}
}
/**
* Returns true if the postback argument is "session"
*
* @param sPostback
* @return
*/
public static boolean isSessionPostback(String sPostback)
{
if (sPostback == null)
{
return false;
}
else
{
return "session".equalsIgnoreCase(sPostback.trim());
}
}
/**
* Returns true if the postback argument is "request"
*
* @param sPostback
* @return
*/
public static boolean isRequestPostback(String sPostback)
{
if (sPostback == null)
{
return false;
}
else
{
return "request".equalsIgnoreCase(sPostback.trim());
}
}
/**
* Returns true if the postback argument is "none"
*
* @param sPostback
* @return
*/
public static boolean isNoPostback(String sPostback)
{
if (sPostback == null)
{
return false;
}
else
{
return "none".equalsIgnoreCase(sPostback.trim());
}
}
/**
* Prints a trace message (as a comment tag) in the generated page <br>
*
* @param out
* @param sMessage
*/
public static void printTrace(JspWriter out, String sMessage)
{
try
{
out.println("<!-- TRACE : " + sMessage + " -->");
} catch (IOException ioex)
{
ioex.printStackTrace();
}
}
/**
* Prints a trace message (as a comment tag) in the generated page <br>
*
* @param pageContext
* @param sMessage
*/
public static void printTrace(PageContext pageContext, String sMessage)
{
printTrace(pageContext.getOut(), sMessage);
}
/**
* Prints an error message (as a comment tag) in the generated page <br>
*
* @param out
* @param sMessage
*/
public static void printError(JspWriter out, String sMessage)
{
try
{
out.println("<!-- ERROR : " + sMessage + " -->");
} catch (IOException ioex)
{
ioex.printStackTrace();
}
}
/**
* Prints an error message (as a comment tag) in the generated page <br>
*
* @param pageContext
* @param sMessage
*/
public static void printError(PageContext pageContext, String sMessage)
{
printError(pageContext.getOut(), sMessage);
}
//===============================================================================
public static HttpServletRequest getHttpServletRequest(PageContext pageContext)
{
if ( pageContext != null )
{
ServletRequest servletRequest = pageContext.getRequest();
if (servletRequest != null)
{
if (servletRequest instanceof HttpServletRequest)
{
return (HttpServletRequest) servletRequest;
}
}
}
return null;
}
public static HttpServletResponse getHttpServletResponse(PageContext pageContext)
{
if ( pageContext != null )
{
ServletResponse servletResponse = pageContext.getResponse();
if (servletResponse != null)
{
if (servletResponse instanceof HttpServletResponse)
{
return (HttpServletResponse) servletResponse;
}
}
}
return null;
}
protected static ScreenSession getScreenSession(PageContext pageContext)
{
HttpServletRequest request = getHttpServletRequest(pageContext);
if (request != null)
{
return ScreenSessionManager.findScreenSession(request);
}
else
{
// Error
return null;
}
}
/**
* Returns the screen configuration language
* ( the language used to design the screen )
* @return the language ( e.g. "fr", "en", ...) or null
*/
protected static String getScreenBaseLanguage()
{
String sLanguage = null ;
ScreenDefConfig config = getScreenDefConfig();
if ( config != null )
{
sLanguage = config.getBaseLanguage();
}
return sLanguage ;
}
/**
* Returns the language of the current session ( or null )
* @return the language ( e.g. "fr", "en", ...) or null
*/
protected static String getSessionLanguage(PageContext pageContext)
{
ScreenSession screenSession = getScreenSession(pageContext);
if ( screenSession != null )
{
return screenSession.getLanguage();
}
return null ;
}
/**
* Returns the translated "txt" attribute for the given key, or the original text if no translation needed
* @param sTxt
* @param sId
* @param pageContext
* @return
*/
public static String getTranslatedTxt( String sTxt, String sId, PageContext pageContext )
{
if ( sId != null )
{
return getTranslatedText( sTxt, sId+".txt", pageContext );
}
return sTxt ;
}
/**
* Returns the translated text for the given key, or the original text if no translation needed
* @param sText the original text
* @param sKey the key identifying the text
* @param pageContext the current pageContext
* @return the translated text or the original text if no translation needed
*/
private static String getTranslatedText( String sText, String sKey, PageContext pageContext )
{
//--- Is there a key ?
if ( sKey == null )
{
return sText ;
}
//--- Is there a pageContext ?
if ( pageContext == null )
{
return sText ;
}
//--- The screen base language ( language used when the JSP was designed )
String sScreenBaseLanguage = getScreenBaseLanguage();
if ( sScreenBaseLanguage == null )
{
return sText ;
}
//--- The current language ( for the current session )
String sSessionLanguage = getSessionLanguage(pageContext);
if ( sSessionLanguage == null )
{
return sText ;
}
//--- Compare the 2 languages
if ( sSessionLanguage.equals(sScreenBaseLanguage) != true )
{
//--- The screen language is not the same as session language
//--- => translate the text ( get the text for the session language )
Translator translator = TagCommons.getTranslator(pageContext);
if ( translator != null )
{
String sTranslatedText = translator.translate(sKey, sSessionLanguage);
if ( sTranslatedText != null )
{
return sTranslatedText ;
}
}
}
return sText;
}
/**
* Returns the internal SCOPE constant for the given String <br>
* The default scope is "page" (even for an invalid scope name)
* @param scope
* @return
*/
public static int getScope( String scope )
{
int ret = PageContext.PAGE_SCOPE; // default
if ( "request".equalsIgnoreCase(scope) )
{
ret = PageContext.REQUEST_SCOPE;
}
else if ( "session".equalsIgnoreCase(scope) )
{
ret = PageContext.SESSION_SCOPE;
}
else if ( "application".equalsIgnoreCase(scope) )
{
ret = PageContext.APPLICATION_SCOPE;
}
return ret;
}
}
Generated by GNU enscript 1.6.4.