123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /*
- * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
- *
- * 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 org.maxkey.client.http;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.maxkey.client.http.AuthorizationHeader;
- import org.maxkey.client.http.HttpVerb;
- import org.maxkey.client.http.Response;
- import org.maxkey.client.utils.Preconditions;
- public class RestClient {
-
- private static Log _log = LogFactory.getLog(RestClient. class );
-
- //action method
- private HttpVerb method = HttpVerb.GET;
-
- //request Service
- private Request request;
-
-
- public RestClient(String url) {
- Preconditions.checkEmptyString(url, "Invalid request url");
-
- request = new Request(method, url);
- }
-
-
- /**
- * 鏋勯�犳柟娉�
- *
- * @param method 璇锋眰鏂规硶
- * @param url 璇锋眰url
- */
- public RestClient(String url,HttpVerb method) {
- Preconditions.checkEmptyString(url, "Invalid request url");
- if(method != null) {
- this.method = method;
- }
- request = new Request(method, url);
- }
-
-
- /**
- * 娣诲姞璇锋眰鍙傛暟
- *
- * @param name 鍙傛暟鍚�
- * @param value 鍙傛暟鍊�
- */
- public void addParameter(String name,String value) {
- Preconditions.checkEmptyString(name, "parameter name is null");
- Preconditions.checkNotNull(request, "RestClient is null");
- if(HttpVerb.GET == method) {
- request.addQuerystringParameter(name, value);
- } else if(HttpVerb.POST == method) {
- request.addBodyParameter(name, value);
- }
- }
-
- /**
- * 娣诲姞璇锋眰Header
- *
- * @param name 鍙傛暟鍚�
- * @param value 鍙傛暟鍊�
- */
- public void addHeader(String name,String value) {
- Preconditions.checkEmptyString(name, "parameter name is null");
- Preconditions.checkNotNull(request, "OAuthRequest is null");
- request.addHeader(name, value);
- }
-
- /**
- * 鍙戦�佽姹傦紝鑾峰彇杩斿洖鐨勬暟鎹�
- *
- * @return Response 鏈嶅姟绔繑鍥炵殑鏁版嵁
- */
- public Response execute() {
- return request.send();
- }
-
-
- public void addBasicAuthorization(String username,String password) {
- Preconditions.checkEmptyString(username, "parameter username is null");
- Preconditions.checkEmptyString(password, "parameter password is null");
- Preconditions.checkNotNull(request, "RestClient is null");
- request.addHeader(AuthorizationHeader.AUTHORIZATION_HEADERNAME, AuthorizationHeader.createBasic(username, password));
- }
-
- public void addBearerAuthorization(String bearer) {
- Preconditions.checkEmptyString(bearer, "parameter bearer is null");
- Preconditions.checkNotNull(request, "RestClient is null");
- request.addHeader(AuthorizationHeader.AUTHORIZATION_HEADERNAME, AuthorizationHeader.createBearer(bearer));
- }
-
- /**
- * set REST Content
- *
- * @param content
- */
- public void addRestContent(String content)
- {
- this.request.addRestContent(content) ;
- }
-
- /**
- * set REST Content
- *
- * @param content
- */
- public void addRestObject(Object content)
- {
- this.request.addRestObject(content);
- }
-
- /**
- * set REST Content
- *
- * @param content
- */
- public void addRestContent(byte[] content)
- {
- this.request.addRestContent(content);
- }
-
- }
|