use of com.xpn.xwiki.web.XWikiRequest in project xwiki-platform by xwiki.
the class XWiki method getWebAppPath.
public String getWebAppPath(XWikiContext context) {
String contextPath = getConfiguration().getProperty("xwiki.webapppath");
if (contextPath == null) {
// Try getting the context path by asking the request for it (if a request exists!) and if it doesn't
// work try extracting it from the context URL.
// TODO: Instead of trying to extract from the URL, save the context path at webapp init (using a
// ServlettContextListener for example).
XWikiRequest request = context.getRequest();
if (request != null) {
contextPath = request.getContextPath();
}
if (contextPath == null) {
// Extract the context by getting the first path segment
contextPath = StringUtils.substringBefore(StringUtils.stripStart(context.getURL().getPath(), "/"), "/");
}
}
// Remove any leading or trailing slashes
contextPath = StringUtils.strip(contextPath, "/");
// while we need a trailing /. This code ensure we always have CONTEXTNAME + "/".
return contextPath + "/";
}
use of com.xpn.xwiki.web.XWikiRequest in project xwiki-platform by xwiki.
the class StatsUtil method findVisitByCookieOrIPUA.
/**
* Try to find the visit object in the database from cookie if it's not a new cookie, search by unique id otherwise.
*
* @param context the XWiki context.
* @return the visit statistics object found.
* @since 1.4M1
*/
private static VisitStats findVisitByCookieOrIPUA(XWikiContext context) {
VisitStats visitStats = null;
XWikiRequest request = context.getRequest();
Cookie cookie = (Cookie) context.get(CONTPROP_STATS_COOKIE);
boolean newcookie = ((Boolean) context.get(CONTPROP_STATS_NEWCOOKIE)).booleanValue();
if (!newcookie) {
try {
visitStats = findVisitByCookie(cookie.getValue(), context);
} catch (XWikiException e) {
LOGGER.error("Failed to find visit by cookie", e);
}
} else {
try {
String ip = request.getRemoteAddr();
String ua = request.getHeader(REQPROP_USERAGENT);
visitStats = findVisitByIPUA(computeUniqueID(ip, ua), context);
} catch (XWikiException e) {
LOGGER.error("Failed to find visit by unique id", e);
}
}
return visitStats;
}
use of com.xpn.xwiki.web.XWikiRequest in project xwiki-platform by xwiki.
the class StatsUtil method findVisit.
/**
* Try to find the visiting session of the current request, or create a new one if this request is not part of a
* visit. The session is searched in the following way:
* <ol>
* <li>the java session is searched for the visit object</li>
* <li>try to find the stored session using the cookie</li>
* <li>try to find the session by matching the IP and User Agent</li>
* </ol>
* The session is invalidated if:
* <ul>
* <li>the cookie is not the same as the stored cookie</li>
* <li>more than 30 minutes have elapsed from the previous request</li>
* <li>the user is not the same</li>
* </ul>
*
* @param context The context of this request.
* @return The visiting session, retrieved from the database or created.
* @since 1.4M1
*/
public static VisitStats findVisit(XWikiContext context) {
XWikiRequest request = context.getRequest();
HttpSession session = request.getSession(true);
VisitStats visitObject = StatsUtil.getVisitFromSession(session);
Cookie cookie = (Cookie) context.get(CONTPROP_STATS_COOKIE);
boolean newcookie = ((Boolean) context.get(CONTPROP_STATS_NEWCOOKIE)).booleanValue();
if (visitObject == null) {
visitObject = findVisitByCookieOrIPUA(context);
}
if (visitObject == null || !isVisitObjectValid(visitObject, context)) {
visitObject = createNewVisit(context);
} else {
if (!newcookie) {
// If the cookie is not yet the unique ID we need to change that
String uniqueID = visitObject.getUniqueID();
String oldcookie = visitObject.getCookie();
if (!uniqueID.equals(oldcookie)) {
// We need to store the oldID so that we can remove the older entry
// since the entry identifiers are changing
VisitStats newVisitObject = (VisitStats) visitObject.clone();
newVisitObject.rememberOldObject(visitObject);
newVisitObject.setUniqueID(cookie.getValue());
visitObject = newVisitObject;
}
}
if ((!context.getUser().equals(XWikiRightService.GUEST_USER_FULLNAME)) && (visitObject.getUser().equals(XWikiRightService.GUEST_USER_FULLNAME))) {
// The user has changed from guest to an authenticated user
// We want to record this
VisitStats newVisitObject = visitObject;
newVisitObject.rememberOldObject(visitObject);
newVisitObject.setName(context.getUser());
visitObject = newVisitObject;
}
}
// Keep the visit object in the session
StatsUtil.setVisitInSession(session, visitObject);
return visitObject;
}
use of com.xpn.xwiki.web.XWikiRequest in project xwiki-platform by xwiki.
the class StatsUtil method createNewVisit.
/**
* Create and initialize a new visit statistics object.
*
* @param context the XWiki context.
* @return the new visit statistics object.
* @since 1.4M1
*/
private static VisitStats createNewVisit(XWikiContext context) {
VisitStats visitStats = null;
XWikiRequest request = context.getRequest();
Date nowDate = new Date();
Cookie cookie = (Cookie) context.get(CONTPROP_STATS_COOKIE);
boolean newcookie = ((Boolean) context.get(CONTPROP_STATS_NEWCOOKIE)).booleanValue();
// we need to create the session
String ip = request.getRemoteAddr();
String ua = request.getHeader(REQPROP_USERAGENT);
if (ua == null) {
ua = "";
}
String uniqueID;
if (newcookie) {
// We cannot yet ID the user using the cookie
// we need to use the IP and UA
uniqueID = computeUniqueID(ip, ua);
} else {
// In this case we got the cookie from the request
// so we id the user using the cookie
uniqueID = cookie.getValue();
}
visitStats = new VisitStats(context.getUser(), uniqueID, cookie.getValue(), ip, ua, nowDate, PeriodType.MONTH);
visitStats.setEndDate(nowDate);
return visitStats;
}
use of com.xpn.xwiki.web.XWikiRequest in project xwiki-platform by xwiki.
the class AbstractSxAction method renderExtension.
/**
* This method must be called by render(XWikiContext). Render is in charge of creating the proper source and
* extension type, and pass it as an argument to this method which will forge the proper response using those.
*
* @param sxSource the source of the extension.
* @param sxType the type of extension
* @param context the XWiki context when rendering the skin extension.
* @throws XWikiException when an error occurs when building the response.
*/
public void renderExtension(SxSource sxSource, Extension sxType, XWikiContext context) throws XWikiException {
XWikiRequest request = context.getRequest();
XWikiResponse response = context.getResponse();
String extensionContent = sxSource.getContent();
response.setContentType(sxType.getContentType());
if (sxSource.getLastModifiedDate() > 0) {
response.setDateHeader(LAST_MODIFIED_HEADER, sxSource.getLastModifiedDate());
}
CachePolicy cachePolicy = sxSource.getCachePolicy();
if (cachePolicy != CachePolicy.FORBID) {
response.setHeader(CACHE_CONTROL_HEADER, "public");
}
if (cachePolicy == CachePolicy.LONG) {
// Cache for one month (30 days)
response.setDateHeader(CACHE_EXPIRES_HEADER, (new Date()).getTime() + LONG_CACHE_DURATION);
} else if (cachePolicy == CachePolicy.SHORT) {
// Cache for one day
response.setDateHeader(CACHE_EXPIRES_HEADER, (new Date()).getTime() + SHORT_CACHE_DURATION);
} else if (cachePolicy == CachePolicy.FORBID) {
response.setHeader(CACHE_CONTROL_HEADER, "no-cache, no-store, must-revalidate");
}
if (BooleanUtils.toBoolean(StringUtils.defaultIfEmpty(request.get(COMPRESS_SCRIPT_REQUEST_PARAMETER), "true"))) {
extensionContent = sxType.getCompressor().compress(extensionContent);
}
try {
response.setContentLength(extensionContent.getBytes(RESPONSE_CHARACTER_SET).length);
response.getOutputStream().write(extensionContent.getBytes(RESPONSE_CHARACTER_SET));
} catch (IOException ex) {
getLogger().warn("Failed to send SX content: [{}]", ex.getMessage());
}
}
Aggregations