View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. 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 org.apache.logging.log4j.catalog.controller;
18  
19  import java.lang.reflect.Type;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  import org.apache.logging.log4j.catalog.api.Attribute;
29  import org.apache.logging.log4j.catalog.api.Constraint;
30  import org.apache.logging.log4j.catalog.jpa.model.ConstraintModel;
31  import org.apache.logging.log4j.catalog.jpa.service.ConstraintService;
32  import org.modelmapper.ModelMapper;
33  import org.modelmapper.TypeToken;
34  import org.springframework.beans.factory.annotation.Autowired;
35  import org.springframework.http.HttpStatus;
36  import org.springframework.http.ResponseEntity;
37  import org.springframework.web.bind.annotation.DeleteMapping;
38  import org.springframework.web.bind.annotation.GetMapping;
39  import org.springframework.web.bind.annotation.PostMapping;
40  import org.springframework.web.bind.annotation.PutMapping;
41  import org.springframework.web.bind.annotation.RequestBody;
42  import org.springframework.web.bind.annotation.RequestMapping;
43  import org.springframework.web.bind.annotation.RequestParam;
44  import org.springframework.web.bind.annotation.RestController;
45  
46  /**
47   * Constraint controller
48   */
49  
50  @RequestMapping(value = "/api/constraints")
51  @RestController
52  public class ConstraintController {
53      private static final Logger LOGGER = LogManager.getLogger();
54  
55      private ModelMapper modelMapper = new ModelMapper();
56  
57      @Autowired
58      private ConstraintService constraintService;
59  
60      @PostMapping(value = "/list")
61      public ResponseEntity<Map<String, Object>> attributeList(@RequestParam("attributeId") Long attributeId) {
62          Type listType = new TypeToken<List<Attribute>>() {}.getType();
63          Map<String, Object> response = new HashMap<>();
64          try {
65              List<Attribute> attributes = modelMapper.map(constraintService.getConstraints(), listType);
66              if (attributes == null) {
67                  attributes = new ArrayList<>();
68              }
69              response.put("Result", "OK");
70              response.put("Records", attributes);
71          } catch (Exception ex) {
72              response.put("Result", "FAILURE");
73          }
74          return new ResponseEntity<>(response, HttpStatus.OK);
75      }
76  
77      @GetMapping(value = "/types")
78      public ResponseEntity<Set<String>> getConstraintTypes() {
79          return new ResponseEntity<>(constraintService.getConstraintTypes(), HttpStatus.OK);
80      }
81  
82      @PostMapping(value = "/constraint")
83      public ResponseEntity<Long> addConstraint(@RequestBody Constraint constraint) {
84          ConstraintModel model = modelMapper.map(constraint, ConstraintModel.class);
85          model = constraintService.saveConstraint(model);
86          return new ResponseEntity<>(model.getId(), HttpStatus.CREATED);
87      }
88  
89      @PutMapping(value = "/constraint/{id}")
90      public ResponseEntity<Long> updateConstraint(@RequestParam Long id, @RequestBody Constraint constraint) {
91          ConstraintModel model = modelMapper.map(constraint, ConstraintModel.class);
92          model.setId(id);
93          model = constraintService.saveConstraint(model);
94          return new ResponseEntity<>(model.getId(), HttpStatus.OK);
95      }
96  
97      @DeleteMapping(value = "/constraint/{id}")
98      public ResponseEntity<?> deleteConstraint(@RequestParam Long id) {
99          constraintService.deleteConstraint(id);
100         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
101     }
102 }