Java Code Examples for io.javalin.Context#result()

The following examples show how to use io.javalin.Context#result() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AllAggregatedReportsController.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        List<Report> reports = reportDao.fetchAllAggregated();

        context.json(reports);
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }
}
 
Example 2
Source File: ReportController.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        int id = Integer.parseInt(context.param("id"));

        Report report = reportDao.fetch(id);

        if (report != null) {
            context.json(report);
        }
        else {
            context.status(404);
        }
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }


}
 
Example 3
Source File: LatencyStatisticsReportController.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        int id = Integer.parseInt(context.param("id"));

        Report report = reportDao.fetch(id);

        LatencyStatisticsResponse response = new LatencyStatisticsResponse();

        processReports(report, response);

        context.json(response);
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }


}
 
Example 4
Source File: AggregatedLatencyStatisticsReportController.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        int testId = Integer.parseInt(context.param("id"));
        int testNumber = Integer.parseInt(context.param("number"));

        Report report = reportDao.fetchAggregated(testId, testNumber);

        LatencyStatisticsResponse response = new LatencyStatisticsResponse();

        processReports(report, response);

        context.json(response);
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }


}
 
Example 5
Source File: AggregatedLatencyReportController.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        int testId = Integer.parseInt(
                Objects.requireNonNull(context.param("id"), "The ID must be provided"));

        int testNumber = Integer.parseInt(
                Objects.requireNonNull(context.param("number"), "The test number must be provided"));

        Report report = reportDao.fetchAggregated(testId, testNumber);

        LatencyResponse response = new LatencyResponse();

        processReports(report, response);

        context.json(response);
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }
}
 
Example 6
Source File: WorkerHandler.java    From openmessaging-benchmark with Apache License 2.0 6 votes vote down vote up
private void handleCumulativeLatencies(Context ctx) throws Exception {
    CumulativeLatencies stats = localWorker.getCumulativeLatencies();

    // Serialize histograms
    synchronized (histogramSerializationBuffer) {
        histogramSerializationBuffer.clear();
        stats.publishLatency.encodeIntoCompressedByteBuffer(histogramSerializationBuffer);
        stats.publishLatencyBytes = new byte[histogramSerializationBuffer.position()];
        histogramSerializationBuffer.flip();
        histogramSerializationBuffer.get(stats.publishLatencyBytes);

        histogramSerializationBuffer.clear();
        stats.endToEndLatency.encodeIntoCompressedByteBuffer(histogramSerializationBuffer);
        stats.endToEndLatencyBytes = new byte[histogramSerializationBuffer.position()];
        histogramSerializationBuffer.flip();
        histogramSerializationBuffer.get(stats.endToEndLatencyBytes);
    }

    ctx.result(writer.writeValueAsString(stats));
}
 
Example 7
Source File: SutNodeInfoController.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        int testId = Integer.parseInt(context.param("test"));

        List<SutNodeInfo> sutNodeInfo = dao.fetchByTest(testId);

        context.json(sutNodeInfo);
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }
}
 
Example 8
Source File: WorkerHandler.java    From openmessaging-benchmark with Apache License 2.0 6 votes vote down vote up
private void handlePeriodStats(Context ctx) throws Exception {
    PeriodStats stats = localWorker.getPeriodStats();

    // Serialize histograms
    synchronized (histogramSerializationBuffer) {
        histogramSerializationBuffer.clear();
        stats.publishLatency.encodeIntoCompressedByteBuffer(histogramSerializationBuffer);
        stats.publishLatencyBytes = new byte[histogramSerializationBuffer.position()];
        histogramSerializationBuffer.flip();
        histogramSerializationBuffer.get(stats.publishLatencyBytes);

        histogramSerializationBuffer.clear();
        stats.endToEndLatency.encodeIntoCompressedByteBuffer(histogramSerializationBuffer);
        stats.endToEndLatencyBytes = new byte[histogramSerializationBuffer.position()];
        histogramSerializationBuffer.flip();
        histogramSerializationBuffer.get(stats.endToEndLatencyBytes);
    }

    ctx.result(writer.writeValueAsString(stats));
}
 
Example 9
Source File: RawFileController.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        int id = Integer.parseInt(context.param("id"));
        String name = context.param("name");

        Report report = reportDao.fetch(id);

        File file = getReportFile(report, name);

        context.result(new FileInputStream(file))
                .header("Content-Length: ", String.valueOf(FileUtils.sizeOf(file)))
                .header("Content-Type", "application/octet-stream");
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }
}
 
Example 10
Source File: RateReportController.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        int id = Integer.parseInt(context.param("id"));

        Report report = reportDao.fetch(id);

        SingleData<Long> rateData = processReport(report, report.getTestHostRole());

        RateResponse rateResponse = new RateResponse();
        rateResponse.setPeriods(rateData.getPeriods());
        rateResponse.setRate(rateData.getValues());

        context.json(rateResponse);
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }
}
 
Example 11
Source File: RestHandler.java    From openmessaging-connect-runtime with Apache License 2.0 6 votes vote down vote up
private void handleCreateConnector(Context context) {
    String connectorName = context.param("connectorName");
    String arg = context.queryParam("config");
    Map keyValue = JSON.parseObject(arg, Map.class);
    ConnectKeyValue configs = new ConnectKeyValue();
    for(Object key : keyValue.keySet()){
        configs.put((String)key, (String)keyValue.get(key));
    }
    try {

        String result = connectController.getConfigManagementService().putConnectorConfig(connectorName, configs);
        if(result != null && result.length() > 0){
            context.result(result);
        }else{
            context.result("success");
        }
    } catch (Exception e) {
        context.result("failed");
    }
}
 
