add weibo src
[freespace.git] / src / weibo4android / http / PostParameter.java
blobd892386000c6040f5c79414ba0683e17e46ac92f
1 /*
2 Copyright (c) 2007-2009, Yusuke Yamamoto
3 All rights reserved.
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;
29 import java.io.File;
30 import java.net.URLEncoder;
31 import java.util.List;
35 /**
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 {
40 String name;
41 String value;
42 private File file = null;
44 private static final long serialVersionUID = -8708108746980739212L;
46 public PostParameter(String name, String value) {
47 this.name = name;
48 this.value = value;
51 public PostParameter(String name, double value) {
52 this.name = name;
53 this.value = String.valueOf(value);
56 public PostParameter(String name, int value) {
57 this.name = name;
58 this.value = String.valueOf(value);
61 public PostParameter(String name, File file) {
62 this.name = name;
63 this.file = file;
66 public String getName(){
67 return name;
69 public String getValue(){
70 return value;
73 public File getFile() {
74 return file;
77 public boolean isFile(){
78 return null != file;
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";
86 /**
88 * @return content-type
90 public String getContentType() {
91 if (!isFile()) {
92 throw new IllegalStateException("not a file");
94 String contentType;
95 String extensions = file.getName();
96 int index = extensions.lastIndexOf(".");
97 if (-1 == index) {
98 // no extension
99 contentType = OCTET;
100 } else {
101 extensions = extensions.substring(extensions.lastIndexOf(".") + 1).toLowerCase();
102 if (extensions.length() == 3) {
103 if ("gif".equals(extensions)) {
104 contentType = GIF;
105 } else if ("png".equals(extensions)) {
106 contentType = PNG;
107 } else if ("jpg".equals(extensions)) {
108 contentType = JPEG;
109 } else {
110 contentType = OCTET;
112 } else if (extensions.length() == 4) {
113 if ("jpeg".equals(extensions)) {
114 contentType = JPEG;
115 } else {
116 contentType = OCTET;
118 } else {
119 contentType = OCTET;
122 return contentType;
126 public static boolean containsFile(PostParameter[] params) {
127 boolean containsFile = false;
128 if(null == params){
129 return false;
131 for (PostParameter param : params) {
132 if (param.isFile()) {
133 containsFile = true;
134 break;
137 return containsFile;
139 /*package*/ static boolean containsFile(List<PostParameter> params) {
140 boolean containsFile = false;
141 for (PostParameter param : params) {
142 if (param.isFile()) {
143 containsFile = true;
144 break;
147 return containsFile;
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));
167 @Override
168 public int hashCode() {
169 int result = name.hashCode();
170 result = 31 * result + value.hashCode();
171 result = 31 * result + (file != null ? file.hashCode() : 0);
172 return result;
175 @Override public boolean equals(Object obj) {
176 if (null == obj) {
177 return false;
179 if (this == obj) {
180 return true;
182 if (obj instanceof PostParameter) {
183 PostParameter that = (PostParameter) obj;
185 if (file != null ? !file.equals(that.file) : that.file != null)
186 return false;
188 return this.name.equals(that.name) &&
189 this.value.equals(that.value);
191 return false;
194 @Override
195 public String toString() {
196 return "PostParameter{" +
197 "name='" + name + '\'' +
198 ", value='" + value + '\'' +
199 ", file=" + file +
200 '}';
203 public int compareTo(Object o) {
204 int compared;
205 PostParameter that = (PostParameter) o;
206 compared = name.compareTo(that.name);
207 if (0 == compared) {
208 compared = value.compareTo(that.value);
210 return compared;
213 public static String encodeParameters(PostParameter[] httpParams) {
214 if (null == httpParams) {
215 return "";
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");
222 if (j != 0) {
223 buf.append("&");
225 try {
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();