Skip to content

Commit 08fefcb

Browse files
committed
jfServlets : add HttpRequest.Part
1 parent 5d47793 commit 08fefcb

9 files changed

Lines changed: 110 additions & 16 deletions

File tree

projects/jfpbx/src/jfpbx/core/WebConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ private String doMsgsPage(String args[], WebRequest req) {
954954
if (WebUpload.isMultipartContent(req)) {
955955
String dirName = Paths.sounds + Paths.lang + "/";
956956
WebUpload webUpload = new WebUpload();
957-
WebUpload.WebFile files[] = webUpload.processRequest(req, dirName);
957+
WebFile[] files = webUpload.processRequest(req, dirName);
958958
JFLog.log("files=" + files.length);
959959
for(int a=0;a<files.length;a++) {
960960
String fileName = files[a].name;

projects/jfservlets/src/javax/servlet/http/HttpServletRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ public interface HttpServletRequest extends ServletRequest {
1212
public StringBuffer getRequestURL();
1313
public String getMethod();
1414
public HttpSession getSession();
15+
public Part getPart(String name);
1516
}

projects/jfservlets/src/javax/servlet/http/HttpServletRequestImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import javax.servlet.*;
1212

13+
import javaforce.service.*;
1314

1415
public class HttpServletRequestImpl implements HttpServletRequest {
1516
private HashMap<String, Object> req;
@@ -105,4 +106,15 @@ public String getMethod() {
105106
public HttpSession getSession() {
106107
return session;
107108
}
109+
110+
public Part getPart(String name) {
111+
WebFile[] files = (WebFile[])req.get("files");
112+
if (files == null) return null;
113+
for(WebFile file : files) {
114+
if (file.name.equalsIgnoreCase(name)) {
115+
return new PartImpl(file);
116+
}
117+
}
118+
return null;
119+
}
108120
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package javax.servlet.http;
2+
3+
/** HTTP Request Part
4+
*
5+
* @author pquiring
6+
*/
7+
8+
import java.io.*;
9+
10+
public interface Part {
11+
public String getContentType();
12+
public InputStream getInputStream();
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package javax.servlet.http;
2+
3+
/** HTTP Part Implementation
4+
*
5+
* @author pquiring
6+
*/
7+
8+
import java.io.*;
9+
10+
import javaforce.service.*;
11+
12+
public class PartImpl implements Part {
13+
14+
private WebFile file;
15+
16+
public PartImpl(WebFile file) {
17+
this.file = file;
18+
}
19+
20+
public String getContentType() {
21+
return file.getContentType();
22+
}
23+
24+
public InputStream getInputStream() {
25+
return file.getInputStream();
26+
}
27+
}

src/javaforce/service/WebFile.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package javaforce.service;
2+
3+
/** Web File uploaded to server.
4+
*
5+
* @author pquiring
6+
*/
7+
8+
import java.io.*;
9+
10+
import javaforce.*;
11+
12+
public class WebFile {
13+
private FileInputStream is;
14+
/** Uploaded file. */
15+
public File file;
16+
/** Name of file (excluding any path info) */
17+
public String name;
18+
/** Content Type. */
19+
public String contentType;
20+
/** Moves uploaded file to dest file. (optional) */
21+
public boolean move(File dest) throws Exception {
22+
if (dest.exists()) return false;
23+
return JF.moveFile(file, dest);
24+
}
25+
public String getName() {return name;}
26+
public String getContentType() {return contentType;}
27+
public InputStream getInputStream() {
28+
if (is == null) {
29+
try {
30+
is = new FileInputStream(file);
31+
} catch (Exception e) {
32+
JFLog.log(e);
33+
}
34+
}
35+
return is;
36+
}
37+
}

src/javaforce/service/WebRequest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class WebRequest {
2222
public int serverPort, remotePort;
2323
public String method;
2424
public HTTP.Parameters params;
25+
public WebFile[] files;
2526
public boolean secure;
2627

2728
public static class Session {
@@ -148,6 +149,16 @@ public String getParameter(String name) {
148149
return params.get(name);
149150
}
150151

152+
public WebFile getPart(String name) {
153+
if (files == null) return null;
154+
for(WebFile file : files) {
155+
if (file.name.equalsIgnoreCase(name)) {
156+
return file;
157+
}
158+
}
159+
return null;
160+
}
161+
151162
/** Reads and returns POST data. */
152163
public byte[] getData() {
153164
String method = getMethod();
@@ -180,6 +191,7 @@ public HashMap<String, Object> toHashMap() {
180191
map.put("URL", getURL());
181192
map.put("Method", getMethod());
182193
map.put("session", session.props);
194+
map.put("files", files);
183195
return map;
184196
}
185197
};

src/javaforce/service/WebServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ else if (WebUpload.isMultipartContent(req)) {
161161
res.setStatus(501, "Error - Uploads disabled");
162162
} else {
163163
WebUpload upload = new WebUpload();
164-
upload.processRequest(req, upload_folder);
164+
req.files = upload.processRequest(req, upload_folder);
165165
}
166166
}
167167
else if (req.fields0[0].equals("GET")) {

src/javaforce/service/WebUpload.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ public static boolean isMultipartContent(WebRequest req) {
2727
return (contentType.trim().startsWith("multipart/form-data;"));
2828
}
2929

30-
/** Uploaded file. */
31-
public static class WebFile {
32-
/** Uploaded file. */
33-
public File file;
34-
/** Name of file (excluding any path info) */
35-
public String name;
36-
/** Moves uploaded file to dest file. (optional) */
37-
public boolean move(File dest) throws Exception {
38-
if (dest.exists()) return false;
39-
return JF.moveFile(file, dest);
40-
}
41-
public String getName() {return name;}
42-
}
43-
4430
private static long maxlength = 64 * JF.MB; //64MBs
4531
/** Sets max file upload (-1 = unlimited) (default = 64MBs) */
4632
public static void setMaxLength(long maxlength) {
@@ -60,6 +46,7 @@ public static void setMaxLength(long maxlength) {
6046
public WebFile[] processRequest(WebRequest req, String out_folder) {
6147
String file_size = null;
6248
Status status = null;
49+
String file_content_type = null;
6350
OutputStream fos = null;
6451
try {
6552
//Content-Type: multipart/form-data; boundary=----WebKitFormBoundary...
@@ -195,6 +182,10 @@ public WebFile[] processRequest(WebRequest req, String out_folder) {
195182
file_size = p_value;
196183
break;
197184
}
185+
case "Content-Type": {
186+
file_content_type = p_value;
187+
break;
188+
}
198189
}
199190
}
200191
int type = 0;
@@ -221,6 +212,7 @@ public WebFile[] processRequest(WebRequest req, String out_folder) {
221212
WebFile uploadFile = new WebFile();
222213
uploadFile.name = cd_filename;
223214
uploadFile.file = new File(out_folder + "/" + cd_filename);
215+
uploadFile.contentType = file_content_type;
224216
files.add(uploadFile);
225217
fos = new FileOutputStream(uploadFile.file);
226218
if (status != null) {

0 commit comments

Comments
 (0)