001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *      André Justo
018 */
019
020package org.nuxeo.ecm.media.publishing.wistia.rest;
021
022import com.sun.jersey.api.client.ClientResponse;
023import org.codehaus.jackson.JsonNode;
024import org.codehaus.jackson.map.ObjectMapper;
025
026import java.io.IOException;
027
028public class RestResponse {
029
030    protected ClientResponse clientResponse;
031
032    protected JsonNode responseAsJson;
033
034    protected ObjectMapper objectMapper = new ObjectMapper();
035
036    public RestResponse(ClientResponse clientResponse) {
037        this.clientResponse = clientResponse;
038    }
039
040    public ClientResponse getClientResponse() {
041        return this.clientResponse;
042    }
043
044    public int getStatus() {
045        return this.clientResponse.getStatus();
046    }
047
048    public JsonNode asJson() {
049        if (responseAsJson == null) {
050            try {
051                responseAsJson = objectMapper.readTree(clientResponse.getEntityInputStream());
052            } catch (IOException e) {
053                throw new RuntimeException(e);
054            }
055        }
056        return responseAsJson;
057    }
058}