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;
}
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;
}
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);
}
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);
}
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;
}
Aggregations