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

com.hfg.webapp.HfgHttpServletRequest Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.webapp;


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.URLDecoder;
import java.security.Principal;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.hfg.util.StringUtil;
import com.hfg.util.collection.OrderedMap;
import com.hfg.util.io.RuntimeIOException;
import com.hfg.util.mime.MimeEntity;
import com.hfg.util.mime.MimeType;
import com.hfg.util.mime.MultipartMimeParser;

//------------------------------------------------------------------------------
/**
 Wrapper for the standard HttpServletRequest that adds support for multi-part mime uploads.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class HfgHttpServletRequest implements HttpServletRequest { private HttpServletRequest mRequest; private File mTmpDir; private File mTmpDirRoot = sDefaultTmpDirRoot; private Map mParameterMap; private static File sDefaultTmpDirRoot = new File("/tmp"); //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public HfgHttpServletRequest(HttpServletRequest inRequest) throws IOException { mRequest = inRequest; init(); } //--------------------------------------------------------------------------- private void init() throws IOException { mParameterMap = new OrderedMap<>(50); // GET params? if (StringUtil.isSet(mRequest.getQueryString())) { extractParamsFromQueryString(mRequest.getQueryString()); } // POST params? if (mRequest.getMethod().equalsIgnoreCase("POST")) { if (null == mRequest.getContentType() || mRequest.getContentType().startsWith(MimeType.APPLICATION_WWW_FORM_URLENCODED.toString())) { for (String key : mRequest.getParameterMap().keySet()) { setParameter(key, mRequest.getParameterValues(key)); } } else if (mRequest.getContentType().startsWith(MimeType.MULTIPART_FORM_DATA.toString())) { try { // Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryqLdKEwR9tfg86rj1 if (MultipartMimeParser.isMultipartMimeRequest(mRequest)) { MultipartMimeParser multipartMimeParser = new MultipartMimeParser().setFileCacheDir(getTmpDir()); List mimeEntities = multipartMimeParser.parse(mRequest); for (MimeEntity mimeEntity : mimeEntities) { if (mimeEntity.getCachedContentFile() != null) { addParameter(mimeEntity.getContentDisposition().getName(), mimeEntity.getCachedContentFile().getPath()); } else { addParameter(mimeEntity.getContentDisposition().getName(), mimeEntity.getContent()); } } } } catch (SocketTimeoutException e) { throw e; // This isn't our fault } catch (IOException e) { throw new RuntimeIOException("Problem extracting multi-part mime data!", e); } } } } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public static void setDefaultTmpDirRoot(File inDir) { sDefaultTmpDirRoot = inDir; } //--------------------------------------------------------------------------- public static File getDefaultTmpDirRoot() { return sDefaultTmpDirRoot; } //--------------------------------------------------------------------------- public void setTmpDirRoot(File inDir) { mTmpDirRoot = inDir; } //--------------------------------------------------------------------------- public File getTmpDirRoot() { return mTmpDirRoot; } //--------------------------------------------------------------------------- public String[] removeParameter(String inName) { String[] result = null; if (mParameterMap != null) { result = mParameterMap.remove(inName); } return result; } //--------------------------------------------------------------------------- public File getFileParameter(String inName) { File file = null; String filename = getParameter(inName); if (StringUtil.isSet(filename)) { file = new File(filename); } return file; } //--------------------------------------------------------------------------- public HfgHttpServletRequest setParameter(String inName, String[] inValues) { if (null == mParameterMap) { mParameterMap = new OrderedMap<>(); } mParameterMap.put(inName, inValues); return this; } //--------------------------------------------------------------------------- public HfgHttpServletRequest addParameter(String inName, String inValue) { String[] existingValues = getParameterValues(inName); String[] newValues = new String[existingValues != null ? existingValues.length + 1 : 1]; if (existingValues != null) { System.arraycopy(existingValues, 0, newValues, 0, existingValues.length); } newValues[newValues.length - 1] = inValue; setParameter(inName, newValues); return this; } //--------------------------------------------------------------------------- @Override public String getAuthType() { return mRequest.getAuthType(); } @Override public Cookie[] getCookies() { return mRequest.getCookies(); } @Override public long getDateHeader(String s) { return mRequest.getDateHeader(s); } @Override public String getHeader(String s) { return mRequest.getHeader(s); } @Override public Enumeration getHeaders(String s) { return mRequest.getHeaders(s); } @Override public Enumeration getHeaderNames() { return mRequest.getHeaderNames(); } @Override public int getIntHeader(String s) { return mRequest.getIntHeader(s); } @Override public String getMethod() { return mRequest.getMethod(); } @Override public String getPathInfo() { return mRequest.getPathInfo(); } @Override public String getPathTranslated() { return mRequest.getPathTranslated(); } @Override public String getContextPath() { return mRequest.getContextPath(); } @Override public String getQueryString() { return mRequest.getQueryString(); } @Override public String getRemoteUser() { return mRequest.getRemoteUser(); } @Override public boolean isUserInRole(String s) { return mRequest.isUserInRole(s); } @Override public Principal getUserPrincipal() { return mRequest.getUserPrincipal(); } @Override public String getRequestedSessionId() { return mRequest.getRequestedSessionId(); } @Override public String getRequestURI() { return mRequest.getRequestURI(); } @Override public StringBuffer getRequestURL() { return mRequest.getRequestURL(); } @Override public String getServletPath() { return mRequest.getServletPath(); } @Override public HttpSession getSession(boolean b) { return mRequest.getSession(b); } @Override public HttpSession getSession() { return mRequest.getSession(); } @Override public boolean isRequestedSessionIdValid() { return mRequest.isRequestedSessionIdValid(); } @Override public boolean isRequestedSessionIdFromCookie() { return mRequest.isRequestedSessionIdFromCookie(); } @Override public boolean isRequestedSessionIdFromURL() { return mRequest.isRequestedSessionIdFromURL(); } @Override @SuppressWarnings("deprecation") public boolean isRequestedSessionIdFromUrl() { return mRequest.isRequestedSessionIdFromUrl(); } @Override public boolean authenticate(HttpServletResponse httpServletResponse) throws IOException, ServletException { return mRequest.authenticate(httpServletResponse); } @Override public void login(String s, String s1) throws ServletException { mRequest.login(s, s1); } @Override public void logout() throws ServletException { mRequest.logout(); } @Override public Collection getParts() throws IOException, ServletException { return mRequest.getParts(); } @Override public Part getPart(String s) throws IOException, ServletException { return mRequest.getPart(s); } @Override public Object getAttribute(String s) { return mRequest.getAttribute(s); } @Override public Enumeration getAttributeNames() { return mRequest.getAttributeNames(); } @Override public String getCharacterEncoding() { return mRequest.getCharacterEncoding(); } @Override public void setCharacterEncoding(String s) throws UnsupportedEncodingException { mRequest.setCharacterEncoding(s); } @Override public int getContentLength() { return mRequest.getContentLength(); } @Override public String getContentType() { return mRequest.getContentType(); } @Override public ServletInputStream getInputStream() throws IOException { return mRequest.getInputStream(); } @Override public String getParameter(String s) { String[] values = mParameterMap.get(s); return (values != null ? values[0] : null); } @Override public Enumeration getParameterNames() { return mRequest.getParameterNames(); } @Override public String[] getParameterValues(String s) { return mParameterMap.get(s); } @Override public Map getParameterMap() { return mParameterMap != null ? Collections.unmodifiableMap(mParameterMap) : null; } @Override public String getProtocol() { return mRequest.getProtocol(); } @Override public String getScheme() { return mRequest.getScheme(); } @Override public String getServerName() { return mRequest.getServerName(); } @Override public int getServerPort() { return mRequest.getServerPort(); } @Override public BufferedReader getReader() throws IOException { return mRequest.getReader(); } @Override public String getRemoteAddr() { return mRequest.getRemoteAddr(); } @Override public String getRemoteHost() { return mRequest.getRemoteHost(); } @Override public void setAttribute(String s, Object o) { mRequest.setAttribute(s, o); } @Override public void removeAttribute(String s) { mRequest.removeAttribute(s); } @Override public Locale getLocale() { return mRequest.getLocale(); } @Override public Enumeration getLocales() { return mRequest.getLocales(); } @Override public boolean isSecure() { return mRequest.isSecure(); } @Override public RequestDispatcher getRequestDispatcher(String s) { return mRequest.getRequestDispatcher(s); } @Override @SuppressWarnings("deprecation") public String getRealPath(String s) { return mRequest.getRealPath(s); } @Override public int getRemotePort() { return mRequest.getRemotePort(); } @Override public String getLocalName() { return mRequest.getLocalName(); } @Override public String getLocalAddr() { return mRequest.getLocalAddr(); } @Override public int getLocalPort() { return mRequest.getLocalPort(); } @Override public ServletContext getServletContext() { return mRequest.getServletContext(); } @Override public AsyncContext startAsync() throws IllegalStateException { return mRequest.startAsync(); } @Override public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException { return mRequest.startAsync(servletRequest, servletResponse); } @Override public boolean isAsyncStarted() { return mRequest.isAsyncStarted(); } @Override public boolean isAsyncSupported() { return mRequest.isAsyncSupported(); } @Override public AsyncContext getAsyncContext() { return mRequest.getAsyncContext(); } @Override public DispatcherType getDispatcherType() { return mRequest.getDispatcherType(); } //########################################################################### // PRIVATE METHODS //########################################################################### //--------------------------------------------------------------------------- private File getTmpDir() { if (null == mTmpDir) { mTmpDir = new File(getTmpDirRoot(), "tmp_request_" + System.currentTimeMillis()); } return mTmpDir; } //--------------------------------------------------------------------------- private void extractParamsFromQueryString(String inQueryString) { String fields[] = inQueryString.split("&"); try { for (String field : fields) { String pieces[] = field.split("="); addParameter(URLDecoder.decode(pieces[0], "UTF-8"), (pieces.length > 1 ? URLDecoder.decode(pieces[1], "UTF-8") : null)); } } catch (Exception e) { throw new RuntimeException("Problem during extraction of GET parameters!", e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy