001package org.hl7.fhir.r4.context;
002
003import java.io.FileNotFoundException;
004import java.io.FileOutputStream;
005import java.io.PrintStream;
006import java.io.UnsupportedEncodingException;
007import java.util.List;
008
009import org.hl7.fhir.r4.utils.client.ToolingClientLogger;
010import org.hl7.fhir.utilities.Utilities;
011
012public class HTMLClientLogger implements ToolingClientLogger {
013
014  private PrintStream file;
015  private int id = 0;
016  private String lastId;
017  
018  public HTMLClientLogger(String log) {
019    if (log != null) {
020      try {
021        file = new PrintStream(new FileOutputStream(log));
022      } catch (FileNotFoundException e) {
023      }
024    }
025  }
026
027  @Override
028  public void logRequest(String method, String url, List<String> headers, byte[] body) {
029    if (file == null)
030      return;
031    id++;
032    lastId = Integer.toString(id);
033    file.println("<hr/><a name=\"l"+lastId+"\"> </a>");
034    file.println("<pre>");
035    file.println(method+" "+url+" HTTP/1.0");
036    for (String s : headers)  
037      file.println(Utilities.escapeXml(s));
038    if (body != null) {
039      file.println("");
040      try {
041        file.println(Utilities.escapeXml(new String(body, "UTF-8")));
042      } catch (UnsupportedEncodingException e) {
043      }
044    }
045    file.println("</pre>");
046  }
047
048  @Override
049  public void logResponse(String outcome, List<String> headers, byte[] body) {
050    if (file == null)
051      return;
052    file.println("<pre>");
053    file.println(outcome);
054    for (String s : headers)  
055      file.println(Utilities.escapeXml(s));
056    if (body != null) {
057      file.println("");
058      try {
059        file.println(Utilities.escapeXml(new String(body, "UTF-8")));
060      } catch (UnsupportedEncodingException e) {
061      }
062    }
063    file.println("</pre>");
064  }
065
066  public String getLastId() {
067    return lastId;
068  }
069
070  public void clearLastId() {
071    lastId = null;    
072  }
073
074}