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