Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.common;
 
 import static com.ovh.ws.jsonizer.common.api.OvhConditions.checkResponse;
 
 import java.util.List;
 
 
 public class RestTemplate {
 
 	private static final RequestListener DEFAULT_REQUEST_LISTENER = new DefaultRequestListener();
 
 	private final Parser parser;
 	private final HttpRequester httpRequester;
 
 
 	protected RestTemplate(HttpClient httpClient) {
 		 = Jsonizer.createParser();
 		 = new HttpRequester(httpClient);
 	}
 
 	protected RestTemplate(Parser parserHttpRequester httpRequester) {
 		this. = parser;
 		this. = httpRequester;
 	}
 
 	public <T> T getForObject(OvhURI uriClass<T> clazzthrows OvhWsException {
 		try {
 			String content = .execute(uri);
 			JavaType javaType = JavaType.constructType(clazz);
 			return response(uricontentjavaType);
 
 		} catch (OvhWsException caught) {
 			.onFailure(uricaught);
 			throw caught;
 		}
 	}
 
 	public <T> void getForObject(OvhURI uriClass<T> clazzAsyncCallback<T> callback) {
 		JavaType javaType = JavaType.constructType(clazz);
 		HttpRequestListener httpRequestListener = createHttpListener(urijavaTypecallback);
 		.execute(urihttpRequestListener);
 	}
 
 	public <T> List<T> getForList(OvhURI uriClass<T> clazzthrows OvhWsException {
 		try {
 			String content = .execute(uri);
 			CollectionType javaType = JavaType.constructCollectionType(List.classclazz);
 			return response(uricontentjavaType);
 
 		} catch (OvhWsException caught) {
 			.onFailure(uricaught);
 			throw caught;
 		}
 	}
 
 	public <T> void getForList(OvhURI uriClass<T> clazzAsyncCallback<List<T>> callback) {
 		CollectionType javaType = JavaType.constructCollectionType(List.classclazz);
 		HttpRequestListener httpRequestListener = createHttpListener(urijavaTypecallback);
 		.execute(urihttpRequestListener);
 	}
 
 	private HttpRequestListener createHttpListener(final OvhURI urifinal JavaType typefinal AsyncCallback<?> back) {
 		return new HttpRequestListener() {
 			public void onComplete(String result) {
 				response(uriresulttypeback);
 			}
 
 			public void onFailure(OvhWsException caught) {
 				.onFailure(uricaught);
 				.onFailure(caughtback);
 			}
		};
	}
	private <T> void response(OvhURI uriString contentJavaType javaTypeAsyncCallback<T> callback) {
		try {
result = response(uricontentjavaType);
			.onSuccess(resultcallback);
catch (OvhWsException caught) {
			.onFailure(caughtcallback);
		}
	}
	private <T> T response(OvhURI uriString contentJavaType javaTypethrows OvhWsException {
		ResponseNode response = readAndCheckResponse(content);
result = .convertValue(response.getAnswer(), javaType);
		return result;
	}
		ResponseNode response = .readValue(contentResponseNode.class);
		return checkResponse(response);
	}
	protected HttpClient getHttpClient() {
	}
	protected void setAsyncCallbackListener(AsyncCallbackListener callbackListener) {
		this. = callbackListener == null ?  : callbackListener;
	}
	protected void setRequestListener(RequestListener requestListener) {
		this. = requestListener == null ?  : requestListener;
	}
New to GrepCode? Check out our FAQ X