use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.
the class XWiki method flushCache.
public void flushCache(XWikiContext context) {
// We need to flush the virtual wiki list
this.initializedWikis = new ConcurrentHashMap<>();
// We need to flush the group service cache
if (this.groupService != null) {
this.groupService.flushCache();
}
// If we use the Cache Store layer.. we need to flush it
XWikiStoreInterface store = getStore();
if ((store != null) && (store instanceof XWikiCacheStoreInterface)) {
((XWikiCacheStoreInterface) getStore()).flushCache();
}
// Flush renderers.. Groovy renderer has a cache
getOldRendering().flushCache();
getParseGroovyFromString().flushCache();
XWikiPluginManager pmanager = getPluginManager();
if (pmanager != null) {
pmanager.flushCache(context);
}
// Make sure we call all classes flushCache function
try {
List<String> classes = getClassList(context);
for (int i = 0; i < classes.size(); i++) {
String className = classes.get(i);
try {
getClass(className, context).flushCache();
} catch (Exception e) {
}
}
} catch (Exception e) {
}
}
use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.
the class DownloadActionTest method before.
@Before
public void before() throws Exception {
this.oldcore.registerMockEnvironment();
this.request = mock(XWikiRequest.class);
this.oldcore.getXWikiContext().setRequest(this.request);
this.response = mock(XWikiResponse.class);
this.oldcore.getXWikiContext().setResponse(this.response);
this.ec = mock(XWikiEngineContext.class);
this.oldcore.getXWikiContext().setEngineContext(this.ec);
this.out = mock(ServletOutputStream.class);
XWikiPluginManager pluginManager = new XWikiPluginManager();
pluginManager.initInterface();
this.documentReference = new DocumentReference("wiki", "space", "page");
this.document = new XWikiDocument(this.documentReference);
this.oldcore.getXWikiContext().setDoc(this.document);
doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
when(this.ec.getMimeType("file.txt")).thenReturn("text/plain");
when(this.response.getOutputStream()).thenReturn(this.out);
when(this.oldcore.getMockRightService().hasAccessLevel(eq("programming"), any(), any(), any(XWikiContext.class))).thenReturn(false);
// Mock what's needed for extracting the filename from the URL
this.resourceReferenceManager = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
}
use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.
the class MockitoDownloadActionTest method renderWhenZipExplorerPluginURL.
@Test
public void renderWhenZipExplorerPluginURL() throws Exception {
DownloadAction action = new DownloadAction();
XWikiContext xcontext = this.oldcore.getXWikiContext();
// Set the Request URL
XWikiServletRequestStub request = new XWikiServletRequestStub();
request.setRequestURI("http://localhost:8080/xwiki/bin/download/space/page/file.ext/some/path");
xcontext.setRequest(request);
// Set the current doc and current wiki
XWikiDocument document = mock(XWikiDocument.class);
when(document.getAttachment("path")).thenReturn(null);
xcontext.setDoc(document);
xcontext.setWikiId("wiki");
xcontext.setAction("download");
// Set the Response
XWikiResponse response = mock(XWikiResponse.class);
StubServletOutputStream ssos = new StubServletOutputStream();
when(response.getOutputStream()).thenReturn(ssos);
xcontext.setResponse(response);
// Set the Resource Reference Manager used to parse the URL and extract the attachment name
ResourceReferenceManager rrm = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
when(rrm.getResourceReference()).thenReturn(new EntityResourceReference(new AttachmentReference("path", new DocumentReference("wiki", Arrays.asList("space", "page", "file.ext"), "some")), EntityResourceAction.VIEW));
// Setup the returned attachment
XWikiAttachment attachment = mock(XWikiAttachment.class);
when(attachment.getContentSize(xcontext)).thenReturn(100);
Date now = new Date();
when(attachment.getDate()).thenReturn(now);
when(attachment.getFilename()).thenReturn("file.ext");
when(attachment.getContentInputStream(xcontext)).thenReturn(new ByteArrayInputStream("test".getBytes()));
when(attachment.getMimeType(xcontext)).thenReturn("mimetype");
when(attachment.clone()).thenReturn(attachment);
// Configure an existing doc in the store
XWiki xwiki = this.oldcore.getSpyXWiki();
XWikiDocument backwardCompatDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
backwardCompatDocument.addAttachment(attachment);
xwiki.saveDocument(backwardCompatDocument, xcontext);
// Make sure the user has permission to access the doc
doReturn(true).when(xwiki).checkAccess(eq("download"), any(XWikiDocument.class), any(XWikiContext.class));
// Setup ExecutionContextManager & VelocityManager using in the context backup
ExecutionContextManager ecm = this.oldcore.getMocker().registerMockComponent(ExecutionContextManager.class);
ExecutionContext ec = this.oldcore.getExecutionContext();
when(ecm.clone(ec)).thenReturn(ec);
VelocityManager vm = this.oldcore.getMocker().registerMockComponent(VelocityManager.class);
// Set the Plugin Manager
XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
assertNull(action.render(xcontext));
// This is the test, we verify what is set in the response
verify(response).setContentType("mimetype");
verify(response).setHeader("Accept-Ranges", "bytes");
verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.ext");
verify(response).setDateHeader("Last-Modified", now.getTime());
verify(response).setContentLength(100);
assertEquals("test", ssos.baos.toString());
}
use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.
the class DefaultRenderingCache method buildCachedItem.
/**
* Create cached item with all dependencies.
*
* @param context current xwiki context
* @param renderedContent rendered page content
* @return properly cached item
*/
private CachedItem buildCachedItem(XWikiContext context, String renderedContent) {
CachedItem cachedItem = new CachedItem();
for (RenderingCacheAware component : this.renderingCacheAwareProvider.get()) {
cachedItem.extensions.put(component, component.getCacheResources(context));
}
// support for legacy core -> build non-blocking list (lazy)
if (this.legacyRenderingCacheAware == null) {
this.legacyRenderingCacheAware = new LinkedList<RenderingCacheAware>();
XWikiPluginManager pluginManager = context.getWiki().getPluginManager();
for (String pluginName : pluginManager.getPlugins()) {
XWikiPluginInterface plugin = pluginManager.getPlugin(pluginName);
if (plugin instanceof RenderingCacheAware) {
this.legacyRenderingCacheAware.add((RenderingCacheAware) plugin);
}
}
}
for (RenderingCacheAware component : this.legacyRenderingCacheAware) {
cachedItem.extensions.put(component, component.getCacheResources(context));
}
cachedItem.rendered = renderedContent;
return cachedItem;
}
use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.
the class DownloadAction method render.
@Override
public String render(XWikiContext context) throws XWikiException {
XWikiRequest request = context.getRequest();
XWikiResponse response = context.getResponse();
XWikiDocument doc = context.getDoc();
String filename = getFileName();
XWikiAttachment attachment = getAttachment(request, doc, filename);
Map<String, Object> backwardCompatibilityContextObjects = null;
if (attachment == null) {
// If some plugins extend the Download URL format for the Standard Scheme the document in the context will
// most likely not have a reference that corresponds to what the plugin expects. For example imagine that
// the URL is a Zip Explorer URL like .../download/space/page/attachment/index.html. This will be parsed
// as space.page.attachment@index.html by the Standard URL scheme parsers. Thus the attachment won't be
// found since index.html is not the correct attachment for the Zip Explorer plugin's URL format.
//
// Thus in order to preserve backward compatibility for existing plugins that have custom URL formats
// extending the Download URL format, we parse again the URL by considering that it doesn't contain any
// Nested Space. This also means that those plugins will need to completely reparse the URL if they wish to
// support Nested Spaces.
//
// Also note that this code below is not compatible with the notion of having several URL schemes. The real
// fix will be to not allow plugins to support custom URL formats and instead to have them register new
// Actions if they need a different URL format.
Pair<XWikiDocument, XWikiAttachment> result = extractAttachmentAndDocumentFromURLWithoutSupportingNestedSpaces(request, context);
if (result == null) {
throwNotFoundException(filename);
}
XWikiDocument backwardCompatibilityDocument = result.getLeft();
attachment = result.getRight();
// Set the new doc as the context doc so that plugins see it as the context doc
backwardCompatibilityContextObjects = new HashMap<>();
pushDocumentInContext(backwardCompatibilityContextObjects, backwardCompatibilityDocument.getDocumentReference());
}
try {
XWikiPluginManager plugins = context.getWiki().getPluginManager();
attachment = plugins.downloadAttachment(attachment, context);
if (attachment == null) {
throwNotFoundException(filename);
}
// This will throw an exception if the attachment content isn't available
try {
attachment.getContentSize(context);
} catch (XWikiException e) {
Object[] args = { filename };
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment content {0} not found", null, args);
}
long lastModifiedOnClient = request.getDateHeader("If-Modified-Since");
long lastModifiedOnServer = attachment.getDate().getTime();
if (lastModifiedOnClient != -1 && lastModifiedOnClient >= lastModifiedOnServer) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return null;
}
// Sending the content of the attachment
if (request.getHeader(RANGE_HEADER_NAME) != null) {
try {
if (sendPartialContent(attachment, request, response, context)) {
return null;
}
} catch (IOException ex) {
// Broken response...
}
}
sendContent(attachment, request, response, filename, context);
return null;
} finally {
if (backwardCompatibilityContextObjects != null) {
popDocumentFromContext(backwardCompatibilityContextObjects);
}
}
}
Aggregations