001package org.hl7.fhir.utilities.http;
002
003import java.io.IOException;
004import java.nio.charset.StandardCharsets;
005
006import org.hl7.fhir.utilities.TextFile;
007import org.hl7.fhir.utilities.Utilities;
008
009public class HTTPResult {
010  private int code;
011  private String contentType;
012  private byte[] content;
013  private String source;
014  private String message;
015  
016  
017  public HTTPResult(String source, int code, String message, String contentType, byte[] content) {
018    super();
019    this.source = source;
020    this.code = code;
021    this.contentType = contentType;
022    this.content = content;
023    this.message = message;
024  }
025  
026  public int getCode() {
027    return code;
028  }
029  public String getContentType() {
030    return contentType;
031  }
032  public byte[] getContent() {
033    return content;
034  }
035
036  public String getSource() {
037    return source;
038  }
039
040  public void checkThrowException() throws IOException {
041    if (code >= 300) {
042      String filename = Utilities.path("[tmp]", "http-log", "fhir-http-"+(SimpleHTTPClient.nextCounter())+".log");
043      if (content == null || content.length == 0) {
044        HTTPResultException exception = new HTTPResultException(code, message, source, null);
045        throw new IOException(exception.message, exception);
046      } else {
047        Utilities.createDirectory(Utilities.path("[tmp]", "http-log"));
048        TextFile.bytesToFile(content, filename);
049        HTTPResultException exception = new HTTPResultException(code, message, source, filename);
050        throw new IOException(exception.message, exception);
051      }
052    }      
053  }
054
055  public String getMessage() {
056    return message;
057  }
058
059  public String getContentAsString() {
060    return new String(content, StandardCharsets.UTF_8);
061  }    
062}