Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2014 Dell, Inc.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *     http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.dell.doradus.common;
 
 import java.util.Map;

Represents an HTTP response, including the HTTP status code, message body (entity), and additional response headers.
 
 public class RESTResponse {
     // Members:
     private final HttpCode m_statusCode;
     private final byte[] m_bodyBytes;
     private final Map<StringStringm_headers;

    
Create a response with a code only. The response will contain no body or additional headers.

Parameters:
statusCode HTTP status code to use in reponse.
 
     public RESTResponse(HttpCode statusCode) {
          = statusCode;
          = null;
          = null;
     }   // constructor
 
    
Create a response with the given HTTP status code and body as a String. The response will contain no extra headers. The body's content-type will be text/plain.

Parameters:
statusCode HTTP status code to use in response.
body Body to be included in response (copied).
 
     public RESTResponse(HttpCode statusCodeString body) {
          = statusCode;
          = body == null ? null : Utils.toBytes(body);
          = null;
     }   // constructor
 
    
Create a response with the given HTTP status code, String body, and the given content-type header.

Parameters:
statusCode HTTP status code to use in response.
body Body to be included in response (copied).
contentType ContentType value to use for content-type header.
 
     public RESTResponse(HttpCode statusCodeString bodyContentType contentType) {
          = statusCode;
          = body == null ? null : Utils.toBytes(body);
          = new HashMap<>();
         .put(.contentType.toString());
     }   // constructor
     
    
Create a response with the given HTTP status, binary body, and additional headers.

Parameters:
statusCode HTTP status code to use in response.
body Body to be included in the response (copied).
headers Additional headers to be included in response.
 
     public RESTResponse(HttpCode statusCodebyte[] bodyMap<StringStringheaders) {
          = statusCode;
          = body == null ? null : Arrays.copyOf(bodybody.length);
         
          = new HashMap<StringString>(headers);
         if (headers != null) {
             // Copy headers as up-cased.
             for (Map.Entry<StringStringmapEntry : headers.entrySet()) {
                 .put(mapEntry.getKey().toUpperCase(), mapEntry.getValue());
             }
         }
     }   // constructor
   
     ///// getters
     
    
Get this response's HTTP status code (e.g., 200 = OK).

Returns:
This response's HTTP status code.
    public HttpCode getCode() {
        return ;
    }   // getCode
    
    
Get this response's message body (entity) as a String. The binary message body is converted to a String using UTF-8. If there is no message body, an empty string is returned.

Returns:
This response's entity as a String or an empty string if there isn't one.
    public String getBody() {
        return  == null ? "" : Utils.toString();
    }   // getBody
    
    
Get the content-type header from this response as a ContentType object.

Returns:
The ContentType header from this response or null if there isn't one.
    public ContentType getContentType() {
        String contentType = .get(.);
        if (contentType == null) {
            return null;
        }
        return new ContentType(contentType);
    }   // getContentType
    
    
Get this response's binary response body, if any.

Returns:
This response's binary body or null if there is none.
    public byte[] getBodyBytes() {
        return ;
    }   // getBodyBytes
    
    
Get this response's headers as a field/value map. Note that the headers are not copied so the caller shouldn't modify these.

Returns:
This response's headers as a field/value map. The map may be null or empty if there are no additional headers in this response.
    public Map<StringStringgetHeaders() {
        return ;
    }   // getHeaders
    
    
Return a summary of this response object. If the response's HTTP code is HttpCode.OK, then just just "{code}" is returned. If any other code is present, the result is "{code}: {body}". In both cases, {code} is whatever is returned by HttpCode.toString().

Returns:
The string "{code}" or "{code}: {body}".
    @Override
    public String toString() {
        if ( != . && . > 0) {
            return .toString() + ": " + Utils.toString();
        }
        return .toString();
    }   // toString
    
}   // class RESTResponse
New to GrepCode? Check out our FAQ X