Search in sources :

Example 1 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference in project xwiki-platform by xwiki.

the class ExtendedURLTemporaryResourceReferenceResolver method resolve.

@Override
public TemporaryResourceReference resolve(ExtendedURL extendedURL, ResourceType resourceType, Map<String, Object> parameters) throws CreateResourceReferenceException, UnsupportedResourceReferenceException {
    TemporaryResourceReference reference;
    List<String> segments = extendedURL.getSegments();
    if (segments.size() < 3) {
        throw new CreateResourceReferenceException(String.format("Invalid temporary resource URL format [%s].", extendedURL.toString()));
    } else {
        // The first segment is the module id.
        String moduleId = segments.get(0);
        // The second segment is the serialized owning entity reference. This is used to check access rights.
        EntityReference owningEntityReference = resolveEntityReference(segments.get(1));
        // The other segments point to the resource path.
        List<String> resourcePath = segments.subList(2, segments.size());
        reference = new TemporaryResourceReference(moduleId, resourcePath, owningEntityReference);
        copyParameters(extendedURL, reference);
    }
    return reference;
}
Also used : TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) EntityReference(org.xwiki.model.reference.EntityReference) CreateResourceReferenceException(org.xwiki.resource.CreateResourceReferenceException)

Example 2 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference in project xwiki-platform by xwiki.

the class BatikSVGRasterizer method rasterizeToTemporaryFile.

@Override
public File rasterizeToTemporaryFile(String content, int width, int height) throws IOException {
    String fileName = getTemporaryFileName(content, width, height);
    TemporaryResourceReference reference = new TemporaryResourceReference(TEMP_DIR_NAME, fileName, null);
    File out = this.temporaryResourceStore.getTemporaryFile(reference);
    if (rasterizeToFile(content, out, width, height)) {
        return out;
    }
    return null;
}
Also used : TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) File(java.io.File)

Example 3 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference in project xwiki-platform by xwiki.

the class BatikSVGRasterizerTest method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    this.rasterFile = new File(this.baseDirectory.getRoot() + "/temp/svg/wiki/Space/Document/" + RASTER_FILE_NAME);
    this.temporaryFile = new File(this.baseDirectory.getRoot() + "/temp/svg/" + RASTER_FILE_NAME);
    this.temporaryFilePath = this.temporaryFile.getAbsolutePath();
    this.temporaryResourceStore = this.mocker.getInstance(TemporaryResourceStore.class);
    TemporaryResourceReference rasterFileReferece = new TemporaryResourceReference("svg", RASTER_FILE_NAME, dref);
    when(this.temporaryResourceStore.getTemporaryFile(rasterFileReferece)).thenReturn(rasterFile);
    TemporaryResourceReference temporaryFileReferece = new TemporaryResourceReference("svg", RASTER_FILE_NAME, null);
    when(this.temporaryResourceStore.getTemporaryFile(temporaryFileReferece)).thenReturn(temporaryFile);
    String invalidRasterFileName = Math.abs(INVALID_SVG.hashCode()) + ".png";
    File invalidRasterFile = new File(this.baseDirectory.getRoot() + "/temp/svg/wiki/Space/Document/" + invalidRasterFileName);
    File invalidTemporaryFile = new File(this.baseDirectory.getRoot() + "/temp/svg/" + invalidRasterFileName);
    TemporaryResourceReference invalidRasterFileReferece = new TemporaryResourceReference("svg", invalidRasterFileName, dref);
    when(this.temporaryResourceStore.getTemporaryFile(invalidRasterFileReferece)).thenReturn(invalidRasterFile);
    TemporaryResourceReference invalidTemporaryFileReferece = new TemporaryResourceReference("svg", invalidRasterFileName, null);
    when(this.temporaryResourceStore.getTemporaryFile(invalidTemporaryFileReferece)).thenReturn(invalidTemporaryFile);
    this.resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
    when(this.resolver.resolve("")).thenReturn(this.dref);
    this.container = this.mocker.getInstance(Container.class);
    when(this.container.getResponse()).thenReturn(this.sresponse);
    when(this.sresponse.getHttpServletResponse()).thenReturn(this.hsresponse);
}
Also used : Container(org.xwiki.container.Container) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) TemporaryResourceStore(org.xwiki.resource.temporary.TemporaryResourceStore) File(java.io.File) Before(org.junit.Before)

Example 4 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference in project xwiki-platform by xwiki.

the class BatikSVGRasterizerTest method rasterizeToTemporaryResourceReturnsNullWhenContextTempDirCannotBeCreated.

@Test
public void rasterizeToTemporaryResourceReturnsNullWhenContextTempDirCannotBeCreated() throws Exception {
    this.baseDirectory.getRoot().mkdirs();
    writeTestFile(new File(new File(new File(new File(this.baseDirectory.getRoot(), "temp"), "svg"), "wiki"), "Space"));
    TemporaryResourceReference tref = this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(VALID_SVG, 100, 200);
    Assert.assertNull(tref);
}
Also used : TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) File(java.io.File) Test(org.junit.Test)

Example 5 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference 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

TemporaryResourceReference (org.xwiki.resource.temporary.TemporaryResourceReference)11 File (java.io.File)6 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)2 ImageBlock (org.xwiki.rendering.block.ImageBlock)2 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)2 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)2 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)2 TemporaryResourceStore (org.xwiki.resource.temporary.TemporaryResourceStore)2 InputStream (java.io.InputStream)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 CacheException (org.xwiki.cache.CacheException)1 InitializationException (org.xwiki.component.phase.InitializationException)1 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)1 Container (org.xwiki.container.Container)1 AttachmentReference (org.xwiki.model.reference.AttachmentReference)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1