1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.webapp;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.apache.myfaces.tobago.internal.webapp.TobagoMultipartFormdataRequest;
25
26 import javax.servlet.Filter;
27 import javax.servlet.FilterChain;
28 import javax.servlet.FilterConfig;
29 import javax.servlet.ServletException;
30 import javax.servlet.ServletRequest;
31 import javax.servlet.ServletResponse;
32 import javax.servlet.http.HttpServletRequest;
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.Locale;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class TobagoMultipartFormdataFilter implements Filter {
70
71 private static final Logger LOG = LoggerFactory.getLogger(TobagoMultipartFormdataFilter.class);
72
73 private String repositoryPath = System.getProperty("java.io.tmpdir");
74 private long maxSize = TobagoMultipartFormdataRequest.ONE_MB;
75
76 public void init(FilterConfig filterConfig) throws ServletException {
77 String repositoryPath = filterConfig.getInitParameter("uploadRepositoryPath");
78 if (repositoryPath != null) {
79 File file = new File(repositoryPath);
80 if (!file.exists()) {
81 LOG.error("Given repository Path for " + getClass().getName() + " " + repositoryPath + " doesn't exists");
82 } else if (!file.isDirectory()) {
83 LOG.error("Given repository Path for " + getClass().getName() + " " + repositoryPath + " is not a directory");
84 } else {
85 this.repositoryPath = repositoryPath;
86 }
87 }
88
89
90 maxSize = TobagoMultipartFormdataRequest.getMaxSize(filterConfig.getInitParameter("uploadMaxFileSize"));
91 if (LOG.isInfoEnabled()) {
92 LOG.info("Configure uploadRepositryPath for " + getClass().getName() + " to " + this.repositoryPath);
93 LOG.info("Configure uploadMaxFileSize for " + getClass().getName() + " to " + this.maxSize);
94 }
95
96 }
97
98 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
99 throws IOException, ServletException {
100 ServletRequest wrapper;
101 if (request instanceof HttpServletRequest) {
102 if (request instanceof TobagoMultipartFormdataRequest) {
103 wrapper = request;
104 } else {
105 String contentType = request.getContentType();
106 if (contentType != null
107 && contentType.toLowerCase(Locale.ENGLISH).startsWith("multipart/form-data")) {
108 if (LOG.isDebugEnabled()) {
109 LOG.debug("Wrapping " + request.getClass().getName()
110 + " with ContentType=\"" + contentType + "\" "
111 + "into TobagoMultipartFormdataRequest");
112 }
113 wrapper = new TobagoMultipartFormdataRequest(
114 (HttpServletRequest) request, repositoryPath, maxSize);
115 } else {
116 wrapper = request;
117 }
118 }
119 } else {
120 LOG.error("Not implemented for non HttpServletRequest");
121 wrapper = request;
122 }
123
124 chain.doFilter(wrapper, response);
125 }
126
127 public void destroy() {
128 }
129
130 }