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.fileupload;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.apache.myfaces.tobago.internal.util.JndiUtils;
25 import org.apache.myfaces.tobago.internal.webapp.TobagoMultipartFormdataRequest;
26
27 import javax.faces.FacesException;
28 import javax.faces.application.FacesMessage;
29 import javax.faces.context.FacesContext;
30 import javax.faces.context.FacesContextFactory;
31 import javax.faces.lifecycle.Lifecycle;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import javax.servlet.http.HttpServletRequest;
35 import java.io.File;
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 public class FileUploadFacesContextFactoryImpl extends FacesContextFactory {
65 private static final Logger LOG = LoggerFactory.getLogger(FileUploadFacesContextFactoryImpl.class);
66 private FacesContextFactory facesContextFactory;
67 private String repositoryPath = System.getProperty("java.io.tmpdir");
68 private long maxSize = TobagoMultipartFormdataRequest.ONE_MB;
69
70 public FileUploadFacesContextFactoryImpl(FacesContextFactory facesContextFactory) {
71
72 this.facesContextFactory = facesContextFactory;
73 if (LOG.isDebugEnabled()) {
74 LOG.debug("Wrap FacesContext for file upload");
75 }
76 InitialContext ic = null;
77 try {
78 ic = new InitialContext();
79
80 try {
81 String repositoryPath = (String) JndiUtils.getJndiProperty(ic, "uploadRepositoryPath");
82 if (repositoryPath != null) {
83 File file = new File(repositoryPath);
84 if (!file.exists()) {
85 LOG.error("Given repository Path for "
86 + getClass().getName() + " " + repositoryPath + " doesn't exists");
87 } else if (!file.isDirectory()) {
88 LOG.error("Given repository Path for "
89 + getClass().getName() + " " + repositoryPath + " is not a directory");
90 } else {
91 this.repositoryPath = repositoryPath;
92 }
93 }
94 } catch (NamingException ne) {
95
96 }
97
98 try {
99 String size = (String) JndiUtils.getJndiProperty(ic, "uploadMaxFileSize");
100 maxSize = TobagoMultipartFormdataRequest.getMaxSize(size);
101 } catch (NamingException ne) {
102
103 }
104 } catch (NamingException e) {
105
106 } finally {
107 if (ic != null) {
108 try {
109 ic.close();
110 } catch (NamingException e) {
111
112 }
113 }
114 }
115 if (LOG.isInfoEnabled()) {
116 LOG.info("Configure uploadMaxFileSize for "+ getClass().getName() + " to "+ this.maxSize);
117 LOG.info("Configure uploadRepositryPath for "+ getClass().getName() + " to "+ this.repositoryPath);
118 }
119 }
120
121 public FacesContext getFacesContext(Object context, Object request, Object response, Lifecycle lifecycle)
122 throws FacesException {
123 if (request instanceof HttpServletRequest && !(request instanceof TobagoMultipartFormdataRequest)) {
124 String contentType = ((HttpServletRequest) request).getContentType();
125 if (contentType != null && contentType.toLowerCase().startsWith("multipart/form-data")) {
126 if (LOG.isDebugEnabled()) {
127 LOG.debug("Wrap HttpServletRequest for file upload");
128 }
129 try {
130 request = new TobagoMultipartFormdataRequest((HttpServletRequest) request, repositoryPath, maxSize);
131 } catch (FacesException e) {
132 LOG.error("", e);
133 FacesContext facesContext = facesContextFactory.getFacesContext(context, request, response, lifecycle);
134
135 FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getCause().getMessage(), null);
136 facesContext.addMessage(null, facesMessage);
137 facesContext.renderResponse();
138 return facesContext;
139 }
140 }
141 }
142 return facesContextFactory.getFacesContext(context, request, response, lifecycle);
143 }
144 }