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

com.threewks.thundr.http.service.ning.HttpServiceNing Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * This file is a component of thundr, a software library from 3wks.
 * Read more: http://www.3wks.com.au/thundr
 * Copyright (C) 2013 3wks, 
 *
 * 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.threewks.thundr.http.service.ning;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.atomicleopard.expressive.Cast;
import com.atomicleopard.expressive.ETransformer;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.threewks.thundr.exception.BaseException;
import com.threewks.thundr.http.service.HttpRequest;
import com.threewks.thundr.http.service.HttpService;
import com.threewks.thundr.http.service.typeTransformer.IncomingByteArrayTypeTransformer;
import com.threewks.thundr.http.service.typeTransformer.IncomingInputStreamTypeTransformer;
import com.threewks.thundr.http.service.typeTransformer.IncomingStringTypeTransformer;
import com.threewks.thundr.http.service.typeTransformer.OutgoingDefaultTypeTransformer;
import com.threewks.thundr.http.service.typeTransformer.OutgoingStringTypeTransformer;

public class HttpServiceNing implements HttpService {
	private Map, ETransformer> outgoingTypeConvertors = new LinkedHashMap, ETransformer>();
	private List> outgoingTypeConvertorOrder = new ArrayList>();
	private Map, ETransformer> incomingTypeConvertors = new HashMap, ETransformer>();
	private AsyncHttpClientConfig config;
	private AsyncHttpClient client;

	public HttpServiceNing() {
		this(new AsyncHttpClientConfig.Builder().setMaximumConnectionsPerHost(10).setMaximumConnectionsTotal(100). build());
	}

	public HttpServiceNing(AsyncHttpClientConfig config) {
		this.config = config;
		this.client = createHttpClient();
		// default outgoing transformers
		addOutgoingTypeConvertor(Object.class, new OutgoingDefaultTypeTransformer());
		addOutgoingTypeConvertor(String.class, new OutgoingStringTypeTransformer());

		// default incoming transformers
		addIncomingTypeConvertor(String.class, new IncomingStringTypeTransformer());
		addIncomingTypeConvertor(InputStream.class, new IncomingInputStreamTypeTransformer());
		addIncomingTypeConvertor(byte[].class, new IncomingByteArrayTypeTransformer());
	}

	public  void addIncomingTypeConvertor(Class type, ETransformer convertor) {
		incomingTypeConvertors.put(type, convertor);
	}

	public  void addOutgoingTypeConvertor(Class type, ETransformer convertor) {
		outgoingTypeConvertors.put(type, convertor);
		outgoingTypeConvertorOrder.add(type);
	}

	public HttpRequest request(String url) {
		return new HttpRequestNing(this, client, url);
	}

	@SuppressWarnings("unchecked")
	public  InputStream convertOutgoing(T t) {
		for (int i = outgoingTypeConvertorOrder.size() - 1; i >= 0; i--) {
			Class type = outgoingTypeConvertorOrder.get(i);
			if (Cast.is(t, type)) {
				ETransformer transformer = (ETransformer) outgoingTypeConvertors.get(type);
				return transformer.from(t);
			}
		}
		throw new BaseException("Unable to convert the given object to an input stream, no convertor found. Object: %s", t);
	}

	@SuppressWarnings("unchecked")
	@Override
	public  T convertIncoming(InputStream is, Class type) {
		ETransformer convertor = incomingTypeConvertors.get(type);
		if (convertor == null) {
			throw new BaseException("Unable to convert the response to the type %s, please make sure a convertor is registered", type.getName());
		}
		return (T) convertor.from(is);
	}

	AsyncHttpClient createHttpClient() {
		return new AsyncHttpClient(config);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy