001/*
002 * (C) Copyright 2020 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 *     bdelbosc
018 */
019package org.nuxeo.ecm.restapi.server.jaxrs.management;
020
021import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
022import static org.nuxeo.ecm.core.bulk.introspection.StreamIntrospectionComputation.INTROSPECTION_KEY;
023import static org.nuxeo.ecm.core.bulk.introspection.StreamIntrospectionComputation.INTROSPECTION_KV_STORE;
024
025import javax.ws.rs.GET;
026import javax.ws.rs.Path;
027import javax.ws.rs.Produces;
028
029import org.nuxeo.ecm.core.bulk.introspection.StreamIntrospectionConverter;
030import org.nuxeo.ecm.webengine.model.WebObject;
031import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
032import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
033import org.nuxeo.runtime.api.Framework;
034import org.nuxeo.runtime.kv.KeyValueService;
035import org.nuxeo.runtime.kv.KeyValueStore;
036
037import com.fasterxml.jackson.databind.ObjectMapper;
038
039/**
040 * Nuxeo Stream Introspection endpoint
041 *
042 * @since 11.5
043 */
044@WebObject(type = ManagementObject.MANAGEMENT_OBJECT_PREFIX + "stream")
045@Produces(APPLICATION_JSON)
046public class StreamObject extends AbstractResource<ResourceTypeImpl> {
047
048    protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
049
050    @GET
051    public String doGetJson() {
052        return getJson();
053    }
054
055    @GET
056    @Path("/puml")
057    public String doGetPuml() {
058        String json = getJson();
059        return new StreamIntrospectionConverter(json).getPuml();
060    }
061
062    protected String getJson() {
063        return getKvStore().getString(INTROSPECTION_KEY);
064    }
065
066    protected KeyValueStore getKvStore() {
067        return Framework.getService(KeyValueService.class).getKeyValueStore(INTROSPECTION_KV_STORE);
068    }
069}