下面列出了怎么用javax.servlet.http.HttpUtils的API类实例代码及写法,或者点击链接到github查看源代码。
static void addParametersToQuery(RequestData rd) {
Hashtable ht = null;
String queryString = rd.getAttributeValue("queryString"); //NOI18N
try {
ht = javax.servlet.http.HttpUtils.parseQueryString(queryString);
}
catch(Exception ex) { }
if(ht != null && ht.size() > 0) {
Enumeration e = ht.keys();
while(e.hasMoreElements()) {
String name = (String)e.nextElement();
String[] value = (String[])(ht.get(name));
for(int i=0; i<value.length; ++i) {
if(debug)
System.out.println("Adding " + name + //NOI18N
" " + value); //NOI18N
Param p = new Param(name, value[i]);
rd.addParam(p);
}
}
}
}
static boolean removeParametersFromQuery(RequestData rd) {
// Data wasn't parameterized
if(rd.sizeParam() == 0) return false;
String queryString =
rd.getAttributeValue("queryString"); //NOI18N
// MULTIBYTE - I think this isn't working...
Hashtable ht = null;
try {
ht = javax.servlet.http.HttpUtils.parseQueryString(queryString);
}
catch(IllegalArgumentException iae) {
// do nothing, that's OK
return false;
}
if(ht == null || ht.isEmpty()) return false;
Enumeration e = ht.keys();
while(e.hasMoreElements()) {
String name = (String)e.nextElement();
try {
String[] value = (String[])(ht.get(name));
for(int i=0; i<value.length; ++i) {
if(debug) System.out.println("Removing " + //NOI18N
name + " " + //NOI18N
value);
Param p = findParam(rd.getParam(), name, value[i]);
rd.removeParam(p);
}
}
catch(Exception ex) {
}
}
return true;
}
private Hashtable<String, String[]> parseQueryString()
{
if (queryString != null) {
try {
@SuppressWarnings("unchecked")
final Hashtable<String, String[]> res =
HttpUtils.parseQueryString(queryString);
return res;
} catch (final IllegalArgumentException ignore) {
}
}
return new Hashtable<String, String[]>();
}
Map<String, String[]> getParameterMap() throws IOException {
final Map<String, String[]> parameterMap = new HashMap<String, String[]>();
if (request.getQueryString() != null) {
final Map<String, String[]> queryParams = HttpUtils
.parseQueryString(request.getQueryString());
parameterMap.putAll(queryParams);
}
if (request.getContentType() != null
&& request.getContentType().startsWith("application/x-www-form-urlencoded")) {
//get form params from body data
//note this consumes the inputstream! But that's what happens on Tomcat
final Map<String, String[]> bodyParams = HttpUtils.parsePostData(body.length(),
request.getInputStream());
//merge body params and query params
for (final String key : bodyParams.keySet()) {
final String[] queryValues = parameterMap.get(key);
final String[] bodyValues = bodyParams.get(key);
final List<String> values = new ArrayList<String>();
if (queryValues != null) {
values.addAll(Arrays.asList(queryValues));
}
values.addAll(Arrays.asList(bodyValues));
parameterMap.put(key, values.toArray(new String[0]));
}
} //end if form-encoded params in request body
return parameterMap;
}
private Hashtable<String, String[]> parseParameters()
{
final Hashtable<String, String[]> parameters = getQueryParameters();
final String contentType = getContentType();
if (POST_METHOD.equals(method) && null != contentType
&& contentType.startsWith(FORM_MIME_TYPE)) {
// Check that the input stream has not been touched
// Can not use HttpUtils.parsePostData() here since it
// does not honor the character encoding.
byte[] bodyBytes = null;
try {
final int length = getContentLength();
final InputStream in = getBody();
bodyBytes = new byte[length];
int offset = 0;
do {
final int readLength = in.read(bodyBytes, offset, length - offset);
if (readLength <= 0) {
// Bytes are missing, skip.
throw new IOException("Body data to shoort!");
}
offset += readLength;
} while (length - offset > 0);
} catch (final IOException ioe) {
return parameters;
}
String paramData = null;
try {
paramData = new String(bodyBytes, getCharacterEncoding());
} catch (final UnsupportedEncodingException usee) {
// Fallback to use the default character encoding.
paramData = new String(bodyBytes);
}
// Note that HttpUtils.parseQueryString() does not handle UTF-8
// characters that are '%' encoded! Encoding UTF-8 chars in that
// way should not be needed, since the body may contain UTF-8
// chars as long as the correct character encoding has been used
// for the post.
@SuppressWarnings("unchecked")
final Hashtable<String, String[]> p = HttpUtils.parseQueryString(paramData);
// Merge posted parameters with URL parameters.
final Enumeration<String> e = p.keys();
while (e.hasMoreElements()) {
final String key = e.nextElement();
final String[] val = p.get(key);
String[] valArray;
final String oldVals[] = parameters.get(key);
if (oldVals == null) {
valArray = val;
} else {
valArray = new String[oldVals.length + val.length];
for (int i = 0; i < val.length; i++) {
valArray[i] = val[i];
}
for (int i = 0; i < oldVals.length; i++) {
valArray[val.length + i] = oldVals[i];
}
}
parameters.put(key, valArray);
}
}
return parameters;
}
public synchronized DownloadResponse getJnlpFile( JnlpResource jnlpres, DownloadRequest dreq )
throws IOException
{
String path = jnlpres.getPath();
URL resource = jnlpres.getResource();
long lastModified = jnlpres.getLastModified();
_log.addDebug( "lastModified: " + lastModified + " " + new Date( lastModified ) );
if ( lastModified == 0 )
{
_log.addWarning( "servlet.log.warning.nolastmodified", path );
}
// fix for 4474854: use the request URL as key to look up jnlp file
// in hash map
String reqUrl = HttpUtils.getRequestURL( dreq.getHttpRequest() ).toString();
// Check if entry already exist in HashMap
JnlpFileEntry jnlpFile = (JnlpFileEntry) _jnlpFiles.get( reqUrl );
if ( jnlpFile != null && jnlpFile.getLastModified() == lastModified )
{
// Entry found in cache, so return it
return jnlpFile.getResponse();
}
// Read information from WAR file
long timeStamp = lastModified;
String mimeType = _servletContext.getMimeType( path );
if ( mimeType == null )
{
mimeType = JNLP_MIME_TYPE;
}
StringBuilder jnlpFileTemplate = new StringBuilder();
URLConnection conn = resource.openConnection();
BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream(), "UTF-8" ) );
String line = br.readLine();
if ( line != null && line.startsWith( "TS:" ) )
{
timeStamp = parseTimeStamp( line.substring( 3 ) );
_log.addDebug( "Timestamp: " + timeStamp + " " + new Date( timeStamp ) );
if ( timeStamp == 0 )
{
_log.addWarning( "servlet.log.warning.notimestamp", path );
timeStamp = lastModified;
}
line = br.readLine();
}
while ( line != null )
{
jnlpFileTemplate.append( line );
line = br.readLine();
}
String jnlpFileContent = specializeJnlpTemplate( dreq.getHttpRequest(), path, jnlpFileTemplate.toString() );
// Convert to bytes as a UTF-8 encoding
byte[] byteContent = jnlpFileContent.getBytes( "UTF-8" );
// Create entry
DownloadResponse resp =
DownloadResponse.getFileDownloadResponse( byteContent, mimeType, timeStamp, jnlpres.getReturnVersionId() );
jnlpFile = new JnlpFileEntry( resp, lastModified );
_jnlpFiles.put( reqUrl, jnlpFile );
return resp;
}