001package org.nuxeo.ecm.restapi.server.jaxrs.targetplatforms; 002 003import java.io.ByteArrayOutputStream; 004import java.util.ArrayList; 005import java.util.List; 006import java.util.concurrent.TimeUnit; 007 008import javax.ws.rs.GET; 009import javax.ws.rs.Path; 010import javax.ws.rs.Produces; 011import javax.ws.rs.core.MediaType; 012import javax.ws.rs.core.Response; 013 014import org.nuxeo.ecm.webengine.model.WebObject; 015import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 016import org.nuxeo.runtime.api.Framework; 017import org.nuxeo.targetplatforms.api.TargetPlatform; 018import org.nuxeo.targetplatforms.api.impl.TargetPlatformFilterImpl; 019import org.nuxeo.targetplatforms.api.service.TargetPlatformService; 020import org.nuxeo.targetplatforms.io.JSONExporter; 021 022import com.google.common.cache.CacheBuilder; 023import com.google.common.cache.CacheLoader; 024import com.google.common.cache.LoadingCache; 025 026@WebObject(type = "target-platforms") 027@Produces(MediaType.APPLICATION_JSON) 028public class TargetPlatformObject extends DefaultObject { 029 private static final String PUBLIC_TP_CACHE_KEY = "PUBLIC_TP"; 030 031 private static final LoadingCache<String, String> PUBLIC_CACHE = CacheBuilder // 032 .newBuilder() // 033 .expireAfterAccess(5, TimeUnit.MINUTES) // 034 .refreshAfterWrite(10, TimeUnit.MINUTES) // 035 .recordStats() // 036 .maximumSize(5).build(new CacheLoader<String, String>() { 037 @Override 038 public String load(String key) throws Exception { 039 return key; 040 } 041 }); 042 043 @GET 044 public Object doGet() { 045 return Response.status(Response.Status.NOT_FOUND).build(); 046 } 047 048 @GET 049 @Path("public") 050 public Object doGetPublic() throws Exception { 051 String platforms = PUBLIC_CACHE.get(PUBLIC_TP_CACHE_KEY, () -> { 052 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { 053 TargetPlatformService tps = Framework.getService(TargetPlatformService.class); 054 List<TargetPlatform> res = tps.getAvailableTargetPlatforms( 055 new TargetPlatformFilterImpl(false, true, true, false, null)); 056 if (res == null) { 057 res = new ArrayList<>(); 058 } 059 JSONExporter.exportToJson(res, baos, false); 060 return new String(baos.toByteArray()); 061 } 062 }); 063 064 return Response.status(Response.Status.OK).entity(platforms).build(); 065 } 066}