com.lonepulse.zombielink.response.PlainDeserializer Maven / Gradle / Ivy
package com.lonepulse.zombielink.response;
/*
* #%L
* ZombieLink
* %%
* Copyright (C) 2013 - 2014 Lonepulse
* %%
* 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.
* #L%
*/
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import com.lonepulse.zombielink.proxy.InvocationContext;
/**
* This {@link AbstractDeserializer} extracts the response data as a raw String.
*
* @version 1.1.0
*
* @since 1.3.0
*
* @author Lahiru Sahan Jayasinghe
*/
public class PlainDeserializer extends AbstractDeserializer {
/**
* Creates a new {@link PlainDeserializer} and register the type {@link CharSequence} as the entity
* which results from its deserialization operation.
*
* @since 1.3.0
*/
public PlainDeserializer() {
super(CharSequence.class);
}
/**
* Deserializes the response content to a type which is assignable to {@link CharSequence}.
*
* @see AbstractDeserializer#run(InvocationContext, HttpResponse)
*/
@Override
public CharSequence deserialize(InvocationContext context, HttpResponse response) {
try {
HttpEntity entity = response.getEntity();
return entity == null? "" :EntityUtils.toString(entity);
}
catch(Exception e) {
throw new DeserializerException(new StringBuilder("Plain deserialization failed for request <")
.append(context.getRequest().getName())
.append("> on endpoint <")
.append(context.getEndpoint().getName())
.append(">").toString(), e);
}
}
}