|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.jooby.internal.sitemap; |
| 20 | + |
| 21 | +import static java.util.Objects.requireNonNull; |
| 22 | +import static javaslang.API.Case; |
| 23 | +import static javaslang.API.Match; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.function.Consumer; |
| 28 | +import java.util.function.Predicate; |
| 29 | + |
| 30 | +import org.jooby.Env; |
| 31 | +import org.jooby.Jooby; |
| 32 | +import org.jooby.Route; |
| 33 | +import org.jooby.sitemap.WebPageProvider; |
| 34 | + |
| 35 | +import com.google.inject.Binder; |
| 36 | +import com.google.inject.Key; |
| 37 | +import com.google.inject.name.Names; |
| 38 | +import com.typesafe.config.Config; |
| 39 | + |
| 40 | +import cz.jiripinkas.jsitemapgenerator.WebPage; |
| 41 | +import javaslang.Function1; |
| 42 | + |
| 43 | +@SuppressWarnings("rawtypes") |
| 44 | +public abstract class JSitemap<T extends JSitemap> implements Jooby.Module { |
| 45 | + |
| 46 | + protected static final String SITEMAP = "/sitemap.xml"; |
| 47 | + |
| 48 | + static final Predicate<Route.Definition> NOT_ME = r -> { |
| 49 | + return !r.pattern().equals(SITEMAP); |
| 50 | + }; |
| 51 | + |
| 52 | + static final Predicate<Route.Definition> GET = r -> r.method().equals("GET"); |
| 53 | + |
| 54 | + private static final String SITEMAP_BASEURL = "sitemap.url"; |
| 55 | + |
| 56 | + private String path; |
| 57 | + |
| 58 | + private Consumer<Binder> wpp; |
| 59 | + |
| 60 | + private Predicate<Route.Definition> filter = GET; |
| 61 | + |
| 62 | + private Optional<String> baseurl; |
| 63 | + |
| 64 | + public JSitemap(final String path, final Optional<String> baseurl, final WebPageProvider wpp) { |
| 65 | + this.path = path; |
| 66 | + this.baseurl = baseurl; |
| 67 | + this.wpp = binder -> binder |
| 68 | + .bind(Key.get(WebPageProvider.class, Names.named(path))).toInstance(wpp); |
| 69 | + } |
| 70 | + |
| 71 | + @SuppressWarnings("unchecked") |
| 72 | + public T filter(final Predicate<Route.Definition> filter) { |
| 73 | + this.filter = requireNonNull(filter, "Filter is required."); |
| 74 | + return (T) this; |
| 75 | + } |
| 76 | + |
| 77 | + @SuppressWarnings("unchecked") |
| 78 | + public T with(final WebPageProvider wpp) { |
| 79 | + requireNonNull(wpp, "WebPageProvider is required."); |
| 80 | + this.wpp = binder -> binder.bind(Key.get(WebPageProvider.class, Names.named(path))) |
| 81 | + .toInstance(wpp); |
| 82 | + return (T) this; |
| 83 | + } |
| 84 | + |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + public T with(final Class<? extends WebPageProvider> wpp) { |
| 87 | + requireNonNull(wpp, "WebPageProvider is required."); |
| 88 | + this.wpp = binder -> binder.bind(Key.get(WebPageProvider.class, Names.named(path))) |
| 89 | + .to(wpp); |
| 90 | + return (T) this; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void configure(final Env env, final Config conf, final Binder binder) { |
| 95 | + String baseurl = this.baseurl.orElseGet(() -> Match(conf.hasPath(SITEMAP_BASEURL)).of( |
| 96 | + Case(true, () -> conf.getString(SITEMAP_BASEURL)), |
| 97 | + Case(false, () -> { |
| 98 | + Config $ = conf.getConfig("application"); |
| 99 | + return "http://" + $.getString("host") + ":" + $.getString("port") + $.getString("path"); |
| 100 | + }))); |
| 101 | + |
| 102 | + wpp.accept(binder); |
| 103 | + env.routes().get(path, new SitemapHandler(path, NOT_ME.and(filter), gen(baseurl))); |
| 104 | + } |
| 105 | + |
| 106 | + protected abstract Function1<List<WebPage>, String> gen(String baseurl); |
| 107 | + |
| 108 | +} |
0 commit comments