001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.server.convention; 018 019import java.io.*; 020 021import org.apache.juneau.rest.server.*; 022import org.apache.juneau.rest.server.servlet.*; 023 024/** 025 * Standalone servlet companion of the {@link VersionMixin} mixin. 026 * 027 * <p> 028 * Mounts as a <b>sibling top-level servlet</b> at {@code /version/*} and serves the same 029 * deployment-introspection JSON map as the mixin by delegating to a shared flavor-neutral 030 * {@link VersionProvider} worker bean — so the two forms cannot drift. 031 * 032 * <p> 033 * Whereas the {@link VersionMixin} mixin pins its op at {@code /version} for composition 034 * into a host at {@code /}, this companion declares the mount at the class level 035 * ({@link Rest#paths() @Rest(paths="/version/*")}) and pins the op at {@code /*}. 036 * 037 * <h5 class='figure'>Microservice registration:</h5> 038 * 039 * <p class='bjava'> 040 * <ja>@Bean</ja> Servlet version() { 041 * <jk>return new</jk> VersionServlet(); <jc>// auto-mounted at /version/* by JettyServerComponent</jc> 042 * } 043 * </p> 044 * 045 * <h5 class='section'>See Also:</h5><ul> 046 * <li class='jc'>{@link VersionMixin} 047 * <li class='jc'>{@link VersionProvider} 048 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerComposition">REST Server — Mixins and Multi-Mount Paths</a> 049 * </ul> 050 * 051 * @serial exclude 052 * @since 10.0.0 053 */ 054// @formatter:off 055@Rest(paths="/version/*") 056public class VersionServlet extends RestServlet { 057 058 private static final long serialVersionUID = 1L; 059 060 private final transient VersionProvider worker; 061 062 /** No-arg constructor — uses a default {@link VersionProvider} worker. */ 063 public VersionServlet() { 064 this(new VersionProvider()); 065 } 066 067 /** 068 * Worker constructor. 069 * 070 * @param worker The shared flavor-neutral version worker this servlet delegates to. Must not be 071 * {@code null}. 072 */ 073 protected VersionServlet(VersionProvider worker) { 074 this.worker = worker; 075 } 076 077 /** 078 * [GET /*] — emit the assembled metadata as a JSON map. 079 * 080 * @param res The current REST response. 081 * @throws IOException If an I/O error occurs while writing the response. 082 */ 083 @RestGet( 084 path="/*", 085 summary="Version / build metadata", 086 description="Deployment-introspection metadata (name, version, git, build).", 087 swagger=@OpSwagger(ignore=true) 088 ) 089 public void getInfo(RestResponse res) throws IOException { 090 worker.serve(res); 091 } 092}