Back to project page khandroid.
The source code is released under:
Apache License
If you think the Android project khandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2012-2014 Ognyan Bankov *//from w w w .ja v a 2 s .c o m * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.khandroid.rest; import java.util.ArrayList; import com.github.khandroid.JsonFunctionality; import com.github.khandroid.http.request.GetRequestBuilder; import com.github.khandroid.http.request.PostRequestBuilder; import com.github.khandroid.misc.StringUtils; import khandroid.ext.apache.http.NameValuePair; import khandroid.ext.apache.http.client.methods.HttpUriRequest; import khandroid.ext.apache.http.message.BasicNameValuePair; public class RestExchangeBuilder<T> { public enum RequestType { GET, POST }; private String mBaseUrl; private String mEndpoint; private Class<T> mResultClass; private JsonFunctionality mJson; private Object mTag; private RequestType mRequestType = RequestType.POST; ArrayList<NameValuePair> mGetParams = new ArrayList<NameValuePair>(); ArrayList<NameValuePair> mPostParams = new ArrayList<NameValuePair>(); public RestExchangeBuilder() { super(); } public RestExchangeBuilder(String baseUrl, String endpoint, Class<T> resultClass, JsonFunctionality json) { super(); mBaseUrl = baseUrl; mResultClass = resultClass; mEndpoint = endpoint; mJson = json; } public RestExchangeBuilder<T> baseUrl(String baseUrl) { mBaseUrl = baseUrl; return this; } public RestExchangeBuilder<T> endpoint(String endpoint) { mEndpoint = endpoint; return this; } public RestExchangeBuilder<T> resultClass(Class<T> resultClass) { mResultClass = resultClass; return this; } public RestExchangeBuilder<T> json(JsonFunctionality json) { mJson = json; return this; } public RestExchangeBuilder<T> requestType(RequestType type) { mRequestType = type; return this; } public RestExchange<T> build() { HttpUriRequest request; checkRequired(); if (mRequestType == RequestType.GET) { if (mPostParams.size() > 0) { throw new IllegalStateException("You requested GET request but added some POST parameters."); } GetRequestBuilder b = new GetRequestBuilder(mBaseUrl + mEndpoint); for (NameValuePair p : mGetParams) { b.addParameter(p.getName(), p.getValue()); } request = b.build(); } else { PostRequestBuilder b = new PostRequestBuilder(mBaseUrl + mEndpoint); for (NameValuePair p : mPostParams) { b.addPostParameter(p.getName(), p.getValue()); } request = b.build(); } RestExchange<T> ret = new RestExchange<T>(request, mJson, mResultClass); ret.setTag(mTag); return ret; } private void checkRequired() { if (StringUtils.isEmpty(mBaseUrl)) { throw new IllegalStateException("base URL not set"); } else { if (StringUtils.isEmpty(mEndpoint)) { throw new IllegalStateException("endpoint not set"); } else { if (mResultClass == null) { throw new IllegalStateException("result class not set"); } } } } public void addPostParameter(String key, String value) { if (!isPostParameterPresent(key)) { NameValuePair tmp = new BasicNameValuePair(key, value); mPostParams.add(tmp); } else { throw new IllegalArgumentException("There is already parameter named " + key); } } public boolean isPostParameterPresent(String key) { boolean ret = false; for (NameValuePair pair : mPostParams) { if (pair.getName().equals(key)) { ret = true; break; } } return ret; } public void addGetParameter(String key, String value) { if (!isParameterPresent(key)) { NameValuePair tmp = new BasicNameValuePair(key, value); mGetParams.add(tmp); } else { throw new IllegalArgumentException("There is already parameter named " + key); } } public boolean isParameterPresent(String key) { boolean ret = false; for (NameValuePair pair : mGetParams) { if (pair.getName().equals(key)) { ret = true; break; } } return ret; } public void tag(Object tag) { mTag = tag; } }