001package org.hl7.fhir.utilities.http; 002 003import java.io.IOException; 004import java.nio.charset.StandardCharsets; 005import java.util.*; 006 007import lombok.Getter; 008import org.hl7.fhir.utilities.FileUtilities; 009import org.hl7.fhir.utilities.Utilities; 010 011public class HTTPResult { 012 013 @Getter 014 private final int code; 015 @Getter 016 private final String contentType; 017 @Getter 018 private final byte[] content; 019 @Getter 020 private final String source; 021 @Getter 022 private final String message; 023 @Getter 024 private final Iterable<HTTPHeader> headers; 025 026 public HTTPResult(String source, int code, String message, String contentType, byte[] content) { 027 this(source, code, message, contentType, content, Collections.emptyList()); 028 } 029 030 031 public HTTPResult(String source, int code, String message, String contentType, byte[] content, Iterable<HTTPHeader> headers) { 032 super(); 033 this.source = source; 034 this.code = code; 035 this.contentType = contentType; 036 this.content = content; 037 this.message = message; 038 this.headers = headers; 039 } 040 041 public void checkThrowException() throws IOException { 042 if (code >= 300) { 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 String filename = Utilities.path("[tmp]", "http-log", "fhir-http-"+(SimpleHTTPClient.nextCounter())+".log"); 048 FileUtilities.createDirectory(Utilities.path("[tmp]", "http-log")); 049 FileUtilities.bytesToFile(content, filename); 050 HTTPResultException exception = new HTTPResultException(code, message, source, filename); 051 throw new IOException(exception.message, exception); 052 } 053 } 054 } 055 056 public String getContentAsString() { 057 return new String(content, StandardCharsets.UTF_8); 058 } 059 060 061 public String getMessagefromCode() { 062 switch (getCode()) { 063 case 100: return ""+getCode()+" Continue"; 064 case 101: return ""+getCode()+" Switching Protocols"; 065 case 102: return ""+getCode()+" Processing Deprecated"; 066 case 103: return ""+getCode()+" Early Hints"; 067 case 200: return ""+getCode()+" OK"; 068 case 201: return ""+getCode()+" Created"; 069 case 202: return ""+getCode()+" Accepted"; 070 case 203: return ""+getCode()+" Non-Authoritative Information"; 071 case 204: return ""+getCode()+" No Content"; 072 case 205: return ""+getCode()+" Reset Content"; 073 case 206: return ""+getCode()+" Partial Content"; 074 case 207: return ""+getCode()+" Multi-Status"; 075 case 208: return ""+getCode()+" Already Reported"; 076 case 226: return ""+getCode()+" IM Used"; 077 case 300: return ""+getCode()+" Multiple Choices"; 078 case 301: return ""+getCode()+" Moved Permanently"; 079 case 302: return ""+getCode()+" Found"; 080 case 303: return ""+getCode()+" See Other"; 081 case 304: return ""+getCode()+" Not Modified"; 082 case 305: return ""+getCode()+" Use Proxy Deprecated"; 083 case 306: return ""+getCode()+" unused"; 084 case 307: return ""+getCode()+" Temporary Redirect"; 085 case 308: return ""+getCode()+" Permanent Redirect"; 086 case 400: return ""+getCode()+" Bad Request"; 087 case 401: return ""+getCode()+" Unauthorized"; 088 case 402: return ""+getCode()+" Payment Required"; 089 case 403: return ""+getCode()+" Forbidden"; 090 case 404: return ""+getCode()+" Not Found"; 091 case 405: return ""+getCode()+" Method Not Allowed"; 092 case 406: return ""+getCode()+" Not Acceptable"; 093 case 407: return ""+getCode()+" Proxy Authentication Required"; 094 case 408: return ""+getCode()+" Request Timeout"; 095 case 409: return ""+getCode()+" Conflict"; 096 case 410: return ""+getCode()+" Gone"; 097 case 411: return ""+getCode()+" Length Required"; 098 case 412: return ""+getCode()+" Precondition Failed"; 099 case 413: return ""+getCode()+" Content Too Large"; 100 case 414: return ""+getCode()+" URI Too Long"; 101 case 415: return ""+getCode()+" Unsupported Media Type"; 102 case 416: return ""+getCode()+" Range Not Satisfiable"; 103 case 417: return ""+getCode()+" Expectation Failed"; 104 case 418: return ""+getCode()+" I'm a teapot"; 105 case 421: return ""+getCode()+" Misdirected Request"; 106 case 422: return ""+getCode()+" Unprocessable Content"; 107 case 423: return ""+getCode()+" Locked"; 108 case 424: return ""+getCode()+" Failed Dependency"; 109 case 425: return ""+getCode()+" Too Early Experimental"; 110 case 426: return ""+getCode()+" Upgrade Required"; 111 case 428: return ""+getCode()+" Precondition Required"; 112 case 429: return ""+getCode()+" Too Many Requests"; 113 case 431: return ""+getCode()+" Request Header Fields Too Large"; 114 case 451: return ""+getCode()+" Unavailable For Legal Reasons"; 115 case 500: return ""+getCode()+" Internal Server Error"; 116 case 501: return ""+getCode()+" Not Implemented"; 117 case 502: return ""+getCode()+" Bad Gateway"; 118 case 503: return ""+getCode()+" Service Unavailable"; 119 case 504: return ""+getCode()+" Gateway Timeout"; 120 case 505: return ""+getCode()+" HTTP Version Not Supported"; 121 case 506: return ""+getCode()+" Variant Also Negotiates"; 122 case 507: return ""+getCode()+" Insufficient Storage"; 123 case 508: return ""+getCode()+" Loop Detected"; 124 case 510: return ""+getCode()+" Not Extended"; 125 case 511: return ""+getCode()+" Network Authentication Required"; 126 default: return "HTTP code "+code; 127 } 128 } 129}