001/*
002 * Copyright 2011-2016 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package org.apache.wink.client;
019
020import javax.ws.rs.core.MultivaluedMap;
021import javax.ws.rs.core.Response;
022import java.io.ByteArrayInputStream;
023import java.io.ByteArrayOutputStream;
024import java.io.IOException;
025import java.io.InputStream;
026
027/**
028 *  Wink compatibility layer class - see Wink docs.
029 */
030public class ClientResponse
031{
032  private Response response;
033
034  /**
035   *  Wink compatibility layer class - see Wink docs.
036   * @param response Wink compatibility layer class - see Wink docs.
037   */
038  ClientResponse(final Response response)
039  {
040    this.response = response;
041  }
042
043  /**
044   *  Wink compatibility layer class - see Wink docs.
045   * @param entityClass Wink compatibility layer class - see Wink docs.
046   * @param <T> Wink compatibility layer class - see Wink docs.
047   * @return Wink compatibility layer class - see Wink docs.
048   */
049  public <T> T getEntity(final Class<T> entityClass)
050  {
051    T entity = response.readEntity(entityClass);
052
053    if(entity instanceof InputStream)
054    {
055      byte [] buffer = new byte[1024];
056      ByteArrayOutputStream bos = new ByteArrayOutputStream();
057      int readLength;
058      try
059      {
060        do
061        {
062          readLength = ((InputStream) entity).read(buffer);
063          if (readLength > 0)
064          {
065            bos.write(buffer, 0, readLength);
066          }
067        } while (readLength != -1);
068
069        return (T) (new ByteArrayInputStream(bos.toByteArray()));
070      }
071      catch (IOException ex)
072      {
073        throw new RuntimeException(
074            "Caught exception trying to read entity.", ex);
075      }
076    }
077
078    return entity;
079  }
080
081  /**
082   *  Wink compatibility layer class - see Wink docs.
083   * @return Wink compatibility layer class - see Wink docs.
084   */
085  public Response.Status getStatusType()
086  {
087    return Response.Status.fromStatusCode(response.getStatus());
088  }
089
090  /**
091   *  Wink compatibility layer class - see Wink docs.
092   * @return Wink compatibility layer class - see Wink docs.
093   */
094  public int getStatusCode()
095  {
096    return response.getStatus();
097  }
098
099  /**
100   *  Wink compatibility layer class - see Wink docs.
101   * @return Wink compatibility layer class - see Wink docs.
102   */
103  public String getMessage()
104  {
105    return response.getStatusInfo().getReasonPhrase();
106  }
107
108  /**
109   *  Wink compatibility layer class - see Wink docs.
110   * @throws IOException Wink compatibility layer class - see Wink docs.
111   */
112  public void consumeContent() throws IOException
113  {
114    InputStream inputStream = getEntity(InputStream.class);
115    try
116    {
117      while (inputStream.read(new byte[512]) != -1)
118      {
119
120      }
121    }
122    catch (IOException ex)
123    {
124      // nothing can really be done about this, so ignore it.
125    }
126  }
127
128  /**
129   *  Wink compatibility layer class - see Wink docs.
130   * @return Wink compatibility layer class - see Wink docs.
131   */
132  public MultivaluedMap<String, String> getHeaders()
133  {
134    return response.getStringHeaders();
135  }
136
137  /**
138   * Wink compatibility layer class.
139   * This method provides a way to close the private Response member for this
140   * ClientResponse.
141   */
142  public void close()
143  {
144    if (response != null)
145    {
146      response.close();
147    }
148  }
149}