001/* 002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Antoine Taillefer <[email protected]> 018 */ 019package org.nuxeo.jaxrs.test; 020 021import java.io.InputStream; 022 023import javax.ws.rs.core.Response.StatusType; 024 025import com.sun.jersey.api.client.ClientResponse; 026import com.sun.jersey.core.header.InBoundHeaders; 027import com.sun.jersey.spi.MessageBodyWorkers; 028 029/** 030 * Wraps a {@link ClientResponse} to make it {@link AutoCloseable}. 031 * 032 * @since 9.3 033 */ 034public class CloseableClientResponse extends ClientResponse implements AutoCloseable { 035 036 public CloseableClientResponse(StatusType statusType, InBoundHeaders headers, InputStream entity, 037 MessageBodyWorkers workers) { 038 super(statusType, headers, entity, workers); 039 } 040 041 /** 042 * Makes the given {@link ClientResponse} {@link AutoCloseable} by wrapping it in a {@link CloseableClientResponse}. 043 */ 044 public static CloseableClientResponse of(ClientResponse response) { 045 return new CloseableClientResponse(response.getStatusInfo(), getResponseHeaders(response), 046 response.getEntityInputStream(), response.getClient().getMessageBodyWorkers()); 047 } 048 049 protected static InBoundHeaders getResponseHeaders(ClientResponse response) { 050 InBoundHeaders inBoundHeaders = new InBoundHeaders(); 051 inBoundHeaders.putAll(response.getHeaders()); 052 return inBoundHeaders; 053 } 054 055}