All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ovh.ws.jsonizer.api.RestService Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2012, OVH. All rights reserved.
 *
 * 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.ovh.ws.jsonizer.api;

import static com.ovh.ws.jsonizer.api.base.OvhObject.checkNotNull;
import static com.ovh.ws.jsonizer.api.base.OvhObject.newUrl;

import java.lang.reflect.Type;
import java.net.URL;
import java.util.Collections;
import java.util.Map;

import com.ovh.ws.api.AsyncCallback;
import com.ovh.ws.api.OvhService;
import com.ovh.ws.api.OvhWsException;
import com.ovh.ws.api.auth.AuthProvider;
import com.ovh.ws.api.request.RequestListener;
import com.ovh.ws.jsonizer.api.http.HttpClient;
import com.ovh.ws.jsonizer.api.parser.TypeReference;
import com.ovh.ws.jsonizer.api.request.ObjectRequest;
import com.ovh.ws.jsonizer.api.request.ParamsHelper;
import com.ovh.ws.jsonizer.api.request.RequestHandler;
import com.ovh.ws.jsonizer.api.request.RestRequest;

public abstract class RestService implements OvhService {

	private final JsonizerConfig config;
	private final DefaultRequestSender requestSender;

	private URL serviceUrl;
	private RequestHandler requestHandler;

	protected RestService(String url) {
		this(url, new JsonizerConfig());
	}

	protected RestService(String url, AuthProvider authProvider) {
		this(url);
		config.setAuthProvider(authProvider);
	}

	protected RestService(String url, JsonizerConfig config) {
		this(url, config, new DefaultRequestSender(config));
	}

	protected RestService(String url, JsonizerConfig config, DefaultRequestSender requestSender) {
		serviceUrl = newUrl(url);
		this.config = config;
		this.requestSender = requestSender;
	}

	public  T send(RestRequest request) throws OvhWsException {
		return getRequestHandler().beforeSend(requestSender, request);
	}

	public  void send(RestRequest request, AsyncCallback callback) {
		getRequestHandler().beforeSend(requestSender, request, callback);
	}

	protected  ObjectRequest createRequest(String methodName, Class responseType) {
		return newRequest(methodName, responseType, Collections. emptyMap());
	}

	protected  ObjectRequest createRequest(String methodName, TypeReference typeRef) {
		return newRequest(methodName, typeRef.getType(), Collections. emptyMap());
	}

	protected  ObjectRequest createRequest(String methodName, Class responseType, ParamsHelper params) {
		return newRequest(methodName, responseType, params.get());
	}

	protected  ObjectRequest createRequest(String methodName, TypeReference typeRef, ParamsHelper params) {
		return newRequest(methodName, typeRef.getType(), params.get());
	}

	private  ObjectRequest newRequest(String methodName, Type type, Map params) {
		return new ObjectRequest(this, methodName, type, params);
	}

	private RequestHandler getRequestHandler() {
		if (requestHandler == null) {
			return config.getRequestHandler();
		}
		return requestHandler;
	}

	public void setRequestHandler(RequestHandler requestHandler) {
		this.requestHandler = requestHandler;
	}

	@Override
	public void setRequestListener(RequestListener requestListener) {
		requestSender.setRequestListener(requestListener);
	}

	public void setAsyncCallbackListener(AsyncCallbackListener callbackListener) {
		requestSender.setAsyncCallbackListener(callbackListener);
	}

	public HttpClient getHttpClient() {
		return requestSender.getHttpClient();
	}

	public void setHttpClient(HttpClient httpClient) {
		requestSender.setHttpClient(httpClient);
	}

	@Override
	public URL getUrl() {
		return serviceUrl;
	}

	@Override
	public void setUrl(URL url) {
		serviceUrl = checkNotNull(url, "Url is required");
	}

	@Override
	public void setUrl(String url) {
		serviceUrl = newUrl(url);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy