Search in sources :

Example 6 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.

the class DefaultResourceReferenceEntityReferenceResolverTest method resolveTypeSpace.

@Test
public void resolveTypeSpace() throws ComponentLookupException {
    assertEquals(new SpaceReference(WIKI, SPACE), this.mocker.getComponentUnderTest().resolve(spaceResource(WIKI + ':' + SPACE, true), null));
    assertEquals(new SpaceReference(CURRENT_WIKI, SPACE), this.mocker.getComponentUnderTest().resolve(spaceResource(SPACE, true), null));
    assertEquals(new SpaceReference(CURRENT_WIKI, CURRENT_SPACE), this.mocker.getComponentUnderTest().resolve(spaceResource("", true), null));
    when(this.currentEntityReferenceResolver.resolve(eq(WIKI + ':' + SPACE + '.' + PAGE), eq(EntityType.DOCUMENT), any())).thenReturn(new DocumentReference(WIKI, SPACE, PAGE));
    ResourceReference withBaseReference = spaceResource("", true);
    withBaseReference.addBaseReference(WIKI + ':' + SPACE + '.' + PAGE);
    assertEquals(new SpaceReference(WIKI, SPACE), this.mocker.getComponentUnderTest().resolve(withBaseReference, null));
    assertEquals(new SpaceReference(WIKI, SPACE), this.mocker.getComponentUnderTest().resolve(spaceResource("", true), null, new DocumentReference(WIKI, SPACE, PAGE)));
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) SpaceResourceReference(org.xwiki.rendering.listener.reference.SpaceResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 7 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.

the class XWikiWikiModelTest method getDocumentViewURLWhenNoBaseReference.

@Test
public void getDocumentViewURLWhenNoBaseReference() throws Exception {
    ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference);
    when(this.documentAccessBridge.getDocumentURL(documentReference, "view", null, null)).thenReturn("viewurl");
    assertEquals("viewurl", this.mocker.getComponentUnderTest().getDocumentViewURL(reference));
}
Also used : AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 8 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.

the class XWikiWikiModelTest method getXDOM.

