Demo entry 6718625

MaskResponseHandler

   

Submitted by anonymous on Mar 05, 2018 at 12:56
Language: Java. Code size: 1.2 kB.

package com.webank.bdp.mask;

import com.alibaba.fastjson.JSON;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author chriscchen
 * @createtime 2018-03-04
 */
public class MaskResponseHandler implements ResponseHandler<MaskResponse> {
  @Override
  public MaskResponse handleResponse(HttpResponse response) throws IOException {
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
      throw new HttpResponseException(statusCode, statusLine.getReasonPhrase());
    }

    HttpEntity entity = response.getEntity();

    if (entity == null) {
      throw new ClientProtocolException("Response contains no content");
    }

    return JSON.parseObject(EntityUtils.toString(entity), MaskResponse.class);
  }
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).