All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.browsermob.proxy.jetty.servlet.MultiPartRequest Maven / Gradle / Ivy

The newest version!
// ========================================================================
// $Id: MultiPartRequest.java,v 1.16 2005/12/02 20:13:52 gregwilkins Exp $
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at 
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================

package org.browsermob.proxy.jetty.servlet;

import org.apache.commons.logging.Log;
import org.browsermob.proxy.jetty.http.HttpFields;
import org.browsermob.proxy.jetty.log.LogFactory;
import org.browsermob.proxy.jetty.util.LineInput;
import org.browsermob.proxy.jetty.util.MultiMap;
import org.browsermob.proxy.jetty.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

/* ------------------------------------------------------------ */
/** Multipart Form Data request.
 * 

* This class decodes the multipart/form-data stream sent by * a HTML form that uses a file input item. * *

Usage

* Each part of the form data is named from the HTML form and * is available either via getString(name) or getInputStream(name). * Furthermore the MIME parameters and filename can be requested for * each part. *
 * 
* * @version $Id: MultiPartRequest.java,v 1.16 2005/12/02 20:13:52 gregwilkins Exp $ * @author Greg Wilkins * @author Jim Crossley */ public class MultiPartRequest { private static Log log = LogFactory.getLog(MultiPartRequest.class); /* ------------------------------------------------------------ */ HttpServletRequest _request; LineInput _in; String _boundary; String _encoding; byte[] _byteBoundary; MultiMap _partMap = new MultiMap(10); int _char=-2; boolean _lastPart=false; /* ------------------------------------------------------------ */ /** Constructor. * @param request The request containing a multipart/form-data * request * @exception IOException IOException */ public MultiPartRequest(HttpServletRequest request) throws IOException { _request=request; String content_type = request.getHeader(HttpFields.__ContentType); if (!content_type.startsWith("multipart/form-data")) throw new IOException("Not multipart/form-data request"); if(log.isDebugEnabled())log.debug("Multipart content type = "+content_type); _encoding = request.getCharacterEncoding(); if (_encoding != null) _in = new LineInput(request.getInputStream(), 2048, _encoding); else _in = new LineInput(request.getInputStream()); // Extract boundary string _boundary="--"+ value(content_type.substring(content_type.indexOf("boundary="))); if(log.isDebugEnabled())log.debug("Boundary="+_boundary); _byteBoundary= (_boundary+"--").getBytes(StringUtil.__ISO_8859_1); loadAllParts(); } /* ------------------------------------------------------------ */ /** Get the part names. * @return an array of part names */ public String[] getPartNames() { Set s = _partMap.keySet(); return (String[]) s.toArray(new String[s.size()]); } /* ------------------------------------------------------------ */ /** Check if a named part is present * @param name The part * @return true if it was included */ public boolean contains(String name) { Part part = (Part)_partMap.get(name); return (part!=null); } /* ------------------------------------------------------------ */ /** Get the data of a part as a string. * @param name The part name * @return The part data */ public String getString(String name) { List part = _partMap.getValues(name); if (part==null) return null; if (_encoding != null) { try { return new String(((Part)part.get(0))._data, _encoding); } catch (UnsupportedEncodingException uee) { if (log.isDebugEnabled())log.debug("Invalid character set: " + uee); return null; } } else return new String(((Part)part.get(0))._data); } /* ------------------------------------------------------------ */ /** * @param name The part name * @return The parts data */ public String[] getStrings(String name) { List parts = _partMap.getValues(name); if (parts==null) return null; String[] strings = new String[parts.size()]; if (_encoding == null) { for (int i=0; i0) value=value.substring(0,i); } return value; } /* ------------------------------------------------------------ */ private class Part { String _name=null; String _filename=null; Hashtable _headers= new Hashtable(10); byte[] _data=null; } };




© 2015 - 2024 Weber Informatics LLC | Privacy Policy