Package org.restlet.resource

Examples of org.restlet.resource.Representation


     * @return The encoded representation or the original one if no encoding
     *         supported by the client.
     */
    public Representation encode(ClientInfo client,
            Representation representation) {
        Representation result = representation;
        Encoding bestEncoding = getBestEncoding(client);

        if (bestEncoding != null) {
            result = new EncodeRepresentation(bestEncoding, representation);
        }
View Full Code Here


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

            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
View Full Code Here

                || request.getMethod().equals(Method.HEAD)) {
            String basePath = request.getResourceRef().getPath();
            int lastSlashIndex = basePath.lastIndexOf('/');
            String entry = (lastSlashIndex == -1) ? basePath : basePath
                    .substring(lastSlashIndex + 1);
            Representation output = null;

            if (basePath.endsWith("/")) {
                // 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(
                        basePath);
                if (ris != null) {
                    output = new InputRepresentation(ris, metadataService
                            .getDefaultMediaType());
                    output.setIdentifier(request.getResourceRef());
                    updateMetadata(metadataService, entry, output);

                    // See if the Servlet context specified a particular Mime
                    // Type
                    String mediaType = getServletContext()
                            .getMimeType(basePath);

                    if (mediaType != null) {
                        output.setMediaType(new MediaType(mediaType));
                    }
                }
            }

            response.setEntity(output);
View Full Code Here

            }

            // If an entity was set during the call, copy it to the output
            // stream;
            if (response.getEntity() != null) {
                Representation entity = response.getEntity();

                if (entity.getExpirationDate() != null) {
                    responseHeaders.add(HttpConstants.HEADER_EXPIRES, response
                            .getHttpCall().formatDate(
                                    entity.getExpirationDate(), false));
                }

                if (!entity.getEncodings().isEmpty()) {
                    StringBuilder value = new StringBuilder();
                    for (Encoding encoding : entity.getEncodings()) {
                        if (!encoding.equals(Encoding.IDENTITY)) {
                            if (value.length() > 0)
                                value.append(", ");
                            value.append(encoding.getName());
                        }
                        responseHeaders.add(
                                HttpConstants.HEADER_CONTENT_ENCODING, value
                                        .toString());
                    }
                }

                if (!entity.getLanguages().isEmpty()) {
                    StringBuilder value = new StringBuilder();
                    for (int i = 0; i < entity.getLanguages().size(); i++) {
                        if (i > 0)
                            value.append(", ");
                        value.append(entity.getLanguages().get(i).getName());
                    }
                    responseHeaders.add(HttpConstants.HEADER_CONTENT_LANGUAGE,
                            value.toString());
                }

                if (entity.getMediaType() != null) {
                    StringBuilder contentType = new StringBuilder(entity
                            .getMediaType().getName());

                    if (entity.getCharacterSet() != null) {
                        // Specify the character set parameter
                        contentType.append("; charset=").append(
                                entity.getCharacterSet().getName());
                    }

                    for (Parameter parameter : entity.getMediaType()
                            .getParameters()) {
                        contentType.append("; ").append(parameter.getName())
                                .append("=").append(parameter.getValue());
                    }

                    responseHeaders.add(HttpConstants.HEADER_CONTENT_TYPE,
                            contentType.toString());
                }

                if (entity.getModificationDate() != null) {
                    responseHeaders.add(HttpConstants.HEADER_LAST_MODIFIED,
                            response.getHttpCall().formatDate(
                                    entity.getModificationDate(), false));
                }

                if (entity.getTag() != null) {
                    responseHeaders.add(HttpConstants.HEADER_ETAG, entity
                            .getTag().format());
                }

                if (response.getEntity().getSize() != Representation.UNKNOWN_SIZE) {
                    responseHeaders.add(HttpConstants.HEADER_CONTENT_LENGTH,
View Full Code Here

                            ref.toString());
                    if (contextResponse.getStatus().isSuccess()
                            && (contextResponse.getEntity() != null)) {
                        filePath = ref.toString(false, false).substring(
                                rootLength);
                        Representation rep = contextResponse.getEntity();
                        rep.setIdentifier(baseRef + filePath);
                        resultSet.add(rep);
                    }
                }
            }
