1
2
3
4
5 package oscon2006.web;
6
7 import wicket.RequestCycle;
8 import wicket.markup.html.form.Form;
9 import wicket.protocol.http.WebResponse;
10 import wicket.request.target.basic.EmptyRequestTarget;
11 import java.io.*;
12
13 public abstract class BaseForm extends Form
14 {
15 private static final String DEFAULT_CONTENT_TYPE = "application/x-file-download";
16
17 public BaseForm(String id)
18 {
19 super(id);
20 }
21
22 public void setResponse(byte[] filedata, String filename)
23 {
24
25 RequestCycle.get().setRequestTarget(EmptyRequestTarget.getInstance());
26
27 WebResponse response = (WebResponse) getResponse();
28 response.setContentType(getContentType(filename));
29 response.setAttachmentHeader(filename);
30 response.setHeader("Cache-Control", "max-age=0");
31 OutputStream out = response.getOutputStream();
32
33 try
34 {
35 out.write(filedata);
36 out.flush();
37 }
38 catch (IOException ex)
39 {
40
41 }
42
43 }
44
45 private static String getContentType(String filename)
46 {
47 String contentType;
48
49 String lowercaseFilename = filename.toLowerCase();
50
51 if (lowercaseFilename.endsWith(".xls"))
52 {
53 contentType = "application/vnd.ms-excel";
54 }
55 else if (lowercaseFilename.endsWith(".mdb"))
56 {
57 contentType = "application/vnd.ms-access";
58 }
59 else if (lowercaseFilename.endsWith(".csv"))
60 {
61 contentType = "text/csv";
62 }
63 else if (lowercaseFilename.endsWith(".doc"))
64 {
65 contentType = "application/msword";
66 }
67 else if (lowercaseFilename.endsWith(".ppt"))
68 {
69 contentType = "application/mspowerpoint";
70 }
71 else if (lowercaseFilename.endsWith(".zip"))
72 {
73 contentType = "application/zip";
74 }
75 else if (lowercaseFilename.endsWith(".pdf"))
76 {
77 contentType = "application/pdf";
78 }
79 else if (lowercaseFilename.endsWith(".jpg"))
80 {
81 contentType = "image/jpeg";
82 }
83 else if (lowercaseFilename.endsWith(".png"))
84 {
85 contentType = "image/png";
86 }
87 else if (lowercaseFilename.endsWith(".gif"))
88 {
89 contentType = "image/gif";
90 }
91 else
92 {
93 contentType = DEFAULT_CONTENT_TYPE;
94 }
95
96 return contentType;
97
98 }
99
100 }