001: /*
002: * @(#)readMultipartRequest.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.servlet;
010:
011: import pnuts.servlet.*;
012: import pnuts.lang.*;
013: import java.io.*;
014: import java.util.*;
015: import javax.servlet.*;
016:
017: /*
018: * readMultipartRequest( { request, } handler(mime, name, filename, ctype))
019: */
020: public class readMultipartRequest extends PnutsFunction {
021:
022: public readMultipartRequest() {
023: super ("readMultipartRequest");
024: }
025:
026: public boolean defined(int narg) {
027: return (narg == 1 || narg == 2);
028: }
029:
030: protected Object exec(Object[] args, Context context) {
031: int nargs = args.length;
032: ServletRequest request;
033: PnutsFunction handler;
034: if (nargs == 1) {
035: request = (ServletRequest) context
036: .get(PnutsServlet.SERVLET_REQUEST);
037: handler = (PnutsFunction) args[0];
038: } else if (nargs == 2) {
039: request = (ServletRequest) args[0];
040: handler = (PnutsFunction) args[1];
041: } else {
042: undefined(args, context);
043: return null;
044: }
045: String type = request.getContentType();
046: String name = null;
047: int idx = type.lastIndexOf("boundary=");
048: if (idx < 0) {
049: return null;
050: }
051: byte[] boundary = type.substring(idx + 9).getBytes();
052:
053: try {
054: MultipartInputStream multi = new MultipartInputStream(
055: request.getInputStream(), boundary);
056: while (multi.next()) {
057: MimeInputStream mime = new MimeInputStream(multi);
058: String cdisp = mime.getHeader("content-disposition");
059: String ctype = mime.getHeader("content-type");
060:
061: String filename = null;
062: StringTokenizer st = new StringTokenizer(cdisp, ";");
063: while (st.hasMoreTokens()) {
064: String token = st.nextToken();
065: idx = token.indexOf('=');
066: if (idx > 0) {
067: String key = token.substring(0, idx).trim();
068: if ("filename".equals(key)) {
069: String value = token.substring(idx + 1)
070: .trim();
071: filename = value;
072: int len = value.length();
073: if (len > 1 && value.charAt(0) == '"'
074: && value.charAt(len - 1) == '"') {
075: filename = value.substring(1, len - 1);
076: }
077: } else if ("name".equals(key)) {
078: String value = token.substring(idx + 1)
079: .trim();
080: name = value;
081: int len = value.length();
082: if (len > 1 && value.charAt(0) == '"'
083: && value.charAt(len - 1) == '"') {
084: name = value.substring(1, len - 1);
085: }
086: }
087: }
088: }
089: if (filename != null) {
090: handler.call(new Object[] { mime, name, filename,
091: ctype }, context);
092: } else {
093: Hashtable param = (Hashtable) context
094: .get(PnutsServlet.SERVLET_MULTIPART_PARAM);
095: if (param == null) {
096: param = new Hashtable();
097: context.set(
098: PnutsServlet.SERVLET_MULTIPART_PARAM,
099: param);
100: }
101: ByteArrayOutputStream ba = new ByteArrayOutputStream();
102: byte[] buf = new byte[512];
103: int n;
104: while ((n = mime.read(buf)) != -1) {
105: ba.write(buf, 0, n);
106: }
107: Vector val = (Vector) param.get(name);
108: byte[] barray = ba.toByteArray();
109: if (val == null) {
110: param.put(name, new Object[] { barray });
111: } else {
112: val.addElement(barray);
113: }
114: }
115: }
116: return null;
117: } catch (IOException e) {
118: throw new PnutsException(e, context);
119: }
120: }
121:
122: public String toString() {
123: return "function readMultipartRequest( { request, } handler(mime, name, filename, ctype))";
124: }
125: }
|