@Test
public void getXDOM() throws Exception {
    ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(this.documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(dmb);
    XDOM xdom = new XDOM(Collections.emptyList());
    when(dmb.getXDOM()).thenReturn(xdom);
    assertEquals(xdom, this.mocker.getComponentUnderTest().getXDOM(reference));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.

the class ExtensionVersionFileRESTResource method downloadLocalExtension.

private ResponseBuilder downloadLocalExtension(String extensionId, String extensionVersion) throws ResolveException, IOException, QueryException, XWikiException {
    XWikiDocument extensionDocument = getExistingExtensionDocumentById(extensionId);
    checkRights(extensionDocument);
    ResourceReference resourceReference = repositoryManager.getDownloadReference(extensionDocument, extensionVersion);
    ResponseBuilder response = null;
    if (ResourceType.ATTACHMENT.equals(resourceReference.getType())) {
        // It's an attachment
        AttachmentReference attachmentReference = this.attachmentResolver.resolve(resourceReference.getReference(), extensionDocument.getDocumentReference());
        XWikiContext xcontext = getXWikiContext();
        XWikiDocument document = xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext);
        checkRights(document);
        XWikiAttachment xwikiAttachment = document.getAttachment(attachmentReference.getName());
        response = getAttachmentResponse(xwikiAttachment);
    } else if (ResourceType.URL.equals(resourceReference.getType())) {
        // It's an URL
        URL url = new URL(resourceReference.getReference());
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "XWikiExtensionRepository");
        httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
        httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
        ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
        httpClient.setRoutePlanner(routePlanner);
        HttpGet getMethod = new HttpGet(url.toString());
        HttpResponse subResponse;
        try {
            subResponse = httpClient.execute(getMethod);
        } catch (Exception e) {
            throw new IOException("Failed to request [" + getMethod.getURI() + "]", e);
        }
        response = Response.status(subResponse.getStatusLine().getStatusCode());
        // TODO: find a proper way to do a perfect proxy of the URL without directly using Restlet classes.
        // Should probably use javax.ws.rs.ext.MessageBodyWriter
        HttpEntity entity = subResponse.getEntity();
        InputRepresentation content = new InputRepresentation(entity.getContent(), entity.getContentType() != null ? new MediaType(entity.getContentType().getValue()) : MediaType.APPLICATION_OCTET_STREAM, entity.getContentLength());
        BaseObject extensionObject = getExtensionObject(extensionDocument);
        String type = getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_TYPE);
        Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
        disposition.setFilename(extensionId + '-' + extensionVersion + '.' + type);
        content.setDisposition(disposition);
        response.entity(content);
    } else if (ExtensionResourceReference.TYPE.equals(resourceReference.getType())) {
        ExtensionResourceReference extensionResource;
        if (resourceReference instanceof ExtensionResourceReference) {
            extensionResource = (ExtensionResourceReference) resourceReference;
        } else {
            extensionResource = new ExtensionResourceReference(resourceReference.getReference());
        }
        response = downloadRemoteExtension(extensionResource);
    } else {
        throw new WebApplicationException(Status.NOT_FOUND);
    }
    return response;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) HttpEntity(org.apache.http.HttpEntity) InputRepresentation(org.restlet.representation.InputRepresentation) WebApplicationException(javax.ws.rs.WebApplicationException) HttpGet(org.apache.http.client.methods.HttpGet) XWikiContext(com.xpn.xwiki.XWikiContext) HttpResponse(org.apache.http.HttpResponse) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) URL(java.net.URL) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) XWikiException(com.xpn.xwiki.XWikiException) URISyntaxException(java.net.URISyntaxException) WebApplicationException(javax.ws.rs.WebApplicationException) QueryException(org.xwiki.query.QueryException) IOException(java.io.IOException) ResolveException(org.xwiki.extension.ResolveException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ProxySelectorRoutePlanner(org.apache.http.impl.conn.ProxySelectorRoutePlanner) Disposition(org.restlet.data.Disposition) MediaType(org.restlet.data.MediaType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ExtensionResourceReference(org.xwiki.repository.internal.reference.ExtensionResourceReference) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ExtensionResourceReference(org.xwiki.repository.internal.reference.ExtensionResourceReference)

Example 10 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.

the class DefaultOfficeResourceViewer method processImages.

/**
 * Processes all the image blocks in the given XDOM and changes image URL to point to a temporary file for those
 * images that are view artifacts.
 *
 * @param xdom the XDOM whose image blocks are to be processed
 * @param artifacts specify which of the image blocks should be processed; only the image blocks that were generated
 *            during the office import process should be processed
 * @param ownerDocumentReference specifies the document that owns the office file
 * @param resourceReference a reference to the office file that is being viewed; this reference is used to compute
 *            the path to the temporary directory holding the image artifacts
 * @param parameters the build parameters. Note that currently only {@code filterStyles} is supported and if "true"
 *            it means that styles will be filtered to the maximum and the focus will be put on importing only the
 * @return the set of temporary files corresponding to image artifacts
 */
private Set<File> processImages(XDOM xdom, Map<String, byte[]> artifacts, DocumentReference ownerDocumentReference, String resourceReference, Map<String, ?> parameters) {
    // Process all image blocks.
    Set<File> temporaryFiles = new HashSet<File>();
    List<ImageBlock> imgBlocks = xdom.getBlocks(new ClassBlockMatcher(ImageBlock.class), Block.Axes.DESCENDANT);
    for (ImageBlock imgBlock : imgBlocks) {
        String imageReference = imgBlock.getReference().getReference();
        // Check whether there is a corresponding artifact.
        if (artifacts.containsKey(imageReference)) {
            try {
                List<String> resourcePath = Arrays.asList(String.valueOf(parameters.hashCode()), imageReference);
                TemporaryResourceReference temporaryResourceReference = new TemporaryResourceReference(MODULE_NAME, resourcePath, ownerDocumentReference);
                // Write the image into a temporary file.
                File tempFile = this.temporaryResourceStore.createTemporaryFile(temporaryResourceReference, new ByteArrayInputStream(artifacts.get(imageReference)));
                // Create a URL image reference which links to above temporary image file.
                String temporaryResourceURL = this.urlTemporaryResourceReferenceSerializer.serialize(temporaryResourceReference).serialize();
                ResourceReference urlImageReference = new ResourceReference(temporaryResourceURL, ResourceType.PATH);
                urlImageReference.setTyped(true);
                // Replace the old image block with a new one that uses the above URL image reference.
                Block newImgBlock = new ImageBlock(urlImageReference, false, imgBlock.getParameters());
                imgBlock.getParent().replaceChild(Arrays.asList(newImgBlock), imgBlock);
                // Make sure the new image block is not inside an ExpandedMacroBlock whose's content syntax doesn't
                // support relative path resource references (we use relative paths to refer the temporary files).
                maybeFixExpandedMacroAncestor(newImgBlock);
                // Collect the temporary file so that it can be cleaned up when the view is disposed from cache.
                temporaryFiles.add(tempFile);
            } catch (Exception ex) {
                String message = "Error while processing artifact image [%s].";
                this.logger.error(String.format(message, imageReference), ex);
            }
        }
    }
    return temporaryFiles;
}
Also used : ImageBlock(org.xwiki.rendering.block.ImageBlock) InitializationException(org.xwiki.component.phase.InitializationException) CacheException(org.xwiki.cache.CacheException) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ImageBlock(org.xwiki.rendering.block.ImageBlock) Block(org.xwiki.rendering.block.Block) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)51 DocumentReference (org.xwiki.model.reference.DocumentReference)27 Test (org.junit.Test)26 LinkBlock (org.xwiki.rendering.block.LinkBlock)20 XDOM (org.xwiki.rendering.block.XDOM)16 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)15 AttachmentResourceReference (org.xwiki.rendering.listener.reference.AttachmentResourceReference)13 Block (org.xwiki.rendering.block.Block)12 SpaceReference (org.xwiki.model.reference.SpaceReference)11 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 AttachmentReference (org.xwiki.model.reference.AttachmentReference)9 ImageBlock (org.xwiki.rendering.block.ImageBlock)8 MacroBlock (org.xwiki.rendering.block.MacroBlock)8 ResourceType (org.xwiki.rendering.listener.reference.ResourceType)8 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReference (org.xwiki.model.reference.EntityReference)6 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 GroupBlock (org.xwiki.rendering.block.GroupBlock)5