Search in sources :

Example 1 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class XWikiContextCopier method copy.

/**
 * {@inheritDoc}
 *
 * Any in progress session/transaction on the store retained by the original context will be closed/rollbacked
 * prior cloning (see {@link com.xpn.xwiki.store.XWikiStoreInterface#cleanUp(XWikiContext)}). Therefore,
 * the copy operation has a side effect on the original context. You should never copy a context
 * while a create/update transaction is in progress, since some changes would get rollbacked.
 */
@Override
public XWikiContext copy(XWikiContext originalXWikiContext) {
    // Clean up the store session/transaction before cloning. For the hibernate store, in progress
    // session/transaction is stored in the context, and would be swallow copied when the context is cloned.
    // Cleaning after clone would not help, since it would close/rollback the session/transaction still referenced
    // in the original context as well, causing this context to be corrupted.
    // The correct way would be to not shallow clone the session/transaction when cloning the original context,
    // since session/transaction are lazy initialize on request when missing.
    originalXWikiContext.getWiki().getStore().cleanUp(originalXWikiContext);
    // This is still a shallow clone, but at least for stuff like wikiID and userReference it gets the job done.
    XWikiContext clonedXWikiContext = originalXWikiContext.clone();
    // lets now build the stub context
    // Copy the request from the context.
    clonedXWikiContext.setRequest(this.xwikiRequestCloner.copy(originalXWikiContext.getRequest()));
    // Force forged context response to a stub response, since the current context response
    // will not mean anything anymore when running in the scheduler's thread, and can cause
    // errors.
    XWikiResponse stub = new XWikiServletResponseStub();
    clonedXWikiContext.setResponse(stub);
    // feed the dummy context
    if (clonedXWikiContext.getURL() == null) {
        try {
            clonedXWikiContext.setURL(new URL("http://www.mystuburl.com/"));
        } catch (Exception e) {
        // the URL is clearly well formed
        }
    }
    XWikiURLFactory xurf = originalXWikiContext.getWiki().getURLFactoryService().createURLFactory(originalXWikiContext.getMode(), originalXWikiContext);
    clonedXWikiContext.setURLFactory(xurf);
    return clonedXWikiContext;
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiResponse(com.xpn.xwiki.web.XWikiResponse) XWikiContext(com.xpn.xwiki.XWikiContext) URL(java.net.URL)

Example 2 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class SkinEnvironmentResource method getURL.

@Override
public String getURL(boolean forceSkinAction) throws Exception {
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiURLFactory urlf = xcontext.getURLFactory();
    URL url;
    if (forceSkinAction) {
        url = urlf.createSkinURL(this.resourceName, "skins", getRepository().getId(), xcontext);
    } else {
        url = urlf.createSkinURL(this.resourceName, getRepository().getId(), xcontext);
    }
    return urlf.getURL(url, xcontext);
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiContext(com.xpn.xwiki.XWikiContext) URL(java.net.URL)

Example 3 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class XWikiContextCacheKeyFactoryTest method getCacheKey.

@Test
public void getCacheKey() throws Exception {
    // Mocks
    XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
    when(xcontext.getURLFactory()).thenReturn(urlFactory);
    when(urlFactory.createSkinURL(any(), any(), any(XWikiContext.class))).thenReturn(new URL("http://host/path"));
    // Test
    assertEquals("XWikiContext[URLFactory[" + urlFactory.getClass().getName() + ", /path]]", mocker.getComponentUnderTest().getCacheKey());
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiContext(com.xpn.xwiki.XWikiContext) URL(java.net.URL) Test(org.junit.Test)

Example 4 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class XWikiContextCacheKeyFactory method getCacheKey.

/**
 * @return the cache key corresponding to the current XWikiContext state
 */
public String getCacheKey() {
    XWikiContext xcontext = xcontextProvider.get();
    XWikiURLFactory urlFactory = xcontext.getURLFactory();
    // We serialize the class name of the current URLFactory.
    // Ex: - during HTML export, ExportURLFactory is used.
    // - for the standard 'view' action, XWikiDefaultURLFactory is used
    // - ...
    String urlFactoryName = urlFactory.getClass().getName();
    // We generate a fake URL with the current URL factory so that we take care of the internal state of that object
    // in our cache key.
    // Ex: - if the request comes from a file located in subdirectory, the generated URL will be:
    // '../style.css'
    // - if the request comes form a file located in a deeper subdirectory, the generated URL will be:
    // '../../style.css'
    // It is clear that we cannot cache the same results from a request coming from a subdirectory or an other, but
    // we have no API to get the internal state of the URL Factory. So we use this 'trick' to handle it.
    // Note: only the "path" part of the URL is needed. Otherwise, the cache cannot be share between requests
    // having 2 different hosts: e.g. http://localhost and http://external-url/
    String urlFactoryGeneratedURL = urlFactory.createSkinURL("style.css", "skin", xcontext).getPath();
    return String.format("XWikiContext[URLFactory[%s, %s]]", urlFactoryName, urlFactoryGeneratedURL);
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 5 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class XWiki method getSkinFile.

public String getSkinFile(String filename, boolean forceSkinAction, XWikiContext context) {
    XWikiURLFactory urlf = context.getURLFactory();
    try {
        // Try in the specified skin
        Skin skin = getInternalSkinManager().getCurrentSkin(true);
        if (skin != null) {
            Resource<?> resource = skin.getResource(filename);
            if (resource != null) {
                return resource.getURL(forceSkinAction);
            }
        } else {
            // Try in the current parent skin
            Skin parentSkin = getInternalSkinManager().getCurrentParentSkin(true);
            if (parentSkin != null) {
                Resource<?> resource = parentSkin.getResource(filename);
                if (resource != null) {
                    return resource.getURL(forceSkinAction);
                }
            }
        }
        // Look for a resource file
        if (resourceExists("/resources/" + filename)) {
            URL url = urlf.createResourceURL(filename, forceSkinAction, context);
            return urlf.getURL(url, context);
        }
    } catch (Exception e) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Exception while getting skin file [" + filename + "]", e);
        }
    }
    // If all else fails, use the default base skin, even if the URLs could be invalid.
    URL url;
    if (forceSkinAction) {
        url = urlf.createSkinURL(filename, "skins", getDefaultBaseSkin(context), context);
    } else {
        url = urlf.createSkinURL(filename, getDefaultBaseSkin(context), context);
    }
    return urlf.getURL(url, context);
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) Skin(org.xwiki.skin.Skin) WikiSkin(com.xpn.xwiki.internal.skin.WikiSkin) ExtendedURL(org.xwiki.url.ExtendedURL) URL(java.net.URL) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException)

Aggregations

XWikiURLFactory (com.xpn.xwiki.web.XWikiURLFactory)16 XWikiContext (com.xpn.xwiki.XWikiContext)10 URL (java.net.URL)6 DocumentReference (org.xwiki.model.reference.DocumentReference)6 Test (org.junit.Test)4 ExternalServletURLFactory (com.xpn.xwiki.web.ExternalServletURLFactory)3 XWikiServletResponseStub (com.xpn.xwiki.web.XWikiServletResponseStub)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 IOException (java.io.IOException)2 Locale (java.util.Locale)2 MessagingException (javax.mail.MessagingException)2 VelocityContext (org.apache.velocity.VelocityContext)2 IBlogServiceRole (com.celements.blog.service.IBlogServiceRole)1 Document (com.xpn.xwiki.api.Document)1 WikiSkin (com.xpn.xwiki.internal.skin.WikiSkin)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 XWikiRequest (com.xpn.xwiki.web.XWikiRequest)1