Skip to content

Commit

Permalink
Export: @JsonIgnore (split property) on exportObjects, superExportObj…
Browse files Browse the repository at this point in the history
…ects - temp workaround

Signed-off-by: qGYdXbY2 <[email protected]>
  • Loading branch information
qGYdXbY2 committed Aug 2, 2024
1 parent 85cd8cb commit 0e16852
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class Path extends ApiParam.Path {

public static class HQuery extends Query{
static final String TARGET_SPACEID = "targetSpaceId";
public static final String EXPORT_OBJECTS = "exportObjects";
public static final String FORCE = "force";
public static final String DELETE_DATA = "deleteData";
public static final String H_COMMAND = "command";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_GATEWAY;
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static io.netty.handler.codec.http.HttpResponseStatus.CREATED;
import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;

Expand All @@ -33,18 +34,28 @@
import com.here.xyz.httpconnector.rest.HApiParam.HQuery.Command;
import com.here.xyz.httpconnector.rest.HApiParam.Path;
import com.here.xyz.httpconnector.task.JobHandler;
import com.here.xyz.httpconnector.util.jobs.Export;
import com.here.xyz.httpconnector.util.jobs.ExportObject;
import com.here.xyz.httpconnector.util.jobs.Import;
import com.here.xyz.httpconnector.util.jobs.Job;
import com.here.xyz.httpconnector.util.jobs.RuntimeStatus;
import com.here.xyz.hub.rest.Api;
import com.here.xyz.util.service.HttpException;
import com.here.xyz.util.service.logging.LogUtil;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.EncodeException;
import io.vertx.core.json.Json;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.openapi.RouterBuilder;

import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JobApi extends Api {

Expand Down Expand Up @@ -94,22 +105,75 @@ private void patchJob(final RoutingContext context) {
}
}

private void _sendResponse(RoutingContext context, HttpResponseStatus status, Object o) {
// temp. workaround - only called when parm "exportObjects=true"
HttpServerResponse httpResponse = context.response().setStatusCode(status.code());

byte[] response;
try
{
if(o instanceof Export ex)
{ Map<String,ExportObject> exo = ex.getExportObjects(),
supexo = ex.getSuperExportObjects();

String serExo = (exo == null || exo.isEmpty() ? null : XyzSerializable.serialize(exo) ),
serSupexo = (supexo == null || supexo.isEmpty() ? null : XyzSerializable.serialize(supexo) );

if( serExo == null && serSupexo == null )
{ sendResponse(context, status, o);
return;
}

String injExportJstr = String.format(",%s%s%s", serExo != null ? "\"exportObjects\":" + serExo : ""
, serExo != null && serSupexo != null ? "," : ""
, serSupexo != null ? "\"superExportObjects\":" + serSupexo : "" );

String sJob = Json.encode(o);

int lastCurlyBracket = sJob.lastIndexOf("}");

sJob = sJob.substring(0, lastCurlyBracket) + injExportJstr + "}";

response = sJob.getBytes();
}
else if( o instanceof List listEx )
{ //TODO: in case List<Exports> needs to be supported
response = Json.encode(o).getBytes();
}
else
{
sendResponse(context, status, o);
return;
}

}
catch (EncodeException e) {
sendErrorResponse(context, new HttpException(INTERNAL_SERVER_ERROR, "Could not serialize response.", e));
return;
}

sendResponseBytes(context, httpResponse, response);
}


private void getJob(final RoutingContext context) {
String jobId = context.pathParam(Path.JOB_ID);
boolean exportObjects = HQuery.getBoolean(context, HQuery.EXPORT_OBJECTS , false);

JobHandler.loadJob(jobId, LogUtil.getMarker(context))
.onFailure(e -> this.sendError(e, context))
.onSuccess(job -> this.sendResponse(context, OK, job));
.onSuccess(job -> { if( exportObjects ) this._sendResponse(context, OK, job); else this.sendResponse(context, OK, job); });
}

private void getJobs(final RoutingContext context) {
String jobType = HQuery.getJobType(context);
Job.Status jobStatus = HQuery.getJobStatus(context);
String targetSpaceId = HQuery.getString(context, HQuery.TARGET_SPACEID , null);
boolean exportObjects = HQuery.getBoolean(context, HQuery.EXPORT_OBJECTS , false);

JobHandler.loadJobs(LogUtil.getMarker(context), jobType, jobStatus, targetSpaceId)
.onFailure(e -> this.sendError(e, context))
.onSuccess(jobs -> this.sendResponse(context, OK, jobs));
.onSuccess(jobs -> { if( exportObjects ) this._sendResponse(context, OK, jobs); else this.sendResponse(context, OK, jobs); } );
}

private void deleteJob(final RoutingContext context) {
Expand Down Expand Up @@ -189,7 +253,7 @@ protected void patchJobStatus(RoutingContext context) throws HttpException {
}

protected void getJobStatus(RoutingContext context) {
JobHandler.loadJob(context.pathParam(Path.JOB_ID), LogUtil.getMarker(context))
JobHandler.loadJob(context.pathParam(Path.JOB_ID), LogUtil.getMarker(context) )
.onFailure(e -> this.sendError(e, context))
.onSuccess(job -> this.sendResponse(context, OK, job.getRuntimeStatus()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import com.google.common.collect.ImmutableList;
import com.here.xyz.events.ContextAwareEvent.SpaceContext;
Expand Down Expand Up @@ -114,10 +115,10 @@ public class Export extends JDBCBasedJob<Export> {
@JsonIgnore
private AtomicBoolean executing = new AtomicBoolean();

@JsonView({Public.class})
@JsonIgnore
private Map<String,ExportObject> exportObjects;

@JsonView({Public.class})
@JsonIgnore
private Map<String,ExportObject> superExportObjects;

@JsonView({Public.class})
Expand Down Expand Up @@ -526,7 +527,7 @@ public void addStatistic(ExportStatistic statistic) {
}
}

@JsonView({Public.class})
@JsonIgnore
public Map<String,ExportObject> getSuperExportObjects() {
if (CService.jobS3Client == null) //If being used as a model on the client side
return this.superExportObjects == null ? Collections.emptyMap() : this.superExportObjects;
Expand All @@ -539,6 +540,7 @@ public Map<String,ExportObject> getSuperExportObjects() {
return superExportObjects == null ? Collections.emptyMap() : superExportObjects;
}

@JsonProperty
public void setSuperExportObjects(Map<String, ExportObject> superExportObjects) {
this.superExportObjects = superExportObjects;
}
Expand All @@ -555,7 +557,7 @@ public List<ExportObject> getExportObjectsAsList() {
return exportObjectList;
}

@JsonView({Public.class})
@JsonIgnore
public Map<String,ExportObject> getExportObjects() {
if (CService.jobS3Client == null) //If being used as a model on the client side
return exportObjects == null ? Collections.emptyMap() : exportObjects;
Expand All @@ -578,7 +580,8 @@ else if (getS3Key() != null) {

return exportObjects == null ? Collections.emptyMap() : exportObjects;
}


@JsonProperty
public void setExportObjects(Map<String, ExportObject> exportObjects) {
this.exportObjects = exportObjects;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.here.xyz.XyzSerializable;
import com.here.xyz.XyzSerializable.SerializationView;
import com.here.xyz.httpconnector.rest.HApiParam;
import com.here.xyz.httpconnector.rest.HApiParam.HQuery;
import com.here.xyz.httpconnector.util.jobs.Job;
import com.here.xyz.hub.Service;
import com.here.xyz.hub.auth.Authorization;
Expand Down Expand Up @@ -138,14 +139,17 @@ private void patchJob(final RoutingContext context) {
private void getJob(final RoutingContext context) {
String spaceId = context.pathParam(ApiParam.Path.SPACE_ID);
String jobId = context.pathParam(HApiParam.Path.JOB_ID);
boolean bExportObjects = HApiParam.HQuery.getBoolean(context, HApiParam.HQuery.EXPORT_OBJECTS, false);
String paramExportObjects = bExportObjects ? "?" + HApiParam.HQuery.EXPORT_OBJECTS + "=true" : "";


Authorization.authorizeManageSpacesRights(context, spaceId)
.onSuccess(auth -> {
Service.webClient.getAbs(Service.configuration.HTTP_CONNECTOR_ENDPOINT+"/jobs/"+jobId)
Service.webClient.getAbs(Service.configuration.HTTP_CONNECTOR_ENDPOINT+"/jobs/"+jobId + paramExportObjects )
.timeout(JOB_API_TIMEOUT)
.putHeader("content-type", "application/json; charset=" + Charset.defaultCharset().name())
.send()
.onSuccess(res -> jobAPIResultHandler(context,res,spaceId))
.onSuccess(res -> { if( !bExportObjects ) jobAPIResultHandler(context,res,spaceId); else _jobAPIResultHandler(context,res,spaceId); })
.onFailure(f -> this.sendErrorResponse(context, new HttpException(BAD_GATEWAY, "Job-Api not ready!")));
})
.onFailure(f -> this.sendErrorResponse(context, new HttpException(FORBIDDEN, "No access to this space!")));
Expand All @@ -155,9 +159,11 @@ private void getJobs(final RoutingContext context) {
String spaceId = context.pathParam(ApiParam.Path.SPACE_ID);
Job.Status status = HApiParam.HQuery.getJobStatus(context);

String exportObjects = HApiParam.HQuery.getBoolean(context, HApiParam.HQuery.EXPORT_OBJECTS, false) ? "&" + HApiParam.HQuery.EXPORT_OBJECTS + "=true" : "";

Authorization.authorizeManageSpacesRights(context, spaceId)
.onSuccess(auth -> {
Service.webClient.getAbs(Service.configuration.HTTP_CONNECTOR_ENDPOINT+"/jobs?targetSpaceId="+spaceId+(status != null ? "&status="+status : ""))
Service.webClient.getAbs(Service.configuration.HTTP_CONNECTOR_ENDPOINT+"/jobs?targetSpaceId="+spaceId+(status != null ? "&status="+status : "") + exportObjects)
.timeout(JOB_API_TIMEOUT)
.putHeader("content-type", "application/json; charset=" + Charset.defaultCharset().name())
.send()
Expand Down Expand Up @@ -268,6 +274,31 @@ private void postExecute(final RoutingContext context) {
.onFailure(f -> this.sendErrorResponse(context, new HttpException(FORBIDDEN, "No access to this space!")));
}


private void _jobAPIResultHandler(final RoutingContext context, HttpResponse<Buffer> res, String spaceId){
// temp. workaround - only called when parm "exportObjects=true"
if (res.statusCode() < 500) {
try {
if(!checkSpaceId(res,spaceId)){
this.sendErrorResponse(context, new HttpException(FORBIDDEN, "This job belongs to another space!"));
return;
}
} catch (DecodeException e) {}

// try{
// this.sendResponse(context, HttpResponseStatus.valueOf(res.statusCode()),
// Json.decodeValue(DatabindCodec.mapper().writerWithView(Job.Public.class).writeValueAsString(res.bodyAsJson(Job.class))));
// return;
// } catch (Exception e){}
try{
this.sendResponse(context, HttpResponseStatus.valueOf(res.statusCode()), res.bodyAsJsonObject());
return;
} catch (Exception e){}
}

this.sendErrorResponse(context, new HttpException(HttpResponseStatus.valueOf(res.statusCode()), "Job-Api not ready!"));
}

private void jobAPIResultHandler(final RoutingContext context, HttpResponse<Buffer> res, String spaceId){
if (res.statusCode() < 500) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ protected static Job loadJob(String spaceId, String jobId) {
.accept(APPLICATION_JSON)
.contentType(APPLICATION_JSON)
.headers(getAuthHeaders(AuthProfile.ACCESS_ALL))
.get("/spaces/" + spaceId + "/job/"+jobId);
.param("exportObjects", true)
.get("/spaces/" + spaceId + "/job/"+jobId );

String body = response.getBody().asString();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ protected void sendResponseWithXyzSerialization(RoutingContext context, HttpResp
sendResponseBytes(context, httpResponse, response);
}

private void sendResponseBytes(RoutingContext context, HttpServerResponse httpResponse, byte[] response) {
protected void sendResponseBytes(RoutingContext context, HttpServerResponse httpResponse, byte[] response) {
if (response.length == 0)
httpResponse.setStatusCode(NO_CONTENT.code()).end();
else if (response.length > getMaxResponseLength(context))
Expand Down

0 comments on commit 0e16852

Please sign in to comment.