Skip to content

Commit

Permalink
Refactor code using the Jackson API directly. IQSS#6810
Browse files Browse the repository at this point in the history
  • Loading branch information
poikilotherm committed Sep 11, 2020
1 parent 46b7633 commit 208adf3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.harvard.iq.dataverse.api.batchjob;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.harvard.iq.dataverse.api.AbstractApiBean;
import edu.harvard.iq.dataverse.batch.entities.JobExecutionEntity;

Expand All @@ -9,6 +8,8 @@
import javax.batch.runtime.JobExecution;
import javax.batch.runtime.JobInstance;
import javax.ejb.Stateless;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
Expand All @@ -26,7 +27,7 @@ public class BatchJobResource extends AbstractApiBean {

private static String EMPTY_JSON_LIST = "[]";
private static String EMPTY_JSON_OBJ = "{}";
private static ObjectMapper mapper = new ObjectMapper();
private static Jsonb mapper = JsonbBuilder.create();

@GET
@Path("/jobs")
Expand All @@ -46,7 +47,7 @@ public Response listBatchJobs() {
}
}
}
return Response.ok("{ \"jobs\": \n" + mapper.writeValueAsString(executionEntities) + "\n}").build();
return Response.ok("{ \"jobs\": \n" + mapper.toJson(executionEntities) + "\n}").build();
} catch (Exception e) {
return Response.ok(EMPTY_JSON_LIST).build();
}
Expand All @@ -67,7 +68,7 @@ public Response listBatchJobsByName( @PathParam("jobName") String jobName) {
executionEntities.add(JobExecutionEntity.create(execution));
}
}
return Response.ok("{ \"jobs\": \n" + mapper.writeValueAsString(executionEntities) + "\n}").build();
return Response.ok("{ \"jobs\": \n" + mapper.toJson(executionEntities) + "\n}").build();
} catch (Exception e) {
return Response.ok(EMPTY_JSON_LIST).build();
}
Expand All @@ -80,7 +81,7 @@ public Response listBatchJobsByName( @PathParam("jobName") String jobName) {
public Response listBatchJobById(@PathParam("jobId") String jobId) {
try {
JobExecution execution = BatchRuntime.getJobOperator().getJobExecution(Long.valueOf(jobId));
return Response.ok(mapper.writeValueAsString(JobExecutionEntity.create(execution))).build();
return Response.ok(mapper.toJson(JobExecutionEntity.create(execution))).build();
} catch (Exception e) {
return Response.ok(EMPTY_JSON_OBJ).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package edu.harvard.iq.dataverse.batch.jobs.importer.filesystem;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.harvard.iq.dataverse.DataFileServiceBean;
import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.DatasetLock;
Expand Down Expand Up @@ -55,6 +53,8 @@
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -312,7 +312,7 @@ private void doReport() {
jobExecutionEntity.setExitStatus("COMPLETED");
jobExecutionEntity.setStatus(BatchStatus.COMPLETED);
jobExecutionEntity.setEndTime(date);
jobJson = new ObjectMapper().writeValueAsString(jobExecutionEntity);
jobJson = JsonbBuilder.create().toJson(jobExecutionEntity);

String logDir = System.getProperty("com.sun.aas.instanceRoot") + SEP + "logs" + SEP + "batch-jobs" + SEP;

Expand All @@ -339,7 +339,7 @@ private void doReport() {
getJobLogger().log(Level.SEVERE, "Job execution is null");
}

} catch (NoSuchJobExecutionException | JobSecurityException | JsonProcessingException e) {
} catch (NoSuchJobExecutionException | JobSecurityException | JsonbException e) {
getJobLogger().log(Level.SEVERE, "Creating job json: " + e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
Developed at the Institute for Quantitative Social Science, Harvard University.
*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.path.json.JsonPath;
Expand Down Expand Up @@ -54,6 +53,9 @@
import java.util.Properties;
import java.util.Random;
import java.util.UUID;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbException;

import static com.jayway.restassured.RestAssured.given;
import static junit.framework.Assert.assertEquals;
Expand All @@ -72,7 +74,7 @@
*/
public class FileRecordJobIT {

private static final ObjectMapper mapper = new ObjectMapper();
private static Jsonb mapper = JsonbBuilder.create();
private static ClassLoader classLoader = FileRecordJobIT.class.getClassLoader();
private static final String SEP = File.separator;

Expand Down Expand Up @@ -1281,9 +1283,9 @@ private JobExecutionEntity getJob() {
String jobResult = pollJobStatus(jobId, token, Integer.valueOf(props.getProperty("polling.retries")),
Integer.valueOf(props.getProperty("polling.wait")));
System.out.println("JOB JSON: " + jobResult);
return mapper.readValue(jobResult, JobExecutionEntity.class);
} catch (IOException ioe) {
System.out.println("Error getting job execution entity: " + ioe.getMessage());
return mapper.fromJson(jobResult, JobExecutionEntity.class);
} catch (JsonbException e) {
System.out.println("Error getting job execution entity: " + e.getMessage());
return null;
}
}
Expand All @@ -1305,9 +1307,9 @@ private JobExecutionEntity getJobWithToken(String userToken) {
String jobResult = pollJobStatus(jobId, token, Integer.valueOf(props.getProperty("polling.retries")),
Integer.valueOf(props.getProperty("polling.wait")));
System.out.println("JOB JSON: " + jobResult);
return mapper.readValue(jobResult, JobExecutionEntity.class);
} catch (IOException ioe) {
System.out.println("Error getting job execution entity: " + ioe.getMessage());
return mapper.fromJson(jobResult, JobExecutionEntity.class);
} catch (JsonbException e) {
System.out.println("Error getting job execution entity: " + e.getMessage());
return null;
}
}
Expand All @@ -1328,9 +1330,9 @@ private JobExecutionEntity getJobWithMode(String mode) {
String jobResult = pollJobStatus(jobId, token, Integer.valueOf(props.getProperty("polling.retries")),
Integer.valueOf(props.getProperty("polling.wait")));
System.out.println("JOB JSON: " + jobResult);
return mapper.readValue(jobResult, JobExecutionEntity.class);
} catch (IOException ioe) {
System.out.println("Error getting job execution entity: " + ioe.getMessage());
return mapper.fromJson(jobResult, JobExecutionEntity.class);
} catch (JsonbException e) {
System.out.println("Error getting job execution entity: " + e.getMessage());
return null;
}
}
Expand Down

0 comments on commit 208adf3

Please sign in to comment.