2 Copyright (c) 2007-2009, Yusuke Yamamoto
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Yusuke Yamamoto nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
17 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 package weibo4android
.http
;
30 import java
.net
.URLEncoder
;
31 import java
.util
.List
;
36 * A data class representing HTTP Post parameter
37 * @author Yusuke Yamamoto - yusuke at mac.com
39 public class PostParameter
implements java
.io
.Serializable
, Comparable
{
42 private File file
= null;
44 private static final long serialVersionUID
= -8708108746980739212L;
46 public PostParameter(String name
, String value
) {
51 public PostParameter(String name
, double value
) {
53 this.value
= String
.valueOf(value
);
56 public PostParameter(String name
, int value
) {
58 this.value
= String
.valueOf(value
);
61 public PostParameter(String name
, File file
) {
66 public String
getName(){
69 public String
getValue(){
73 public File
getFile() {
77 public boolean isFile(){
81 private static final String JPEG
= "image/jpeg";
82 private static final String GIF
= "image/gif";
83 private static final String PNG
= "image/png";
84 private static final String OCTET
= "application/octet-stream";
88 * @return content-type
90 public String
getContentType() {
92 throw new IllegalStateException("not a file");
95 String extensions
= file
.getName();
96 int index
= extensions
.lastIndexOf(".");
101 extensions
= extensions
.substring(extensions
.lastIndexOf(".") + 1).toLowerCase();
102 if (extensions
.length() == 3) {
103 if ("gif".equals(extensions
)) {
105 } else if ("png".equals(extensions
)) {
107 } else if ("jpg".equals(extensions
)) {
112 } else if (extensions
.length() == 4) {
113 if ("jpeg".equals(extensions
)) {
126 public static boolean containsFile(PostParameter
[] params
) {
127 boolean containsFile
= false;
131 for (PostParameter param
: params
) {
132 if (param
.isFile()) {
139 /*package*/ static boolean containsFile(List
<PostParameter
> params
) {
140 boolean containsFile
= false;
141 for (PostParameter param
: params
) {
142 if (param
.isFile()) {
150 public static PostParameter
[] getParameterArray(String name
, String value
) {
151 return new PostParameter
[]{new PostParameter(name
,value
)};
153 public static PostParameter
[] getParameterArray(String name
, int value
) {
154 return getParameterArray(name
,String
.valueOf(value
));
157 public static PostParameter
[] getParameterArray(String name1
, String value1
158 , String name2
, String value2
) {
159 return new PostParameter
[]{new PostParameter(name1
, value1
)
160 , new PostParameter(name2
, value2
)};
162 public static PostParameter
[] getParameterArray(String name1
, int value1
163 , String name2
, int value2
) {
164 return getParameterArray(name1
,String
.valueOf(value1
),name2
,String
.valueOf(value2
));
168 public int hashCode() {
169 int result
= name
.hashCode();
170 result
= 31 * result
+ value
.hashCode();
171 result
= 31 * result
+ (file
!= null ? file
.hashCode() : 0);
175 @Override public boolean equals(Object obj
) {
182 if (obj
instanceof PostParameter
) {
183 PostParameter that
= (PostParameter
) obj
;
185 if (file
!= null ?
!file
.equals(that
.file
) : that
.file
!= null)
188 return this.name
.equals(that
.name
) &&
189 this.value
.equals(that
.value
);
195 public String
toString() {
196 return "PostParameter{" +
197 "name='" + name
+ '\'' +
198 ", value='" + value
+ '\'' +
203 public int compareTo(Object o
) {
205 PostParameter that
= (PostParameter
) o
;
206 compared
= name
.compareTo(that
.name
);
208 compared
= value
.compareTo(that
.value
);
213 public static String
encodeParameters(PostParameter
[] httpParams
) {
214 if (null == httpParams
) {
217 StringBuffer buf
= new StringBuffer();
218 for (int j
= 0; j
< httpParams
.length
; j
++) {
219 if (httpParams
[j
].isFile()) {
220 throw new IllegalArgumentException("parameter [" + httpParams
[j
].name
+ "]should be text");
226 buf
.append(URLEncoder
.encode(httpParams
[j
].name
, "UTF-8"))
227 .append("=").append(URLEncoder
.encode(httpParams
[j
].value
, "UTF-8"));
228 } catch (java
.io
.UnsupportedEncodingException neverHappen
) {
231 return buf
.toString();