001package org.hl7.fhir.r4.utils.client; 002 003 004 005 006 007/* 008 Copyright (c) 2011+, HL7, Inc. 009 All rights reserved. 010 011 Redistribution and use in source and binary forms, with or without modification, 012 are permitted provided that the following conditions are met: 013 014 * Redistributions of source code must retain the above copyright notice, this 015 list of conditions and the following disclaimer. 016 * Redistributions in binary form must reproduce the above copyright notice, 017 this list of conditions and the following disclaimer in the documentation 018 and/or other materials provided with the distribution. 019 * Neither the name of HL7 nor the names of its contributors may be used to 020 endorse or promote products derived from this software without specific 021 prior written permission. 022 023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 025 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 026 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 027 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 028 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 029 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 030 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 031 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 032 POSSIBILITY OF SUCH DAMAGE. 033 034*/ 035 036 037import java.util.ArrayList; 038import java.util.List; 039 040import org.hl7.fhir.r4.model.Resource; 041 042public class ResourceRequest<T extends Resource> { 043 private T payload; 044 private int httpStatus = -1; 045 private String location; 046 private List<Integer> successfulStatuses = new ArrayList<Integer>(); 047 private List<Integer> errorStatuses = new ArrayList<Integer>(); 048 049 public ResourceRequest(T payload, int httpStatus, List<Integer> successfulStatuses, List<Integer> errorStatuses, String location) { 050 this.payload = payload; 051 this.httpStatus = httpStatus; 052 if(successfulStatuses != null) { 053 this.successfulStatuses.addAll(successfulStatuses); 054 } 055 if(errorStatuses != null) { 056 this.errorStatuses.addAll(errorStatuses); 057 } 058 this.location = location; 059 } 060 061 public ResourceRequest(T payload, int httpStatus, String location) { 062 this.payload = payload; 063 this.httpStatus = httpStatus; 064 this.location = location; 065 } 066 067 public ResourceRequest(T payload, int httpStatus, int successfulStatus, String location) { 068 this.payload = payload; 069 this.httpStatus = httpStatus; 070 this.successfulStatuses.add(successfulStatus); 071 this.location = location; 072 } 073 074 public int getHttpStatus() { 075 return httpStatus; 076 } 077 078 public T getPayload() { 079 return payload; 080 } 081 082 public T getReference() { 083 T payloadResource = null; 084 if(payload != null) { 085 payloadResource = payload; 086 } 087 return payloadResource; 088 } 089 090 public boolean isSuccessfulRequest() { 091 return successfulStatuses.contains(httpStatus) && !errorStatuses.contains(httpStatus) && httpStatus > 0; 092 } 093 094 public boolean isUnsuccessfulRequest() { 095 return !isSuccessfulRequest(); 096 } 097 098 public void addSuccessStatus(int status) { 099 this.successfulStatuses.add(status); 100 } 101 102 public void addErrorStatus(int status) { 103 this.errorStatuses.add(status); 104 } 105 106 public String getLocation() { 107 return location; 108 } 109}