001/* 002 * (C) Copyright 2014 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 * Anahide Tchertchian 018 */ 019package org.nuxeo.targetplatforms.jaxrs; 020 021import java.io.IOException; 022import java.io.OutputStream; 023import java.lang.annotation.Annotation; 024import java.lang.reflect.Type; 025 026import javax.servlet.http.HttpServletRequest; 027import javax.ws.rs.Produces; 028import javax.ws.rs.WebApplicationException; 029import javax.ws.rs.core.Context; 030import javax.ws.rs.core.MediaType; 031import javax.ws.rs.core.MultivaluedMap; 032import javax.ws.rs.ext.MessageBodyWriter; 033import javax.ws.rs.ext.Provider; 034 035import org.nuxeo.targetplatforms.api.TargetPackage; 036import org.nuxeo.targetplatforms.api.TargetPackageInfo; 037import org.nuxeo.targetplatforms.api.TargetPlatform; 038import org.nuxeo.targetplatforms.api.TargetPlatformInfo; 039import org.nuxeo.targetplatforms.api.TargetPlatformInstance; 040import org.nuxeo.targetplatforms.io.JSONExporter; 041 042/** 043 * @since 5.9.3 044 */ 045@Provider 046@Produces({ MediaType.APPLICATION_JSON, "text/plain" }) 047public class JsonWriter implements MessageBodyWriter<Object> { 048 049 @Context 050 protected HttpServletRequest request; 051 052 @Override 053 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { 054 return TargetPackageInfo.class.isAssignableFrom(type) || TargetPackage.class.isAssignableFrom(type) 055 || TargetPlatformInfo.class.isAssignableFrom(type) 056 || TargetPlatformInstance.class.isAssignableFrom(type) || TargetPlatform.class.isAssignableFrom(type) 057 || TargetPlatformsInfo.class.isAssignableFrom(type) || TargetPlatforms.class.isAssignableFrom(type); 058 } 059 060 @Override 061 public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { 062 return -1; 063 } 064 065 @Override 066 public void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, 067 MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, 068 WebApplicationException { 069 boolean pretty = Boolean.parseBoolean(request.getParameter("pretty")); 070 if (TargetPackage.class.isAssignableFrom(type)) { 071 JSONExporter.exportToJson((TargetPackage) t, entityStream, pretty); 072 } else if (TargetPackageInfo.class.isAssignableFrom(type)) { 073 JSONExporter.exportToJson((TargetPackageInfo) t, entityStream, pretty); 074 } else if (TargetPlatform.class.isAssignableFrom(type)) { 075 JSONExporter.exportToJson((TargetPlatform) t, entityStream, pretty); 076 } else if (TargetPlatformInstance.class.isAssignableFrom(type)) { 077 JSONExporter.exportToJson((TargetPlatformInstance) t, entityStream, pretty); 078 } else if (TargetPlatformInfo.class.isAssignableFrom(type)) { 079 JSONExporter.exportToJson((TargetPlatformInfo) t, entityStream, pretty); 080 } else if (TargetPlatforms.class.isAssignableFrom(type)) { 081 JSONExporter.exportToJson((TargetPlatforms) t, entityStream, pretty); 082 } else if (TargetPlatformsInfo.class.isAssignableFrom(type)) { 083 JSONExporter.exportInfosToJson((TargetPlatformsInfo) t, entityStream, pretty); 084 } else { 085 throw new IllegalArgumentException(String.format("Unsupported type '%s'", type)); 086 } 087 } 088}