com.hfg.webapp.WebappUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.webapp;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import com.hfg.javascript.JsCollection;
import com.hfg.util.StringUtil;
import com.hfg.util.mime.MimeType;
import com.hfg.xml.XMLDoc;
//------------------------------------------------------------------------------
/**
Webapp-related static functions.
@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 WebappUtil
{
//---------------------------------------------------------------------------
public static void setupResponseForFileDownload(HttpServletResponse inResponse, MimeType inMimeType, File inFile)
throws Exception
{
URI filenameURI = new URI(null, null, inFile.getName(), null);
inResponse.setContentType(inMimeType.toString());
String disposition = "attachment; filename=\"" + filenameURI.toString() + "\"";
inResponse.setHeader("Content-disposition", disposition);
}
//---------------------------------------------------------------------------
/**
Encodes the cookie value according to section 4.1.1 of RFC 6265.
(See http://www.rfc-editor.org/rfc/rfc6265.txt)
Note that the cookie-value is limited to US-ASCII but this implementation will include
characters above ASCII without escaping them.
*/
public static String encodeCookieValue(String inUnencodedValue)
{
StringBuilder encodedValue = new StringBuilder();
if (StringUtil.isSet(inUnencodedValue))
{
for (int i = 0; i < inUnencodedValue.length();)
{
int codePoint = inUnencodedValue.codePointAt(i);
if ( ((codePoint == '!') // %x21
|| (codePoint >= '#' && codePoint <= '+') // %x23-2B
|| (codePoint >= '-' && codePoint <= ':') // %x2D-3A
|| (codePoint >= '<' && codePoint <= '[') // %x3C-5B
|| (codePoint >= ']' && codePoint <= '~') // %x5D-7E
|| (codePoint > 127))
&& (codePoint != '%')) // Need to encode '%' due to it's use in the hex encoding
{
// The character doesn't need to be encoded
encodedValue.append((char)codePoint);
}
else
{
// Encode the character as the hex-equivalent value
encodedValue.append(String.format("%%%02X", codePoint));
}
i += Character.charCount(codePoint);
}
}
return encodedValue.toString();
}
//---------------------------------------------------------------------------
/**
Trys to determine if the cookie value needs to be encoded according to section 4.1.1 of RFC 6265.
(See http://www.rfc-editor.org/rfc/rfc6265.txt)
*/
public static boolean cookieNeedsEncoding(Cookie inCookie)
{
boolean needsEncoding = false;
String cookieValue = inCookie.getValue();
if (StringUtil.isSet(cookieValue))
{
for (int i = 0; i < cookieValue.length(); i++)
{
int codePoint = cookieValue.codePointAt(i);
if (codePoint > 255
|| Character.isWhitespace(codePoint)
|| Character.isISOControl(codePoint)
|| codePoint == '"'
|| codePoint == ','
|| codePoint == ';'
|| codePoint == '/')
{
needsEncoding = true;
break;
}
}
}
return needsEncoding;
}
//---------------------------------------------------------------------------
/**
Trys to determine if the cookie value has been encoded according to section 4.1.1 of RFC 6265.
(See http://www.rfc-editor.org/rfc/rfc6265.txt)
*/
public static boolean isCookieEncoded(Cookie inCookie)
{
return isCookieEncoded(inCookie.getValue());
}
//---------------------------------------------------------------------------
/**
Trys to determine if the cookie value has been encoded according to section 4.1.1 of RFC 6265.
(See http://www.rfc-editor.org/rfc/rfc6265.txt)
*/
public static boolean isCookieEncoded(String inCookieValue)
{
boolean isEncoded = false;
if (StringUtil.isSet(inCookieValue))
{
for (int i = 0; i < inCookieValue.length();)
{
int codePoint = inCookieValue.codePointAt(i);
if (codePoint == '%')
{
// Is the '%' encoding a character that should be encoded?
if (i + 2 < inCookieValue.length())
{
try
{
char potentialDecodedChar = (char) Integer.parseInt(inCookieValue.substring(i + 1, i + 3), 16);
if (potentialDecodedChar == '%'
|| (potentialDecodedChar != '!'
&& !(potentialDecodedChar >= '#' && potentialDecodedChar <= '+')
&& !(potentialDecodedChar >= '-' && potentialDecodedChar <= ':')
&& !(potentialDecodedChar >= '<' && potentialDecodedChar <= '[')
&& !(potentialDecodedChar >= ']' && potentialDecodedChar <= '~')))
{
isEncoded = true;
break;
}
}
catch (NumberFormatException e)
{
// The string must not be encoded
break;
}
}
}
i += Character.charCount(codePoint);
}
}
return isEncoded;
}
//---------------------------------------------------------------------------
/**
Decodes the cookie value according to section 4.1.1 of RFC 6265.
(See http://www.rfc-editor.org/rfc/rfc6265.txt)
@param inEncodedValue the encoded cookie value to be decoded
@return the decoded cookie value
*/
public static String decodeCookieValue(String inEncodedValue)
{
StringBuilder decodedValue = new StringBuilder();
if (StringUtil.isSet(inEncodedValue))
{
for (int i = 0; i < inEncodedValue.length();)
{
int codePoint = inEncodedValue.codePointAt(i);
if (codePoint == '%')
{
decodedValue.append((char)Integer.parseInt(inEncodedValue.substring(i + 1, i + 3), 16));
i += 3;
}
else
{
decodedValue.append((char)codePoint);
i += Character.charCount(codePoint);
}
}
}
return decodedValue.toString();
}
//---------------------------------------------------------------------------
/**
Writes the specified JSON to the servlet response after setting the mime type and character encoding.
@param inServletResponse the servlet response to which the JSON should be written
@param inJSON the JSON to be written to the servlet response
*/
public static void emitJSON(HttpServletResponse inServletResponse, JsCollection inJSON)
throws IOException
{
inServletResponse.setContentType(MimeType.TEXT_PLAIN.toString());
inServletResponse.setCharacterEncoding("UTF-8"); // If we don't specify the encoding as UTF-8, Unicode chars will get sent as '?'
PrintWriter writer = inServletResponse.getWriter();
inJSON.toJavascript(writer);
writer.close();
}
//---------------------------------------------------------------------------
/**
Writes the specified XML file to the servlet response after setting the mime type and character encoding.
@param inServletResponse the servlet response to which the XML file should be written
@param inXMLDoc the XML file to be written to the servlet response
*/
public static void emitXMLDoc(HttpServletResponse inServletResponse, XMLDoc inXMLDoc)
throws Exception
{
setupResponseForFileDownload(inServletResponse, MimeType.TEXT_XML, new File(inXMLDoc.name()));
inServletResponse.setCharacterEncoding("UTF-8"); // If we don't specify the encoding as UTF-8, Unicode chars will get sent as '?'
PrintWriter writer = inServletResponse.getWriter();
inXMLDoc.toIndentedXML(writer, 0, 2);
writer.close();
}
}