Skip to content

Commit

Permalink
Merge pull request #100 from usdot-jpo-ode/content-type-fixes
Browse files Browse the repository at this point in the history
Added Content type header to conflict monitor requests
  • Loading branch information
John-Wiens authored Jul 1, 2024
2 parents db28d70 + f872020 commit d5d0933
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.kafka.core.KafkaTemplate;
Expand Down Expand Up @@ -74,17 +76,24 @@ public class ConfigController {

String resourceURL = String.format(defaultConfigTemplate, props.getCmServerURL(), config.getKey());
ResponseEntity<DefaultConfig> response = restTemplate.getForEntity(resourceURL, DefaultConfig.class);


if(response.getStatusCode().is2xxSuccessful()){
DefaultConfig previousConfig = response.getBody();
previousConfig.setValue(config.getValue());
restTemplate.postForEntity(resourceURL, previousConfig, DefaultConfig.class);


HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<DefaultConfig> requestEntity = new HttpEntity<>(previousConfig, headers);

restTemplate.postForEntity(resourceURL, requestEntity, DefaultConfig.class);
defaultConfigRepository.save(previousConfig);
}else{
return ResponseEntity.status(response.getStatusCode()).contentType(MediaType.TEXT_PLAIN).body("Conflict Monitor API was unable to change setting on conflict monitor.");
}


return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.TEXT_PLAIN).body(config.toString());
} catch (Exception e) {
logger.error("Failure in Default Config" + e.getStackTrace());
Expand All @@ -104,16 +113,19 @@ public class ConfigController {

if(response.getStatusCode().is2xxSuccessful()){
IntersectionConfig previousConfig = response.getBody();
System.out.println(previousConfig);

if(previousConfig == null){
previousConfig = config;
}
previousConfig.setValue(config.getValue());
restTemplate.postForEntity(resourceURL, previousConfig, DefaultConfig.class);
System.out.println("PostBack Complete");

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<IntersectionConfig> requestEntity = new HttpEntity<>(previousConfig, headers);

restTemplate.postForEntity(resourceURL, requestEntity, IntersectionConfig.class);

intersectionConfigRepository.save(previousConfig);
System.out.println("Database Postback Complete");
}else{
return ResponseEntity.status(response.getStatusCode()).contentType(MediaType.TEXT_PLAIN).body("Conflict Monitor API was unable to change setting on conflict monitor.");
}
Expand All @@ -138,8 +150,6 @@ public class ConfigController {
intersectionConfigRepository.delete(query);
return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.TEXT_PLAIN).body(config.toString());
} catch (Exception e) {
System.out.println("Received exception when deleting config");
System.out.println(ExceptionUtils.getStackTrace(e));
return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.TEXT_PLAIN)
.body(ExceptionUtils.getStackTrace(e));
}
Expand Down Expand Up @@ -223,15 +233,14 @@ public class ConfigController {
for (IntersectionConfig intersectionConfig : intersectionList) {
if (intersectionConfig.getKey().equals(defaultConfig.getKey())) {
addConfig = intersectionConfig;
System.out.println(defaultConfig.getKey());
System.out.println(addConfig);
break;
}
}
finalConfig.add(addConfig);
}



if (finalConfig.size() > -1) {
return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(finalConfig);
} else {
Expand Down

0 comments on commit d5d0933

Please sign in to comment.