Example usage for com.google.common.net MediaType JSON_UTF_8

List of usage examples for com.google.common.net MediaType JSON_UTF_8

Introduction

In this page you can find the example usage for com.google.common.net MediaType JSON_UTF_8.

Prototype

MediaType JSON_UTF_8

To view the source code for com.google.common.net MediaType JSON_UTF_8.

Click Source Link

Usage

From source file:controllers.api.InputsApiController.java

public Result connections(String nodeId, String inputId) {
    try {// w  w  w  . j a  v a  2 s.  c o m
        Map<String, Object> result = Maps.newHashMap();

        final Node node = nodeService.loadNode(nodeId);
        final Input input = node.getInput(inputId);

        result.put("active", input.getConnections());
        result.put("total", input.getTotalConnections());

        return ok(Json.toJsonString(result)).as(MediaType.JSON_UTF_8.toString());
    } catch (IOException e) {
        return internalServerError("io exception");
    } catch (APIException e) {
        return internalServerError("api exception " + e);
    } catch (NodeService.NodeNotFoundException e) {
        return status(404, views.html.errors.error.render(ApiClient.ERROR_MSG_NODE_NOT_FOUND, e, request()));
    }
}

From source file:rapture.dp.invocable.notification.steps.NotificationStep.java

private int doPost(URL url, byte[] body) throws IOException {
    HttpURLConnection http = (HttpURLConnection) url.openConnection();
    http.setFixedLengthStreamingMode(body.length);
    http.setRequestProperty("Content-Type", MediaType.JSON_UTF_8.toString());
    http.setRequestMethod("POST");
    http.setDoOutput(true);//from w w w  . j  ava 2 s  . co  m
    http.connect();
    try (OutputStream stream = http.getOutputStream()) {
        stream.write(body);
    }
    int response = http.getResponseCode();
    http.disconnect();
    return response;
}

From source file:controllers.api.ToolsApiController.java

public Result jsonTest() {
    final JsonNode json = request().body().asJson();
    final JsonTestRequest request = Json.fromJson(json, JsonTestRequest.class);

    try {//from   w  w w .  j a va2s . c o m
        return ok(Json.toJsonString(jsonTest.test(request))).as(MediaType.JSON_UTF_8.toString());
    } catch (IOException e) {
        return internalServerError("io exception");
    } catch (APIException e) {
        if (e.getHttpCode() == 422) {
            return status(422);
        }
        return internalServerError("api exception " + e);
    }
}

From source file:controllers.api.DashboardsApiController.java

public Result listWritable() {
    try {/*from  w w  w.  j  ava2  s  . c  o  m*/
        Map<String, Object> result = Maps.newHashMap();
        for (Dashboard d : getAllWritable(currentUser())) {
            Map<String, String> dashboard = Maps.newHashMap();

            dashboard.put("title", d.getTitle());
            dashboard.put("description", d.getDescription());
            dashboard.put("created_by", (d.getCreatorUser() == null) ? null : d.getCreatorUser().getName());
            dashboard.put("content_pack", d.getContentPack());

            result.put(d.getId(), dashboard);
        }

        return ok(Json.toJsonString(result)).as(MediaType.JSON_UTF_8.toString());
    } catch (APIException e) {
        String message = "Could not get dashboards. We expected HTTP 200, but got a HTTP " + e.getHttpCode()
                + ".";
        return status(504, views.html.errors.error.render(message, e, request()));
    } catch (IOException e) {
        return status(504, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request()));
    }
}

From source file:controllers.api.SystemApiController.java

public Result radioThroughput(String radioId) {
    try {//  w  w w . j av a2s. co  m
        Map<String, Object> result = Maps.newHashMap();
        final Radio radio = nodeService.loadRadio(radioId);
        result.put("throughput", radio.getThroughput());

        return ok(Json.toJsonString(result)).as(MediaType.JSON_UTF_8.toString());
    } catch (NodeService.NodeNotFoundException e) {
        return status(404, "node not found");
    }
}

From source file:controllers.api.SearchApiController.java

