001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.sitemaps;
019
020import io.stallion.restfulEndpoints.EndpointResource;
021import io.stallion.restfulEndpoints.EndpointsRegistry;
022import io.stallion.settings.SecondaryDomain;
023import io.stallion.settings.Settings;
024import io.stallion.templating.TemplateRenderer;
025
026import javax.ws.rs.GET;
027import javax.ws.rs.Path;
028import javax.ws.rs.Produces;
029
030import java.util.List;
031import java.util.Map;
032
033import static io.stallion.Context.request;
034import static io.stallion.utils.Literals.*;
035
036
037public class SiteMapEndpoints implements EndpointResource {
038
039    @GET
040    @Path("/sitemap.xml")
041    @Produces("text/xml")
042    public String siteMap() {
043        Map<String, Object> context = map();
044        List<SiteMapItem> items = SiteMapController.instance().getAllItemsForDomain(request().getHost());
045        context.put("items", items);
046        return TemplateRenderer.instance().renderTemplate(getClass().getResource("/templates/sitemap.xml.jinja"), context);
047    }
048
049    public static void registerEndpoints() {
050        EndpointsRegistry.instance().addResource("", new SiteMapEndpoints());
051        for(SecondaryDomain sd: Settings.instance().getSecondaryDomains()) {
052            SiteMapEndpoints sme = new SiteMapEndpoints();
053            /*
054            BaseRestEndpoint endpoint = new JavaRestEndpoint()
055                    .setMethodName("siteMap")
056                    .setResource(sme)
057                    .setRole(Role.ANON)
058                    .setMethod("GET")
059                    .setRoute(sd.getRewriteRoot() + "/sitemap.xml");
060             */
061            EndpointsRegistry.instance().addResource(sd.getRewriteRoot(), new SiteMapEndpoints());
062
063        }
064    }
065
066}