Package org.restlet.service

Examples of org.restlet.service.MetadataService


        // As the path may be percent-encoded, it has to be percent-decoded.
        // Then, all generated uris must be encoded.
        String decodedPath = LocalReference
                .localizePath(Reference.decode(path));
        File file = new File(decodedPath);
        MetadataService metadataService = getMetadataService(request);

        if (request.getMethod().equals(Method.GET)
                || request.getMethod().equals(Method.HEAD)) {
            Representation output = null;

            // Get variants for a resource
            boolean found = false;
            Iterator<Preference<MediaType>> iterator = request.getClientInfo()
                    .getAcceptedMediaTypes().iterator();
            while (iterator.hasNext() && !found) {
                Preference<MediaType> pref = iterator.next();
                found = pref.getMetadata().equals(MediaType.TEXT_URI_LIST);
            }
            if (found) {
                // Try to list all variants of this resource
                // 1- set up base name as the longest part of the name without
                // known extensions (beginning from the left)
                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 (baseName.equals(getBaseName(entry, metadataService))) {
                                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);
                    }
                }
            }

            if (output == null) {
                response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            } else {
                output.setIdentifier(request.getResourceRef());
                response.setEntity(output);
                response.setStatus(Status.SUCCESS_OK);
            }
        } else if (request.getMethod().equals(Method.PUT)) {
            // Several checks : first the consistency of the metadata and the
            // filename
            if (!checkMetadataConsistency(file.getName(), metadataService,
                    request.getEntity())) {
                // ask the client to reiterate properly its request
                response.setStatus(new Status(Status.REDIRECTION_SEE_OTHER,
                        "The metadata are not consistent with the URI"));
            } else {
                // Deals with directory
                boolean isDirectory = false;
                if (file.exists()) {
                    if (file.isDirectory()) {
                        isDirectory = true;
                        response
                                .setStatus(new Status(
                                        Status.CLIENT_ERROR_FORBIDDEN,
                                        "Can't put a new representation of a directory"));
                    }
                } else {
                    // No existing file or directory found
                    if (path.endsWith("/")) {
                        isDirectory = true;
                        // Create a new directory and its necessary parents
                        if (file.mkdirs()) {
                            response.setStatus(Status.SUCCESS_NO_CONTENT);
                        } else {
                            getLogger().log(Level.WARNING,
                                    "Unable to create the new directory");
                            response.setStatus(new Status(
                                    Status.SERVER_ERROR_INTERNAL,
                                    "Unable to create the new directory"));
                        }
                    }
                }

                if (!isDirectory) {
                    // We look for the possible variants
                    // 1- set up base name as the longest part of the name
                    // without known extensions (beginning from the left)
                    String baseName = getBaseName(file, metadataService);
                    Set<String> extensions = getExtensions(file,
                            metadataService);
                    // 2- loooking for resources with the same base name
                    File[] files = file.getParentFile().listFiles();
                    File uniqueVariant = null;

                    List<File> variantsList = new ArrayList<File>();
                    if (files != null) {
                        for (File entry : files) {
                            if (entry.getName().startsWith(baseName)) {
                                Set<String> entryExtensions = getExtensions(
                                        entry, metadataService);
                                if (entryExtensions.containsAll(extensions)) {
                                    variantsList.add(entry);
                                    if (extensions.containsAll(entryExtensions)) {
                                        // The right representation has been
                                        // found.
                                        uniqueVariant = entry;
                                    }
                                }
                            }
                        }
                    }
                    if (uniqueVariant != null) {
                        file = uniqueVariant;
                    } else {
                        if (!variantsList.isEmpty()) {
                            // Negociated resource (several variants, but not
                            // the right one).
                            // Check if the request could be completed or not.
                            // The request could be more precise
                            response
                                    .setStatus(new Status(
                                            Status.CLIENT_ERROR_NOT_ACCEPTABLE,
                                            "Unable to process properly the request. Several variants exist but none of them suits precisely."));
                        } else {
                            // This resource does not exist, yet.
                            // Complete it with the default metadata
                            updateMetadata(metadataService, file.getName(),
                                    request.getEntity());
                            if (request.getEntity().getLanguages().isEmpty()) {
                                if (metadataService.getDefaultLanguage() != null) {
                                    request.getEntity().getLanguages().add(
                                            metadataService
                                                    .getDefaultLanguage());
                                }
                            }
                            if (request.getEntity().getMediaType() == null) {
                                request.getEntity().setMediaType(
                                        metadataService.getDefaultMediaType());
                            }
                            if (request.getEntity().getEncodings().isEmpty()) {
                                if (metadataService.getDefaultEncoding() != null
                                        && !metadataService
                                                .getDefaultEncoding().equals(
                                                        Encoding.IDENTITY)) {
                                    request.getEntity().getEncodings().add(
                                            metadataService
                                                    .getDefaultEncoding());
                                }
                            }
                            // Update the URI
                            StringBuilder fileName = new StringBuilder(baseName);
                            if (metadataService.getExtension(request
                                    .getEntity().getMediaType()) != null) {
                                fileName.append("."
                                        + metadataService.getExtension(request
                                                .getEntity().getMediaType()));
                            }
                            for (Language language : request.getEntity()
                                    .getLanguages()) {
                                if (metadataService.getExtension(language) != null) {
                                    fileName.append("."
                                            + metadataService
                                                    .getExtension(language));
                                }
                            }
                            for (Encoding encoding : request.getEntity()
                                    .getEncodings()) {
                                if (metadataService.getExtension(encoding) != null) {
                                    fileName.append("."
                                            + metadataService
                                                    .getExtension(encoding));
                                }
                            }
                            file = new File(file.getParentFile(), fileName
                                    .toString());
View Full Code Here


     * @param path
     *            The file or directory path.
     */
    protected void handleFile(Request request, Response response, String path) {
        File file = new File(LocalReference.localizePath(path));
        MetadataService metadataService = getMetadataService(request);

        if (request.getMethod().equals(Method.GET)
                || request.getMethod().equals(Method.HEAD)) {
            Representation output = null;

            // Get variants for a resource
            boolean found = false;
            Iterator<Preference<MediaType>> iterator = request.getClientInfo()
                    .getAcceptedMediaTypes().iterator();
            while (iterator.hasNext() && !found) {
                Preference<MediaType> pref = iterator.next();
                found = pref.getMetadata().equals(MediaType.TEXT_URI_LIST);
            }
            if (found) {
                // 1- set up base name as the longest part of the name without
                // known extensions (beginning from the left)
                String baseName = getBaseName(file, metadataService);
                // 2- loooking for resources with the same base name
                File[] files = file.getParentFile().listFiles();
                ReferenceList rl = new ReferenceList(files.length);
                rl.setIdentifier(request.getResourceRef());

                for (File entry : files) {
                    try {
                        if (entry.getName().startsWith(baseName)) {
                            rl.add(LocalReference.createFileReference(entry));
                        }
                    } catch (IOException ioe) {
                        getLogger().log(Level.WARNING,
                                "Unable to create file reference", ioe);
                    }
                }
                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());

                        for (File entry : files) {
                            try {
                                rl.add(LocalReference
                                        .createFileReference(entry));
                            } catch (IOException ioe) {
                                getLogger().log(Level.WARNING,
                                        "Unable to create file reference", ioe);
                            }
                        }

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

            if (output == null) {
                response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            } else {
                response.setEntity(output);
                response.setStatus(Status.SUCCESS_OK);
            }
        } else if (request.getMethod().equals(Method.PUT)) {
            // Several checks : first the consistency of the metadata and the
            // filename
            if (!checkMetadataConsistency(file.getName(), metadataService,
                    request.getEntity())) {
                // ask the client to reiterate properly its request
                response.setStatus(new Status(Status.REDIRECTION_SEE_OTHER,
                        "The metadata are not consistent with the URI"));
            } else {
                // Deals with directory
                boolean isDirectory = false;
                if (file.exists()) {
                    if (file.isDirectory()) {
                        isDirectory = true;
                        response
                                .setStatus(new Status(
                                        Status.CLIENT_ERROR_FORBIDDEN,
                                        "Can't put a new representation of a directory"));
                    }
                } else {
                    // No existing file or directory found
                    if (path.endsWith("/")) {
                        isDirectory = true;
                        // Create a new directory and its necessary parents
                        if (file.mkdirs()) {
                            response.setStatus(Status.SUCCESS_NO_CONTENT);
                        } else {
                            getLogger().log(Level.WARNING,
                                    "Unable to create the new directory");
                            response.setStatus(new Status(
                                    Status.SERVER_ERROR_INTERNAL,
                                    "Unable to create the new directory"));
                        }
                    }
                }

                if (!isDirectory) {
                    // We look for the possible variants
                    // 1- set up base name as the longest part of the name
                    // without known extensions (beginning from the left)
                    String baseName = getBaseName(file, metadataService);
                    Set<String> extensions = getExtensions(file,
                            metadataService);
                    // 2- loooking for resources with the same base name
                    File[] files = file.getParentFile().listFiles();
                    File uniqueVariant = null;

                    List<File> variantsList = new ArrayList<File>();
                    if (files != null) {
                        for (File entry : files) {
                            if (entry.getName().startsWith(baseName)) {
                                Set<String> entryExtensions = getExtensions(
                                        entry, metadataService);
                                if (entryExtensions.containsAll(extensions)) {
                                    variantsList.add(entry);
                                    if (extensions.containsAll(entryExtensions)) {
                                        // The right representation has been
                                        // found.
                                        uniqueVariant = entry;
                                    }
                                }
                            }
                        }
                    }
                    if (uniqueVariant != null) {
                        file = uniqueVariant;
                    } else {
                        if (!variantsList.isEmpty()) {
                            // Negociated resource (several variants, but not
                            // the right one).
                            // Check if the request could be completed or not.
                            // The request could be more precise
                            response
                                    .setStatus(new Status(
                                            Status.CLIENT_ERROR_NOT_ACCEPTABLE,
                                            "Unable to process properly the request. Several variants exist but none of them suits precisely."));
                        } else {
                            // This resource does not exist, yet.
                            // Complete it with the default metadata
                            updateMetadata(metadataService, file.getName(),
                                    request.getEntity());
                            if (request.getEntity().getLanguages().isEmpty()) {
                                if (metadataService.getDefaultLanguage() != null) {
                                    request.getEntity().getLanguages().add(
                                            metadataService
                                                    .getDefaultLanguage());
                                }
                            }
                            if (request.getEntity().getMediaType() == null) {
                                request.getEntity().setMediaType(
                                        metadataService.getDefaultMediaType());
                            }
                            if (request.getEntity().getEncodings().isEmpty()) {
                                if (metadataService.getDefaultEncoding() != null
                                        && !metadataService
                                                .getDefaultEncoding().equals(
                                                        Encoding.IDENTITY)) {
                                    request.getEntity().getEncodings().add(
                                            metadataService
                                                    .getDefaultEncoding());
                                }
                            }
                            // Update the URI
                            StringBuilder fileName = new StringBuilder(baseName);
                            if (metadataService.getExtension(request
                                    .getEntity().getMediaType()) != null) {
                                fileName.append("."
                                        + metadataService.getExtension(request
                                                .getEntity().getMediaType()));
                            }
                            for (Language language : request.getEntity()
                                    .getLanguages()) {
                                if (metadataService.getExtension(language) != null) {
                                    fileName.append("."
                                            + metadataService
                                                    .getExtension(language));
                                }
                            }
                            for (Encoding encoding : request.getEntity()
                                    .getEncodings()) {
                                if (metadataService.getExtension(encoding) != null) {
                                    fileName.append("."
                                            + metadataService
                                                    .getExtension(encoding));
                                }
                            }
                            file = new File(file.getParentFile(), fileName
                                    .toString());
View Full Code Here

     * @param response
     *            The response to update.
     */
    protected void handleClassLoader(Request request, Response response,
            ClassLoader classLoader) {
        MetadataService metadataService = getMetadataService(request);

        if (request.getMethod().equals(Method.GET)
                || request.getMethod().equals(Method.HEAD)) {
            String path = request.getResourceRef().getPath();

            // Prepare a classloader URI, removing the leading slash
            if ((path != null) && path.startsWith("/"))
                path = path.substring(1);
            URL url = classLoader.getResource(path);

            // The ClassLoader returns a directory listing in some cases.
            // As this listing is partial, is it of little value in the context
            // of the CLAP client, so we have to ignore them.
            if (url != null) {
                if (url.getProtocol().equals("file")) {
                    File file = new File(url.getFile());
                    if (file.isDirectory()) {
                        url = null;
                    }
                }
            }

            if (url != null) {
                try {
                    Representation output = new InputRepresentation(url
                            .openStream(), metadataService
                            .getDefaultMediaType());

                    // Update the metadata based on file extensions
                    String name = path.substring(path.lastIndexOf('/') + 1);
                    updateMetadata(metadataService, name, output);
View Full Code Here

     * @param response
     *            The response to update.
     */
    protected void handleClassLoader(Request request, Response response,
            ClassLoader classLoader) {
        MetadataService metadataService = getMetadataService();

        if (request.getMethod().equals(Method.GET)
                || request.getMethod().equals(Method.HEAD)) {
            String path = request.getResourceRef().getPath();
            URL url = null;
            Date modificationDate = null;

            // Prepare a classloader URI, removing the leading slash
            if ((path != null) && path.startsWith("/")) {
                path = path.substring(1);
            }

            // Get the URL to the classloader 'resource'
            if (classLoader != null) {
                // As the path may be percent-encoded, it has to be
                // percent-decoded.
                url = classLoader.getResource(Reference.decode(path));
            } else {
                getLogger()
                        .warning(
                                "Unable to get the resource. The selected classloader is null.");
            }

            // The ClassLoader returns a directory listing in some cases.
            // As this listing is partial, it is of little value in the context
            // of the CLAP client, so we have to ignore them.
            if (url != null) {
                if (url.getProtocol().equals("file")) {
                    File file = new File(url.getFile());
                    modificationDate = new Date(file.lastModified());

                    if (file.isDirectory()) {
                        url = null;
                    }
                }
            }

            if (url != null) {
                try {
                    Representation output = new InputRepresentation(url
                            .openStream(), metadataService
                            .getDefaultMediaType());
                    output.setLocationRef(request.getResourceRef());
                    output.setModificationDate(modificationDate);

                    // Update the expiration date
View Full Code Here

        this.services.add(new DecoderService());
        this.services.add(new EncoderService(false));
        this.services.add(new RangeService());
        this.services.add(new ConnectorService());
        this.services.add(new ConverterService());
        this.services.add(new MetadataService());

        this.services.add(new org.restlet.service.TaskService());
    }
View Full Code Here

     * new instance is created.
     *
     * @return The metadata service.
     */
    public MetadataService getMetadataService() {
        MetadataService result = null;

        if (getHelped() != null) {
            org.restlet.Application application = getHelped().getApplication();

            if (application != null) {
                result = application.getMetadataService();
            }
        }

        if (result == null) {
            result = new MetadataService();
        }

        return result;
    }
View Full Code Here

    MetadataService ms;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        ms = new MetadataService();
    }
View Full Code Here

            this.extensionLanguage = null;
            this.extensionMedia = null;
            this.extensionOthers = null;
            return this;
        }
        MetadataService metadataService;
        metadataService = Application.getCurrent().getMetadataService();
        StringTokenizer stt = new StringTokenizer(extensions, ".");
        while (stt.hasMoreTokens()) {
            String extension = stt.nextToken();
            Metadata metadata = metadataService.getMetadata(extension);
            if (metadata instanceof Language) {
                this.extensionLanguage = extension;
            } else if (metadata instanceof MediaType) {
                this.extensionMedia = extension;
            } else {
View Full Code Here

     * Returns the application's metadata service or create a new one.
     *
     * @return The metadata service.
     */
    public MetadataService getMetadataService() {
        MetadataService result = null;

        result = getApplication().getMetadataService();

        if (result == null) {
            result = new MetadataService();
        }

        return result;
    }
View Full Code Here

     *            The server resource to describe.
     */
    public static void describeAnnotations(MethodInfo info,
            ServerResource resource) {
        // Loop over the annotated Java methods
        MetadataService metadataService = resource.getMetadataService();
        List<AnnotationInfo> annotations = resource.isAnnotated() ? AnnotationUtils
                .getAnnotations(resource.getClass()) : null;

        if (annotations != null && metadataService != null) {
            for (AnnotationInfo annotationInfo : annotations) {
View Full Code Here

TOP

Related Classes of org.restlet.service.MetadataService

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.