use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class AdminConsole method showAdminPreferenceFunction.
/**
* Shows administrator preference function with the specified context.
*
* @param request the specified request
* @param context the specified context
*/
@RequestProcessing(value = "/admin-preference.do", method = HTTPRequestMethod.GET)
public void showAdminPreferenceFunction(final HttpServletRequest request, final HTTPRequestContext context) {
final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
context.setRenderer(renderer);
final String templateName = "admin-preference.ftl";
renderer.setTemplateName(templateName);
final Locale locale = Latkes.getLocale();
final Map<String, String> langs = langPropsService.getAll(locale);
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.putAll(langs);
dataModel.put(Option.ID_C_LOCALE_STRING, locale.toString());
JSONObject preference = null;
try {
preference = preferenceQueryService.getPreference();
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, "Loads preference failed", e);
}
final StringBuilder timeZoneIdOptions = new StringBuilder();
final String[] availableIDs = TimeZone.getAvailableIDs();
for (int i = 0; i < availableIDs.length; i++) {
final String id = availableIDs[i];
String option;
if (id.equals(preference.optString(Option.ID_C_TIME_ZONE_ID))) {
option = "<option value=\"" + id + "\" selected=\"true\">" + id + "</option>";
} else {
option = "<option value=\"" + id + "\">" + id + "</option>";
}
timeZoneIdOptions.append(option);
}
dataModel.put("timeZoneIdOptions", timeZoneIdOptions.toString());
fireFreeMarkerActionEvent(templateName, dataModel);
}
use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class AdminConsole method exportSQL.
/**
* Exports data as SQL file.
*
* @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/export/sql", method = HTTPRequestMethod.GET)
public void exportSQL(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
if (!userQueryService.isAdminLoggedIn(request)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
if (!Latkes.runsWithJDBCDatabase() || RuntimeDatabase.MYSQL != Latkes.getRuntimeDatabase()) {
context.renderJSON().renderMsg("Just support MySQL export now");
return;
}
final String dbUser = Latkes.getLocalProperty("jdbc.username");
final String dbPwd = Latkes.getLocalProperty("jdbc.password");
final String dbURL = Latkes.getLocalProperty("jdbc.URL");
String db = StringUtils.substringAfterLast(dbURL, "/");
db = StringUtils.substringBefore(db, "?");
String sql;
try {
if (StringUtils.isNotBlank(dbPwd)) {
sql = Execs.exec("mysqldump -u" + dbUser + " -p" + dbPwd + " --databases " + db);
} else {
sql = Execs.exec("mysqldump -u" + dbUser + " --databases " + db);
}
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Export failed", e);
context.renderJSON().renderMsg("Export failed, please check log");
return;
}
final String tmpDir = System.getProperty("java.io.tmpdir");
String localFilePath = tmpDir + "/b3_solo_" + UUID.randomUUID().toString() + ".sql";
LOGGER.info(localFilePath);
final File localFile = new File(localFilePath);
try {
final byte[] data = sql.getBytes("UTF-8");
OutputStream output = new FileOutputStream(localFile);
IOUtils.write(data, output);
IOUtils.closeQuietly(output);
final File zipFile = ZipUtil.zip(localFile);
final FileInputStream inputStream = new FileInputStream(zipFile);
final byte[] zipData = IOUtils.toByteArray(inputStream);
IOUtils.closeQuietly(inputStream);
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"solo.sql.zip\"");
final ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(zipData);
outputStream.flush();
outputStream.close();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Export failed", e);
context.renderJSON().renderMsg("Export failed, please check log");
return;
}
}
use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class AdminConsole method showAdminIndex.
/**
* Shows administrator index with the specified context.
*
* @param request the specified request
* @param context the specified context
*/
@RequestProcessing(value = "/admin-index.do", method = HTTPRequestMethod.GET)
public void showAdminIndex(final HttpServletRequest request, final HTTPRequestContext context) {
final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
context.setRenderer(renderer);
final String templateName = "admin-index.ftl";
renderer.setTemplateName(templateName);
final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.putAll(langs);
final JSONObject currentUser = userQueryService.getCurrentUser(request);
final String userName = currentUser.optString(User.USER_NAME);
dataModel.put(User.USER_NAME, userName);
final String roleName = currentUser.optString(User.USER_ROLE);
dataModel.put(User.USER_ROLE, roleName);
final String email = currentUser.optString(User.USER_EMAIL);
final String userAvatar = currentUser.optString(UserExt.USER_AVATAR);
if (!Strings.isEmptyOrNull(userAvatar)) {
dataModel.put(Common.GRAVATAR, userAvatar);
} else {
final String gravatar = Thumbnails.getGravatarURL(email, "128");
dataModel.put(Common.GRAVATAR, gravatar);
}
try {
final JSONObject qiniu = optionQueryService.getOptions(Option.CATEGORY_C_QINIU);
dataModel.put(Option.ID_C_QINIU_DOMAIN, "");
dataModel.put("qiniuUploadToken", "");
if (null != qiniu && StringUtils.isNotBlank(qiniu.optString(Option.ID_C_QINIU_ACCESS_KEY)) && StringUtils.isNotBlank(qiniu.optString(Option.ID_C_QINIU_SECRET_KEY)) && StringUtils.isNotBlank(qiniu.optString(Option.ID_C_QINIU_BUCKET)) && StringUtils.isNotBlank(qiniu.optString(Option.ID_C_QINIU_DOMAIN))) {
try {
final Auth auth = Auth.create(qiniu.optString(Option.ID_C_QINIU_ACCESS_KEY), qiniu.optString(Option.ID_C_QINIU_SECRET_KEY));
final String uploadToken = auth.uploadToken(qiniu.optString(Option.ID_C_QINIU_BUCKET), null, 3600 * 6, null);
dataModel.put("qiniuUploadToken", uploadToken);
dataModel.put(Option.ID_C_QINIU_DOMAIN, qiniu.optString(Option.ID_C_QINIU_DOMAIN));
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Qiniu settings error", e);
}
}
final JSONObject preference = preferenceQueryService.getPreference();
dataModel.put(Option.ID_C_LOCALE_STRING, preference.getString(Option.ID_C_LOCALE_STRING));
dataModel.put(Option.ID_C_BLOG_TITLE, preference.getString(Option.ID_C_BLOG_TITLE));
dataModel.put(Option.ID_C_BLOG_SUBTITLE, preference.getString(Option.ID_C_BLOG_SUBTITLE));
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)));
dataModel.put(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT, preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT));
dataModel.put(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE, preference.getInt(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE));
dataModel.put(Option.ID_C_LOCALE_STRING, preference.getString(Option.ID_C_LOCALE_STRING));
dataModel.put(Option.ID_C_EDITOR_TYPE, preference.getString(Option.ID_C_EDITOR_TYPE));
dataModel.put(Skin.SKIN_DIR_NAME, preference.getString(Skin.SKIN_DIR_NAME));
Keys.fillRuntime(dataModel);
filler.fillMinified(dataModel);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Admin index render failed", e);
}
fireFreeMarkerActionEvent(templateName, dataModel);
}
use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class ArticleConsole method addArticle.
/**
* Adds an article with the specified request.
*
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "sc": boolean,
* "oId": "", // Generated article id
* "msg": ""
* }
* </pre>
* </p>
*
* @param request the specified http servlet request, for example, <pre>
* {
* "article": {
* "articleTitle": "",
* "articleAbstract": "",
* "articleContent": "",
* "articleTags": "tag1,tag2,tag3",
* "articlePermalink": "", // optional
* "articleIsPublished": boolean,
* "postToCommunity": boolean,
* "articleSignId": "" // optional
* "articleCommentable": boolean,
* "articleViewPwd": ""
* }
* }
* </pre>
*
* @param response the specified http servlet response
* @param context the specified http request context
* @throws Exception exception
*/
@RequestProcessing(value = "/console/article/", method = HTTPRequestMethod.POST)
public void addArticle(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);
final JSONObject ret = new JSONObject();
try {
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final JSONObject currentUser = userQueryService.getCurrentUser(request);
requestJSONObject.getJSONObject(Article.ARTICLE).put(Article.ARTICLE_AUTHOR_EMAIL, currentUser.getString(User.USER_EMAIL));
final String articleId = articleMgmtService.addArticle(requestJSONObject);
ret.put(Keys.OBJECT_ID, articleId);
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());
final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, e.getMessage());
}
}
use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.
the class ArticleConsole method cancelTopArticle.
/**
* Cancels an top article by the specified request.
*
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "sc": boolean,
* "msg": ""
* }
* </pre>
* </p>
*
* @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 = "/console/article/canceltop/*", method = HTTPRequestMethod.PUT)
public void cancelTopArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
if (!userQueryService.isLoggedIn(request, response)) {
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(Keys.MSG, langPropsService.get("forbiddenLabel"));
ret.put(Keys.STATUS_CODE, false);
return;
}
try {
final String articleId = request.getRequestURI().substring((Latkes.getContextPath() + "/console/article/canceltop/").length());
articleMgmtService.topArticle(articleId, false);
ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.MSG, langPropsService.get("cancelTopSuccLabel"));
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = new JSONObject();
renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.STATUS_CODE, false);
jsonObject.put(Keys.MSG, langPropsService.get("cancelTopFailLabel"));
}
}
Aggregations