Search in sources :

Example 11 with XWikiRequest

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 + "/";
}
Also used : XWikiRequest(com.xpn.xwiki.web.XWikiRequest) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)

Example 12 with XWikiRequest

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;
}
Also used : Cookie(javax.servlet.http.Cookie) XWikiRequest(com.xpn.xwiki.web.XWikiRequest) XWikiException(com.xpn.xwiki.XWikiException)

Example 13 with XWikiRequest

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;
}
Also used : Cookie(javax.servlet.http.Cookie) XWikiRequest(com.xpn.xwiki.web.XWikiRequest) HttpSession(javax.servlet.http.HttpSession)

Example 14 with XWikiRequest

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;
}
Also used : Cookie(javax.servlet.http.Cookie) XWikiRequest(com.xpn.xwiki.web.XWikiRequest) Date(java.util.Date)

Example 15 with XWikiRequest

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());
    }
}
Also used : XWikiRequest(com.xpn.xwiki.web.XWikiRequest) CachePolicy(com.xpn.xwiki.web.sx.SxSource.CachePolicy) XWikiResponse(com.xpn.xwiki.web.XWikiResponse) IOException(java.io.IOException) Date(java.util.Date)

Aggregations

XWikiRequest (com.xpn.xwiki.web.XWikiRequest)21 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWiki (com.xpn.xwiki.XWiki)4 IOException (java.io.IOException)4 Cookie (javax.servlet.http.Cookie)4 XWikiException (com.xpn.xwiki.XWikiException)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)3 BaseObject (com.xpn.xwiki.objects.BaseObject)3 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)3 Date (java.util.Date)3 Test (org.junit.Test)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 FileNotFoundException (java.io.FileNotFoundException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 NamingException (javax.naming.NamingException)2 HttpSession (javax.servlet.http.HttpSession)2 URIException (org.apache.commons.httpclient.URIException)2