001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *      André Justo
016 */
017
018package org.nuxeo.ecm.media.publishing.wistia.rest;
019
020import com.sun.jersey.api.client.ClientResponse;
021import org.nuxeo.ecm.media.publishing.wistia.model.Media;
022import org.nuxeo.ecm.media.publishing.wistia.model.Project;
023import org.nuxeo.ecm.media.publishing.wistia.model.Account;
024import org.codehaus.jackson.map.ObjectMapper;
025
026import javax.ws.rs.core.Response;
027import java.io.IOException;
028import java.util.List;
029
030public class WistiaResponseParser {
031
032    static ObjectMapper mapper = new ObjectMapper();
033
034    public static Project asProject(ClientResponse clientResponse) {
035        Project project;
036        try {
037            project = mapper.readValue(clientResponse.getEntityInputStream(), Project.class);
038        } catch (IOException e) {
039            throw new RuntimeException(e);
040        }
041        return project;
042    }
043
044    public static List<Project> asProjectList(ClientResponse clientResponse) {
045        List<Project> projects;
046        ObjectMapper mapper = new ObjectMapper();
047        try {
048            projects = mapper.readValue(clientResponse.getEntityInputStream(), mapper.getTypeFactory().constructCollectionType(List.class, Project.class));
049        } catch (IOException e) {
050            throw new RuntimeException(e);
051        }
052        return projects;
053    }
054
055    public static Media asMedia(ClientResponse clientResponse) {
056        Media media;
057        if (clientResponse.getStatusInfo().getStatusCode() == Response.Status.NOT_FOUND.getStatusCode()) {
058            return null;
059        }
060
061        try {
062            media = mapper.readValue(clientResponse.getEntityInputStream(), Media.class);
063        } catch (IOException e) {
064            throw new RuntimeException(e);
065        }
066        return media;
067    }
068
069    public static List<Media> asMediaList(ClientResponse clientResponse) {
070        List<Media> medias;
071        try {
072            medias = mapper.readValue(clientResponse.getEntityInputStream(), mapper.getTypeFactory().constructCollectionType(List.class, Media.class));
073        } catch (IOException e) {
074            throw new RuntimeException(e);
075        }
076        return medias;
077    }
078
079    public static Account asAccount(ClientResponse clientResponse) {
080        Account account;
081        ObjectMapper mapper = new ObjectMapper();
082        try {
083            account = mapper.readValue(clientResponse.getEntityInputStream(), Account.class);
084        } catch (IOException e) {
085            throw new RuntimeException(e);
086        }
087        return account;
088    }
089}