Package org.b3log.latke.servlet.renderer

Examples of org.b3log.latke.servlet.renderer.JSONRenderer


        if (!userQueryService.isAdminLoggedIn(request)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }

        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);

        final JSONObject ret = new JSONObject();

        try {
            final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);

            userMgmtService.updateUser(requestJSONObject);

            ret.put(Keys.STATUS_CODE, true);
            ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));

            renderer.setJSONObject(ret);
        } catch (final ServiceException e) {
            LOGGER.log(Level.ERROR, e.getMessage(), e);

            final JSONObject jsonObject = QueryResults.defaultResult();

            renderer.setJSONObject(jsonObject);
            jsonObject.put(Keys.MSG, e.getMessage());
        }
    }
View Full Code Here


     */
    @RequestProcessing(value = "/console/user/", method = HTTPRequestMethod.POST)
    public void addUser(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context)
        throws Exception {

        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);

        final JSONObject ret = new JSONObject();

        try {
            final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);

            if (userQueryService.isAdminLoggedIn(request)) { // if the administrator register a new user, treats the new user as a normal user
                // (defaultRole) who could post article
                requestJSONObject.put(User.USER_ROLE, Role.DEFAULT_ROLE);
            } else { // if a normal user or a visitor register a new user, treates the new user as a visitor (visitorRole) who couldn't
                // post article
                requestJSONObject.put(User.USER_ROLE, Role.VISITOR_ROLE);
            }

            final String userId = userMgmtService.addUser(requestJSONObject);

            ret.put(Keys.OBJECT_ID, userId);
            ret.put(Keys.MSG, langPropsService.get("addSuccLabel"));
            ret.put(Keys.STATUS_CODE, true);

            renderer.setJSONObject(ret);
        } catch (final ServiceException e) {
            LOGGER.log(Level.ERROR, e.getMessage(), e);

            final JSONObject jsonObject = QueryResults.defaultResult();

            renderer.setJSONObject(jsonObject);
            jsonObject.put(Keys.MSG, e.getMessage());
        }
    }
View Full Code Here

        if (!userQueryService.isAdminLoggedIn(request)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }

        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);

        final JSONObject jsonObject = new JSONObject();

        renderer.setJSONObject(jsonObject);

        try {
            final String userId = request.getRequestURI().substring((Latkes.getContextPath() + "/console/user/").length());

            userMgmtService.removeUser(userId);
View Full Code Here

     * @throws Exception exception
     */
    @RequestProcessing(value = "/console/users/*/*/*"/* Requests.PAGINATION_PATH_PATTERN */, method = HTTPRequestMethod.GET)
    public void getUsers(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context)
        throws Exception {
        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);

        if (!userQueryService.isAdminLoggedIn(request)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }

        try {
            final String requestURI = request.getRequestURI();
            final String path = requestURI.substring((Latkes.getContextPath() + "/console/users/").length());

            final JSONObject requestJSONObject = Requests.buildPaginationRequest(path);

            final JSONObject result = userQueryService.getUsers(requestJSONObject);

            result.put(Keys.STATUS_CODE, true);

            renderer.setJSONObject(result);
        } catch (final ServiceException e) {
            LOGGER.log(Level.ERROR, e.getMessage(), e);

            final JSONObject jsonObject = QueryResults.defaultResult();

            renderer.setJSONObject(jsonObject);
            jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
        }
    }
View Full Code Here

        if (!userQueryService.isAdminLoggedIn(request)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }

        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);

        try {
            final String requestURI = request.getRequestURI();
            final String userId = requestURI.substring((Latkes.getContextPath() + "/console/user/").length());

            final JSONObject result = userQueryService.getUser(userId);

            if (null == result) {
                renderer.setJSONObject(QueryResults.defaultResult());

                return;
            }

            renderer.setJSONObject(result);
            result.put(Keys.STATUS_CODE, true);

        } catch (final ServiceException e) {
            LOGGER.log(Level.ERROR, e.getMessage(), e);

            final JSONObject jsonObject = QueryResults.defaultResult();

            renderer.setJSONObject(jsonObject);
            jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
        }
    }
View Full Code Here

        if (!userQueryService.isAdminLoggedIn(request)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }

        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);

        final JSONObject jsonObject = new JSONObject();

        renderer.setJSONObject(jsonObject);

        try {
            final String userId = request.getRequestURI().substring((Latkes.getContextPath() + "/console/changeRole/").length());

            userMgmtService.changeRole(userId);
View Full Code Here

     * @throws Exception
     */
    @RequestProcessing(value = "/console/plugins/b3log-broadcast/chance", method = HTTPRequestMethod.POST)
    public void addChance(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response)
        throws Exception {
        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);

        final JSONObject ret = new JSONObject();

        renderer.setJSONObject(ret);

        try {
            // TODO: verify b3 key

            final String time = request.getParameter("time");

            if (Strings.isEmptyOrNull(time)) {
                ret.put(Keys.STATUS_CODE, false);

                return;
            }

            final long expirationTime = Long.valueOf(time);

            final JSONObject option = new JSONObject();

            option.put(Keys.OBJECT_ID, Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);
            option.put(Option.OPTION_VALUE, expirationTime);
            option.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_BROADCAST);

            optionMgmtService.addOrUpdateOption(option);

            ret.put(Keys.STATUS_CODE, true);
        } catch (final Exception e) {
            final String msg = "Broadcast plugin exception";

            LOGGER.log(Level.ERROR, msg, e);

            final JSONObject jsonObject = QueryResults.defaultResult();

            renderer.setJSONObject(jsonObject);
            jsonObject.put(Keys.MSG, msg);
        }
    }
