View Javadoc
1   package info.mikethomas.fahservices.service;
2   
3   /*
4    * #%L
5    * This file is part of FAHServices.
6    * %%
7    * Copyright (C) 2014 - 2024 Mike Thomas <mikepthomas@outlook.com>
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import info.mikethomas.jfold.Connection;
26  import info.mikethomas.jfold.exceptions.SlotInfoException;
27  import info.mikethomas.jfold.slot.Slot;
28  
29  import io.swagger.v3.oas.annotations.Operation;
30  import io.swagger.v3.oas.annotations.Parameter;
31  import io.swagger.v3.oas.annotations.media.ArraySchema;
32  import io.swagger.v3.oas.annotations.media.Content;
33  import io.swagger.v3.oas.annotations.media.Schema;
34  import io.swagger.v3.oas.annotations.responses.ApiResponse;
35  import io.swagger.v3.oas.annotations.tags.Tag;
36  
37  import java.util.List;
38  
39  import org.springframework.beans.factory.annotation.Autowired;
40  import org.springframework.http.HttpStatus;
41  import org.springframework.http.MediaType;
42  import org.springframework.http.ResponseEntity;
43  import org.springframework.web.bind.annotation.ExceptionHandler;
44  import org.springframework.web.bind.annotation.PathVariable;
45  import org.springframework.web.bind.annotation.RequestMapping;
46  import org.springframework.web.bind.annotation.RequestMethod;
47  import org.springframework.web.bind.annotation.ResponseBody;
48  import org.springframework.web.bind.annotation.ResponseStatus;
49  import org.springframework.web.bind.annotation.RestController;
50  
51  /**
52   * REST Web Service.
53   *
54   * @author Michael Thomas (mikepthomas@outlook.com)
55   * @version $Id: $Id
56   */
57  @RestController("slot-info")
58  @Tag(name = "Slot Info", description = "Get slot information.")
59  public class SlotInfoResource {
60  
61      @Autowired
62      private Connection connection;
63  
64      /**
65       * Retrieves representation of an instance of
66       * info.mikethomas.fahservices.service.SlotInfoResource.
67       *
68       * @return an instance of java.lang.String
69       * @throws info.mikethomas.jfold.exceptions.SlotInfoException if any.
70       */
71      @Operation(summary = "slot-info", description = "Get List of slot information.", responses = {
72              @ApiResponse(responseCode = "200", description = "OK", content = {
73                      @Content(array = @ArraySchema(schema = @Schema(implementation = Slot.class)))
74              })
75      })
76      @RequestMapping(
77              value = "/slot-info",
78              method = RequestMethod.GET,
79              produces = {
80                  MediaType.APPLICATION_JSON_VALUE,
81                  MediaType.APPLICATION_XML_VALUE,
82                  MediaType.TEXT_XML_VALUE
83              })
84      @ResponseBody
85      public ResponseEntity<List<Slot>> getSlotInfo() throws SlotInfoException {
86          return new ResponseEntity<>(getSlotInfoList(), HttpStatus.OK);
87      }
88  
89      /**
90       * Retrieves representation of an instance of
91       * info.mikethomas.fahservices.service.SlotInfoResource.
92       *
93       * @param slot Slot number
94       * @return an instance of java.lang.String
95       * @throws info.mikethomas.jfold.exceptions.SlotInfoException if any.
96       */
97      @Operation(summary = "slot-info {slot}", description = "Get slot information at specified index.", responses = {
98              @ApiResponse(responseCode = "200", description = "OK", content = {
99                      @Content(schema = @Schema(implementation = Slot.class))
100             })
101     })
102     @RequestMapping(
103             value = "/slot-info/{slot}",
104             method = RequestMethod.GET,
105             produces = {
106                 MediaType.APPLICATION_JSON_VALUE,
107                 MediaType.APPLICATION_XML_VALUE,
108                 MediaType.TEXT_XML_VALUE
109             })
110     @ResponseBody
111     public ResponseEntity<Slot> getSlotInfo(
112             @Parameter(description = "slot number", required = true)
113             @PathVariable("slot") final int slot) throws SlotInfoException {
114         return new ResponseEntity<>(getSlotInfoList().get(slot), HttpStatus.OK);
115     }
116 
117     /**
118      * <p>handleException.</p>
119      *
120      * @param ex a {@link java.lang.Exception} object
121      * @return a {@link org.springframework.http.ResponseEntity} object
122      */
123     @ExceptionHandler(SlotInfoException.class)
124     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
125     public ResponseEntity<Exception> handleException(Exception ex) {
126         return new ResponseEntity<>(ex, HttpStatus.INTERNAL_SERVER_ERROR);
127     }
128 
129     /**
130      * <p>getSlotInfoList.</p>
131      *
132      * @return a {@link java.util.List} object
133      * @throws info.mikethomas.jfold.exceptions.SlotInfoException if any.
134      */
135     private List<Slot> getSlotInfoList() throws SlotInfoException {
136         return connection.slotInfo();
137     }
138 }