Package org.restlet.data

Examples of org.restlet.data.ReferenceList


                String baseName = getBaseName(file, metadataService);
                // 2- looking for resources with the same base name
                if (file.getParentFile() != null) {
                    File[] files = file.getParentFile().listFiles();
                    if (files != null) {
                        ReferenceList rl = new ReferenceList(files.length);

                        String encodedParentDirectoryURI = path.substring(0,
                                path.lastIndexOf("/"));
                        String encodedFileName = path.substring(path
                                .lastIndexOf("/") + 1);

                        for (File entry : files) {
                            if (entry.getName().startsWith(baseName)) {
                                rl
                                        .add(LocalReference
                                                .createFileReference(encodedParentDirectoryURI
                                                        + "/"
                                                        + getReencodedVariantFileName(
                                                                encodedFileName,
                                                                entry.getName())));
                            }
                        }
                        output = rl.getTextRepresentation();
                    }
                }
            } else {
                if ((file != null) && file.exists()) {
                    if (file.isDirectory()) {
                        // Return the directory listing
                        File[] files = file.listFiles();
                        ReferenceList rl = new ReferenceList(files.length);
                        rl.setIdentifier(request.getResourceRef());
                        String directoryUri = request.getResourceRef()
                                .toString();

                        // Ensures that the directory URI ends with a slash
                        if (!directoryUri.endsWith("/")) {
                            directoryUri += "/";
                        }

                        for (File entry : files) {
                            rl.add(directoryUri + entry.getName());
                        }

                        output = rl.getTextRepresentation();
                    } else {
                        // Return the file content
                        output = new FileRepresentation(file, metadataService
                                .getDefaultMediaType(), getTimeToLive());
                        updateMetadata(metadataService, file.getName(), output);
View Full Code Here


                // Return the directory listing
                Set<String> entries = getServletContext().getResourcePaths(
                        basePath);
                // Directory listing may be null.
                if (entries != null) {
                    ReferenceList rl = new ReferenceList(entries.size());
                    rl.setIdentifier(request.getResourceRef());

                    for (Iterator<String> iter = entries.iterator(); iter
                            .hasNext();) {
                        entry = iter.next();
                        rl.add(new Reference(basePath
                                + entry.substring(basePath.length())));
                    }

                    output = rl.getTextRepresentation();
                }
            } else {
                // Return the entry content
                MetadataService metadataService = getMetadataService(request);
                InputStream ris = getServletContext().getResourceAsStream(
View Full Code Here

      if (basePath.endsWith("/")) {
        // Return the directory listing
        Set<String> entries = getServletContext().getResourcePaths(
            basePath);
        ReferenceList rl = new ReferenceList(entries.size());
        rl.setIdentifier(request.getResourceRef());

        for (Iterator<String> iter = entries.iterator(); iter.hasNext();) {
          entry = iter.next();
          rl.add(new Reference(basePath
              + entry.substring(basePath.length())));
        }

        output = rl.getTextRepresentation();
      } else {
        // Return the entry content
        MetadataService metadataService = getMetadataService(request);
        InputStream ris = getServletContext().getResourceAsStream(
            basePath);
View Full Code Here

                // Return the directory listing
                Set<String> entries = getServletContext().getResourcePaths(
                        basePath);
                // Directory listing may be null.
                if (entries != null) {
                    ReferenceList rl = new ReferenceList(entries.size());
                    rl.setIdentifier(request.getResourceRef());

                    for (Iterator<String> iter = entries.iterator(); iter
                            .hasNext();) {
                        entry = iter.next();
                        rl.add(new Reference(basePath
                                + entry.substring(basePath.length())));
                    }

                    output = rl.getTextRepresentation();
                }
            } else {
                // Return the entry content
                MetadataService metadataService = getMetadataService(request);
                InputStream ris = getServletContext().getResourceAsStream(
View Full Code Here

                // "MediaType.TEXT_URI_LIST" when handling directories
                if (MediaType.TEXT_URI_LIST.equals(contextResponse.getEntity()
                        .getMediaType())) {
                    this.directoryTarget = true;
                    this.fileTarget = false;
                    this.directoryContent = new ReferenceList(contextResponse
                            .getEntity());
                    if (!request.getResourceRef().getIdentifier().endsWith("/")) {
                        // All requests will be automatically redirected
                        this.directoryRedirection = true;
                    }

                    if (!this.targetUri.endsWith("/")) {
                        this.targetUri += "/";
                        this.relativePart += "/";
                    }

                    // Append the index name
                    if ((getDirectory().getIndexName() != null)
                            && (getDirectory().getIndexName().length() > 0)) {
                        this.directoryUri = this.targetUri;
                        this.baseName = getDirectory().getIndexName();
                        this.targetUri = this.directoryUri + this.baseName;
                        this.indexTarget = true;
                    } else {
                        this.directoryUri = this.targetUri;
                        this.baseName = null;
                    }
                } else {
                    // Allows underlying helpers that do not support "content
                    // negotiation" to return the targetted file.
                    this.directoryTarget = false;
                    this.fileTarget = true;
                    this.fileContent = contextResponse.getEntity();
                }
            } else {
                this.directoryTarget = false;
                this.fileTarget = false;

                // Let's try with the facultative index, in case the underlying
                // client connector does not handle directory listing.
                if (this.targetUri.endsWith("/")) {
                    // In this case, the trailing "/" shows that the URI must
                    // point to a directory
                    if ((getDirectory().getIndexName() != null)
                            && (getDirectory().getIndexName().length() > 0)) {
                        this.directoryUri = this.targetUri;
                        this.directoryTarget = true;

                        contextResponse = getClientDispatcher().get(
                                this.directoryUri
                                        + getDirectory().getIndexName());
                        if (contextResponse.getEntity() != null) {
                            this.baseName = getDirectory().getIndexName();
                            this.targetUri = this.directoryUri + this.baseName;
                            this.directoryContent = new ReferenceList();
                            this.directoryContent.add(new Reference(
                                    this.targetUri));
                            this.indexTarget = true;
                        }
                    }
                } else {
                    // Try to determine if this target URI with no trailing "/"
                    // is a directory, in order to force the redirection.
                    if ((getDirectory().getIndexName() != null)
                            && (getDirectory().getIndexName().length() > 0)) {
                        // Append the index name
                        contextResponse = getClientDispatcher().get(
                                this.targetUri + "/"
                                        + getDirectory().getIndexName());
                        if (contextResponse.getEntity() != null) {
                            this.directoryUri = this.targetUri + "/";
                            this.baseName = getDirectory().getIndexName();
                            this.targetUri = this.directoryUri + this.baseName;
                            this.directoryTarget = true;
                            this.directoryRedirection = true;
                            this.directoryContent = new ReferenceList();
                            this.directoryContent.add(new Reference(
                                    this.targetUri));
                            this.indexTarget = true;
                        }
                    }
                }
            }

            // In case the request does not target a directory and the file has
            // not been found, try with the tunnelled URI.
            if (isNegotiateContent() && !this.directoryTarget
                    && !this.fileTarget && (this.originalRef != null)) {
                this.relativePart = request.getResourceRef().getRemainingPart();

                // The target uri does not take into account the query and
                // fragment parts of the resource.
                this.targetUri = new Reference(directory.getRootRef()
                        .toString()
                        + this.relativePart).normalize().toString(false, false);
                if (!this.targetUri.startsWith(directory.getRootRef()
                        .toString())) {
                    // Prevent the client from accessing resources in upper
                    // directories
                    this.targetUri = directory.getRootRef().toString();
                }
            }

            // Try to get the directory content, in case the request does not
            // target a directory
            if (!this.directoryTarget) {
                final int lastSlashIndex = this.targetUri.lastIndexOf('/');
                if (lastSlashIndex == -1) {
                    this.directoryUri = "";
                    this.baseName = this.targetUri;
                } else {
                    this.directoryUri = this.targetUri.substring(0,
                            lastSlashIndex + 1);
                    this.baseName = this.targetUri
                            .substring(lastSlashIndex + 1);
                }

                contextResponse = getClientDispatcher().get(this.directoryUri);
                if ((contextResponse.getEntity() != null)
                        && MediaType.TEXT_URI_LIST.equals(contextResponse
                                .getEntity().getMediaType())) {
                    this.directoryContent = new ReferenceList(contextResponse
                            .getEntity());
                }
            }

            if (this.baseName != null) {
View Full Code Here

            results.addAll(resultSet);

            if (resultSet.isEmpty()) {
                if (this.directoryTarget && getDirectory().isListingAllowed()) {
                    final ReferenceList userList = new ReferenceList(
                            this.directoryContent.size());
                    // Set the list identifier
                    userList.setIdentifier(baseRef);

                    final SortedSet<Reference> sortedSet = new TreeSet<Reference>(
                            getDirectory().getComparator());
                    sortedSet.addAll(this.directoryContent);

                    for (final Reference ref : sortedSet) {
                        final String filePart = ref.toString(false, false)
                                .substring(rootLength);
                        final StringBuilder filePath = new StringBuilder();
                        if ((!baseRef.endsWith("/"))
                                && (!filePart.startsWith("/"))) {
                            filePath.append('/');
                        }
                        filePath.append(filePart);
                        userList.add(baseRef + filePath);
                    }
                    final List<Variant> list = getDirectory().getIndexVariants(
                            userList);
                    for (final Variant variant : list) {
                        results.add(getDirectory().getIndexRepresentation(
View Full Code Here

     *
     * @return The list of variants references
     */
    private ReferenceList getVariantsReferences() {
        this.uniqueReference = null;
        final ReferenceList result = new ReferenceList(0);
        try {
            final Request contextCall = new Request(Method.GET, this.targetUri);
            // Ask for the list of all variants of this resource
            contextCall.getClientInfo().getAcceptedMediaTypes().add(
                    new Preference<MediaType>(MediaType.TEXT_URI_LIST));
            final Response contextResponse = getClientDispatcher().handle(
                    contextCall);
            if (contextResponse.getEntity() != null) {
                // Test if the given response is the list of all variants for
                // this resource
                if (MediaType.TEXT_URI_LIST.equals(contextResponse.getEntity()
                        .getMediaType())) {
                    final ReferenceList listVariants = new ReferenceList(
                            contextResponse.getEntity());
                    Set<String> extensions = null;
                    String entryUri;
                    String fullEntryName;
                    String baseEntryName;
View Full Code Here

                getClientDispatcher().handle(contextRequest, contextResponse);
            } else {
                // Check if there is only one representation

                // Try to get the unique representation of the resource
                final ReferenceList references = getVariantsReferences();
                if (!references.isEmpty()) {
                    if (this.uniqueReference != null) {
                        contextRequest.setResourceRef(this.uniqueReference);
                        getClientDispatcher().handle(contextRequest,
                                contextResponse);
                    } else {
View Full Code Here

            if (parent != null) {
                final Collection<Entity> entities = parent.getChildren();

                if (entities != null) {
                    final ReferenceList rl = new ReferenceList(entities.size());
                    final String scheme = request.getResourceRef().getScheme();
                    final String encodedParentDirectoryURI = path.substring(0,
                            path.lastIndexOf("/"));
                    final String encodedEntityName = path.substring(path
                            .lastIndexOf("/") + 1);

                    for (final Entity entry : entities) {
                        if (baseName.equals(entry.getBaseName(metadataService))) {
                            rl.add(createReference(scheme,
                                    encodedParentDirectoryURI,
                                    encodedEntityName, entry.getName()));
                        }
                    }

                    output = rl.getTextRepresentation();
                }
            }
        } else {
            if (entity.exists()) {
                if (entity.isDirectory()) {
                    // Return the directory listing
                    final Collection<Entity> children = entity.getChildren();
                    final ReferenceList rl = new ReferenceList(children.size());
                    String directoryUri = request.getResourceRef().toString();

                    // Ensures that the directory URI ends with a slash
                    if (!directoryUri.endsWith("/")) {
                        directoryUri += "/";
                    }

                    for (final Entity entry : children) {
                        rl
                                .add(directoryUri
                                        + Reference.encode(entry.getName()));
                    }

                    output = rl.getTextRepresentation();
                } else {
                    // Return the file content
                    output = entity.getRepresentation(metadataService
                            .getDefaultMediaType(), getTimeToLive());
                    output.setIdentifier(request.getResourceRef());
View Full Code Here

                // "MediaType.TEXT_URI_LIST" when handling directories
                if (MediaType.TEXT_URI_LIST.equals(contextResponse.getEntity()
                        .getMediaType())) {
                    this.directoryTarget = true;
                    this.fileTarget = false;
                    this.directoryContent = new ReferenceList(contextResponse
                            .getEntity());
                    if (!request.getResourceRef().getIdentifier().endsWith("/")) {
                        // All requests will be automatically redirected
                        this.directoryRedirection = true;
                    }

                    if (!this.targetUri.endsWith("/")) {
                        this.targetUri += "/";
                        this.relativePart += "/";
                    }

                    // Append the index name
                    if ((getDirectory().getIndexName() != null)
                            && (getDirectory().getIndexName().length() > 0)) {
                        this.directoryUri = this.targetUri;
                        this.baseName = getDirectory().getIndexName();
                        this.targetUri = this.directoryUri + this.baseName;
                        this.indexTarget = true;
                    } else {
                        this.directoryUri = this.targetUri;
                        this.baseName = null;
                    }
                } else {
                    // Allows underlying helpers that do not support "content
                    // negotiation" to return the targetted file.
                    this.directoryTarget = false;
                    this.fileTarget = true;
                    this.fileContent = contextResponse.getEntity();
                }
            } else {
                this.directoryTarget = false;
                this.fileTarget = false;

                // Let's try with the facultative index, in case the underlying
                // client connector does not handle directory listing.
                if (this.targetUri.endsWith("/")) {
                    // In this case, the trailing "/" shows that the URI must
                    // point to a directory
                    if ((getDirectory().getIndexName() != null)
                            && (getDirectory().getIndexName().length() > 0)) {
                        this.directoryUri = this.targetUri;
                        this.directoryTarget = true;

                        contextResponse = getClientDispatcher().get(
                                this.directoryUri
                                        + getDirectory().getIndexName());
                        if (contextResponse.getEntity() != null) {
                            this.baseName = getDirectory().getIndexName();
                            this.targetUri = this.directoryUri + this.baseName;
                            this.directoryContent = new ReferenceList();
                            this.directoryContent.add(new Reference(
                                    this.targetUri));
                            this.indexTarget = true;
                        }
                    }
                } else {
                    // Try to determine if this target URI with no trailing "/"
                    // is a directory, in order to force the redirection.
                    if ((getDirectory().getIndexName() != null)
                            && (getDirectory().getIndexName().length() > 0)) {
                        // Append the index name
                        contextResponse = getClientDispatcher().get(
                                this.targetUri + "/"
                                        + getDirectory().getIndexName());
                        if (contextResponse.getEntity() != null) {
                            this.directoryUri = this.targetUri + "/";
                            this.baseName = getDirectory().getIndexName();
                            this.targetUri = this.directoryUri + this.baseName;
                            this.directoryTarget = true;
                            this.directoryRedirection = true;
                            this.directoryContent = new ReferenceList();
                            this.directoryContent.add(new Reference(
                                    this.targetUri));
                            this.indexTarget = true;
                        }
                    }
                }
            }

            // In case the request does not target a directory and the file has
            // not been found, try with the tunnelled URI.
            if (isNegotiateContent() && !this.directoryTarget
                    && !this.fileTarget && (this.originalRef != null)) {
                this.relativePart = request.getResourceRef().getRemainingPart();

                // The target uri does not take into account the query and
                // fragment parts of the resource.
                this.targetUri = new Reference(directory.getRootRef()
                        .toString()
                        + this.relativePart).normalize().toString(false, false);
                if (!this.targetUri.startsWith(directory.getRootRef()
                        .toString())) {
                    // Prevent the client from accessing resources in upper
                    // directories
                    this.targetUri = directory.getRootRef().toString();
                }
            }

            // Try to get the directory content, in case the request does not
            // target a directory
            if (!this.directoryTarget) {
                final int lastSlashIndex = this.targetUri.lastIndexOf('/');
                if (lastSlashIndex == -1) {
                    this.directoryUri = "";
                    this.baseName = this.targetUri;
                } else {
                    this.directoryUri = this.targetUri.substring(0,
                            lastSlashIndex + 1);
                    this.baseName = this.targetUri
                            .substring(lastSlashIndex + 1);
                }

                contextResponse = getClientDispatcher().get(this.directoryUri);
                if ((contextResponse.getEntity() != null)
                        && MediaType.TEXT_URI_LIST.equals(contextResponse
                                .getEntity().getMediaType())) {
                    this.directoryContent = new ReferenceList(contextResponse
                            .getEntity());
                }
            }

            if (this.baseName != null) {
View Full Code Here

TOP

Related Classes of org.restlet.data.ReferenceList

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.