处理主体json和HttpServletRequest的内容类型

时间:2018-11-15 15:50:03

标签: java json ndjson

我正在使用http Gateway代码制作补丁。

问题:应用程序发送内容类型为“ application / json”的http请求,但其主体具有多个json。我的工作是通过以下方式过滤并重写此类http请求:

  1. 提取Json身体
  2. 将Json主体重写为NEWLINE分隔格式(NDJSON)
  3. 将内容类型更改为“ application / x-ndjson”

我尝试仅更改内容类型,但不能解决问题,因为正文是错误格式的。

使用简单的json接收到错误的正文:“ content-type” =“ application / json”,

{json body 1} {json body 2} {json body 3}

具有多个json的正确正文:content-type =“ application / x-ndjson”,应采用这种格式

{json body 1} NEWLINE
{json body 2} NEWLINE
{json body 3} NEWLINE
NEWLINE

这是我的代码:

import com.google.common.collect.Lists;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.hadoop.gateway.SpiGatewayMessages;
import org.apache.hadoop.gateway.SpiGatewayResources;
import org.apache.hadoop.gateway.audit.api.Auditor;
import org.apache.hadoop.gateway.dispatch.DefaultDispatch;
import org.apache.hadoop.gateway.i18n.messages.MessagesFactory;
import org.apache.hadoop.gateway.i18n.resources.ResourcesFactory;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.client.methods.RequestBuilder;

public class ElasticsearchDispatch
  extends DefaultDispatch
{
  protected static SpiGatewayMessages LOG = (SpiGatewayMessages)MessagesFactory.get(SpiGatewayMessages.class);
  protected static SpiGatewayResources RES = (SpiGatewayResources)ResourcesFactory.get(SpiGatewayResources.class);


  public ElasticsearchDispatch() {}

  protected void executeRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest, HttpServletResponse outboundResponse)
    throws IOException
  {
    List<String> excludeHeaders = Lists.newArrayList(new String[] { "Content-Length" });
    Enumeration<String> headersName = inboundRequest.getHeaderNames();
    while (headersName.hasMoreElements()) {
      String headerName = (String)headersName.nextElement();
      if ((outboundRequest.getHeaders(headerName).length == 0) && (!excludeHeaders.contains(headerName))) {
        outboundRequest.addHeader(headerName, inboundRequest.getHeader(headerName));
      }
    }

    HttpResponse inboundResponse = executeOutboundRequest(outboundRequest);
    writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse);

  }

  private HttpUriRequest removeParameter(HttpUriRequest request, String param) throws URISyntaxException
  {
    URI uri = request.getURI();
    URIBuilder builder = new URIBuilder(uri);
    List<NameValuePair> nameValuePairs = builder.getQueryParams();
    if ((nameValuePairs != null) && (!nameValuePairs.isEmpty())) {
      builder.clearParameters();
      for (NameValuePair nvp : nameValuePairs) {
        if (!param.equals(nvp.getName())) {
          builder.setParameter(nvp.getName(), nvp.getValue());
        }
      }
    }
    ((HttpRequestBase)request).setURI(builder.build());

    return request;
  }

  protected HttpResponse executeOutboundRequest(HttpUriRequest outboundRequest) throws IOException
  {
    LOG.dispatchRequest(outboundRequest.getMethod(), outboundRequest.getURI());
    HttpResponse inboundResponse = null;
    try {
      inboundResponse = client.execute(removeParameter(outboundRequest, "doAs"));
    }
    catch (IOException e) {
      LOG.dispatchServiceConnectionException(outboundRequest.getURI(), e);
      auditor.audit("dispatch", outboundRequest.getURI().toString(), "uri", "failure");
      throw new IOException(RES.dispatchConnectionError());
    }
    catch (URISyntaxException e) {
      e.printStackTrace();
    } finally {
      if (inboundResponse != null) {
        int statusCode = inboundResponse.getStatusLine().getStatusCode();
        if (statusCode != 201) {
          LOG.dispatchResponseStatusCode(statusCode);
        } else {
          Header location = inboundResponse.getFirstHeader("Location");
          if (location == null) {
            LOG.dispatchResponseStatusCode(statusCode);
          } else {
            LOG.dispatchResponseCreatedStatusCode(statusCode, location.getValue());
          }
        }
        auditor.audit("dispatch", outboundRequest.getURI().toString(), "uri", "success", RES.responseStatus(statusCode));
      } else {
        auditor.audit("dispatch", outboundRequest.getURI().toString(), "uri", "unavailable");
      }
    }
    return inboundResponse;
  }

  protected HttpEntity createRequestEntity(HttpServletRequest request)
    throws IOException
  {
    String contentType = request.getContentType();
    int contentLength = request.getContentLength();
    InputStream contentStream = request.getInputStream();
    HttpEntity entity;
    if (contentType == null) {
      entity = new InputStreamEntity(contentStream, contentLength);
    } else {
      entity = new InputStreamEntity(contentStream, contentLength, ContentType.parse(contentType));
    }
    return entity;
  }
}

0 个答案:

没有答案