Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@

package org.restlet.ext.servlet.internal;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -179,18 +180,27 @@ public Entity getParent() {
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()));

try {
URL resource = getServletContext().getResource(path);
if (resource != null) {
URLConnection connection = resource.openConnection();
result = new InputRepresentation(connection.getInputStream(), defaultMediaType);

// Sets the modification date
result.setModificationDate(new Date(connection.getLastModified()));

// Sets the expiration date
if (timeToLive == 0) {
result.setExpirationDate(null);
} else if (timeToLive > 0) {
result.setExpirationDate(new Date(System.currentTimeMillis()
+ (1000L * timeToLive)));
}
}
} catch (IOException e) {
Context.getCurrentLogger().log(Level.WARNING,
"Error getting the WAR resource.", e);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public Entity getParent() {
@Override
public Representation getRepresentation(MediaType defaultMediaType,
int timeToLive) {
return new ZipEntryRepresentation(defaultMediaType, zipFile, entry);
return new ZipEntryRepresentation(defaultMediaType, zipFile, entry,
timeToLive);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,27 @@ public class ZipEntryRepresentation extends StreamRepresentation {
* The parent Zip archive file.
* @param entry
* The Zip entry.
* @deprecated Use {@link #ZipEntryRepresentation(MediaType, ZipFile, ZipEntry, int)} instead.
*/
public ZipEntryRepresentation(MediaType mediaType, ZipFile zipFile,
ZipEntry entry) {
this(mediaType, zipFile, entry, -1);
}

/**
* Constructor.
*
* @param mediaType
* The entry media type.
* @param zipFile
* The parent Zip archive file.
* @param entry
* The Zip entry.
* @param timeToLive
* The time to live before it expires (in seconds).
*/
public ZipEntryRepresentation(MediaType mediaType, ZipFile zipFile,
ZipEntry entry, int timeToLive) {
super(mediaType);
this.zipFile = zipFile;
this.entry = entry;
Expand All @@ -81,6 +99,13 @@ public ZipEntryRepresentation(MediaType mediaType, ZipFile zipFile,
this.setDisposition(disposition);
setSize(entry.getSize());
setModificationDate(new Date(entry.getTime()));

if (timeToLive == 0) {
setExpirationDate(null);
} else if (timeToLive > 0) {
setExpirationDate(new Date(System.currentTimeMillis()
+ (1000L * timeToLive)));
}
}

@Override
Expand Down