Я работаю над кодом шлюза http, чтобы сделать патч.
Проблема: приложение отправляет http-запрос с типом содержимого "application / json", но в его теле есть несколько json. Моя работа состоит в том, чтобы отфильтровать и переписать этот тип http-запроса:
- Извлечение тела Джсона
- Переписать тело Json в формате NEWLINE с разделителями (NDJSON)
- Изменить тип содержимого на "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;
}
}