package org.cbioportal.proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

@RestController
public class ProxyController {
    @Value("${show.oncokb:true}")
    private Boolean showOncokb;
    @Value("${oncokb.token:}")
    private String oncokbToken;
    @Value("${oncokb.public_api.url:www.oncokb.org}")
    private String oncokbPublicApiUrl;

    private Logger LOG = LoggerFactory.getLogger(ProxyController.class);

    @RequestMapping("/**")
    public String proxy(@RequestBody(required = false) String body, HttpMethod method, HttpServletRequest request)
        throws URISyntaxException {
        HttpHeaders httpHeaders = new HttpHeaders();
        String contentType = request.getHeader("Content-Type");
        if (contentType != null) {
            httpHeaders.setContentType(MediaType.valueOf(contentType));
        }
        return exchangeData(body, buildUri(request.getPathInfo(), request.getQueryString(), false), method, httpHeaders).getBody();
    }

    @RequestMapping("/oncokb/**")
    public ResponseEntity<String> proxyOncokb(@RequestBody(required = false) String body, HttpMethod method, HttpServletRequest request)
        throws URISyntaxException {
        if (!this.showOncokb) {
            return new ResponseEntity<>("OncoKB service is disabled.", HttpStatus.NOT_FOUND);
        }

        HttpHeaders httpHeaders = new HttpHeaders();
        String contentType = request.getHeader("Content-Type");
        if (contentType != null) {
            httpHeaders.setContentType(MediaType.valueOf(contentType));
        }
        if (!StringUtils.isEmpty(this.oncokbToken)) {
            httpHeaders.add("Authorization", "Bearer " + this.oncokbToken);
        }
        String oncokbApiUrl = this.oncokbPublicApiUrl;
        if(StringUtils.isEmpty(oncokbApiUrl)) {
            if (StringUtils.isEmpty(oncokbToken)) {
                // this is a legacy endpoint which does not accept any data updates
                oncokbApiUrl = "oncokb.org";
            } else {
                oncokbApiUrl = "www.oncokb.org";
            }
        }
        return exchangeData(body, buildUri(oncokbApiUrl, request.getQueryString(), true), method, httpHeaders);
    }

    private URI buildUri(String path, String queryString, boolean useSecureProtocol) throws URISyntaxException {
        return new URI((useSecureProtocol ? "https" : "http") + ":/" + path + (queryString == null ? "" : "?" + queryString));
    }

    private ResponseEntity<String> exchangeData(String body, URI uri, HttpMethod method, HttpHeaders httpHeaders) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate.exchange(uri, method, new HttpEntity<>(body, httpHeaders), String.class);
    }
}

@@ -1235,207 +1235,29 @@
 s = 
-new HttpHeaders();%0A        String contentType = request.getHeader(%22Content-Type%22);%0A        if (contentType != null) %7B%0A            httpHeaders.setContentType(MediaType.valueOf(contentType));%0A        %7D
+initHeaders(request);
 %0A   
@@ -1774,207 +1774,38 @@
 s = 
-new HttpHeaders();%0A        String contentType = request.getHeader(%22Content-Type%22);%0A        if (contentType != null) %7B%0A            httpHeaders.setContentType(MediaType.valueOf(contentType));%0A        %7D
+initHeaders(request);%0A        
 %0A   
@@ -2006,16 +2006,17 @@
       if
+ 
 (StringU
@@ -2213,16 +2213,24 @@
 iUrl = %22
+https://
 oncokb.o
@@ -2223,32 +2223,39 @@
 tps://oncokb.org
+/api/v1
 %22;%0A            %7D
@@ -2294,16 +2294,24 @@
 iUrl = %22
+https://
 www.onco
@@ -2316,16 +2316,23 @@
 cokb.org
+/api/v1
 %22;%0A     
@@ -2405,16 +2405,68 @@
 kbApiUrl
+ + request.getPathInfo().replaceFirst(%22/oncokb%22, %22%22)
 , reques
@@ -2487,17 +2487,11 @@
 ng()
+)
 , 
-true), 
 meth
@@ -2505,25 +2505,382 @@
 pHeaders
+);%0A    %7D%0A%0A    private HttpHeaders initHeaders(HttpServletRequest request) %7B%0A        HttpHeaders httpHeaders = new HttpHeaders();%0A        %0A        String contentType = request.getHeader(%22Content-Type%22);%0A        if (contentType != null) %7B%0A            httpHeaders.setContentType(MediaType.valueOf(contentType)
 );%0A    
+  
-%7D%0A
+  %7D%0A        %0A        return httpHeaders;%0A    %7D%0A    
 %0A    pri
@@ -3004,15 +3004,16 @@
 urn 
-new URI
+buildUri
 ((us
@@ -3061,16 +3061,152 @@
 %22 + path
+, queryString);%0A    %7D%0A%0A    private URI buildUri(String path, String queryString) throws URISyntaxException %7B%0A        return new URI(path
  + (quer

Update ProxyController.java