01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.http.server.repository;
07:
08: import java.io.IOException;
09: import java.nio.charset.Charset;
10: import java.util.Map;
11:
12: import javax.servlet.http.HttpServletResponse;
13:
14: import org.springframework.web.servlet.View;
15:
16: import info.aduna.lang.FileFormat;
17:
18: /**
19: * Base class for rendering query results.
20: *
21: * @author Herko ter Horst
22: * @author Arjohn Kampman
23: */
24: public abstract class QueryResultView implements View {
25:
26: /**
27: * Key by which the query result is stored in the model.
28: */
29: public static final String QUERY_RESULT_KEY = "queryResult";
30:
31: /**
32: * Key by which the query result writer factory is stored in the model.
33: */
34: public static final String FACTORY_KEY = "factory";
35:
36: /**
37: * Key by which a filename hint is stored in the model. The filename hint may
38: * be used to present the client with a suggestion for a filename to use for
39: * storing the result.
40: */
41: public static final String FILENAME_HINT_KEY = "filenameHint";
42:
43: protected void setContentType(HttpServletResponse response,
44: FileFormat fileFormat) throws IOException {
45: String mimeType = fileFormat.getDefaultMIMEType();
46: if (fileFormat.hasCharset()) {
47: Charset charset = fileFormat.getCharset();
48: mimeType += "; charset=" + charset.name();
49: }
50: response.setContentType(mimeType);
51: }
52:
53: @SuppressWarnings("unchecked")
54: protected void setContentDisposition(Map model,
55: HttpServletResponse response, FileFormat fileFormat)
56: throws IOException {
57: // Report as attachment to make use in browser more convenient
58: String filename = (String) model.get(FILENAME_HINT_KEY);
59:
60: if (filename == null || filename.length() == 0) {
61: filename = "result";
62: }
63:
64: if (fileFormat.getDefaultFileExtension() != null) {
65: filename += "." + fileFormat.getDefaultFileExtension();
66: }
67:
68: response.setHeader("Content-Disposition",
69: "attachment; filename=" + filename);
70: }
71: }
|