1 2 24 25 26 27 28 29 package com.lutris.appserver.server.httpPresentation; 30 31 import java.io.IOException ; 32 import java.util.Enumeration ; 33 import java.util.Hashtable ; 34 35 import com.lutris.http.HttpUtils; 36 import com.lutris.util.KeywordValueException; 37 import com.lutris.util.KeywordValueTable; 38 39 40 44 public class HttpUtil { 45 46 49 private HttpUtil() {} 50 51 57 public static KeywordValueTable buildQueryTable(Hashtable query) 58 throws KeywordValueException { 59 Enumeration arguments = query.keys(); 60 KeywordValueTable table = new KeywordValueTable(); 61 while (arguments.hasMoreElements()) { 62 String key = (String )(arguments.nextElement()); 63 table.set(key, query.get(key)); 64 } 65 return table; 66 } 67 68 75 public static KeywordValueTable buildQueryTable(String queryString) 76 throws KeywordValueException { 77 return buildQueryTable(HttpUtils.parseQueryString(queryString)); 78 } 79 80 87 private static String getPostData(HttpPresentationRequest httpRequest) 88 throws HttpPresentationException { 89 String cType = httpRequest.getContentType().toLowerCase(); 90 if (cType.indexOf("application/x-www-form-urlencoded") < 0) { 91 return ""; 92 } else { 93 byte[] buffer = new byte[httpRequest.getContentLength()]; 94 try { 95 int count = 0; 96 int c = 0; 97 boolean done = false; 98 while ((count < buffer.length) && !done) { 99 c = httpRequest.getInputStream().read(buffer, 100 count, buffer.length - count); 101 if (c == -1) { 102 done = true; 103 } else { 104 count += c; 105 } 106 } 107 } catch (IOException except) { 108 throw new HttpPresentationException(except); 109 } 110 return new String (buffer); 111 } 112 } 113 114 122 public static String getQueryFromRequest(HttpPresentationRequest httpRequest) 123 throws HttpPresentationException { 124 125 String queryString; 126 if (httpRequest.getMethod().equals("POST")) { 127 queryString = getPostData(httpRequest); 129 } else { 130 queryString = httpRequest.getQueryString(); 132 } 133 if (queryString == null) { 134 queryString = ""; 135 } 136 return queryString; 137 } 138 } 139 | Popular Tags |