001package ca.uhn.fhir.rest.client.exceptions; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2017 University Health Network 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 */ 022import static org.apache.commons.lang3.StringUtils.isBlank; 023 024import java.io.IOException; 025import java.io.Reader; 026 027import org.apache.commons.io.IOUtils; 028 029import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException; 030import ca.uhn.fhir.util.CoverageIgnore; 031 032@CoverageIgnore 033public class NonFhirResponseException extends BaseServerResponseException { 034 035 private static final long serialVersionUID = 1L; 036 037 /** 038 * Constructor 039 * 040 * @param theMessage 041 * The message 042 * @param theResponseText 043 * @param theStatusCode 044 * @param theResponseReader 045 * @param theContentType 046 */ 047 NonFhirResponseException(int theStatusCode, String theMessage) { 048 super(theStatusCode, theMessage); 049 } 050 051 public static NonFhirResponseException newInstance(int theStatusCode, String theContentType, Reader theReader) { 052 String responseBody = ""; 053 try { 054 responseBody = IOUtils.toString(theReader); 055 } catch (IOException e) { 056 IOUtils.closeQuietly(theReader); 057 } 058 059 NonFhirResponseException retVal; 060 if (isBlank(theContentType)) { 061 retVal = new NonFhirResponseException(theStatusCode, "Response contains no Content-Type"); 062 } else if (theContentType.contains("text")) { 063 retVal = new NonFhirResponseException(theStatusCode, "Response contains non FHIR Content-Type '" + theContentType + "' : " + responseBody); 064 } else { 065 retVal = new NonFhirResponseException(theStatusCode, "Response contains non FHIR Content-Type '" + theContentType + "'"); 066 } 067 068 retVal.setResponseBody(responseBody); 069 return retVal; 070 } 071 072}