Search in sources :

Example 26 with JSONRenderer

use of org.b3log.latke.servlet.renderer.JSONRenderer in project solo by b3log.

the class LinkConsole method updateLink.

/**
     * Updates a link by the specified request.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request, for example,
     * <pre>
     * {
     *     "link": {
     *         "oId": "",
     *         "linkTitle": "",
     *         "linkAddress": "",
     *         "linkDescription": ""
     *     }
     * }, see {@link org.b3log.solo.model.Link} for more details
     * </pre>
     * @param context the specified http request context
     * @param response the specified http servlet response
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/link/", method = HTTPRequestMethod.PUT)
public void updateLink(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    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);
        linkMgmtService.updateLink(requestJSONObject);
        ret.put(Keys.STATUS_CODE, true);
        ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
        renderer.setJSONObject(ret);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, langPropsService.get("updateFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 27 with JSONRenderer

use of org.b3log.latke.servlet.renderer.JSONRenderer in project solo by b3log.

the class LinkConsole method getLink.

/**
     * Gets the file with the specified request.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "link": {
     *         "oId": "",
     *         "linkTitle": "",
     *         "linkAddress": "",
     *         "linkDescription": ""
     *     }
     * }
     * </pre>
     * </p>
     * 
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/link/*", method = HTTPRequestMethod.GET)
public void getLink(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    if (!userQueryService.isLoggedIn(request, response)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    try {
        final String requestURI = request.getRequestURI();
        final String linkId = requestURI.substring((Latkes.getContextPath() + "/console/link/").length());
        final JSONObject result = linkQueryService.getLink(linkId);
        if (null == result) {
            renderer.setJSONObject(QueryResults.defaultResult());
            return;
        }
        renderer.setJSONObject(result);
        result.put(Keys.STATUS_CODE, true);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 28 with JSONRenderer

use of org.b3log.latke.servlet.renderer.JSONRenderer in project solo by b3log.

the class LoginProcessor method forgot.

/**
     * Resets forgotten password.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "isLoggedIn": boolean,
     *     "msg": "" // optional, exists if isLoggedIn equals to false
     * }
     * </pre>
     * </p>
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/forgot", method = HTTPRequestMethod.POST)
public void forgot(final HTTPRequestContext context) {
    final HttpServletRequest request = context.getRequest();
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();
    renderer.setJSONObject(jsonObject);
    try {
        jsonObject.put("succeed", false);
        jsonObject.put(Keys.MSG, langPropsService.get("resetPwdSuccessMsg"));
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
        final String userEmail = requestJSONObject.getString(User.USER_EMAIL);
        if (Strings.isEmptyOrNull(userEmail)) {
            LOGGER.log(Level.WARN, "Why user's email is empty");
            return;
        }
        LOGGER.log(Level.INFO, "Login[email={0}]", userEmail);
        final JSONObject user = userQueryService.getUserByEmail(userEmail);
        if (null == user) {
            LOGGER.log(Level.WARN, "Not found user[email={0}]", userEmail);
            jsonObject.put(Keys.MSG, langPropsService.get("userEmailNotFoundMsg"));
            return;
        }
        sendResetUrl(userEmail, jsonObject);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException) IOException(java.io.IOException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 29 with JSONRenderer

use of org.b3log.latke.servlet.renderer.JSONRenderer in project solo by b3log.

the class LoginProcessor method reset.

/**
     * Resets forgotten password.
     *
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "isLoggedIn": boolean,
     *     "msg": "" // optional, exists if isLoggedIn equals to false
     * }
     * </pre>
     * </p>
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/reset", method = HTTPRequestMethod.POST)
public void reset(final HTTPRequestContext context) {
    final HttpServletRequest request = context.getRequest();
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();
    renderer.setJSONObject(jsonObject);
    try {
        final JSONObject requestJSONObject;
        requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
        final String userEmail = requestJSONObject.getString(User.USER_EMAIL);
        final String newPwd = requestJSONObject.getString("newPwd");
        final JSONObject user = userQueryService.getUserByEmail(userEmail);
        user.put(User.USER_PASSWORD, newPwd);
        userMgmtService.updateUser(user);
        LOGGER.log(Level.DEBUG, "[{0}]'s password updated successfully.", new Object[] { userEmail });
        jsonObject.put("succeed", true);
        jsonObject.put("to", Latkes.getServePath() + "/login?from=reset");
        jsonObject.put(Keys.MSG, langPropsService.get("resetPwdSuccessMsg"));
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException) IOException(java.io.IOException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 30 with JSONRenderer

use of org.b3log.latke.servlet.renderer.JSONRenderer in project solo by b3log.

the class LoginProcessor method login.

/**
     * Logins.
     * 
     * <p> 
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "isLoggedIn": boolean,
     *     "msg": "" // optional, exists if isLoggedIn equals to false
     * }
     * </pre>
     * </p>
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/login", method = HTTPRequestMethod.POST)
public void login(final HTTPRequestContext context) {
    final HttpServletRequest request = context.getRequest();
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();
    renderer.setJSONObject(jsonObject);
    try {
        jsonObject.put(Common.IS_LOGGED_IN, false);
        final String loginFailLabel = langPropsService.get("loginFailLabel");
        jsonObject.put(Keys.MSG, loginFailLabel);
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
        final String userEmail = requestJSONObject.getString(User.USER_EMAIL);
        final String userPwd = requestJSONObject.getString(User.USER_PASSWORD);
        if (Strings.isEmptyOrNull(userEmail) || Strings.isEmptyOrNull(userPwd)) {
            return;
        }
        LOGGER.log(Level.INFO, "Login[email={0}]", userEmail);
        final JSONObject user = userQueryService.getUserByEmail(userEmail);
        if (null == user) {
            LOGGER.log(Level.WARN, "Not found user[email={0}]", userEmail);
            return;
        }
        if (MD5.hash(userPwd).equals(user.getString(User.USER_PASSWORD))) {
            Sessions.login(request, context.getResponse(), user);
            LOGGER.log(Level.INFO, "Logged in[email={0}]", userEmail);
            jsonObject.put(Common.IS_LOGGED_IN, true);
            if (Role.VISITOR_ROLE.equals(user.optString(User.USER_ROLE))) {
                jsonObject.put("to", Latkes.getServePath());
            } else {
                jsonObject.put("to", Latkes.getServePath() + Common.ADMIN_INDEX_URI);
            }
            jsonObject.remove(Keys.MSG);
            return;
        }
        LOGGER.log(Level.WARN, "Wrong password[{0}]", userPwd);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException) IOException(java.io.IOException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)67 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)67 JSONObject (org.json.JSONObject)67 ServiceException (org.b3log.latke.service.ServiceException)38 IOException (java.io.IOException)12 JSONException (org.json.JSONException)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 MalformedURLException (java.net.MalformedURLException)4 EventException (org.b3log.latke.event.EventException)4 JSONArray (org.json.JSONArray)4 RepositoryException (org.b3log.latke.repository.RepositoryException)3 Template (freemarker.template.Template)2 StringWriter (java.io.StringWriter)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 ServletException (javax.servlet.ServletException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 HTTPRequest (org.b3log.latke.urlfetch.HTTPRequest)2