View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.bridges.util;
17  
18  import java.util.Iterator;
19  import java.util.Map;
20  
21  import javax.portlet.ActionRequest;
22  import javax.portlet.PortletException;
23  import javax.portlet.PortletPreferences;
24  
25  
26  /***
27   * PreferencesHelper
28   * 
29   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
30   * @version $Id: PreferencesHelper.java 187932 2004-11-12 07:25:24 +0100 (Fri, 12 Nov 2004) taylor $
31   */
32  public class PreferencesHelper
33  {
34      static public void requestParamsToPreferences(ActionRequest request) 
35          throws PortletException
36      {
37          Map params = request.getParameterMap();
38          PortletPreferences prefs = request.getPreferences();
39          Map prefsMap = prefs.getMap();
40  
41          try
42          {
43              Iterator it = params.entrySet().iterator();
44              while (it.hasNext())
45              {
46                  Map.Entry entry = (Map.Entry) it.next();
47                  Object value = entry.getValue();
48                  String key = (String) entry.getKey();
49                  if (null == prefsMap.get(key))
50                  {
51                      continue;
52                  }
53                  if (value instanceof String)
54                  {
55                      prefs.setValue(key, (String)value);
56                  }
57                  else if (value instanceof String[])
58                  {
59                      prefs.setValue(key, ((String[]) value)[0]);
60                  }
61              }
62          }
63          catch (Exception e)
64          {
65              throw new PortletException("Exception mapping request Params to Preferences: ", e);
66          }
67      }
68  
69  }