com.threewks.thundr.http.service.ning.HttpResponseNing Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thundr-http-ning Show documentation
Show all versions of thundr-http-ning Show documentation
An implementation of thundr-http using the ning asynchronous library
The newest version!
/*
* This file is a component of thundr, a software library from 3wks.
* Read more: http://3wks.github.io/thundr/
* Copyright (C) 2014 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.io.UnsupportedEncodingException;
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.BaseHttpResponse;
import com.threewks.thundr.http.service.HttpException;
import com.threewks.thundr.http.service.HttpRequestException;
import com.threewks.thundr.http.service.HttpResponse;
import com.threewks.thundr.http.service.HttpResponseException;
public class HttpResponseNing extends BaseHttpResponse implements HttpResponse {
private ListenableFuture future;
private Response response;
public HttpResponseNing(ListenableFuture listenableFuture, HttpServiceNing service) {
super(service);
this.future = listenableFuture;
}
@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() {
String characterEncoding = getCharset();
try {
return new String(getBodyAsBytes(), characterEncoding);
} catch (UnsupportedEncodingException e) {
throw new HttpException(e, "Failed to read body as string - character encoding '%s' not supported: %s", characterEncoding, e.getMessage());
}
}
@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(e, "Failed to wait for completion of asynchronous request: %s", e.getMessage());
} catch (ExecutionException e) {
throw new HttpRequestException(e, "Failed to get result for asynchronous request: %s", e.getMessage());
}
}
return response;
}
}