Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jcouchdb.db;
  
  import org.slf4j.Logger;
 
 import java.util.List;
 import java.util.Map;

Encapsulates a couchdb server response with error code and received body.

Author(s):
fforw at gmx dot de
 
 public class Response
 {
     protected static Logger log = LoggerFactory.getLogger(Response.class);
 
     private int code;
 
     private JSONParser parser;
 
     private Header[] headers;
 
     private InputStream inputStream;
 
 
     private byte[] content;
 
     public Response(int codeString s)
     {
         this(codenew ByteArrayInputStream(s.getBytes()), null);
     }
 
 
     public Response(int codeInputStream streamint length)
     {
         this(codestreamnull);
     }
 
     public Response(HttpResponse responsethrows IOException
     {
         this(response.getStatusLine().getStatusCode(), response.getEntity().getContent(), response.getAllHeaders());
     }
 
     public Response(int codeInputStream streamHeader[] headers)
     {
         Assert.notNull(stream"stream can't be null");
 
         this. = stream;
         this. = code;
         this. = headers;
 
         .trace("ctor {}"this);
     }
 
     public void setParser(JSONParser parser)
     {
         this. = parser;
     }
 
 
     private JSONParser getParser()
     {
         if ( == null)
         {
              = new JSONParser();
         }
         return ;
     }
 
 
     public int getCode()
     {
         return ;
     }
 
 
     public byte[] getContent()
     {
         if ( == null)
         {
             try
             {
                  = IOUtils.toByteArray();
             }
             catch (IOException e)
             {
                 throw new DataAccessException("error reading content from response"null);
            }
            finally
            {
                try
                {
                    .close();
                }
                catch (IOException e)
                {
                    throw new DataAccessException("error closing input stream of response"null);
                }
            }
        }
        return ;
    }
    public String getContentAsString()
    {
        if (.isDebugEnabled())
        {
            .debug("getContentAsString on " + this);
        }
        return new String(getContent());
    }


    
Returns the contents of the response as List

Returns:
    public List getContentAsList()
    {
        List list = getParser().parse(List.classgetCharacterSource());
        return list;
    }


    
Returns the contents of the response as Map

Returns:
    public Map getContentAsMap()
    {
        Map map = getParser().parse(Map.classgetCharacterSource());
        return map;
    }


    
Returns the contents of the response as bean of the given type.

Returns:
    public <T> T getContentAsBean(Class<T> cls)
    {
        T t = getParser().parse(clsgetCharacterSource());
        return t;
    }
    {
        if ( == null)
        {
             = new InputStreamSource(false);
        }
        return ;
    }
    public Header[] getResponseHeaders()
    {
        return ;
    }
    public InputStream getInputStream()
    {
        return ;
    }


    
Returns true if the response code is between 200 and 299

Returns:
    public boolean isOk()
    {
        return  >= 200 &&  <= 299;
    }
    @Override
    public String toString()
    {
        return super.toString() + ": code = " +  + ", stream = " + ;
    }
    public void destroy()
    {
        if ( != null)
        {
            try
            {
                .close();
            }
            catch (IOException e)
            {
                .warn("error trying to close the input stream"e);
            }
        }
    }
New to GrepCode? Check out our FAQ X