Example 12
Source File: FileSystemViewHandler.java    From hudi with Apache License 2.0 5 votes vote down vote up
private void writeValueAsString(Context ctx, Object obj) throws JsonProcessingException {
  boolean prettyPrint = ctx.queryParam("pretty") != null;
  long beginJsonTs = System.currentTimeMillis();
  String result =
      prettyPrint ? OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj) : OBJECT_MAPPER.writeValueAsString(obj);
  long endJsonTs = System.currentTimeMillis();
  LOG.debug("Jsonify TimeTaken=" + (endJsonTs - beginJsonTs));
  ctx.result(result);
}
 
Example 13
Source File: RestHandler.java    From openmessaging-connect-runtime with Apache License 2.0 5 votes vote down vote up
private void handleStopConnector(Context context){
    String connectorName = context.param("connectorName");
    try {

        connectController.getConfigManagementService().removeConnectorConfig(connectorName);
        context.result("success");
    } catch (Exception e) {
        context.result("failed");
    }
}
 
Example 14
Source File: ReportPropertiesController.java    From maestro-java with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        int id = Integer.parseInt(context.param("id"));

        Report report = reportDao.fetch(id);

        String location = report.getLocation();
        File file = new File(location, "test.properties");

        TestProperties tp = new TestProperties();

        List<ExtendedTestProperties> testPropertiesList = new LinkedList<>();

        if (HostTypes.isWorker(report.getTestHostRole())) {
            PropertyReader reader = new PropertyReader();

            reader.read(file, tp);

            ExtendedTestProperties etp = new ExtendedTestProperties(tp);
            etp.setRole(report.getTestHostRole());
            testPropertiesList.add(etp);
            context.json(testPropertiesList);
        }
        else {
            context.status(500);
            context.result(String.format("Unhandled node type for the report: %s", report.getTestHostRole()));
        }
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        logger.error(t.getMessage(), t);
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }
}
 
Example 15
Source File: RestHandler.java    From openmessaging-connect-runtime with Apache License 2.0 5 votes vote down vote up
private void handleQueryConnectorConfig(Context context){

        String connectorName = context.param("connectorName");

        Map<String, ConnectKeyValue> connectorConfigs = connectController.getConfigManagementService().getConnectorConfigs();
        Map<String, List<ConnectKeyValue>> taskConfigs = connectController.getConfigManagementService().getTaskConfigs();
        StringBuilder sb = new StringBuilder();
        sb.append("ConnectorConfigs:")
            .append(JSON.toJSONString(connectorConfigs.get(connectorName)))
            .append("\n")
            .append("TaskConfigs:")
            .append(JSON.toJSONString(taskConfigs.get(connectorName)));
        context.result(sb.toString());
    }
 
Example 16
Source File: WorkerHandler.java    From openmessaging-benchmark with Apache License 2.0 4 votes vote down vote up
private void handleCountersStats(Context ctx) throws Exception {
    ctx.result(writer.writeValueAsString(localWorker.getCountersStats()));
}
 
Example 17
Source File: WorkerHandler.java    From openmessaging-benchmark with Apache License 2.0 4 votes vote down vote up
private void handleCreateTopics(Context ctx) throws Exception {
    TopicsInfo topicsInfo = mapper.readValue(ctx.body(), TopicsInfo.class);
    log.info("Received create topics request for topics: {}", ctx.body());
    List<String> topics = localWorker.createTopics(topicsInfo);
    ctx.result(writer.writeValueAsString(topics));
}
 
Example 18
Source File: FileListController.java    From maestro-java with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(Context context) {
    try {
        int id = Integer.parseInt(context.param("id"));

        Report report = reportDao.fetch(id);

        File location = new File(report.getLocation());

        if (!location.exists()) {
            context.status(404);
            context.result(String.format("Not found: %s", location.getPath()));

            return;
        }

        if (!location.isDirectory()) {
            throw new MaestroException("Invalid file type: expected a directory, but got something else");
        }

        File files[] = location.listFiles(file -> {
            String extensions[] = { ".csv", ".hdr", ".dat", ".properties", ".json", ".xz"};

            for (String extension : extensions) {
                if (file.getName().endsWith(extension)) {
                    return true;
                }
            }

            return false;
        });

        List<Archive> fileList = Arrays.asList(files).stream().map(f -> new Archive(f, id)).collect(Collectors.toList());

        context.json(fileList);
    }
    catch (DataNotFoundException e) {
        context.status(404);
        context.result(String.format("Not found: %s", e.getMessage()));
    }
    catch (Throwable t) {
        context.status(500);
        context.result(String.format("Internal server error: %s", t.getMessage()));
    }
}
 
Example 19
Source File: RestHandler.java    From openmessaging-connect-runtime with Apache License 2.0 4 votes vote down vote up
private void getClusterInfo(Context context) {
    context.result(JSON.toJSONString(connectController.getClusterManagementService().getAllAliveWorkers()));
}
 
Example 20
Source File: RestHandler.java    From openmessaging-connect-runtime with Apache License 2.0 4 votes vote down vote up
private void getConfigInfo(Context context) {

        Map<String, ConnectKeyValue> connectorConfigs = connectController.getConfigManagementService().getConnectorConfigs();
        Map<String, List<ConnectKeyValue>> taskConfigs = connectController.getConfigManagementService().getTaskConfigs();
        context.result("ConnectorConfigs:"+JSON.toJSONString(connectorConfigs)+"\nTaskConfigs:"+JSON.toJSONString(taskConfigs));
    }