Skip to content

Commit

Permalink
More convenient serving of static files.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Dec 13, 2015
1 parent 1cf06b0 commit 6eb1546
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
9 changes: 9 additions & 0 deletions rapidoid-ctx/src/main/java/org/rapidoid/http/Response.java
Expand Up @@ -20,6 +20,7 @@
* #L% * #L%
*/ */


import java.io.File;
import java.util.Map; import java.util.Map;


import org.rapidoid.commons.MediaType; import org.rapidoid.commons.MediaType;
Expand All @@ -46,6 +47,14 @@ public interface Response {


String redirect(); String redirect();


Response filename(String filename);

String filename();

Response file(File file);

File file();

Response view(String view); Response view(String view);


String view(); String view();
Expand Down
Expand Up @@ -20,6 +20,7 @@
* #L% * #L%
*/ */


import java.io.File;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
Expand All @@ -37,19 +38,23 @@ public class HttpResponse implements Response {


private final ReqImpl req; private final ReqImpl req;


private Object content = null; private volatile Object content = null;


private int code = 200; private volatile int code = 200;


private MediaType contentType = MediaType.HTML_UTF_8; private volatile MediaType contentType = MediaType.HTML_UTF_8;


private final Map<String, String> headers = Collections.synchronizedMap(new HashMap<String, String>()); private final Map<String, String> headers = Collections.synchronizedMap(new HashMap<String, String>());


private final Map<String, String> cookies = Collections.synchronizedMap(new HashMap<String, String>()); private final Map<String, String> cookies = Collections.synchronizedMap(new HashMap<String, String>());


private String redirect = null; private volatile String redirect = null;


private String view = null; private volatile String filename = null;

private volatile File file = null;

private volatile String view = null;


public HttpResponse(ReqImpl req) { public HttpResponse(ReqImpl req) {
this.req = req; this.req = req;
Expand Down Expand Up @@ -113,6 +118,30 @@ public synchronized String redirect() {
return this.redirect; return this.redirect;
} }


@Override
public synchronized Response filename(String filename) {
ensureCanChange();
this.filename = filename;
return this;
}

@Override
public synchronized String filename() {
return this.filename;
}

@Override
public synchronized Response file(File file) {
ensureCanChange();
this.file = file;
return this;
}

@Override
public synchronized File file() {
return this.file;
}

private void ensureCanChange() { private void ensureCanChange() {
U.must(!req.isDone(), "The request was already processed, so the response can't be changed now!"); U.must(!req.isDone(), "The request was already processed, so the response can't be changed now!");
U.must(!req.isRendering(), "The response rendering has already started, so the response can't be changed now!"); U.must(!req.isRendering(), "The response rendering has already started, so the response can't be changed now!");
Expand Down
Expand Up @@ -144,18 +144,18 @@ public static String renderState(Map<String, Serializable> pageLocals) {
} }
} }


public static void setContentTypeForFile(Req req, File file) { public static void setContentTypeForFile(Response resp, File file) {
U.must(file.exists()); U.must(file.exists());
setContentType(req, MediaType.getByFileName(file.getAbsolutePath())); setContentType(resp, MediaType.getByFileName(file.getAbsolutePath()));
} }


public static void setContentTypeForResource(Req req, Res resource) { public static void setContentTypeForResource(Response resp, Res resource) {
U.must(resource.exists()); U.must(resource.exists());
setContentType(req, MediaType.getByFileName(resource.getShortName())); setContentType(resp, MediaType.getByFileName(resource.getShortName()));
} }


public static void setContentType(Req req, MediaType mediaType) { public static void setContentType(Response resp, MediaType mediaType) {
req.response().contentType(mediaType); resp.contentType(mediaType);
} }


public static void setCookie(Req req, String name, String value, String... extras) { public static void setCookie(Req req, String name, String value, String... extras) {
Expand Down Expand Up @@ -209,6 +209,31 @@ public static String getErrorMessage(Throwable err) {


public static void postProcessResponse(Response resp) { public static void postProcessResponse(Response resp) {
postProcessRedirect(resp); postProcessRedirect(resp);
postProcessFile(resp);
postProcessFilename(resp);
}

private static void postProcessFile(Response resp) {
File file = resp.file();
if (file != null) {
U.must(file.exists());

if (resp.filename() == null) {
resp.filename();
}

setContentTypeForFile(resp, file);

resp.content(Res.from(file.getAbsolutePath()).getBytes());
}
}

private static void postProcessFilename(Response resp) {
String filename = resp.filename();
if (filename != null) {
resp.headers().put(HttpHeaders.CONTENT_DISPOSITION.name(), "attachment; filename=\"" + filename + "\"");
resp.headers().put(HttpHeaders.CACHE_CONTROL.name(), "private");
}
} }


private static void postProcessRedirect(Response resp) { private static void postProcessRedirect(Response resp) {
Expand Down

0 comments on commit 6eb1546

Please sign in to comment.