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.nuxeo.ecm.media.publishing.wistia.model.Media; 024import org.nuxeo.ecm.media.publishing.wistia.model.Project; 025import org.nuxeo.ecm.media.publishing.wistia.model.Account; 026import org.codehaus.jackson.map.ObjectMapper; 027 028import javax.ws.rs.core.Response; 029import java.io.IOException; 030import java.util.List; 031 032public class WistiaResponseParser { 033 034 static ObjectMapper mapper = new ObjectMapper(); 035 036 public static Project asProject(ClientResponse clientResponse) { 037 Project project; 038 try { 039 project = mapper.readValue(clientResponse.getEntityInputStream(), Project.class); 040 } catch (IOException e) { 041 throw new RuntimeException(e); 042 } 043 return project; 044 } 045 046 public static List<Project> asProjectList(ClientResponse clientResponse) { 047 List<Project> projects; 048 ObjectMapper mapper = new ObjectMapper(); 049 try { 050 projects = mapper.readValue(clientResponse.getEntityInputStream(), mapper.getTypeFactory().constructCollectionType(List.class, Project.class)); 051 } catch (IOException e) { 052 throw new RuntimeException(e); 053 } 054 return projects; 055 } 056 057 public static Media asMedia(ClientResponse clientResponse) { 058 Media media; 059 if (clientResponse.getStatusInfo().getStatusCode() == Response.Status.NOT_FOUND.getStatusCode()) { 060 return null; 061 } 062 063 try { 064 media = mapper.readValue(clientResponse.getEntityInputStream(), Media.class); 065 } catch (IOException e) { 066 throw new RuntimeException(e); 067 } 068 return media; 069 } 070 071 public static List<Media> asMediaList(ClientResponse clientResponse) { 072 List<Media> medias; 073 try { 074 medias = mapper.readValue(clientResponse.getEntityInputStream(), mapper.getTypeFactory().constructCollectionType(List.class, Media.class)); 075 } catch (IOException e) { 076 throw new RuntimeException(e); 077 } 078 return medias; 079 } 080 081 public static Account asAccount(ClientResponse clientResponse) { 082 Account account; 083 ObjectMapper mapper = new ObjectMapper(); 084 try { 085 account = mapper.readValue(clientResponse.getEntityInputStream(), Account.class); 086 } catch (IOException e) { 087 throw new RuntimeException(e); 088 } 089 return account; 090 } 091}