public Result fieldTerms(String q, String field, String rangeType, int relative, String from, String to,
        String keyword, String streamId) {
    if (q == null || q.isEmpty()) {
        q = "*";//from ww  w.  j  a  va  2s  . com
    }

    // Determine timerange type.
    TimeRange timerange;
    try {
        timerange = TimeRange.factory(rangeType, relative, from, to, keyword);
    } catch (InvalidRangeParametersException e2) {
        return status(400, views.html.errors.error.render("Invalid range parameters provided.", e2, request()));
    } catch (IllegalArgumentException e1) {
        return status(400, views.html.errors.error.render("Invalid range type provided.", e1, request()));
    }

    String filter = null;
    if (streamId != null && !streamId.isEmpty()) {
        filter = "streams:" + streamId;
    }

    try {
        UniversalSearch search = searchFactory.queryWithRangeAndFilter(q, timerange, filter);
        FieldTermsResponse terms = search.fieldTerms(field);

        Map<String, Object> result = Maps.newHashMap();
        result.put("total", terms.total);
        result.put("missing", terms.missing);
        result.put("time", terms.time);
        result.put("other", terms.other);
        result.put("terms", terms.terms);

        return ok(Json.toJsonString(result)).as(MediaType.JSON_UTF_8.toString());
    } catch (IOException e) {
        return internalServerError("io exception");
    } catch (APIException e) {
        if (e.getHttpCode() == 400) {
            // This usually means the field does not have a numeric type. Pass through!
            return badRequest();
        }

        return internalServerError("api exception " + e);
    }
}

From source file:org.graylog2.restclient.models.UniversalSearch.java

public SearchResult search() throws IOException, APIException {
    SearchResultResponse response = doSearch(SearchResultResponse.class, MediaType.JSON_UTF_8, PER_PAGE, null);
    if (response == null) {
        log.error("We should never get an empty result without throwing an IOException.");
        throw new APIException(null, null,
                new RuntimeException("Empty search response, this is likely a bug in exception handling."));
    }// ww w  .j a  v a  2s .co  m

    return new SearchResult(query, response.builtQuery, timeRange, response.total_results, response.time,
            response.messages, response.fields, response.usedIndices, response.getFromDataTime(),
            response.getToDataTime(), fieldMapper);
}

From source file:controllers.api.SystemApiController.java

public Result streamThroughput(String streamId) {
    try {//from   w w w. j av a2 s  .  c  o  m
        Map<String, Object> result = Maps.newHashMap();
        final Stream stream = streamService.get(streamId);
        long throughput = stream.getThroughput();
        result.put("throughput", throughput);
        return ok(Json.toJsonString(result)).as(MediaType.JSON_UTF_8.toString());
    } catch (APIException e) {
        return status(504, "Could not load stream " + streamId);
    } catch (IOException e) {
        return status(504, "Could not load stream " + streamId);
    }
}

From source file:controllers.api.InputsApiController.java

public Result globalRecentMessage(String inputId)
        throws InvalidRangeParametersException, IOException, APIException {
    final String query = "gl2_source_input:" + inputId + " OR gl2_source_radio_input:" + inputId;

    final UniversalSearch search = this.searchFactory.queryWithRange(query, new RelativeRange(86400));

    List<MessageResult> messages = search.search().getMessages();
    if (messages.size() > 0) {
        return ok(Json.toJsonString(buildResultFromMessage(messages.get(0))))
                .as(MediaType.JSON_UTF_8.toString());
    } else {//from ww w.  j av a 2  s  .  c om
        return notFound();
    }
}

From source file:controllers.LdapController.java

public Result apiTestLdapConnection() {
    final DynamicForm dynamicForm = form().bindFromRequest("url", "systemUsername", "systemPassword",
            "ldapType", "useStartTls", "trustAllCertificates");
    final Map<String, String> formData = dynamicForm.data();
    LdapConnectionTestResponse result;/*from   w w  w.j av  a  2 s . c  o  m*/
    try {
        final LdapTestConnectionRequest request = getLdapTestConnectionRequest(formData);
        request.testConnectOnly = true;
        result = ldapSettingsService.testLdapConfiguration(request);
    } catch (APIException e) {
        // couldn't connect
        log.error("Unable to test connection: {}", e.getMessage());
        return internalServerError();
    } catch (IOException e) {
        log.error("Unable to connect", e);
        return internalServerError();
    }
    return ok(Json.toJsonString(result)).as(MediaType.JSON_UTF_8.toString());
}