com.nesscomputing.exception.ExceptionObserver Maven / Gradle / Ivy
/**
* Copyright (C) 2012 Ness Computing, Inc.
*
* 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.nesscomputing.exception;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.core.MediaType;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.nesscomputing.httpclient.HttpClientObserver;
import com.nesscomputing.httpclient.HttpClientResponse;
import com.nesscomputing.logging.Log;
/**
* Intercept exceptions that have been mapped to x-ness/error
responses,
* and rethrow them clientside.
* Rudely consumes the http response body and never lets the actual response handler do anything.
*/
class ExceptionObserver extends HttpClientObserver
{
private static final Log LOG = Log.findLog();
private final ObjectMapper mapper;
private final Map> revivers;
ExceptionObserver(ObjectMapper mapper, Map> revivers) {
this.mapper = mapper;
this.revivers = revivers;
}
@SuppressWarnings("unchecked")
@Override
public HttpClientResponse onResponseReceived(HttpClientResponse response) throws IOException
{
if (StringUtils.isBlank(response.getContentType()))
{
return response;
}
final MediaType type = MediaType.valueOf(response.getContentType());
if ( type.isCompatible(NessApiException.MEDIA_TYPE) ||
NessApiException.LEGACY_TYPES.contains(type))
{
final Map wrapper = mapper.readValue(response.getResponseBodyAsStream(), new TypeReference