use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class DefaultTemporaryResourceStore method getTemporaryFile.
@Override
public File getTemporaryFile(TemporaryResourceReference reference) throws IOException {
List<String> segments = new ArrayList<>();
segments.add("tmp");
segments.add(reference.getModuleId());
int safePathLength = 2;
if (reference.getOwningEntityReference() != null) {
for (EntityReference component : reference.getOwningEntityReference().getReversedReferenceChain()) {
segments.add(component.getName());
safePathLength++;
}
}
if (!reference.getParameters().isEmpty()) {
segments.add(String.valueOf(reference.getParameters().hashCode()));
safePathLength++;
}
segments.addAll(reference.getResourcePath());
String path = StringUtils.join(encode(segments), '/');
String safePath = StringUtils.join(encode(segments.subList(0, safePathLength)), '/');
File rootFolder = this.environment.getTemporaryDirectory();
File safeFolder = new File(rootFolder, safePath);
File temporaryFile = new File(rootFolder, path);
// Make sure the resource path is not relative (e.g. "../../../") and tries to get outside of the safe folder.
if (!temporaryFile.getAbsolutePath().startsWith(safeFolder.getAbsolutePath())) {
String resourcePath = StringUtils.join(encode(segments.subList(safePathLength, segments.size())), '/');
throw new IOException(String.format("Invalid resource path [%s].", resourcePath));
}
return temporaryFile;
}
use of org.xwiki.model.reference.EntityReference 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.model.reference.EntityReference in project xwiki-platform by xwiki.
the class EntityResourceReferenceTest method creation.
@Test
public void creation() {
EntityReference reference = new DocumentReference("wiki", "space", "page");
EntityResourceReference resource = new EntityResourceReference(reference, EntityResourceAction.VIEW);
assertEquals(EntityResourceAction.VIEW, resource.getAction());
assertEquals(reference, resource.getEntityReference());
assertEquals(Collections.EMPTY_MAP, resource.getParameters());
assertNull(resource.getLocale());
resource.addParameter("param1", "value1");
assertEquals("value1", resource.getParameterValue("param1"));
resource.setLocale(Locale.ROOT);
assertEquals(Locale.ROOT, resource.getLocale());
resource.setRevision("1.0");
assertEquals("1.0", resource.getRevision());
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class AbstractEnityComponentManagerFactory method createComponentManager.
@Override
public ComponentManager createComponentManager(String namespace, ComponentManager parentComponentManager) {
// Get entity reference
EntityReference reference = this.resolver.resolve(namespace.substring(getEntityType().getLowerCase().length() + 1), getEntityType());
// Get parent reference
EntityReference parentReference = reference.getParent();
ComponentManager parent;
if (parentReference != null) {
// Get parent namespace
String parentNamespace = parentReference.getType().getLowerCase() + ':' + this.serializer.serialize(parentReference);
// Get parent component manager
parent = this.manager.getComponentManager(parentNamespace, true);
} else {
parent = parentComponentManager;
}
return this.defaultFactory.createComponentManager(namespace, parent);
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class AbstractEntityComponentManager method getComponentManagerInternal.
@Override
protected ComponentManager getComponentManagerInternal() {
// Get current user reference
EntityReference entityReference = getCurrentReference();
if (entityReference == null) {
return null;
}
ExecutionContext econtext = this.execution.getContext();
// If there is no context don't try to find or register the component manager
if (econtext == null) {
return super.getComponentManagerInternal();
}
// Try to find the user component manager in the context
EntityComponentManagerInstance contextComponentManager = (EntityComponentManagerInstance) econtext.getProperty(this.contextKey);
if (contextComponentManager != null && contextComponentManager.entityReference.equals(entityReference)) {
return contextComponentManager.componentManager;
}
// Fallback on regular user component manager search
ComponentManager componentManager = super.getComponentManagerInternal();
// Cache the component manager
econtext.setProperty(this.contextKey, new EntityComponentManagerInstance(entityReference, componentManager));
return componentManager;
}
Aggregations