Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2019/2020 Projektgruppe ROCIT @ Carl von Ossietzky Universität Oldenburg
- *
- * All rights reserved.
- * Unauthorized copying of this file, via any medium is strictly prohibited.
- */
- package space.rocit.predictionservice.service;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.List;
- import lombok.AllArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.http.*;
- import org.springframework.stereotype.Service;
- import org.springframework.web.client.RestTemplate;
- import com.google.gson.*;
- import space.rocit.predictionservice.config.TimeTableConfiguration;
- import space.rocit.predictionservice.model.Journey;
- @Service
- @AllArgsConstructor
- @Slf4j
- public class TimeTableServiceImpl implements TimeTableService {
- private final TimeTableConfiguration timeTableConfiguration;
- private final HttpHeaderService httpHeaderService;
- @Override
- public List<Journey> getJourneysResponsEntityByStopId(long stopId, long stopType, LocalDate localDate, long version, String userAuthToken) {
- final StringBuilder url = new StringBuilder();
- url.append(timeTableConfiguration.getProtocol())
- .append("://")
- .append(timeTableConfiguration.getHostname())
- .append(":")
- .append(timeTableConfiguration.getPort());
- if (timeTableConfiguration.getUrlPrefix() != null && !timeTableConfiguration.getUrlPrefix().isEmpty()) {
- url.append("/")
- .append(timeTableConfiguration.getUrlPrefix());
- }
- log.debug("Using base url: {}", url.toString());
- url.append("/")
- .append("journey")
- .append("/")
- .append(version)
- .append("/")
- .append(localDate.format(DateTimeFormatter.ISO_DATE))
- .append("/")
- .append(stopType)
- .append("/")
- .append(stopId);
- log.debug("Using url with params: {}", url.toString());
- log.debug("Using url with params: {}", url.toString());
- final RestTemplate restTemplate = new RestTemplate();
- HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaderService.createHttpHeaders(userAuthToken));
- final String json = restTemplate.exchange(url.toString(), HttpMethod.GET, entity, String.class).getBody();
- final Gson gson = new GsonBuilder()
- .setPrettyPrinting()
- .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (jsonElement, type, jsonDeserializationContext) -> LocalDateTime
- .parse(jsonElement.toString().replaceAll("\"", "")))
- .create();
- return gson.fromJson(json, List.class);
- }
- @Override
- public Journey getJourneyResponseEntity(long journeyId, LocalDate localDate, long version, String userAuthToken) {
- final StringBuilder url = new StringBuilder();
- url.append(timeTableConfiguration.getProtocol())
- .append("://")
- .append(timeTableConfiguration.getHostname())
- .append(":")
- .append(timeTableConfiguration.getPort());
- if (timeTableConfiguration.getUrlPrefix() != null && !timeTableConfiguration.getUrlPrefix().isEmpty()) {
- url.append("/")
- .append(timeTableConfiguration.getUrlPrefix());
- }
- log.debug("Using base url: {}", url.toString());
- url.append("/")
- .append("journey")
- .append("/")
- .append(version)
- .append("/")
- .append(localDate.format(DateTimeFormatter.ISO_DATE))
- .append("/")
- .append(journeyId);
- log.debug("Using url with params: {}", url.toString());
- final RestTemplate restTemplate = new RestTemplate();
- HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaderService.createHttpHeaders(userAuthToken));
- final String json = restTemplate.exchange(url.toString(), HttpMethod.GET, entity, String.class).getBody();
- final Gson gson = new GsonBuilder()
- .setPrettyPrinting()
- .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (jsonElement, type, jsonDeserializationContext) -> LocalDateTime
- .parse(jsonElement.toString().replaceAll("\"", "")))
- .create();
- return gson.fromJson(json, Journey.class);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement