View Javadoc
1   package ca.uhn.fhir.rest.client.exceptions;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  import static org.apache.commons.lang3.StringUtils.isBlank;
23  
24  import java.io.IOException;
25  import java.io.Reader;
26  
27  import org.apache.commons.io.IOUtils;
28  
29  import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
30  import ca.uhn.fhir.util.CoverageIgnore;
31  
32  @CoverageIgnore
33  public class NonFhirResponseException extends BaseServerResponseException {
34  
35  	private static final long serialVersionUID = 1L;
36  
37  	/**
38  	 * Constructor
39  	 * 
40  	 * @param theMessage
41  	 *            The message
42  	 * @param theResponseText
43  	 * @param theStatusCode
44  	 * @param theResponseReader
45  	 * @param theContentType
46  	 */
47  	NonFhirResponseException(int theStatusCode, String theMessage) {
48  		super(theStatusCode, theMessage);
49  	}
50  
51  	public static NonFhirResponseException newInstance(int theStatusCode, String theContentType, Reader theReader) {
52  		String responseBody = "";
53  		try {
54  			responseBody = IOUtils.toString(theReader);
55  		} catch (IOException e) {
56  			IOUtils.closeQuietly(theReader);
57  		}
58  
59  		NonFhirResponseException retVal;
60  		if (isBlank(theContentType)) {
61  			retVal = new NonFhirResponseException(theStatusCode, "Response contains no Content-Type");
62  		} else if (theContentType.contains("text")) {
63  			retVal = new NonFhirResponseException(theStatusCode, "Response contains non FHIR Content-Type '" + theContentType + "' : " + responseBody);
64  		} else {
65  			retVal = new NonFhirResponseException(theStatusCode, "Response contains non FHIR Content-Type '" + theContentType + "'");
66  		}
67  
68  		retVal.setResponseBody(responseBody);
69  		return retVal;
70  	}
71  
72  }