<commit_before>package uk.nhs.careconnect.ccri.fhirserver;

import ca.uhn.fhir.context.FhirContext;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.hl7.fhir.dstu3.model.CapabilityStatement;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class OpenAPIService {

    // Swagger guide https://swagger.io/docs/specification/2-0/basic-structure/

    @Autowired
    private ApplicationContext appCtx;

    @Autowired
    private FhirContext ctx;

    @Value("${server.port}")
    private String serverPort;

    @Value("${server.servlet.context-path}")
    private String serverPath;

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(OpenAPIService.class);

    @RequestMapping("/apidocs")
    public String greeting() {
        HttpClient client = getHttpClient();

        HttpGet request = new HttpGet("http://127.0.0.1:"+serverPort+serverPath+"/STU3/metadata");
        request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        request.setHeader(HttpHeaders.ACCEPT, "application/json");

        try {

            HttpResponse response = client.execute(request);
            log.trace("Response "+response.getStatusLine().toString());
            if (response.getStatusLine().toString().contains("200")) {

                String encoding = "UTF-8";
                String body = IOUtils.toString(response.getEntity().getContent(), encoding);
                log.trace(body);

                CapabilityStatement capabilityStatement = (CapabilityStatement) ctx.newJsonParser().parseResource(body);
                log.trace("Parsing");
                return parseConformanceStatement(capabilityStatement);

            }

        } catch (UnknownHostException e) {
            log.error("Host not known");
        } catch (IOException ex) {
            log.error("IO Exception " + ex.getMessage());
        }

        return "Unable to resolve swagger/openapi documentation";
    }<commit_msg>Swagger<commit_after>package uk.nhs.careconnect.ccri.fhirserver;

import ca.uhn.fhir.context.FhirContext;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.hl7.fhir.dstu3.model.CapabilityStatement;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class OpenAPIService {

    // Swagger guide https://swagger.io/docs/specification/2-0/basic-structure/

    @Autowired
    private ApplicationContext appCtx;

    @Autowired
    private FhirContext ctx;

    @Value("${server.port}")
    private String serverPort;

    @Value("${server.servlet.context-path}")
    private String serverPath;

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(OpenAPIService.class);

    @RequestMapping("/apidocs")
    public String greeting() {
        HttpClient client = getHttpClient();

        String apidocs = "http://localhost:"+serverPort+serverPath+"/STU3/metadata";

        HttpGet request = new HttpGet(apidocs);
        request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        request.setHeader(HttpHeaders.ACCEPT, "application/json");

        try {

            HttpResponse response = client.execute(request);
            log.trace("Response "+response.getStatusLine().toString());
            if (response.getStatusLine().toString().contains("200")) {

                String encoding = "UTF-8";
                String body = IOUtils.toString(response.getEntity().getContent(), encoding);
                log.trace(body);

                CapabilityStatement capabilityStatement = (CapabilityStatement) ctx.newJsonParser().parseResource(body);
                log.trace("Parsing");
                return parseConformanceStatement(capabilityStatement);

            }

        } catch (UnknownHostException e) {
            log.error("Host not known");
        } catch (IOException ex) {
            log.error("IO Exception " + ex.getMessage());
        }

        return "Unable to resolve swagger/openapi documentation from " + apidocs;
    }
{'fraction_non_alphanumeric': 0.11438343150294508, 'fraction_numerical': 0.005320159604788144, 'mean_word_length': 3.4648006785411365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2565606', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1431, 'n_words': 255}