Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.62 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2019/2020 Projektgruppe ROCIT @ Carl von Ossietzky Universität Oldenburg
  3.  *
  4.  * All rights reserved.
  5.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  6.  */
  7. package space.rocit.predictionservice.service;
  8.  
  9. import java.time.LocalDate;
  10. import java.time.LocalDateTime;
  11. import java.time.format.DateTimeFormatter;
  12. import java.util.List;
  13.  
  14. import lombok.AllArgsConstructor;
  15. import lombok.extern.slf4j.Slf4j;
  16.  
  17. import org.springframework.http.*;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.web.client.RestTemplate;
  20.  
  21. import com.google.gson.*;
  22.  
  23. import space.rocit.predictionservice.config.TimeTableConfiguration;
  24. import space.rocit.predictionservice.model.Journey;
  25.  
  26. @Service
  27. @AllArgsConstructor
  28. @Slf4j
  29. public class TimeTableServiceImpl implements TimeTableService {
  30.  
  31.     private final TimeTableConfiguration timeTableConfiguration;
  32.     private final HttpHeaderService httpHeaderService;
  33.  
  34.     @Override
  35.     public List<Journey> getJourneysResponsEntityByStopId(long stopId, long stopType, LocalDate localDate, long version, String userAuthToken) {
  36.         final StringBuilder url = new StringBuilder();
  37.  
  38.         url.append(timeTableConfiguration.getProtocol())
  39.                 .append("://")
  40.                 .append(timeTableConfiguration.getHostname())
  41.                 .append(":")
  42.                 .append(timeTableConfiguration.getPort());
  43.  
  44.         if (timeTableConfiguration.getUrlPrefix() != null && !timeTableConfiguration.getUrlPrefix().isEmpty()) {
  45.             url.append("/")
  46.                     .append(timeTableConfiguration.getUrlPrefix());
  47.         }
  48.  
  49.         log.debug("Using base url: {}", url.toString());
  50.  
  51.         url.append("/")
  52.                 .append("journey")
  53.                 .append("/")
  54.                 .append(version)
  55.                 .append("/")
  56.                 .append(localDate.format(DateTimeFormatter.ISO_DATE))
  57.                 .append("/")
  58.                 .append(stopType)
  59.                 .append("/")
  60.                 .append(stopId);
  61.  
  62.         log.debug("Using url with params: {}", url.toString());
  63.         log.debug("Using url with params: {}", url.toString());
  64.  
  65.         final RestTemplate restTemplate = new RestTemplate();
  66.         HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaderService.createHttpHeaders(userAuthToken));
  67.         final String json = restTemplate.exchange(url.toString(), HttpMethod.GET, entity, String.class).getBody();
  68.  
  69.         final Gson gson = new GsonBuilder()
  70.                 .setPrettyPrinting()
  71.                 .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (jsonElement, type, jsonDeserializationContext) -> LocalDateTime
  72.                         .parse(jsonElement.toString().replaceAll("\"", "")))
  73.                 .create();
  74.         return gson.fromJson(json, List.class);
  75.  
  76.     }
  77.  
  78.     @Override
  79.     public Journey getJourneyResponseEntity(long journeyId, LocalDate localDate, long version, String userAuthToken) {
  80.         final StringBuilder url = new StringBuilder();
  81.  
  82.         url.append(timeTableConfiguration.getProtocol())
  83.                 .append("://")
  84.                 .append(timeTableConfiguration.getHostname())
  85.                 .append(":")
  86.                 .append(timeTableConfiguration.getPort());
  87.  
  88.         if (timeTableConfiguration.getUrlPrefix() != null && !timeTableConfiguration.getUrlPrefix().isEmpty()) {
  89.             url.append("/")
  90.                     .append(timeTableConfiguration.getUrlPrefix());
  91.         }
  92.  
  93.         log.debug("Using base url: {}", url.toString());
  94.  
  95.         url.append("/")
  96.                 .append("journey")
  97.                 .append("/")
  98.                 .append(version)
  99.                 .append("/")
  100.                 .append(localDate.format(DateTimeFormatter.ISO_DATE))
  101.                 .append("/")
  102.                 .append(journeyId);
  103.  
  104.         log.debug("Using url with params: {}", url.toString());
  105.  
  106.         final RestTemplate restTemplate = new RestTemplate();
  107.         HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaderService.createHttpHeaders(userAuthToken));
  108.         final String json = restTemplate.exchange(url.toString(), HttpMethod.GET, entity, String.class).getBody();
  109.  
  110.         final Gson gson = new GsonBuilder()
  111.                 .setPrettyPrinting()
  112.                 .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (jsonElement, type, jsonDeserializationContext) -> LocalDateTime
  113.                         .parse(jsonElement.toString().replaceAll("\"", "")))
  114.                 .create();
  115.         return gson.fromJson(json, Journey.class);
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement