public static void toJSON(final Object obj, final OutputStream stream) { toJSON(obj, stream, false); }
public static String toJSON(final Object obj) { return toJSON(obj, false); }
public static void toJSON(final Object obj, final File file) throws IOException { toJSON(obj, file, false); }
protected void writeJSON(final HttpServletResponse resp, final Object obj, final boolean pretty) throws IOException { resp.setContentType(JSON_MIME_TYPE); JSONUtils.toJSON(obj, resp.getOutputStream(), true); }
public static void toJSON(final Object obj, final File file, final boolean prettyPrint) throws IOException { final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file)); try { toJSON(obj, stream, prettyPrint); } finally { stream.close(); } }
public static String createJsonResponse(final String status, final String message, final String action, final Map<String, Object> params) { final HashMap<String, Object> response = new HashMap<>(); response.put("status", status); if (message != null) { response.put("message", message); } if (action != null) { response.put("action", action); } if (params != null) { response.putAll(params); } return JSONUtils.toJSON(response); }
public static String toJSONString(final Props props, final boolean localOnly) { final Map<String, String> map = toStringMap(props, localOnly); return JSONUtils.toJSON(map); }
public Map<String, Object> updateExecutions(final Executor executor, final List<ExecutableFlow> executions) throws ExecutorManagerException { final List<Long> updateTimesList = new ArrayList<>(); final List<Integer> executionIdsList = new ArrayList<>(); // We pack the parameters of the same host together before query for (final ExecutableFlow flow : executions) { executionIdsList.add(flow.getExecutionId()); updateTimesList.add(flow.getUpdateTime()); } final Pair<String, String> updateTimes = new Pair<>( ConnectorParams.UPDATE_TIME_LIST_PARAM, JSONUtils.toJSON(updateTimesList)); final Pair<String, String> executionIds = new Pair<>( ConnectorParams.EXEC_ID_LIST_PARAM, JSONUtils.toJSON(executionIdsList)); return callWithExecutionId(executor.getHost(), executor.getPort(), ConnectorParams.UPDATE_ACTION, null, null, executionIds, updateTimes); }
private void uploadFlow(final Project project, final int version, final Flow flow, final EncodingType encType) throws ProjectManagerException, IOException { final String json = JSONUtils.toJSON(flow.toObject()); final byte[] data = convertJsonToBytes(encType, json); logger.info("Flow upload " + flow.getId() + " is byte size " + data.length); final String INSERT_FLOW = "INSERT INTO project_flows (project_id, version, flow_id, modified_time, encoding_type, json) values (?,?,?,?,?,?)"; try { this.dbOperator .update(INSERT_FLOW, project.getId(), version, flow.getId(), System.currentTimeMillis(), encType.getNumVal(), data); } catch (final SQLException e) { logger.error("Error inserting flow", e); throw new ProjectManagerException("Error inserting flow " + flow.getId(), e); } }
private void updateProjectSettings(final Project project, final EncodingType encType) throws ProjectManagerException { final String UPDATE_PROJECT_SETTINGS = "UPDATE projects SET enc_type=?, settings_blob=? WHERE id=?"; final String json = JSONUtils.toJSON(project.toObject()); byte[] data = null; try { data = convertJsonToBytes(encType, json); logger.debug("NumChars: " + json.length() + " Gzip:" + data.length); } catch (final IOException e) { throw new ProjectManagerException("Failed to encode. ", e); } try { this.dbOperator.update(UPDATE_PROJECT_SETTINGS, encType.getNumVal(), data, project.getId()); } catch (final SQLException e) { logger.error("update Project Settings failed.", e); throw new ProjectManagerException( "Error updating project " + project.getName() + " version " + project.getVersion(), e); } }
private void updateExecutions(int count) throws ExecutorManagerException { final List<Integer> executionIdsList = new ArrayList<>(); final List<Long> updateTimesList = new ArrayList<>(); for (int i = 100000; i < 100000 + count; i++) { executionIdsList.add(i); updateTimesList.add(0L); } final Pair<String, String> executionIds = new Pair<>(ConnectorParams.EXEC_ID_LIST_PARAM, JSONUtils.toJSON(executionIdsList)); final Pair<String, String> updateTimes = new Pair<>( ConnectorParams.UPDATE_TIME_LIST_PARAM, JSONUtils.toJSON(updateTimesList)); Map<String, Object> results = apiGateway.callWithExecutionId("localhost", 12321, ConnectorParams.UPDATE_ACTION, null, null, executionIds, updateTimes); Assert.assertTrue(results != null); final List<Map<String, Object>> executionUpdates = (List<Map<String, Object>>) results .get(ConnectorParams.RESPONSE_UPDATED_FLOWS); Assert.assertEquals(count, executionUpdates.size()); System.out.println("executionUpdates.get(count - 1): " + executionUpdates.get(count - 1)); }
@Test public void testExecutorInfoJsonParser() throws Exception { final ExecutorInfo exeInfo = new ExecutorInfo(99.9, 14095, 50, System.currentTimeMillis(), 89, 10); final String json = JSONUtils.toJSON(exeInfo); when(this.client.httpPost(any(), any())).thenReturn(json); final ExecutorInfo exeInfo2 = this.gateway .callForJsonType("localhost", 1234, "executor", null, ExecutorInfo.class); Assert.assertTrue(exeInfo.equals(exeInfo2)); }
@Override public void updateFlow(final Project project, final int version, final Flow flow) throws ProjectManagerException { logger.info("Uploading flow " + flow.getId()); try { final String json = JSONUtils.toJSON(flow.toObject()); final byte[] data = convertJsonToBytes(this.defaultEncodingType, json); logger.info("Flow upload " + flow.getId() + " is byte size " + data.length); final String UPDATE_FLOW = "UPDATE project_flows SET encoding_type=?,json=? WHERE project_id=? AND version=? AND flow_id=?"; try { this.dbOperator .update(UPDATE_FLOW, this.defaultEncodingType.getNumVal(), data, project.getId(), version, flow.getId()); } catch (final SQLException e) { logger.error("Error inserting flow", e); throw new ProjectManagerException("Error inserting flow " + flow.getId(), e); } } catch (final IOException e) { throw new ProjectManagerException("Flow Upload failed.", e); } }
private void mockResultWithData() throws Exception { final ExecutableFlow flow = TestUtils.createTestExecutableFlow("exectest1", "exec1"); final String json = JSONUtils.toJSON(flow.toObject()); final byte[] data = json.getBytes("UTF-8"); mockExecution(EncodingType.PLAIN.getNumVal(), data); }
@Test public void updateExecutions() throws Exception { final ImmutableMap<String, String> map = ImmutableMap.of("test", "response"); when(this.client .httpPost(eq(new URI("http://executor-2:1234/executor")), this.params.capture())) .thenReturn(JSONUtils.toJSON(map)); final Map<String, Object> response = this.gateway .updateExecutions(new Executor(2, "executor-2", 1234, true), Collections.singletonList(new ExecutableFlow())); assertEquals(map, response); assertEquals(new Pair<>("executionId", "[-1]"), this.params.getValue().get(0)); assertEquals(new Pair<>("updatetime", "[-1]"), this.params.getValue().get(1)); assertEquals(new Pair<>("action", "update"), this.params.getValue().get(2)); assertEquals(new Pair<>("execid", "null"), this.params.getValue().get(3)); assertEquals(new Pair<>("user", null), this.params.getValue().get(4)); } }
private void updateExecutableFlow(final ExecutableFlow flow, final EncodingType encType) throws ExecutorManagerException { final String UPDATE_EXECUTABLE_FLOW_DATA = "UPDATE execution_flows " + "SET status=?,update_time=?,start_time=?,end_time=?,enc_type=?,flow_data=? " + "WHERE exec_id=?"; final String json = JSONUtils.toJSON(flow.toObject()); byte[] data = null; try { final byte[] stringData = json.getBytes("UTF-8"); data = stringData; // Todo kunkun-tang: use a common method to transform stringData to data. if (encType == EncodingType.GZIP) { data = GZIPUtils.gzipBytes(stringData); } } catch (final IOException e) { throw new ExecutorManagerException("Error encoding the execution flow."); } try { this.dbOperator.update(UPDATE_EXECUTABLE_FLOW_DATA, flow.getStatus() .getNumVal(), flow.getUpdateTime(), flow.getStartTime(), flow .getEndTime(), encType.getNumVal(), data, flow.getExecutionId()); } catch (final SQLException e) { throw new ExecutorManagerException("Error updating flow.", e); } }
public void uploadExecutableNode(final ExecutableNode node, final Props inputProps) throws ExecutorManagerException { final String INSERT_EXECUTION_NODE = "INSERT INTO execution_jobs " + "(exec_id, project_id, version, flow_id, job_id, start_time, " + "end_time, status, input_params, attempt) VALUES (?,?,?,?,?,?,?,?,?,?)"; byte[] inputParam = null; if (inputProps != null) { try { final String jsonString = JSONUtils.toJSON(PropsUtils.toHierarchicalMap(inputProps)); inputParam = GZIPUtils.gzipString(jsonString, "UTF-8"); } catch (final IOException e) { throw new ExecutorManagerException("Error encoding input params"); } } final ExecutableFlow flow = node.getExecutableFlow(); final String flowId = node.getParentFlow().getFlowPath(); logger.info("Uploading flowId " + flowId); try { this.dbOperator.update(INSERT_EXECUTION_NODE, flow.getExecutionId(), flow.getProjectId(), flow.getVersion(), flowId, node.getId(), node.getStartTime(), node.getEndTime(), node.getStatus().getNumVal(), inputParam, node.getAttempt()); } catch (final SQLException e) { throw new ExecutorManagerException("Error writing job " + node.getId(), e); } }
@Test public void testExecutorFlowJson() throws Exception { final Flow flow = this.project.getFlow("jobe"); Assert.assertNotNull(flow); final ExecutableFlow exFlow = new ExecutableFlow(this.project, flow); final Object obj = exFlow.toObject(); final String exFlowJSON = JSONUtils.toJSON(obj); final Map<String, Object> flowObjMap = (Map<String, Object>) JSONUtils.parseJSONFromString(exFlowJSON); final ExecutableFlow parsedExFlow = ExecutableFlow.createExecutableFlowFromObject(flowObjMap); testEquals(exFlow, parsedExFlow); }
public void updateExecutableNode(final ExecutableNode node) throws ExecutorManagerException { final String UPSERT_EXECUTION_NODE = "UPDATE execution_jobs " + "SET start_time=?, end_time=?, status=?, output_params=? " + "WHERE exec_id=? AND flow_id=? AND job_id=? AND attempt=?"; byte[] outputParam = null; final Props outputProps = node.getOutputProps(); if (outputProps != null) { try { final String jsonString = JSONUtils.toJSON(PropsUtils.toHierarchicalMap(outputProps)); outputParam = GZIPUtils.gzipString(jsonString, "UTF-8"); } catch (final IOException e) { throw new ExecutorManagerException("Error encoding input params"); } } try { this.dbOperator.update(UPSERT_EXECUTION_NODE, node.getStartTime(), node .getEndTime(), node.getStatus().getNumVal(), outputParam, node .getExecutableFlow().getExecutionId(), node.getParentFlow() .getFlowPath(), node.getId(), node.getAttempt()); } catch (final SQLException e) { throw new ExecutorManagerException("Error updating job " + node.getId(), e); } }
@Test public void testToAndFromObject() throws Exception { final Project project = new Project(1, "tesTing"); project.setCreateTimestamp(1L); project.setLastModifiedTimestamp(2L); project.setDescription("I am a test"); project.setUserPermission("user1", new Permission(new Type[]{Type.ADMIN, Type.EXECUTE})); final Object obj = project.toObject(); final String json = JSONUtils.toJSON(obj); final Object jsonObj = JSONUtils.parseJSONFromString(json); final Project parsedProject = Project.projectFromObject(jsonObj); assertTrue(project.equals(parsedProject)); }