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