View Full Code Here

     */
    public Status sendRequest(Request request) {
        Status result = null;

        try {
            Representation entity = request.isEntityAvailable() ? request
                    .getEntity() : null;

            if (entity != null) {
                // Get the connector service to callback
                ConnectorService connectorService = getConnectorService(request);
                if (connectorService != null)
                    connectorService.beforeSend(entity);

                // In order to workaround bug #6472250
                // (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6472250),
                // it is very important to reuse that exact same "rs" reference
                // when manipulating the request stream, otherwise "insufficient
                // data sent" exceptions will occur in "fixedLengthMode"
                OutputStream rs = getRequestStream();
                WritableByteChannel wbc = getRequestChannel();
                if (wbc != null) {
                    if (entity != null) {
                        entity.write(wbc);
                    }
                } else if (rs != null) {
                    if (entity != null) {
                        entity.write(rs);
                    }

                    rs.flush();
                }

View Full Code Here

     * associated by default, you have to manually set them from your headers.
     *
     * @return The response entity if available.
     */
    public Representation getResponseEntity() {
        Representation result = null;

        if (getResponseStream() != null) {
            result = new InputRepresentation(getResponseStream(), null);
        } else if (getResponseChannel() != null) {
            result = new ReadableRepresentation(getResponseChannel(), null);
        } else if (getMethod().equals(Method.HEAD.getName())) {
            result = new Representation() {
                @Override
                public ReadableByteChannel getChannel() throws IOException {
                    return null;
                }

                @Override
                public InputStream getStream() throws IOException {
                    return null;
                }

                @Override
                public void write(OutputStream outputStream) throws IOException {
                    // Do nothing
                }

                @Override
                public void write(WritableByteChannel writableChannel)
                        throws IOException {
                    // Do nothing
                }
            };
        }

        if (result != null) {
            for (Parameter header : getResponseHeaders()) {
                if (header.getName().equalsIgnoreCase(
                        HttpConstants.HEADER_CONTENT_TYPE)) {
                    ContentType contentType = new ContentType(header.getValue());
                    if (contentType != null) {
                        result.setMediaType(contentType.getMediaType());
                        result.setCharacterSet(contentType.getCharacterSet());
                    }
                } else if (header.getName().equalsIgnoreCase(
                        HttpConstants.HEADER_CONTENT_LENGTH)) {
                    result.setSize(Long.parseLong(header.getValue()));
                } else if (header.getName().equalsIgnoreCase(
                        HttpConstants.HEADER_EXPIRES)) {
                    result
                            .setExpirationDate(parseDate(header.getValue(),
                                    false));
                } else if (header.getName().equalsIgnoreCase(
                        HttpConstants.HEADER_CONTENT_ENCODING)) {
                    HeaderReader hr = new HeaderReader(header.getValue());
                    String value = hr.readValue();
                    while (value != null) {
                        Encoding encoding = new Encoding(value);
                        if (!encoding.equals(Encoding.IDENTITY)) {
                            result.getEncodings().add(encoding);
                        }
                        value = hr.readValue();
                    }
                } else if (header.getName().equalsIgnoreCase(
                        HttpConstants.HEADER_CONTENT_LANGUAGE)) {
                    HeaderReader hr = new HeaderReader(header.getValue());
                    String value = hr.readValue();
                    while (value != null) {
                        result.getLanguages().add(new Language(value));
                        value = hr.readValue();
                    }
                } else if (header.getName().equalsIgnoreCase(
                        HttpConstants.HEADER_LAST_MODIFIED)) {
                    result.setModificationDate(parseDate(header.getValue(),
                            false));
                } else if (header.getName().equalsIgnoreCase(
                        HttpConstants.HEADER_ETAG)) {
                    result.setTag(Tag.parse(header.getValue()));
                } else if (header.getName().equalsIgnoreCase(
                        HttpConstants.HEADER_CONTENT_LOCATION)) {
                    result.setIdentifier(header.getValue());
                }
            }
        }

        return result;
View Full Code Here

     *            The representation to encode.
     * @return The decoded representation or the original one if the encoding
     *         isn't supported by NRE.
     */
    public Representation decode(Representation representation) {
        Representation result = representation;

        // Check if all encodings of the representation are supported in order
        // to avoid the creation of a useless decodeRepresentation object.
        // False if an encoding is not supported
        boolean supported = true;
View Full Code Here

    }

    @Override
    public Representation getRepresentation(MediaType defaultMediaType,
            int timeToLive) {
        Representation result = null;

        InputStream ris = getServletContext().getResourceAsStream(path);
        if (ris != null) {
            result = new InputRepresentation(ris, defaultMediaType);
            // Sets the modification date
            String realPath = getServletContext().getRealPath(path);
            if (realPath != null) {
                File file = new File(realPath);
                if (file != null) {
                    result.setModificationDate(new Date(file.lastModified()));
                }
            }
        }
        return result;
    }
View Full Code Here

        || request.getMethod().equals(Method.HEAD)) {
      String basePath = request.getResourceRef().getPath();
      int lastSlashIndex = basePath.lastIndexOf('/');
      String entry = (lastSlashIndex == -1) ? basePath : basePath
          .substring(lastSlashIndex + 1);
      Representation output = null;

      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);
        if (ris != null) {
          output = new InputRepresentation(ris, metadataService
              .getDefaultMediaType());
          output.setIdentifier(request.getResourceRef());
          updateMetadata(metadataService, entry, output);

          // See if the Servlet context specified a particular Mime
          // Type
          String mediaType = getServletContext()
              .getMimeType(basePath);

          if (mediaType != null) {
            output.setMediaType(new MediaType(mediaType));
          }
        }
      }

      response.setEntity(output);
View Full Code Here

TOP

Related Classes of org.restlet.resource.Representation

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.