Search in sources :

Example 46 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class InitProcessor method showInit.

/**
     * Shows initialization page.
     *
     * @param context the specified http request context
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @throws Exception exception
     */
@RequestProcessing(value = "/init", method = HTTPRequestMethod.GET)
public void showInit(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    if (initService.isInited()) {
        response.sendRedirect("/");
        return;
    }
    final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
    renderer.setTemplateName("init.ftl");
    context.setRenderer(renderer);
    final Map<String, Object> dataModel = renderer.getDataModel();
    final Map<String, String> langs = langPropsService.getAll(Locales.getLocale(request));
    dataModel.putAll(langs);
    dataModel.put(Common.VERSION, SoloServletListener.VERSION);
    dataModel.put(Common.STATIC_RESOURCE_VERSION, Latkes.getStaticResourceVersion());
    dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
    Keys.fillRuntime(dataModel);
    filler.fillMinified(dataModel);
}
Also used : JSONObject(org.json.JSONObject) ConsoleRenderer(org.b3log.solo.processor.renderer.ConsoleRenderer) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 47 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing 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 48 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing 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 49 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing 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)

Example 50 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class LoginProcessor method logout.

/**
     * Logout.
     *
     * @param context the specified context
     * @throws IOException io exception
     */
@RequestProcessing(value = "/logout", method = HTTPRequestMethod.GET)
public void logout(final HTTPRequestContext context) throws IOException {
    final HttpServletRequest httpServletRequest = context.getRequest();
    Sessions.logout(httpServletRequest, context.getResponse());
    String destinationURL = httpServletRequest.getParameter(Common.GOTO);
    if (Strings.isEmptyOrNull(destinationURL)) {
        destinationURL = "/";
    }
    context.getResponse().sendRedirect(destinationURL);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)104 JSONObject (org.json.JSONObject)94 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)67 ServiceException (org.b3log.latke.service.ServiceException)51 IOException (java.io.IOException)35 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 JSONException (org.json.JSONException)12 JSONArray (org.json.JSONArray)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 EventException (org.b3log.latke.event.EventException)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 TextHTMLRenderer (org.b3log.latke.servlet.renderer.TextHTMLRenderer)7 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)7 ConsoleRenderer (org.b3log.solo.processor.renderer.ConsoleRenderer)7 Date (java.util.Date)6 ExecutionException (java.util.concurrent.ExecutionException)6 ArrayList (java.util.ArrayList)5 MalformedURLException (java.net.MalformedURLException)4 HttpSession (javax.servlet.http.HttpSession)4