View Full Code Here

            response.sendError(HttpServletResponse.SC_FORBIDDEN);

            return;
        }

        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);

        final JSONObject ret = new JSONObject();

        renderer.setJSONObject(ret);

        if (!userQueryService.isAdminLoggedIn(request)) {
            ret.put(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME, 0L);
            ret.put(Keys.STATUS_CODE, false);

            return;
        }

        try {
            final JSONObject option = optionQueryService.getOptionById(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);

            if (null == option) {
                ret.put(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME, 0L);
                ret.put(Keys.STATUS_CODE, false);

                return;
            }

            ret.put(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME, option.getLong(Option.OPTION_VALUE));
            ret.put(Keys.STATUS_CODE, true);
        } catch (final Exception e) {
            LOGGER.log(Level.ERROR, "Broadcast plugin exception", e);

            final JSONObject jsonObject = QueryResults.defaultResult();

            renderer.setJSONObject(jsonObject);
        }
    }
View Full Code Here

            response.sendError(HttpServletResponse.SC_FORBIDDEN);

            return;
        }

        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);

        final JSONObject ret = new JSONObject();

        renderer.setJSONObject(ret);

        try {
            final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);

            final JSONObject broadcast = requestJSONObject.getJSONObject("broadcast");
            final JSONObject preference = preferenceQueryService.getPreference();
            final String b3logKey = preference.getString(Preference.KEY_OF_SOLO);
            final String email = preference.getString(Preference.ADMIN_EMAIL);
            final String clientName = "B3log Solo";
            final String clientVersion = SoloServletListener.VERSION;
            final String clientTitle = preference.getString(Preference.BLOG_TITLE);
            final String clientRuntimeEnv = Latkes.getRuntimeEnv().name();

            final JSONObject broadcastRequest = new JSONObject();

            broadcastRequest.put("b3logKey", b3logKey);
            broadcastRequest.put("email", email);
            broadcastRequest.put("broadcast", broadcast);
            broadcastRequest.put("clientRuntimeEnv", clientRuntimeEnv);
            broadcastRequest.put("clientTitle", clientTitle);
            broadcastRequest.put("clientVersion", clientVersion);
            broadcastRequest.put("clientName", clientName);
            broadcastRequest.put("clientHost", Latkes.getServePath());

            final HTTPRequest httpRequest = new HTTPRequest();

            httpRequest.setURL(ADD_BROADCAST_URL);
            httpRequest.setRequestMethod(HTTPRequestMethod.POST);
            httpRequest.setPayload(broadcastRequest.toString().getBytes("UTF-8"));

            @SuppressWarnings("unchecked")
            final Future<HTTPResponse> future = (Future<HTTPResponse>) urlFetchService.fetchAsync(httpRequest);
            final HTTPResponse result = future.get();

            if (HttpServletResponse.SC_OK == result.getResponseCode()) {
                ret.put(Keys.STATUS_CODE, true);

                optionMgmtService.removeOption(Option.ID_C_BROADCAST_CHANCE_EXPIRATION_TIME);

                LOGGER.info("Submits broadcast successfully");

                return;
            }

            ret.put(Keys.STATUS_CODE, false);
        } catch (final Exception e) {
            LOGGER.log(Level.ERROR, "Submits broadcast failed", e);

            final JSONObject jsonObject = QueryResults.defaultResult();

            renderer.setJSONObject(jsonObject);
            jsonObject.put(Keys.MSG, e.getMessage());
        }
    }
View Full Code Here

        final int displayCnt = preference.getInt(Preference.RANDOM_ARTICLES_DISPLAY_CNT);

        if (0 == displayCnt) {
            jsonObject.put(Common.RANDOM_ARTICLES, new ArrayList<JSONObject>());

            final JSONRenderer renderer = new JSONRenderer();

            context.setRenderer(renderer);
            renderer.setJSONObject(jsonObject);

            return;
        }

        Stopwatchs.start("Get Random Articles");
        final List<JSONObject> randomArticles = getRandomArticles(preference);

        jsonObject.put(Common.RANDOM_ARTICLES, randomArticles);

        final JSONRenderer renderer = new JSONRenderer();

        context.setRenderer(renderer);
        renderer.setJSONObject(jsonObject);

        Stopwatchs.end();
    }
View Full Code Here

TOP

Related Classes of org.b3log.latke.servlet.renderer.JSONRenderer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.