Search in sources :

Example 36 with EntityReference

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;
}
Also used : ArrayList(java.util.ArrayList) EntityReference(org.xwiki.model.reference.EntityReference) IOException(java.io.IOException) File(java.io.File)

Example 37 with EntityReference

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;
}
Also used : TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) EntityReference(org.xwiki.model.reference.EntityReference) CreateResourceReferenceException(org.xwiki.resource.CreateResourceReferenceException)

Example 38 with EntityReference

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());
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 39 with EntityReference

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);
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) ComponentManager(org.xwiki.component.manager.ComponentManager)

Example 40 with EntityReference

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;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) EntityReference(org.xwiki.model.reference.EntityReference) ComponentManager(org.xwiki.component.manager.ComponentManager) AbstractGenericComponentManager(org.xwiki.component.internal.multi.AbstractGenericComponentManager)

Aggregations

EntityReference (org.xwiki.model.reference.EntityReference)338 Test (org.junit.Test)157 DocumentReference (org.xwiki.model.reference.DocumentReference)107 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)39 BaseObject (com.xpn.xwiki.objects.BaseObject)38 ArrayList (java.util.ArrayList)27 XWikiContext (com.xpn.xwiki.XWikiContext)24 WikiReference (org.xwiki.model.reference.WikiReference)24 SpaceReference (org.xwiki.model.reference.SpaceReference)23 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)18 XWikiException (com.xpn.xwiki.XWikiException)17 EntityType (org.xwiki.model.EntityType)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 EntityReferenceProvider (org.xwiki.model.reference.EntityReferenceProvider)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)8 XDOM (org.xwiki.rendering.block.XDOM)8 URL (java.net.URL)7 AttachmentReference (org.xwiki.model.reference.AttachmentReference)7 HashMap (java.util.HashMap)6 Before (org.junit.Before)6