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.SystemApiController.java

public Result heap(String nodeId) {
    try {/*from  w  ww.j  av  a2s  . c  o m*/
        Map<String, Object> result = Maps.newHashMap();
        Node node = nodeService.loadNode(nodeId);

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

From source file:controllers.api.InputsApiController.java

public Result recentMessage(String nodeId, String inputId) {
    try {/*from   w w w . j  a v  a  2s. c  o  m*/
        Node node = nodeService.loadNode(nodeId);
        MessageResult recentlyReceivedMessage = node.getInput(inputId).getRecentlyReceivedMessage(nodeId);

        if (recentlyReceivedMessage == null) {
            return notFound();
        }

        return ok(Json.toJsonString(buildResultFromMessage(recentlyReceivedMessage)))
                .as(MediaType.JSON_UTF_8.toString());
    } catch (IOException e) {
        return status(500);
    } catch (APIException e) {
        return status(e.getHttpCode());
    } catch (NodeService.NodeNotFoundException e) {
        return status(404, views.html.errors.error.render(ApiClient.ERROR_MSG_NODE_NOT_FOUND, e, request()));
    }
}

From source file:controllers.api.DashboardsApiController.java

public Result create() {
    if (!isPermitted(RestPermissions.DASHBOARDS_CREATE)) {
        return forbidden();
    }//from  w  w  w. ja v a2  s . c o  m

    CreateDashboardRequest cdr = Json.fromJson(request().body().asJson(), CreateDashboardRequest.class);
    if (isNullOrEmpty(cdr.getTitle()) || isNullOrEmpty(cdr.getDescription())) {
        return badRequest("Missing field");
    }

    try {
        final String dashboardId = dashboardService.create(cdr);

        return ok(Json.toJsonString(dashboardId)).as(MediaType.JSON_UTF_8.toString());
    } catch (APIException e) {
        String message = "Could not create dashboard. We expected HTTP 201, 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:org.openqa.selenium.remote.server.DriverServlet.java

private void handleCrossDomainRpc(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {
    CrossDomainRpc rpc;/*from  w ww  .j a va 2s  . co m*/

    try {
        rpc = new CrossDomainRpcLoader().loadRpc(servletRequest);
    } catch (IllegalArgumentException e) {
        servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        servletResponse.getOutputStream().println(e.getMessage());
        servletResponse.getOutputStream().flush();
        return;
    }

    HttpRequest request = new HttpRequest(HttpMethod.valueOf(rpc.getMethod()), rpc.getPath());
    request.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString());
    request.setContent(rpc.getContent());

    HttpResponse response = commandHandler.handleRequest(request);
    sendResponse(response, servletResponse);
}

From source file:controllers.api.SystemApiController.java

public Result journal(String nodeId) {
    try {/*w  ww .  jav a 2  s. c om*/
        final Map<String, Object> result = Maps.newHashMap();
        final Node node = nodeService.loadNode(nodeId);
        final JournalInfo journalInfo = node.getJournalInfo();

        result.put("uncommitted_entries", journalInfo.uncommittedJournalEntries);

        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.SystemApiController.java

public Result radioHeap(String radioId) {
    try {//  ww  w  . j a  v a2  s. c o  m
        Radio radio = nodeService.loadRadio(radioId);
        return ok(Json.toJsonString(jvmMap(radio.jvm(), radio.getBuffers())))
                .as(MediaType.JSON_UTF_8.toString());
    } catch (NodeService.NodeNotFoundException e) {
        return status(404, "radio not found");
    }
}

From source file:org.glowroot.ui.HttpSessionManager.java

private CommonResponse createSession(String username, Set<String> roles, boolean ldap) throws Exception {
    String sessionId = new BigInteger(130, secureRandom).toString(32);
    ImmutableSession session = ImmutableSession.builder().caseAmbiguousUsername(username).ldap(ldap)
            .roles(roles).lastRequest(clock.currentTimeMillis()).build();
    sessionMap.put(sessionId, session);//from  w  w  w  .j  a  v  a2s . c  o m

    String layoutJson = layoutService.getLayoutJson(session.createAuthentication(central, configRepository));
    CommonResponse response = new CommonResponse(OK, MediaType.JSON_UTF_8, layoutJson);
    Cookie cookie = new DefaultCookie(configRepository.getWebConfig().sessionCookieName(), sessionId);
    cookie.setHttpOnly(true);
    cookie.setPath("/");
    response.setHeader(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
    purgeExpiredSessions();

    auditSuccessfulLogin(username);
    return response;
}

From source file:controllers.api.SearchApiController.java

public Result fieldHistogram(String q, String field, String rangeType, int relative, String from, String to,
        String keyword, String interval, String valueType, String streamId) {
    if (q == null || q.isEmpty()) {
        q = "*";//from www  .j av a2s  . c o  m
    }

    // Interval.
    if (interval == null || interval.isEmpty() || !SearchTools.isAllowedDateHistogramInterval(interval)) {
        interval = "hour";
    }

    // 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);
        final boolean isCardinality = "cardinality".equalsIgnoreCase(valueType);
        FieldHistogramResponse histo = search.fieldHistogram(field, interval, isCardinality);

        Map<String, Object> result = Maps.newHashMap();
        AbsoluteRange boundaries = histo.getHistogramBoundaries();
        result.put("time", histo.time);
        result.put("interval", histo.interval);
        result.put("values", histo.getFormattedResults(valueType));
        result.put("from", boundaries.getFrom());
        result.put("to", boundaries.getTo());

        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:controllers.LdapController.java

public Result apiTestLdapLogin() {
    final DynamicForm dynamicForm = form().bindFromRequest("url", "systemUsername", "systemPassword",
            "ldapType", "useStartTls", "trustAllCertificates", "searchBase", "searchPattern", "principal",
            "password", "groupSearchBase", "groupIdAttribute");
    final Map<String, String> formData = dynamicForm.data();

    LdapConnectionTestResponse result;/* w  ww .j  av  a  2 s.  c  om*/
    try {
        final LdapTestConnectionRequest request = getLdapTestConnectionRequest(formData);
        // also try to login, don't just test the connection
        request.testConnectOnly = false;

        request.searchBase = formData.get("searchBase");
        request.searchPattern = formData.get("searchPattern");
        request.principal = formData.get("principal");
        request.password = formData.get("password");
        request.groupSearchBase = formData.get("groupSearchBase");
        request.groupIdAttribute = formData.get("groupIdAttribute");
        request.groupSearchPattern = formData.get("groupSearchPattern");
        result = ldapSettingsService.testLdapConfiguration(request);
    } catch (APIException e) {
        log.error("Unable to test login: {}", 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());
}

From source file:org.openqa.selenium.remote.server.WebDriverServlet.java

private void handleCrossDomainRpc(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {
    CrossDomainRpc rpc;//from ww  w .  j  a v a 2  s .  c  om

    try {
        rpc = new CrossDomainRpcLoader().loadRpc(servletRequest);
    } catch (IllegalArgumentException e) {
        servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        servletResponse.getOutputStream().println(e.getMessage());
        servletResponse.getOutputStream().flush();
        return;
    }

    servletRequest.setAttribute(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString());
    HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(servletRequest) {
        @Override
        public String getMethod() {
            return rpc.getMethod();
        }

        @Override
        public String getPathInfo() {
            return rpc.getPath();
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {
            return new InputStreamWrappingServletInputStream(new ByteArrayInputStream(rpc.getContent()));
        }
    };

    handle(wrapper, servletResponse);
}