1 5 6 13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp; 14 15 16 import javax.portlet.ActionRequest; 17 import javax.servlet.http.HttpServletRequest ; 18 19 import org.apache.commons.logging.Log; 20 import org.exoplatform.Constants; 21 import org.exoplatform.container.RootContainer; 22 import org.exoplatform.services.log.LogService; 23 import org.exoplatform.services.portletcontainer.impl.PortletApplicationHandler; 24 25 import java.io.InputStream ; 26 import java.io.UnsupportedEncodingException ; 27 import java.util.*; 28 29 public class ActionRequestImp extends PortletRequestImp 30 implements ActionRequest { 31 32 private Vector paramNames; 33 private Map filteredMap; 34 private boolean isCallGetReader; 35 36 private String enc; 37 private boolean encodingModified; 38 private Map decodedParameters; 39 private Log log_; 40 41 public ActionRequestImp(HttpServletRequest httpServletRequest) { 42 super(httpServletRequest); 43 this.enc = httpServletRequest.getCharacterEncoding(); 44 LogService logService = (LogService) RootContainer.getInstance(). 45 getComponentInstanceOfType(LogService.class); 46 log_ = logService.getLog("org.exoplatform.services.portletcontainer"); 47 } 48 49 public void init() { 50 isCallGetReader = false; 51 decodedParameters = new HashMap(); 52 enc = super.getCharacterEncoding(); 53 } 54 55 public void emptyActionRequest() { 56 this.paramNames = null; 57 this.filteredMap = null; 58 this.decodedParameters = null; 59 } 60 61 public String getParameter(String param) { 62 if (param == null || param.startsWith(Constants.PARAMETER_ENCODER)) 63 return null; 64 if (decodedParameters.containsKey(param)){ 65 if (decodedParameters.get(param) instanceof String ) { 66 return (String ) decodedParameters.get(param); 67 } else { 68 Object paramValue = decodedParameters.get(param); 69 if(paramValue == null) 70 return null; 71 return ((String []) paramValue)[0]; 72 } 73 } 74 75 Object obj = super.getParameter(param); 76 if (obj instanceof String []) { 77 String [] tmp = decode((String []) obj); 78 decodedParameters.put(param, tmp); 79 return tmp[0]; 80 } else { 81 String tmp = decode((String ) obj); 82 decodedParameters.put(param, tmp); 83 return tmp; 84 } 85 } 86 87 public Enumeration getParameterNames() { 88 if (paramNames == null) { 89 Enumeration e = super.getParameterNames(); 90 paramNames = new Vector(); 91 while (e.hasMoreElements()) { 92 String key = (String ) e.nextElement(); 93 if (!key.startsWith(Constants.PARAMETER_ENCODER)) 94 paramNames.add(key); 95 } 96 } 97 return paramNames.elements(); 98 } 99 100 public String [] getParameterValues(String s) { 101 if (s == null || s.startsWith(Constants.PARAMETER_ENCODER)) 102 return null; 103 if (decodedParameters.containsKey(s)) { 104 if (decodedParameters.get(s) instanceof String []) { 105 return (String []) decodedParameters.get(s); 106 } else { 107 String [] array = { (String ) decodedParameters.get(s) }; 108 return array; 109 } 110 } 111 String [] tmp = decode(super.getParameterValues(s)); 112 decodedParameters.put(s, tmp); 113 return tmp; 114 } 115 116 public Map getParameterMap() { 117 if (filteredMap == null) { 118 filteredMap = new HashMap(); 119 Map requestMap = super.getParameterMap(); 120 Set keySet = requestMap.keySet(); 121 Iterator names = keySet.iterator(); 122 while (names.hasNext()) { 123 String name = (String ) names.next(); 124 String [] obj = (String []) requestMap.get(name); 125 if (!name.startsWith(Constants.PARAMETER_ENCODER)) { 126 filteredMap.put(name, decode(obj)); 127 } 128 } 129 } 130 return Collections.unmodifiableMap(filteredMap); 131 } 132 133 private String [] decode(String [] tmp) { 134 if(tmp == null) 135 return null; 136 if(!encodingModified) 137 return tmp; 138 for (int i = 0; i < tmp.length; i++) { 139 try { 140 tmp[i] = new String (tmp[i].getBytes(), enc); 141 } catch (UnsupportedEncodingException e) { 142 log_.error(e); 143 } 144 } 145 return tmp; 146 } 147 148 private String decode(String tmp) { 149 if(tmp == null) 150 return null; 151 if(!encodingModified) 152 return tmp; 153 try { 154 tmp = new String (tmp.getBytes(), enc); 155 } catch (UnsupportedEncodingException e) { 156 log_.error(e); 157 } 158 return tmp; 159 160 } 161 162 public java.io.BufferedReader getReader() 163 throws java.io.UnsupportedEncodingException , java.io.IOException { 164 String contentType = getHeader("content-type"); 165 if ("application/x-www-form-urlencoded".equals(contentType)) { 166 throw new IllegalStateException ("content type cannot be application/x-www-form-urlencoded"); 167 } 168 this.isCallGetReader = true; 169 return super.getReader(); 170 } 171 172 public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException { 173 if (this.isCallGetReader) { 174 throw new IllegalStateException ("This method cannot be called, when getReader() method has been called"); 175 } 176 super.setCharacterEncoding(enc); 177 this.enc = enc; 178 this.encodingModified = true; 179 } 180 181 public InputStream getPortletInputStream() throws java.io.IOException { 182 if (this.isCallGetReader) { 183 throw new IllegalStateException ("This method cannot be called, when getReader() method has been called"); 184 } 185 String contentType = getHeader("content-type"); 186 if ("application/x-www-form-urlencoded".equals(contentType)) { 187 throw new IllegalStateException ("content type cannot be application/x-www-form-urlencoded"); 188 } 189 return super.getInputStream(); 190 } 191 192 } 193 | Popular Tags |