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.util.ArrayList;
022import java.util.Arrays;
023import java.util.List;
024
025import javax.ws.rs.GET;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.QueryParam;
029import javax.ws.rs.core.Response;
030import javax.ws.rs.core.Response.Status;
031
032import org.nuxeo.runtime.api.Framework;
033import org.nuxeo.targetplatforms.api.TargetPackage;
034import org.nuxeo.targetplatforms.api.TargetPackageInfo;
035import org.nuxeo.targetplatforms.api.TargetPlatform;
036import org.nuxeo.targetplatforms.api.TargetPlatformInfo;
037import org.nuxeo.targetplatforms.api.TargetPlatformInstance;
038import org.nuxeo.targetplatforms.api.impl.TargetPlatformFilterImpl;
039import org.nuxeo.targetplatforms.api.service.TargetPlatformService;
040
041/**
042 * @since 5.9.3
043 */
044@Path("target-platforms")
045public class RootResource {
046
047    @GET
048    public Object doGet(@QueryParam("filterDisabled") boolean filterDisabled,
049                        @QueryParam("filterRestricted") boolean filterRestricted,
050                        @QueryParam("filterDeprecated") boolean filterDeprecated,
051                        @QueryParam("filterDefault") Boolean filterDefault, @QueryParam("filterType") String filterType)
052            throws Exception {
053        return getPlatforms(filterDisabled, filterRestricted, filterDeprecated, filterDefault, filterType);
054    }
055
056    @GET
057    @Path("platform/{id}")
058    public Object getPlatform(@PathParam("id") String id) throws Exception {
059        TargetPlatformService tps = Framework.getService(TargetPlatformService.class);
060        TargetPlatform res = tps.getTargetPlatform(id);
061        if (res != null) {
062            return res;
063        }
064        return Response.status(Status.NOT_FOUND).build();
065    }
066
067    @GET
068    @Path("platforms")
069    public Object getPlatforms(@QueryParam("filterDisabled") boolean filterDisabled,
070            @QueryParam("filterRestricted") boolean filterRestricted,
071            @QueryParam("filterDeprecated") boolean filterDeprecated,
072            @QueryParam("filterDefault") Boolean filterDefault, @QueryParam("filterType") String filterType)
073            throws Exception {
074        TargetPlatformService tps = Framework.getService(TargetPlatformService.class);
075        boolean doFilterDefault = Boolean.TRUE.equals(filterDefault);
076        List<TargetPlatform> res = tps.getAvailableTargetPlatforms(new TargetPlatformFilterImpl(filterDisabled,
077                filterRestricted, filterDeprecated, doFilterDefault, filterType));
078        if (res == null) {
079            return new TargetPlatforms();
080        } else {
081            return new TargetPlatforms(res);
082        }
083    }
084
085    @GET
086    @Path("platform-info/{id}")
087    public Object getPlatformInfo(@PathParam("id") String id) throws Exception {
088        TargetPlatformService tps = Framework.getService(TargetPlatformService.class);
089        TargetPlatformInfo res = tps.getTargetPlatformInfo(id);
090        if (res != null) {
091            return res;
092        }
093        return Response.status(Status.NOT_FOUND).build();
094    }
095
096    @GET
097    @Path("platforms-info")
098    public Object getPlatformInfos(@QueryParam("filterDisabled") boolean filterDisabled,
099            @QueryParam("filterRestricted") boolean filterRestricted,
100            @QueryParam("filterDeprecated") boolean filterDeprecated,
101            @QueryParam("filterDefault") Boolean filterDefault, @QueryParam("filterType") String filterType)
102            throws Exception {
103        TargetPlatformService tps = Framework.getService(TargetPlatformService.class);
104        boolean doFilterDefault = Boolean.TRUE.equals(filterDefault);
105        List<TargetPlatformInfo> res = tps.getAvailableTargetPlatformsInfo(new TargetPlatformFilterImpl(filterDisabled,
106                filterRestricted, filterDeprecated, doFilterDefault, filterType));
107        if (res == null) {
108            return new TargetPlatformsInfo();
109        } else {
110            return new TargetPlatformsInfo(res);
111        }
112    }
113
114    @GET
115    @Path("platform-instance/{id}")
116    public Object getPlatformInstance(@PathParam("id") String id, @QueryParam("packages") String packages)
117            throws Exception {
118        TargetPlatformService tps = Framework.getService(TargetPlatformService.class);
119        List<String> plist = new ArrayList<>();
120        if (packages != null) {
121            plist.addAll(Arrays.asList(packages.split(",")));
122        }
123        TargetPlatformInstance res = tps.getTargetPlatformInstance(id, plist);
124        if (res != null) {
125            return res;
126        }
127        return Response.status(Status.NOT_FOUND).build();
128    }
129
130    @GET
131    @Path("package/{id}")
132    public Object getPackage(@PathParam("id") String id) throws Exception {
133        TargetPlatformService tps = Framework.getService(TargetPlatformService.class);
134        TargetPackage res = tps.getTargetPackage(id);
135        if (res != null) {
136            return res;
137        }
138        return Response.status(Status.NOT_FOUND).build();
139    }
140
141    @GET
142    @Path("package-info/{id}")
143    public Object getPackageInfo(@PathParam("id") String id) throws Exception {
144        TargetPlatformService tps = Framework.getService(TargetPlatformService.class);
145        TargetPackageInfo res = tps.getTargetPackageInfo(id);
146        if (res != null) {
147            return res;
148        }
149        return Response.status(Status.NOT_FOUND).build();
150    }
151
152}