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

com.threewks.thundr.http.service.ning.HttpResponseNing 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.IOException;
import java.io.InputStream;
import java.net.HttpCookie;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.atomicleopard.expressive.Expressive;
import com.ning.http.client.ListenableFuture;
import com.ning.http.client.Response;
import com.threewks.thundr.http.service.HttpRequestException;
import com.threewks.thundr.http.service.HttpResponse;
import com.threewks.thundr.http.service.HttpResponseException;

public class HttpResponseNing implements HttpResponse {

	private ListenableFuture future;
	private Response response;
	private HttpServiceNing service;

	public HttpResponseNing(ListenableFuture listenableFuture, HttpServiceNing service) {
		this.future = listenableFuture;
		this.service = service;
	}

	@Override
	public int getStatus() {
		return response().getStatusCode();
	}

	@Override
	public String getContentType() {
		return response().getContentType();
	}

	@Override
	public String getHeader(String name) {
		return response().getHeader(name);
	}

	@Override
	public List getHeaders(String name) {
		return response().getHeaders(name);
	}

	@Override
	public Map> getHeaders() {
		return response().getHeaders();
	}

	@Override
	public HttpCookie getCookie(String cookieName) {
		for (com.ning.http.client.Cookie cookie : response().getCookies()) {
			if (cookieName.equals(cookie.getName())) {
				return NingTransformers.fromNingCookie.from(cookie);
			}
		}
		return null;
	}

	@Override
	public List getCookies(String name) {
		List cookies = new ArrayList();
		for (com.ning.http.client.Cookie cookie : response().getCookies()) {
			if (name.equals(cookie.getName())) {
				cookies.add(NingTransformers.fromNingCookie.from(cookie));
			}
		}
		return cookies.isEmpty() ? null : cookies;
	}

	@Override
	public List getCookies() {
		return Expressive.Transformers.transformAllUsing(NingTransformers.fromNingCookie).from(response().getCookies());
	}

	@Override
	public String getBody() {
		return getBody(String.class);
	}

	@Override
	public  T getBody(Class as) {
		return service.convertIncoming(getBodyAsStream(), as);
	}

	@Override
	public byte[] getBodyAsBytes() {
		try {
			return response().getResponseBodyAsBytes();
		} catch (IOException e) {
			throw new HttpResponseException(e, "Failed to get response body: %s", e.getMessage());
		}
	}

	@Override
	public InputStream getBodyAsStream() {
		try {
			return response().getResponseBodyAsStream();
		} catch (IOException e) {
			throw new HttpResponseException(e, "Failed to get response body: %s", e.getMessage());
		}
	}

	@Override
	public URI getUri() {
		try {
			return response().getUri();
		} catch (MalformedURLException e) {
			throw new HttpResponseException(e, "Uri cannot be parsed: %s", e.getMessage());
		}
	}

	private Response response() {
		if (response == null) {
			try {
				response = future.get();
				return response;
			} catch (InterruptedException e) {
				throw new HttpRequestException("Failed to wait for completion of asynchronous request: %s", e.getMessage());
			} catch (ExecutionException e) {
				throw new HttpRequestException("Failed to get result for asynchronous request: %s", e.getMessage());
			}
		}
		return response;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy