openwfe.org.rest.RestUtils Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2001-2006, John Mettraux, OpenWFE.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name of the "OpenWFE" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id: RestUtils.java 2713 2006-06-01 14:38:45Z jmettraux $
*/
//
// RestUtils.java
//
// [email protected]
//
// generated with
// jtmpl 1.1.01 2004/05/19 ([email protected])
//
package openwfe.org.rest;
import java.net.URLDecoder;
/**
* Utility methods for HTTP / REST handling
*
* CVS Info :
*
$Author: jmettraux $
*
$Id: RestUtils.java 2713 2006-06-01 14:38:45Z jmettraux $
*
* @author [email protected]
*/
public abstract class RestUtils
{
private final static org.apache.log4j.Logger log = org.apache.log4j.Logger
.getLogger(RestUtils.class.getName());
private final static String ENCODING = "UTF-8";
// as recommended by the W3C
//
// METHODS
/**
* Given an HTTP request first line, extracts the value of a parameter.
* For example :
*
* GET /documenation.pl?action=nada&topic=x&year=2005
*
* extractFromLine(l, "topic")
* will yield "x"
*/
public static String extractFromLine
(String line, final String key)
throws
java.io.IOException
{
//log.debug("looking for key >"+key+"<'");
//log.debug("line is >"+line+"<");
int i = line.indexOf(key+"=");
if (i < 0) return null;
line = line.substring(i+key.length()+1);
//log.debug("line is >"+line+"<");
int iamp = line.indexOf("&");
int ispc = line.indexOf(" ");
if (iamp < 0) iamp = line.length();
if (ispc < 0) ispc = line.length();
if (iamp < ispc) i = iamp; else i = ispc;
line = line.substring(0, i);
if (log.isDebugEnabled())
log.debug("extractFromLine() key >"+key+"< value >"+line+"<");
return line;
}
/**
* Turns an HTTP request line into a map of string parameters.
* For example :
*
* GET /documentation.pl?action=nada&topic=x&year=2005
*
* extractParams (l, "topic")
* will yield { "action": "nada", "topic": "x", "year": "2005" }
*/
public static java.util.Map extractParams (final String line)
{
if (log.isDebugEnabled())
log.debug("extractParams() for '"+line+"'");
final java.util.Map params = new java.util.HashMap();
if (line == null) return params;
// this is somehow too kind...
int i = line.indexOf("?");
if (i < 0) return params;
if (i == line.length()-1) return params;
String[] ss = line.substring(i+1).split("&");
for (int j=0; j 1) v = kv[1];
try
{
k = URLDecoder.decode(k, ENCODING);
v = URLDecoder.decode(v, ENCODING);
}
catch (final java.io.UnsupportedEncodingException e)
{
log.warn
("extractParams() encoding problem "+e+
" ('"+k+"': '"+v+"')");
}
if (log.isDebugEnabled())
log.debug("extractParams() found '"+k+"' -> '"+v+"'");
params.put(k, v);
}
return params;
}
/**
* Same thing a extractParams(), but it works with
* HttpServletRequest.getQueryString() returns (which don't entail the
* '/documentation.pl?' part).
*/
public static java.util.Map extractParamsFromQueryString (final String line)
{
return extractParams("?"+line);
}
}