|
| 1 | +package de.muenchen.dbs.personalization.serviceNavigator; |
| 2 | + |
| 3 | +import de.muenchen.dbs.personalization.configuration.P13nConfiguration; |
| 4 | +import io.swagger.v3.oas.annotations.Operation; |
| 5 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 6 | +import java.net.InetSocketAddress; |
| 7 | +import java.net.Proxy; |
| 8 | +import java.util.List; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.apache.commons.lang3.StringUtils; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.http.HttpStatus; |
| 14 | +import org.springframework.http.ResponseEntity; |
| 15 | +import org.springframework.http.client.SimpleClientHttpRequestFactory; |
| 16 | +import org.springframework.web.bind.annotation.GetMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 18 | +import org.springframework.web.bind.annotation.RequestParam; |
| 19 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 20 | +import org.springframework.web.bind.annotation.RestController; |
| 21 | +import org.springframework.web.client.RestTemplate; |
| 22 | + |
| 23 | +@RestController |
| 24 | +@RequestMapping("/public/servicenavigator") |
| 25 | +@Slf4j |
| 26 | +@RequiredArgsConstructor |
| 27 | +@Tag(name = "Public ServiceNavigator Endpoints", description = "Endpoints not requiring authentication") |
| 28 | +public class PublicServiceNavigatorController { |
| 29 | + |
| 30 | + private static final String SERVICENAVIGATOR_QUERY_PARAMETER_ID = "?id="; |
| 31 | + |
| 32 | + private final RestTemplate restTemplate; |
| 33 | + private final P13nConfiguration p13nConfiguration; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + public PublicServiceNavigatorController(P13nConfiguration p13nConfiguration) { |
| 37 | + this.p13nConfiguration = p13nConfiguration; |
| 38 | + |
| 39 | + if(StringUtils.isBlank(p13nConfiguration.getProxyHost())) { |
| 40 | + this.restTemplate = new RestTemplate(); |
| 41 | + } else { |
| 42 | + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(p13nConfiguration.getProxyHost(), p13nConfiguration.getProxyPort())); |
| 43 | + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); |
| 44 | + requestFactory.setProxy(proxy); |
| 45 | + this.restTemplate = new RestTemplate(requestFactory); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @GetMapping |
| 50 | + @Operation(summary = "Lookup ServiceNavigator Services by ServiceID. Returns a list of services for the given service IDs.") |
| 51 | + @ResponseStatus(HttpStatus.OK) |
| 52 | + public List<Object> getServicesByIds(@RequestParam("ids")String serviceIds) { |
| 53 | + final String url = p13nConfiguration.getServiceNavigatorUrl() + SERVICENAVIGATOR_QUERY_PARAMETER_ID + serviceIds; |
| 54 | + ResponseEntity<List> forEntity = restTemplate.getForEntity(url, List.class); |
| 55 | + return forEntity.getBody(); |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments