View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
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 net.sf.jdigg.arguments;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.net.URLEncoder;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import net.sf.jdigg.util.Assert;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * Abstract base class for arguments.
32   * <p>Please read this before you use the API:
33   * <a href="http://apidoc.digg.com/BasicConcepts#BePolitePlease">Be Polite, Please!</a></p>
34   * @author Philip May
35   * @since 1.0 M1
36   */
37  public abstract class AbstractArguments {
38  
39      protected final Log log = LogFactory.getLog(getClass());
40  
41      private static final String APPKEY_ARGUMENT_NAME = "appkey";
42  
43      private static final String CHARACTER_ENCODING = "UTF-8";
44  
45      private Map argumentMap = new HashMap();
46  
47      public AbstractArguments(String appkey) {
48          Assert.notNull(appkey, "'appkey' must not be 'null'!");
49          setAppkey(appkey);
50      }
51  
52      /**
53       * The Application key.
54       * @param appkey
55       * @see <a href="http://apidoc.digg.com/ApplicationKeys">Digg API - Application Keys</a>
56       */
57      private void setAppkey(String appkey) {
58          addArgument(APPKEY_ARGUMENT_NAME, appkey);
59      }
60  
61      protected void addArgument(String argumentName, String value) {
62          try {
63              String encodedValue = URLEncoder.encode(value, CHARACTER_ENCODING);
64              argumentMap.put(argumentName, encodedValue);
65          } catch (UnsupportedEncodingException e) {
66              // character encoding should be supported - just log the exception
67              log.error("Character encoding is not supported:" +  CHARACTER_ENCODING, e);
68          }
69      }
70  
71      /** Arguments as URL encoded String. */
72      public String getUrlEncodedArgumentList() {
73          StringBuffer result = new StringBuffer();
74          boolean isFirst = true;
75  
76          Set argumentMapEntrySet = argumentMap.entrySet();
77  
78          for (Iterator argumentMapEntrySetIterator = argumentMapEntrySet.iterator();
79                  argumentMapEntrySetIterator.hasNext();) {
80              Map.Entry argumentMapEntry = (Map.Entry) argumentMapEntrySetIterator.next();
81              if (isFirst) {
82                  isFirst = false;
83              } else {
84                  result.append("&");
85              }
86              result.append(argumentMapEntry.getKey() + "=" + argumentMapEntry.getValue());
87          }
88  
89          return result.toString();
90      }
91  
92  }