001/*
002 * (C) Copyright 2019 Nuxeo (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 *     Nour Al Kotob
018 */
019
020package org.nuxeo.ecm.restapi.server.jaxrs.management;
021
022import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
023import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
024
025import java.util.ArrayList;
026import java.util.List;
027
028import javax.ws.rs.GET;
029import javax.ws.rs.POST;
030import javax.ws.rs.Path;
031import javax.ws.rs.PathParam;
032import javax.ws.rs.Produces;
033
034import org.nuxeo.ecm.core.api.NuxeoException;
035import org.nuxeo.ecm.core.management.api.ProbeInfo;
036import org.nuxeo.ecm.core.management.api.ProbeManager;
037import org.nuxeo.ecm.webengine.model.WebObject;
038import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
039import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
040import org.nuxeo.runtime.api.Framework;
041
042/**
043 * @since 11.3
044 */
045@WebObject(type = ManagementObject.MANAGEMENT_OBJECT_PREFIX + "probes")
046@Produces(APPLICATION_JSON)
047public class ProbesObject extends AbstractResource<ResourceTypeImpl> {
048
049    /**
050     * Gets the info of a specific probe.
051     *
052     * @param probeName the shortcut name of the probe to get
053     * @return a {@link ProbeInfo}
054     */
055    @GET
056    @Path("{probeName}")
057    public ProbeInfo doGet(@PathParam("probeName") String probeName) {
058        ProbeInfo probeInfo = Framework.getService(ProbeManager.class).getProbeInfo(probeName);
059        return checkProbe(probeName, probeInfo);
060    }
061
062    /**
063     * Gets all the infos of all probes.
064     *
065     * @return a list of all {@link ProbeInfo}
066     */
067    @GET
068    public List<ProbeInfo> doGet() {
069        return new ArrayList<>(Framework.getService(ProbeManager.class).getAllProbeInfos());
070    }
071
072    /**
073     * Launches a specific probe.
074     *
075     * @param probeName the shortcut name of the probe to launch
076     * @return the result of the probe in a {@link ProbeInfo}
077     */
078    @POST
079    @Path("{probeName}")
080    public ProbeInfo launch(@PathParam("probeName") String probeName) {
081        ProbeInfo probeInfo = Framework.getService(ProbeManager.class).runProbe(probeName);
082        return checkProbe(probeName, probeInfo);
083    }
084
085    protected ProbeInfo checkProbe(String probeName, ProbeInfo probeInfo) {
086        if (probeInfo == null) {
087            throw new NuxeoException("No such probe: " + probeName, SC_NOT_FOUND);
088        }
089        return probeInfo;
090    }
091
092}