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.http;
 
 import static com.ovh.ws.jsonizer.common.api.OvhConditions.checkNotNull;
 
 import java.util.Map;
 
 
 public class HttpRequester {
 
 	private final Parser parser;
 	private final HttpClient httpClient;
 	private final RequestSigner requestSigner;
 
 	public HttpRequester(Parser parserHttpClient httpClient) {
 		this(parserhttpClientnew RequestSigner());
 	}
 
 	/* package */HttpRequester(Parser parserHttpClient httpClientRequestSigner requestSigner) {
 		this. = checkNotNull(parser"Parser is required");
 		this. = checkNotNull(httpClient"HttpClient is required");
 		this. = requestSigner;
 	}
 
 	public String execute(OvhURI urithrows OvhWsException {
 		WebResource resource = .resource(uri.toUri());
 		prepareResource(resourceuri);
 		return resource.execute();
 	}
 
 	public void execute(OvhURI urifinal HttpRequestListener listener) {
 		try {
 			AsyncWebResource asyncResource = .asyncResource(uri.toUri());
 			prepareResource(asyncResourceuri);
 			asyncResource.execute(listener);
 
 		} catch (OvhWsException caught) {
 			listener.onFailure(caught);
 		}
 	}
 
 	private void prepareResource(AbstractWebResource resourceOvhURI urithrows OvhWsException {
 		String paramsValue = serializeParams(uri);
 
 		Map<StringStringheaders = .getHeaders();
 		Map<StringStringsigningHeaders = signingIfNecessary(uriparamsValue);
 		headers.putAll(signingHeaders);
 
 		resource.headers(headers);
 
 		Map<StringStringparams = buildParams(uriparamsValue);
 		resource.queryParams(params);
 	}
 
 	private String serializeParams(OvhURI urithrows OvhWsException {
 		return !uri.getParams().isEmpty() ? .writeValue(uri.getParams()) : "";
 	}
 
 	private Map<StringStringsigningIfNecessary(OvhURI uriString paramsValuethrows OvhWsException {
 			return .getSigningHeaders(uri.toString(), paramsValue.getToken());
 		}
 		return Collections.emptyMap();
 	}
 
 	private Map<StringStringbuildParams(OvhURI uriString paramsValue) {
 		Map<StringStringresult = new HashMap<StringString>();
 
 		if (uri.getSessionId() != null) {
 			result.put("session"uri.getSessionId());
 		}
 
 		if (paramsValue != null && paramsValue.length() > 0) {
 			result.put("params"paramsValue);
 		}
 
 		return result;
 	}
 
 	public HttpClient getHttpClient() {
 		return ;
 	}
 
New to GrepCode? Check out our FAQ X