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