Search in sources :

Example 1 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class DeleteAttachmentAction method getFileName.

/**
 * @return the filename of the attachment.
 */
private String getFileName() {
    // Extract the Attachment file name from the parsed request URL that was done before this Action is called
    ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference();
    EntityResourceReference entityResource = (EntityResourceReference) resourceReference;
    return entityResource.getEntityReference().extractReference(EntityType.ATTACHMENT).getName();
}
Also used : ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference)

Example 2 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class XWiki method initializeResourceFromURL.

private static EntityResourceReference initializeResourceFromURL(XWikiContext context) throws XWikiException {
    // Extract the Entity Resource from the URL
    // TODO: This code should be put in an ExecutionContextInitializer but we couldn't do yet since this code
    // requires that the XWiki object be initialized first (the line above). Thus we'll be able to to move it only
    // after the XWiki init is done also in an ExecutionContextInitializer (and with priorities).
    @SuppressWarnings("deprecation") EntityResourceReference entityResourceReference;
    URL url = context.getURL();
    try {
        ExtendedURL extendedURL = new ExtendedURL(url, context.getRequest().getContextPath());
        ResourceTypeResolver<ExtendedURL> typeResolver = Utils.getComponent(new DefaultParameterizedType(null, ResourceTypeResolver.class, ExtendedURL.class));
        ResourceType type = typeResolver.resolve(extendedURL, Collections.<String, Object>emptyMap());
        ResourceReferenceResolver<ExtendedURL> resourceResolver = Utils.getComponent(new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class));
        ResourceReference reference = resourceResolver.resolve(extendedURL, type, Collections.<String, Object>emptyMap());
        entityResourceReference = reference instanceof EntityResourceReference ? (EntityResourceReference) reference : null;
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, String.format("Failed to extract Entity Resource Reference from URL [%s]", url), e);
    }
    Utils.getComponent(Execution.class).getContext().setProperty(ResourceReferenceManager.RESOURCE_CONTEXT_PROPERTY, entityResourceReference);
    return entityResourceReference;
}
Also used : ResourceReferenceResolver(org.xwiki.resource.ResourceReferenceResolver) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceType(org.xwiki.resource.ResourceType) ExtendedURL(org.xwiki.url.ExtendedURL) 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) ResourceTypeResolver(org.xwiki.resource.ResourceTypeResolver) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 3 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class DownloadActionTest method downloadWhenURLNotPointingToAttachment.

@Test
public void downloadWhenURLNotPointingToAttachment() throws XWikiException {
    ResourceReference rr = new EntityResourceReference(this.documentReference, EntityResourceAction.VIEW);
    when(this.resourceReferenceManager.getResourceReference()).thenReturn(rr);
    when(this.request.getRequestURI()).thenReturn("/xwiki/bin/download/space/page");
    try {
        this.action.render(this.oldcore.getXWikiContext());
        fail("Should have thrown an exception before reaching here");
    } catch (XWikiException expected) {
        assertEquals("Error number 11003 in 11: Attachment not found", expected.getMessage());
    }
}
Also used : EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) XWikiException(com.xpn.xwiki.XWikiException) Test(org.junit.Test)

Example 4 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference 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());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) XWikiContext(com.xpn.xwiki.XWikiContext) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) XWiki(com.xpn.xwiki.XWiki) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Date(java.util.Date) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ExecutionContext(org.xwiki.context.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) VelocityManager(org.xwiki.velocity.VelocityManager) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 5 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class ViewAttachRevAction method getFileName.

/**
 * @return the filename of the attachment.
 */
private String getFileName() {
    // Extract the Attachment file name from the parsed request URL that was done before this Action is called
    ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference();
    EntityResourceReference entityResource = (EntityResourceReference) resourceReference;
    return entityResource.getEntityReference().extractReference(EntityType.ATTACHMENT).getName();
}
Also used : ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceReference(org.xwiki.resource.ResourceReference)

Aggregations

EntityResourceReference (org.xwiki.resource.entity.EntityResourceReference)18 ResourceReference (org.xwiki.resource.ResourceReference)8 ResourceReferenceManager (org.xwiki.resource.ResourceReferenceManager)7 URL (java.net.URL)5 Test (org.junit.Test)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 ResourceType (org.xwiki.resource.ResourceType)5 ExtendedURL (org.xwiki.url.ExtendedURL)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 IOException (java.io.IOException)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 EntityReference (org.xwiki.model.reference.EntityReference)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 XWikiPluginManager (com.xpn.xwiki.plugin.XWikiPluginManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Date (java.util.Date)2 WikiReference (org.xwiki.model.reference.WikiReference)2 XWiki (com.xpn.xwiki.XWiki)1