/*
 * Copyright 2021 Martynas Jusevičius <martynas@atomgraph.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.atomgraph.server.filter.response;

import com.atomgraph.core.util.Link;
import com.atomgraph.processor.vocabulary.LDT;
import java.io.IOException;
import java.net.URI;
import java.util.Optional;
import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;
import org.apache.jena.ontology.Ontology;

/**
 *
 * @author {@literal Martynas Jusevičius <martynas@atomgraph.com>}
 */
public class ResponseHeaderFilter implements ContainerResponseFilter
{
    
    @Inject javax.inject.Provider<Optional<Ontology>> ontology;
    @Context UriInfo uriInfo;
    
    @Override
    public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException
    {
        // add Link rel=ldt:base
        response.getHeaders().add(HttpHeaders.LINK, new Link(getUriInfo().getBaseUri(), LDT.base.getURI(), null));

        if (getOntology().isPresent()) // if it's not present, Link headers might be forwarded by ProxyResourceBase
        {
            // add Link rel=ldt:ontology
            response.getHeaders().add(HttpHeaders.LINK, new Link(URI.create(getOntology().get().getURI()), LDT.ontology.getURI(), null));
        }
    }

    public Optional<Ontology> getOntology()
    {
        return ontology.get();
    }
    
    public UriInfo getUriInfo()
    {
        return uriInfo;
    }
    
}

@@ -708,16 +708,67 @@
 l.Link;%0A
+import com.atomgraph.processor.model.TemplateCall;%0A
 import c
@@ -1437,16 +1437,93 @@
 tology;%0A
+    @Inject javax.inject.Provider%3COptional%3CTemplateCall%3E%3E templateCall;%0A    %0A
     @Con
@@ -1683,41 +1683,8 @@
   %7B%0A
-        // add Link rel=ldt:base%0A
     
@@ -1915,59 +1915,8 @@
 ase%0A
-        %7B%0A            // add Link rel=ldt:ontology%0A
     
@@ -2061,90 +2061,383 @@
     
-%7D%0A    %7D%0A%0A    public Optional%3COntology%3E getOntology()%0A    %7B%0A        return ontology
+if (getTemplateCall().isPresent())%0A            response.getHeaders().add(HttpHeaders.LINK, new Link(URI.create(getTemplateCall().get().getTemplate().getURI()), LDT.template.getURI(), null));%0A    %7D%0A%0A    public Optional%3COntology%3E getOntology()%0A    %7B%0A        return ontology.get();%0A    %7D%0A    %0A    public Optional%3CTemplateCall%3E getTemplateCall()%0A    %7B%0A        return templateCall
 .get

Set ldt:template Link header in ResponseHeaderFilter