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;
}
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);
}
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());
}
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);
}
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);
}
Aggregations