Skip to content

Commit

Permalink
add legend response format header to http response (finos#2286)
Browse files Browse the repository at this point in the history
add legend response format header to http response
  • Loading branch information
AFine-gs authored Sep 29, 2023
1 parent 82ed6af commit 7b96273
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.finos.legend.engine.plan.execution.api.concurrent.ConcurrentExecutionNodeExecutorPoolInfo;
import org.finos.legend.engine.plan.execution.api.concurrent.ParallelGraphFetchExecutionExecutorPoolInfo;
import org.finos.legend.engine.plan.execution.api.request.RequestContextHelper;
import org.finos.legend.engine.plan.execution.api.result.ResultManager;
import org.finos.legend.engine.plan.execution.concurrent.ParallelGraphFetchExecutionExecutorPool;
import org.finos.legend.engine.plan.execution.graphFetch.GraphFetchExecutionConfiguration;
import org.finos.legend.engine.plan.execution.service.api.ServiceModelingApi;
Expand Down Expand Up @@ -410,7 +411,7 @@ private void enableCors(Environment environment)
corsFilter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,OPTIONS");
corsFilter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
corsFilter.setInitParameter(CrossOriginFilter.ALLOWED_TIMING_ORIGINS_PARAM, "*");
corsFilter.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin,Access-Control-Allow-Credentials,x-b3-parentspanid,x-b3-sampled,x-b3-spanid,x-b3-traceid," + RequestContextHelper.LEGEND_REQUEST_ID + RequestContextHelper.LEGEND_USE_PLAN_CACHE);
corsFilter.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin,Access-Control-Allow-Credentials,x-b3-parentspanid,x-b3-sampled,x-b3-spanid,x-b3-traceid," + RequestContextHelper.LEGEND_REQUEST_ID + "," + RequestContextHelper.LEGEND_USE_PLAN_CACHE + "," + ResultManager.LEGEND_RESPONSE_FORMAT);
corsFilter.setInitParameter(CrossOriginFilter.CHAIN_PREFLIGHT_PARAM, "false");
corsFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class ResultManager
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(ResultManager.class);
private static final JsonStringEncoder jsonStringEncoder = JsonStringEncoder.getInstance();

public static final String LEGEND_RESPONSE_FORMAT = "x-legend-response-format";


public static Response manageResult(MutableList<CommonProfile> pm, Result result, LoggingEventType loggingEventType)
{
return manageResult(pm, result, SerializationFormat.defaultFormat, loggingEventType);
Expand Down Expand Up @@ -68,7 +71,7 @@ private static Response manageResultWithCustomErrorCodeImpl(MutableList<CommonPr
}
else if (result instanceof StreamingResult)
{
return Response.ok(new StreamingResultHandler((StreamingResult) result, format)).build();
return Response.ok(new StreamingResultHandler((StreamingResult) result, format)).header(LEGEND_RESPONSE_FORMAT, ((StreamingResult) result).resultFormat).build();
}
else if (result instanceof ConstantResult)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

public abstract class StreamingResult extends Result
{
public String resultFormat = "FormatNotSet";

public abstract Builder getResultBuilder();

public abstract Serializer getSerializer(SerializationFormat format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ public class ExternalFormatSerializeResult extends StreamingResult
private final Result childResult;

public ExternalFormatSerializeResult(ExternalFormatWriter externalFormatWriter, Result childResult)
{
this(externalFormatWriter, childResult, "");
}

public ExternalFormatSerializeResult(ExternalFormatWriter externalFormatWriter, Result childResult, String resultFormat)
{
super(Lists.mutable.empty());
this.externalFormatWriter = externalFormatWriter;
this.childResult = childResult;
this.resultFormat = resultFormat;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Result executeExternalizeTDSExecutionNode(ExternalFormatExternalizeTDSExe
private Result streamArrowFromRelational(RelationalResult relationalResult) throws SQLException, IOException
{

return new ExternalFormatSerializeResult(new ArrowDataWriter(relationalResult.getResultSet()), relationalResult);
return new ExternalFormatSerializeResult(new ArrowDataWriter(relationalResult.getResultSet()), relationalResult, CONTENT_TYPE);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testExternalize() throws Exception
RelationalResult result = new RelationalResult(FastList.newListWith(new RelationalExecutionActivity("SELECT * FROM testtable", null)), mockExecutionNode, FastList.newListWith(new SQLResultColumn("testInt", "INTEGER"), new SQLResultColumn("testString", "VARCHAR"), new SQLResultColumn("testDate", "TIMESTAMP"), new SQLResultColumn("testBool", "TIMESTAMP")), null, "GMT", conn, null, null, null, new RequestContext());

ExternalFormatSerializeResult nodeExecute = (ExternalFormatSerializeResult) extension.executeExternalizeTDSExecutionNode(node, result, null, null);

Assert.assertEquals(nodeExecute.resultFormat, "application/x.arrow");
nodeExecute.stream(outputStream, SerializationFormat.DEFAULT);
assertArrow(outputStream, "TESTINT\tTESTSTRING\tTESTDATE\tTESTBOOL\n" +
"1\tA\t1577854800000\ttrue\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Result executeExternalizeExecutionNode(ExternalFormatExternalizeExecution
FlatDataContext<?> context = specifics.createContext();
FlatDataWriter<?> serializer = new FlatDataWriter(context, inputStream);

return new ExternalFormatSerializeResult(serializer, result);
return new ExternalFormatSerializeResult(serializer, result, CONTENT_TYPE);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public Result executeExternalizeExecutionNode(ExternalFormatExternalizeExecution
{
IJsonExternalizeExecutionNodeSpecifics nodeSpecifics = (IJsonExternalizeExecutionNodeSpecifics) specificsClass.getConstructor().newInstance();
JsonDataWriter<?> jsonDataWriter = new JsonDataWriter<>(nodeSpecifics, extractStreamFromResult(result), context);
return new ExternalFormatSerializeResult(jsonDataWriter, result);
return new ExternalFormatSerializeResult(jsonDataWriter, result, CONTENT_TYPE);
}
else
{
Expand Down

0 comments on commit 7b96273

Please sign in to comment.