ParameterParser.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.udapsoft.waf.system;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.util.Enumeration;
  5. import javax.servlet.http.HttpServletRequest;
  6. import kr.co.hsnc.common.logger.Logger;
  7. import kr.co.hsnc.common.util.Translator;
  8. import kr.co.hsnc.common.util.ValueObject;
  9. import kr.co.hsnc.j2ee.components.multipart.MultipartWrapper;
  10. public class ParameterParser {
  11. private ValueObject params = null;
  12. private String as_encoding = "ISO-8859-1";
  13. private String to_encoding = "UTF-8";
  14. /**
  15. * @param request
  16. */
  17. public ParameterParser(HttpServletRequest request) {
  18. this(request, false);
  19. }
  20. /**
  21. * @param request
  22. * @param isCaseSensitive
  23. */
  24. public ParameterParser(HttpServletRequest request, boolean isCaseSensitive) {
  25. super();
  26. //ISO-8859-1
  27. as_encoding = "ISO-8859-1";
  28. to_encoding = "UTF-8";
  29. params = new ValueObject(isCaseSensitive);
  30. setPrams(request);
  31. }
  32. /**
  33. * HttpServletRequest에서 추출한 Parameter를 ValueObject타입으로 리턴
  34. *
  35. * @return
  36. */
  37. public ValueObject getParams() {
  38. return this.params;
  39. }
  40. /**
  41. * HttpServletRequest에서 Parameter를 추출
  42. *
  43. * @param request
  44. */
  45. private void setPrams(HttpServletRequest request) {
  46. String contentType = request.getContentType();
  47. if(contentType != null && contentType.toUpperCase().indexOf("UTF-8") > 0){
  48. as_encoding = "UTF-8";
  49. }else{
  50. as_encoding = "ISO-8859-1";
  51. }
  52. if (contentType != null && contentType.startsWith("application/xml")) {
  53. } else if (contentType != null && contentType.startsWith("text/xml")) {
  54. try {
  55. String xml = "";
  56. BufferedReader br = request.getReader();
  57. while (true) {
  58. String buf = br.readLine();
  59. if (buf == null || "".equals(buf) || buf.length() == 0)
  60. break;
  61. xml += buf;
  62. }
  63. } catch (IOException e) {
  64. Logger.err.printStackTrace(e);
  65. }
  66. } else {
  67. for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
  68. String key = (String) e.nextElement();
  69. String[] temp = request.getParameterValues(key);
  70. if (temp != null && temp.length == 1) {
  71. String value = get(request, key);
  72. params.set(key, value);
  73. } else if (temp != null && temp.length > 1) {
  74. String[] values = gets(request, key);
  75. params.set(key, values);
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * @param request
  82. * @param key
  83. * @return
  84. */
  85. private String get(HttpServletRequest request, String key) {
  86. String value = null;
  87. value = request.getParameter(key);
  88. boolean isMultipart = (request instanceof MultipartWrapper);
  89. if (!isMultipart) {
  90. value = toEncoding(value);
  91. }
  92. if (value == null) {
  93. value = "";
  94. }
  95. return value;
  96. }
  97. /**
  98. * @param request
  99. * @param key
  100. * @return
  101. */
  102. private String[] gets(HttpServletRequest request, String key) {
  103. String[] values = null;
  104. values = request.getParameterValues(key);
  105. if (values == null) {
  106. values = new String[1];
  107. values[0] = "";
  108. }
  109. for (int i = 0; i < values.length; i++) {
  110. values[i] = toEncoding(values[i]);
  111. }
  112. return values;
  113. }
  114. private String toEncoding(String value) {
  115. return Translator.translate(as_encoding, to_encoding, value);
  116. }
  117. }