KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > ActionRequestImp


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6 /**
7  * Created by The eXo Platform SARL
8  * Author : Mestrallet Benjamin
9  * benjmestrallet@users.sourceforge.net
10  * Date: Jul 29, 2003
11  * Time: 1:38:46 PM
12  */

13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp;
14
15
16 import javax.portlet.ActionRequest;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
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 JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
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 JavaDoc enc;
37   private boolean encodingModified;
38   private Map decodedParameters;
39   private Log log_;
40
41   public ActionRequestImp(HttpServletRequest JavaDoc 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 JavaDoc getParameter(String JavaDoc 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 JavaDoc) {
66         return (String JavaDoc) decodedParameters.get(param);
67       } else {
68         Object JavaDoc paramValue = decodedParameters.get(param);
69         if(paramValue == null)
70           return null;
71         return ((String JavaDoc[]) paramValue)[0];
72       }
73     }
74     
75     Object JavaDoc obj = super.getParameter(param);
76     if (obj instanceof String JavaDoc[]) {
77       String JavaDoc[] tmp = decode((String JavaDoc[]) obj);
78       decodedParameters.put(param, tmp);
79       return tmp[0];
80     } else {
81       String JavaDoc tmp = decode((String JavaDoc) 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 JavaDoc key = (String JavaDoc) e.nextElement();
93         if (!key.startsWith(Constants.PARAMETER_ENCODER))
94           paramNames.add(key);
95       }
96     }
97     return paramNames.elements();
98   }
99
100   public String JavaDoc[] getParameterValues(String JavaDoc 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 JavaDoc[]) {
105         return (String JavaDoc[]) decodedParameters.get(s);
106       } else {
107         String JavaDoc[] array = { (String JavaDoc) decodedParameters.get(s) };
108         return array;
109       }
110     }
111     String JavaDoc[] 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 JavaDoc name = (String JavaDoc) names.next();
124         String JavaDoc[] obj = (String JavaDoc[]) 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 JavaDoc[] decode(String JavaDoc[] 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 JavaDoc(tmp[i].getBytes(), enc);
141       } catch (UnsupportedEncodingException JavaDoc e) {
142         log_.error(e);
143       }
144     }
145     return tmp;
146   }
147
148   private String JavaDoc decode(String JavaDoc tmp) {
149     if(tmp == null)
150       return null;
151     if(!encodingModified)
152       return tmp;
153     try {
154       tmp = new String JavaDoc(tmp.getBytes(), enc);
155     } catch (UnsupportedEncodingException JavaDoc e) {
156       log_.error(e);
157     }
158     return tmp;
159
160   }
161
162   public java.io.BufferedReader JavaDoc getReader()
163       throws java.io.UnsupportedEncodingException JavaDoc, java.io.IOException JavaDoc {
164     String JavaDoc contentType = getHeader("content-type");
165     if ("application/x-www-form-urlencoded".equals(contentType)) {
166       throw new IllegalStateException JavaDoc("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 JavaDoc enc) throws java.io.UnsupportedEncodingException JavaDoc {
173     if (this.isCallGetReader) {
174       throw new IllegalStateException JavaDoc("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 JavaDoc getPortletInputStream() throws java.io.IOException JavaDoc {
182     if (this.isCallGetReader) {
183       throw new IllegalStateException JavaDoc("This method cannot be called, when getReader() method has been called");
184     }
185     String JavaDoc contentType = getHeader("content-type");
186     if ("application/x-www-form-urlencoded".equals(contentType)) {
187       throw new IllegalStateException JavaDoc("content type cannot be application/x-www-form-urlencoded");
188     }
189     return super.getInputStream();
190   }
191
192 }
193
Popular Tags