Skip to content

Commit 3ff6efb

Browse files
committed
Use Servlet API 3.0 HttpServletRequest.getPart to process form data
Fixes: #455 - Add @MultipartConfig annotation - use HttpServletRequest.getPart to get the form data
1 parent 98ee624 commit 3ff6efb

File tree

3 files changed

+56
-523
lines changed

3 files changed

+56
-523
lines changed

org/w3c/css/servlet/CssValidator.java

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.w3c.css.parser.CssError;
2323
import org.w3c.css.parser.Errors;
2424
import org.w3c.css.util.ApplContext;
25-
import org.w3c.css.util.Codecs;
2625
import org.w3c.css.util.CssVersion;
2726
import org.w3c.css.util.FakeFile;
2827
import org.w3c.css.util.HTTPURL;
@@ -35,10 +34,12 @@
3534
import javax.servlet.ServletConfig;
3635
import javax.servlet.ServletException;
3736
import javax.servlet.ServletInputStream;
37+
import javax.servlet.annotation.MultipartConfig;
3838
import javax.servlet.http.HttpServlet;
3939
import javax.servlet.http.HttpServletRequest;
4040
import javax.servlet.http.HttpServletResponse;
4141
import java.io.ByteArrayInputStream;
42+
import java.io.ByteArrayOutputStream;
4243
import java.io.IOException;
4344
import java.io.InputStream;
4445
import java.io.OutputStream;
@@ -54,6 +55,7 @@
5455
* @version $Revision$
5556
*/
5657
@SuppressWarnings("serial")
58+
@MultipartConfig
5759
public final class CssValidator extends HttpServlet {
5860

5961
final static String texthtml = "text/html";
@@ -529,13 +531,6 @@ public void doPost(HttpServletRequest req, HttpServletResponse res)
529531
String vendorExtensionAsWarnings = null;
530532
String inputType = "none";
531533

532-
ServletInputStream in = req.getInputStream();
533-
534-
byte[] buf = new byte[2048];
535-
byte[] general = new byte[65536];
536-
int count = 0;
537-
int len;
538-
539534
if (req.getParameter("debug") != null) {
540535
Util.onDebug = req.getParameter("debug").equals("true");
541536
if (Util.onDebug) {
@@ -555,71 +550,62 @@ public void doPost(HttpServletRequest req, HttpServletResponse res)
555550
}
556551

557552
try {
558-
while ((len = in.readLine(buf, 0, buf.length)) != -1) {
559-
if (len >= 2 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
560-
len -= 1;
561-
buf[len - 1] = (byte) '\n';
562-
}
563-
if (len != 0 && buf[len - 1] == '\r') {
564-
buf[len - 1] = (byte) '\n';
565-
}
553+
if (req.getPart(opt_file) != null) {
554+
file = new FakeFile(req.getPart(opt_file).getSubmittedFileName());
555+
file.setContentType(req.getPart(opt_file).getContentType());
566556

567-
if (general.length < (count + len)) {
568-
byte[] old = general;
569-
general = new byte[old.length * 2];
570-
System.arraycopy(old, 0, general, 0, old.length);
571-
}
572-
System.arraycopy(buf, 0, general, count, len);
573-
count += len;
574-
}
575-
} finally {
576-
in.close();
577-
}
557+
InputStream is2 = req.getPart(opt_file).getInputStream();
558+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
578559

579-
try {
580-
buf = new byte[count];
581-
System.arraycopy(general, 0, buf, 0, count);
582-
for (Pair<String, ?> pair : Codecs.mpFormDataDecode(buf, req.getContentType())) {
583-
switch (pair.getKey()) {
584-
case opt_file:
585-
file = (FakeFile) pair.getValue();
586-
break;
587-
case opt_text:
588-
text = (String) pair.getValue();
589-
break;
590-
case opt_textcharset:
591-
textcharset = (Charset) pair.getValue();
592-
break;
593-
case opt_lang:
594-
lang = (String) pair.getValue();
595-
break;
596-
case opt_output:
597-
output = (String) pair.getValue();
598-
break;
599-
case opt_warning:
600-
warning = (String) pair.getValue();
601-
break;
602-
case opt_error:
603-
error = (String) pair.getValue();
604-
break;
605-
case opt_profile:
606-
profile = (String) pair.getValue();
607-
break;
608-
case opt_usermedium:
609-
usermedium = (String) pair.getValue();
610-
break;
611-
case opt_vextwarning:
612-
vendorExtensionAsWarnings = (String) pair.getValue();
613-
break;
614-
case opt_type:
615-
inputType = (String) pair.getValue();
616-
break;
617-
default:
618-
// log extra parameters?
560+
int nRead;
561+
byte[] data = new byte[16384];
562+
563+
while ((nRead = is2.read(data, 0, data.length)) != -1) {
564+
buffer.write(data, 0, nRead);
619565
}
566+
567+
file.write(buffer.toByteArray(), 0, buffer.size());
620568
}
621-
} catch (Exception e) {
622-
System.out.println("Oups! Error in Util/Codecs.java?!?");
569+
570+
textcharset = Charset.forName(req.getCharacterEncoding());
571+
text = req.getPart(opt_text) != null ? new String(
572+
req.getPart(opt_text).getInputStream().readAllBytes(),
573+
Charset.forName(req.getCharacterEncoding())
574+
) : text;
575+
lang = req.getPart(opt_lang) != null ? new String(
576+
req.getPart(opt_lang).getInputStream().readAllBytes(),
577+
Charset.forName(req.getCharacterEncoding())
578+
) : lang;
579+
output = req.getPart(opt_output) != null ? new String(
580+
req.getPart(opt_output).getInputStream().readAllBytes(),
581+
Charset.forName(req.getCharacterEncoding())
582+
) : output;
583+
warning = req.getPart(opt_warning) != null ? new String(
584+
req.getPart(opt_warning).getInputStream().readAllBytes(),
585+
Charset.forName(req.getCharacterEncoding())
586+
) : warning;
587+
error = req.getPart(opt_error) != null ? new String(
588+
req.getPart(opt_error).getInputStream().readAllBytes(),
589+
Charset.forName(req.getCharacterEncoding())
590+
) : error;
591+
profile = req.getPart(opt_profile) != null ? new String(
592+
req.getPart(opt_profile).getInputStream().readAllBytes(),
593+
Charset.forName(req.getCharacterEncoding())
594+
) : profile;
595+
usermedium = req.getPart(opt_usermedium) != null ? new String(
596+
req.getPart(opt_usermedium).getInputStream().readAllBytes(),
597+
Charset.forName(req.getCharacterEncoding())
598+
) : usermedium;
599+
vendorExtensionAsWarnings = req.getPart(opt_vextwarning) != null ? new String(
600+
req.getPart(opt_vextwarning).getInputStream().readAllBytes(),
601+
Charset.forName(req.getCharacterEncoding())
602+
) : vendorExtensionAsWarnings;
603+
inputType = req.getPart(opt_type) != null ? new String(
604+
req.getPart(opt_type).getInputStream().readAllBytes(),
605+
Charset.forName(req.getCharacterEncoding())
606+
) : inputType;
607+
} catch (IOException e) {
608+
System.out.println("Error getting form data");
623609
e.printStackTrace();
624610
}
625611

0 commit comments

Comments
 (0)