Getting server information
(→Get the "warped" time: Added second method. Rewrote exisiting code into a method.) |
m (→Added source lang="java" (formatting). Minor text fixes.) |
||
Line 2: | Line 2: | ||
==Get the Server IP address== | ==Get the Server IP address== | ||
− | To get the IP of the client use the following code. If you use the request object, you most likely will get the proxy IP. | + | To get the IP of the client use the following code. If you use the <tt>request</tt> object, you most likely will get the proxy IP. |
− | + | <source lang="java"> | |
− | + | CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response); | |
+ | String ip = cms.getRequestContext().getRemoteAddress(); | ||
+ | </source> | ||
==Get the hostname== | ==Get the hostname== | ||
This one is pretty typical of any JSP. | This one is pretty typical of any JSP. | ||
− | + | <source lang="java"> | |
+ | String hostname = request.getServerName(); | ||
+ | </source> | ||
==Get the URI of the requested resource== | ==Get the URI of the requested resource== | ||
This will get the URI that was requested, relative to the current site: | This will get the URI that was requested, relative to the current site: | ||
− | + | ||
− | + | <source lang="java"> | |
+ | CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response); | ||
+ | String requestFileUri = cms.getRequestContext().getUri(); // Will return e.g.: "/en/myfolder/mypage.html" | ||
+ | </source> | ||
Similarly, you can get the folder URI of the requested resource: | Similarly, you can get the folder URI of the requested resource: | ||
− | + | <source lang="java"> | |
− | Alternatively, you can use | + | String requestFolderUri = cms.getRequestContext().getFolderUri(); |
− | + | </source> | |
− | + | Alternatively, you can use an <tt>CmsObject</tt> instance, which also offers the <tt>getRequestContext()</tt> method: | |
− | + | <source lang="java"> | |
+ | CmsObject cmso = cms.getCmsObject(); | ||
+ | String requestFileUri = cmso.getRequestContext().getUri(); | ||
+ | String requestFolderUri = cmso.getRequestContext().getFolderUri(); | ||
+ | </source> | ||
==Get the locale of the requested resource== | ==Get the locale of the requested resource== | ||
This will return the locale of the requested resource: | This will return the locale of the requested resource: | ||
− | + | <source lang="java"> | |
− | + | CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response); | |
+ | Locale locale = cms.getRequestContext().getLocale(); | ||
+ | </source> | ||
==Get the "warped" time== | ==Get the "warped" time== | ||
− | |||
If you need to consider situations where the user has activated "time warp" (in the workplace preferences), you can read the current "warped" time from the session: | If you need to consider situations where the user has activated "time warp" (in the workplace preferences), you can read the current "warped" time from the session: | ||
− | |||
<source lang="java"> | <source lang="java"> | ||
/** | /** |
Latest revision as of 11:34, 10 December 2015
You can get a variety of information about your server from OpenCms, however sometimes it is a more round-about route.
Contents |
Get the Server IP address
To get the IP of the client use the following code. If you use the request object, you most likely will get the proxy IP.
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response); String ip = cms.getRequestContext().getRemoteAddress();
Get the hostname
This one is pretty typical of any JSP.
String hostname = request.getServerName();
Get the URI of the requested resource
This will get the URI that was requested, relative to the current site:
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response); String requestFileUri = cms.getRequestContext().getUri(); // Will return e.g.: "/en/myfolder/mypage.html"
Similarly, you can get the folder URI of the requested resource:
String requestFolderUri = cms.getRequestContext().getFolderUri();
Alternatively, you can use an CmsObject instance, which also offers the getRequestContext() method:
CmsObject cmso = cms.getCmsObject(); String requestFileUri = cmso.getRequestContext().getUri(); String requestFolderUri = cmso.getRequestContext().getFolderUri();
Get the locale of the requested resource
This will return the locale of the requested resource:
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response); Locale locale = cms.getRequestContext().getLocale();
Get the "warped" time
If you need to consider situations where the user has activated "time warp" (in the workplace preferences), you can read the current "warped" time from the session:
/** * Gets the workplace timestamp as a Date instance. * * If time warp is active, the returned datetime will be the "warped" time. * Otherwise, the actual "now" is returned. * * @param session The relevant session instance. * @return Date The current workplace "now" - either the actual "now" or a time-warped "now". */ public static Date getOpenCmsCurrentTime(HttpSession session) { long userCurrentTime = new Date().getTime(); try { CmsWorkplaceSettings wpSettings = (CmsWorkplaceSettings)session.getAttribute(CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS); userCurrentTime = wpSettings.getUserSettings().getTimeWarp(); // Note: will return 0 if time warp is not active if (userCurrentTime <= 0) userCurrentTime = new Date().getTime(); } catch (Throwable t) {} return new Date(userCurrentTime); }
... or from the current user's "additional info":
/** * Gets the workplace timestamp as a Date instance. * * If time warp is active, the returned datetime will be the "warped" time. * Otherwise, the actual "now" is returned. * * @param cmso An initialized CmsObject. * @return Date The current workplace "now" - either the actual "now" or a time-warped "now". */ public static Date getOpenCmsCurrentTime(CmsObject cmso) { long userCurrentTime = new Date().getTime(); Object timeWarpObj = cmso.getRequestContext().getCurrentUser().getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_TIMEWARP); try { userCurrentTime = (Long)timeWarpObj; } catch (ClassCastException e) { try { userCurrentTime = Long.parseLong((String)timeWarpObj); if (userCurrentTime <= 0) { userCurrentTime = new Date().getTime(); } } catch (Throwable t) {} } catch (Throwable t) {} return new Date(userCurrentTime); }
Author's note: The latter method, getOpenCmsCurrentTime(CmsObject), is currently untested, but it is based on code from the OpenCms core.