use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.
the class DownloadRevAction method render.
@Override
public String render(XWikiContext context) throws XWikiException {
XWikiRequest request = context.getRequest();
XWikiResponse response = context.getResponse();
XWikiDocument doc = context.getDoc();
String rev = request.getParameter("rev");
String filename = getFileName();
XWikiAttachment attachment;
if (context.getWiki().hasAttachmentRecycleBin(context) && request.getParameter("rid") != null) {
int recycleId = Integer.parseInt(request.getParameter("rid"));
attachment = new XWikiAttachment(doc, filename);
attachment = context.getWiki().getAttachmentRecycleBinStore().restoreFromRecycleBin(attachment, recycleId, context, true);
} else if (request.getParameter("id") != null) {
int id = Integer.parseInt(request.getParameter("id"));
attachment = doc.getAttachmentList().get(id);
} else {
attachment = doc.getAttachment(filename);
}
if (attachment == null) {
Object[] args = { filename };
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_ATTACHMENT_NOT_FOUND, "Attachment {0} not found", null, args);
}
synchronized (attachment) {
try {
attachment = attachment.getAttachmentRevision(rev, context);
if (attachment == null) {
throw new XWikiException();
}
} catch (XWikiException e) {
String url = context.getDoc().getURL("viewattachrev", true, context);
url += "/" + filename;
if (request.getParameter("rid") != null) {
url += "?rid=" + request.getParameter("rid");
}
try {
context.getResponse().sendRedirect(url);
return null;
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
XWikiPluginManager plugins = context.getWiki().getPluginManager();
attachment = plugins.downloadAttachment(attachment, context);
// Choose the right content type
String mimetype = attachment.getMimeType(context);
response.setContentType(mimetype);
response.setDateHeader("Last-Modified", attachment.getDate().getTime());
// Sending the content of the attachment
try {
response.setContentLength(attachment.getContentSize(context));
IOUtils.copy(attachment.getContentInputStream(context), response.getOutputStream());
} catch (IOException e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_SEND_RESPONSE_EXCEPTION, "Exception while sending response", e);
}
return null;
}
use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.
the class XWiki method preparePlugins.
private void preparePlugins(XWikiContext context) {
setPluginManager(new XWikiPluginManager(getXWikiPreference("plugins", context), context));
String plugins = getConfiguration().getProperty("xwiki.plugins", "");
if (!plugins.equals("")) {
getPluginManager().addPlugins(StringUtils.split(plugins, " ,"), context);
}
}
use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.
the class XWiki method getPlugin.
/**
* @deprecated
*/
@Deprecated
public XWikiPluginInterface getPlugin(String name, XWikiContext context) {
XWikiPluginManager plugins = getPluginManager();
Vector<String> pluginlist = plugins.getPlugins();
for (String pluginname : pluginlist) {
if (pluginname.equals(name)) {
return plugins.getPlugin(pluginname);
}
}
return null;
}
use of com.xpn.xwiki.plugin.XWikiPluginManager in project xwiki-platform by xwiki.
the class MockitoDownloadActionTest method renderWhenAttachmentIsInANestedSpace.
@Test
public void renderWhenAttachmentIsInANestedSpace() 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/space1/space2/page/file.ext");
xcontext.setRequest(request);
// 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");
// Set the current doc
XWikiDocument document = mock(XWikiDocument.class);
when(document.getAttachment("file.ext")).thenReturn(attachment);
xcontext.setDoc(document);
// Set the Plugin Manager
XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
// 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("file.ext", new DocumentReference("wiki", Arrays.asList("space1", "space2"), "page")), EntityResourceAction.VIEW));
// Note: we don't give PR and the attachment is not an authorized mime type.
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());
}
Aggregations