001package org.hl7.fhir.r4.context;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r4
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.io.FileNotFoundException;
025import java.io.FileOutputStream;
026import java.io.PrintStream;
027import java.io.UnsupportedEncodingException;
028import java.util.List;
029
030import org.hl7.fhir.r4.utils.client.ToolingClientLogger;
031import org.hl7.fhir.utilities.Utilities;
032
033public class HTMLClientLogger implements ToolingClientLogger {
034
035  private PrintStream file;
036  private int id = 0;
037  private String lastId;
038  
039  public HTMLClientLogger(String log) {
040    if (log != null) {
041      try {
042        file = new PrintStream(new FileOutputStream(log));
043      } catch (FileNotFoundException e) {
044      }
045    }
046  }
047
048  @Override
049  public void logRequest(String method, String url, List<String> headers, byte[] body) {
050    if (file == null)
051      return;
052    id++;
053    lastId = Integer.toString(id);
054    file.println("<hr/><a name=\"l"+lastId+"\"> </a>");
055    file.println("<pre>");
056    file.println(method+" "+url+" HTTP/1.0");
057    for (String s : headers)  
058      file.println(Utilities.escapeXml(s));
059    if (body != null) {
060      file.println("");
061      try {
062        file.println(Utilities.escapeXml(new String(body, "UTF-8")));
063      } catch (UnsupportedEncodingException e) {
064      }
065    }
066    file.println("</pre>");
067  }
068
069  @Override
070  public void logResponse(String outcome, List<String> headers, byte[] body) {
071    if (file == null)
072      return;
073    file.println("<pre>");
074    file.println(outcome);
075    for (String s : headers)  
076      file.println(Utilities.escapeXml(s));
077    if (body != null) {
078      file.println("");
079      try {
080        file.println(Utilities.escapeXml(new String(body, "UTF-8")));
081      } catch (UnsupportedEncodingException e) {
082      }
083    }
084    file.println("</pre>");
085  }
086
087  public String getLastId() {
088    return lastId;
089  }
090
091  public void clearLastId() {
092    lastId = null;    
093  }
094
095}