001package ball.spring.jig;
002/*-
003 * ##########################################################################
004 * Reusable Spring Components
005 * %%
006 * Copyright (C) 2018 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import java.util.NoSuchElementException;
022import lombok.NoArgsConstructor;
023import lombok.ToString;
024import lombok.extern.log4j.Log4j2;
025import org.springframework.beans.factory.NoSuchBeanDefinitionException;
026import org.springframework.context.ApplicationContext;
027import org.springframework.context.ApplicationContextAware;
028import org.springframework.web.bind.annotation.ExceptionHandler;
029import org.springframework.web.bind.annotation.PathVariable;
030import org.springframework.web.bind.annotation.RequestMapping;
031import org.springframework.web.bind.annotation.ResponseBody;
032import org.springframework.web.bind.annotation.ResponseStatus;
033import org.springframework.web.bind.annotation.RestController;
034
035import static org.springframework.http.HttpStatus.NOT_FOUND;
036import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
037import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
038import static org.springframework.web.bind.annotation.RequestMethod.GET;
039
040/**
041 * Bean {@link RestController} implementation.
042 *
043 * {@injected.fields}
044 *
045 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
046 */
047@RestController
048@RequestMapping(value = { "/jig/bean/" })
049@ResponseBody
050@NoArgsConstructor @ToString @Log4j2
051public class BeanRestController implements ApplicationContextAware {
052    private ApplicationContext context = null;
053
054    @Override
055    public void setApplicationContext(ApplicationContext context) {
056        this.context = context;
057    }
058
059    @RequestMapping(method = { GET }, value = { "{name}.json" }, produces = APPLICATION_JSON_VALUE)
060    public Object json(@PathVariable String name) throws Exception {
061        return context.getBean(name);
062    }
063
064    @RequestMapping(method = { GET }, value = { "{name}.xml" }, produces = APPLICATION_XML_VALUE)
065    public Object xml(@PathVariable String name) throws Exception {
066        return context.getBean(name);
067    }
068
069    @ExceptionHandler({ NoSuchBeanDefinitionException.class, NoSuchElementException.class })
070    @ResponseStatus(value = NOT_FOUND, reason = "Resource not found")
071    public void handleNOT_FOUND() { }
072}