|
| 1 | +/* |
| 2 | + Copyright 2018 Ericsson AB. |
| 3 | + For a full list of individual contributors, please see the commit history. |
| 4 | +
|
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +*/ |
| 17 | +package com.ericsson.ei.controller; |
| 18 | + |
| 19 | +import io.swagger.annotations.Api; |
| 20 | +import org.apache.commons.io.IOUtils; |
| 21 | +import org.json.JSONObject; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | +import org.springframework.http.HttpStatus; |
| 25 | +import org.springframework.http.ResponseEntity; |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | +import org.springframework.web.bind.annotation.CrossOrigin; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.InputStream; |
| 31 | + |
| 32 | +@Component |
| 33 | +@CrossOrigin |
| 34 | +@Api(value = "Get Templates", description = "REST endpoints for getting templates") |
| 35 | +public class DownloadControllerImpl implements DownloadController { |
| 36 | + |
| 37 | + private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(DownloadControllerImpl.class); |
| 38 | + |
| 39 | + @Override |
| 40 | + public ResponseEntity<?> getDownload() { |
| 41 | + try { |
| 42 | + JSONObject response = new JSONObject(); |
| 43 | + response.put("subscriptions", "/download/subscriptionsTemplate"); |
| 44 | + response.put("rules", "/download/rulesTemplate"); |
| 45 | + response.put("events", "/download/eventsTemplate"); |
| 46 | + return new ResponseEntity<>(response.toString(), HttpStatus.OK); |
| 47 | + } catch (Exception e) { |
| 48 | + LOGGER.error(e.getMessage(), e); |
| 49 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public ResponseEntity<?> getSubscriptionsTemplate() { |
| 55 | + try { |
| 56 | + InputStream is = getClass().getResourceAsStream("/templates/subscriptionsTemplate.json"); |
| 57 | + return new ResponseEntity<>(IOUtils.toByteArray(is), HttpStatus.OK); |
| 58 | + } catch (IOException e) { |
| 59 | + LOGGER.error(e.getMessage(), e); |
| 60 | + return new ResponseEntity<>("File is not found", HttpStatus.NOT_FOUND); |
| 61 | + } catch (Exception e) { |
| 62 | + LOGGER.error(e.getMessage(), e); |
| 63 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public ResponseEntity<?> getRulesTemplate() { |
| 69 | + try { |
| 70 | + InputStream is = getClass().getResourceAsStream("/templates/rulesTemplate.json"); |
| 71 | + return new ResponseEntity<>(IOUtils.toByteArray(is), HttpStatus.OK); |
| 72 | + } catch (IOException e) { |
| 73 | + LOGGER.error(e.getMessage(), e); |
| 74 | + return new ResponseEntity<>("File is not found", HttpStatus.NOT_FOUND); |
| 75 | + } catch (Exception e) { |
| 76 | + LOGGER.error(e.getMessage(), e); |
| 77 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public ResponseEntity<?> getEventsTemplate() { |
| 83 | + try { |
| 84 | + InputStream is = getClass().getResourceAsStream("/templates/eventsTemplate.json"); |
| 85 | + return new ResponseEntity<>(IOUtils.toByteArray(is), HttpStatus.OK); |
| 86 | + } catch (IOException e) { |
| 87 | + LOGGER.error(e.getMessage(), e); |
| 88 | + return new ResponseEntity<>("File is not found", HttpStatus.NOT_FOUND); |
| 89 | + } catch (Exception e) { |
| 90 | + LOGGER.error(e.getMessage(), e); |
| 91 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments