Skip to content

Commit

Permalink
Improve overall design and message utils
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tamura committed Apr 5, 2017
1 parent 741b837 commit adc01f0
Show file tree
Hide file tree
Showing 32 changed files with 177 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOExce
bodyHtml.append("<td><input type=\"submit\" value=\"" + MessageUtils.getMsg("label.login", locale) + "\"></td>");
bodyHtml.append("</tr>");
bodyHtml.append("</table>");
if (req.getAttribute("login.page.note") != null) {
bodyHtml.append("<br><p>" + MessageUtils.getMsg((String) req.getAttribute("login.page.note"), locale) + "<p>");
}
String queryString = req.getQueryString();
if (queryString != null) {
bodyHtml.append("<input type=\"hidden\" name=\"loginquerystring\" value=\""
Expand All @@ -82,11 +79,13 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOExce
}

HttpSession session = req.getSession(true);

if (session.getAttribute("authNMsg") != null && !"authenticated".equals(session.getAttribute("authNMsg"))) {
bodyHtml.append("<p>" + MessageUtils.getMsg((String)session.getAttribute("authNMsg"), locale) + "</p>");
bodyHtml.append(MessageUtils.getErrMsg((String)session.getAttribute("authNMsg"), locale));
session.setAttribute("authNMsg", null);
}
if (req.getAttribute("login.page.note") != null) {
bodyHtml.append(MessageUtils.getInfoMsg((String) req.getAttribute("login.page.note"), locale));
}
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.login.page", locale),
bodyHtml.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class MessageUtils {
private MessageUtils() {
throw new IllegalAccessError("Utility class");
}

/**
* Return a message for a given property key.
*
Expand All @@ -28,6 +28,15 @@ public static String getMsg(String propertyKey, Locale locale) {
return getMsg(propertyKey, (Object[]) null, locale);
}

/**
* Return an information message for a given property key.
*
* @return An information message for a given property key
*/
public static String getInfoMsg(String propertyKey, Locale locale) {
return getInfoMsg(propertyKey, (Object[]) null, locale);
}

/**
* Return an error message for a given property key.
*
Expand All @@ -49,23 +58,29 @@ public static String getMsg(String propertyKey, Object[] placeholders, Locale lo
if (placeholders != null) {
propertyValue = MessageFormat.format(propertyValue, placeholders);
}
if (propertyValue.indexOf("(i) ") >= 0) {
propertyValue = propertyValue.replace("(i) ",
"<span class=\"glyphicon glyphicon-info-sign\"></span>&nbsp; ");
}
} catch (Exception e) {
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
return propertyValue;
}

/**
* Return an information message for a given property key, replaced with placeholders.
*
* @return An information message for a given property key, replaced with placeholders
*/
public static String getInfoMsg(String propertyKey, Object[] placeholders, Locale locale) {
return "<div class=\"alert alert-info\" role=\"alert\"><span class=\"glyphicon glyphicon-info-sign\"></span>&nbsp; "
+ getMsg(propertyKey, placeholders, locale) + "</div>";
}

/**
* Return an error message for a given property key, replaced with placeholders.
*
* @return An error message for a given property key, replaced with placeholders
*/
public static String getErrMsg(String propertyKey, Object[] placeholders, Locale locale) {
return "<div class=\"alert alert-danger\" role=\"alert\">" + getMsg(propertyKey, placeholders, locale)
+ "</div>";
return "<div class=\"alert alert-danger\" role=\"alert\"><span class=\"glyphicon glyphicon-warning-sign\"></span>&nbsp; "
+ getMsg(propertyKey, placeholders, locale) + "</div>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append(MessageUtils.getMsg("msg.enter.word", locale));
}
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.slow.regular.expression", locale));
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.slow.regular.expression", locale));
bodyHtml.append("</form>");

HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.slow.regular.expression.page", locale),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
Expand Down Expand Up @@ -66,8 +65,6 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S

bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.submit", locale) + "\">");
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.slow.string.plus.operation", locale));
bodyHtml.append("<br><br>");

if (length > 0) {
// StringBuilder builder = new StringBuilder();
Expand All @@ -87,6 +84,8 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
} else {
bodyHtml.append(MessageUtils.getMsg("msg.enter.positive.number", locale));
}
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.slow.string.plus.operation", locale));
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.random.string.generator", locale),
bodyHtml.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
final String dbUrl = ApplicationUtils.getDatabaseURL();
final String dbDriver = ApplicationUtils.getDatabaseDriver();
if(dbUrl == null || "".equals(dbUrl) || dbUrl.startsWith("jdbc:derby:memory:")){
HTTPResponseCreator.createSimpleResponse(res, null, MessageUtils.getMsg("msg.note.not.use.ext.db", locale));
HTTPResponseCreator.createSimpleResponse(res, null, MessageUtils.getInfoMsg("msg.note.not.use.ext.db", locale));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append(MessageUtils.getMsg("msg.warn.select.asc.or.desc", locale));
}
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.sql.deadlock", locale));
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.sql.deadlock", locale));
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, null, bodyHtml.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append("<br><br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.submit", locale) + "\">");
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.enter.count", locale));
bodyHtml.append("<br><br>");

if (count > 0) {
/* create a batch file in the temp directory */
Expand All @@ -62,16 +60,18 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
ProcessBuilder pb = new ProcessBuilder(batFile.getAbsolutePath());
Process process = pb.start();
process.waitFor();
bodyHtml.append(MessageUtils.getMsg("msg.executed.batch", locale) + batFile.getAbsolutePath()
+ "<BR><BR>");
bodyHtml.append(
MessageUtils.getMsg("msg.executed.batch", locale) + batFile.getAbsolutePath() + "<BR><BR>");
bodyHtml.append(MessageUtils.getMsg("label.execution.result", locale) + "<BR><BR>");
bodyHtml.append(printInputStream(process.getInputStream()));
bodyHtml.append(printInputStream(process.getErrorStream()));
}
bodyHtml.append("</form>");
} else {
bodyHtml.append(MessageUtils.getMsg("msg.enter.positive.number", locale));
}
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.enter.count", locale));
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.endless.waiting.page", locale),
bodyHtml.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.positive.number", locale));
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.positive.number", locale));
bodyHtml.append("</form>");

HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.integer.overflow.page", locale),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.enter.decimal.value", locale));
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.enter.decimal.value", locale));
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res,
MessageUtils.getMsg("title.loss.of.trailing.digits.page", locale), bodyHtml.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
hm.put(String.valueOf(cnt++), sb.toString());
StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append(MessageUtils.getMsg("msg.java.heap.space.leak.occur", req.getLocale()));
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.oome.finally.thrown", req.getLocale()));

HTTPResponseCreator.createSimpleResponse(res, null, bodyHtml.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
ClassPool pool = ClassPool.getDefault();
pool.makeClass("eu.plumbr.demo.Generated" + i).toClass();
}
HTTPResponseCreator.createSimpleResponse(res, null,
MessageUtils.getMsg("msg.permgen.space.leak.occur", req.getLocale()));
StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append(MessageUtils.getMsg("msg.permgen.space.leak.occur", req.getLocale()));
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.oome.finally.thrown", req.getLocale()));
HTTPResponseCreator.createSimpleResponse(res, null, bodyHtml.toString());
} catch (Exception e) {
log.error("Exception occurs: ", e);
} catch (Error e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
compresser.setInput(input);
compresser.deflate(output);
}
HTTPResponseCreator.createSimpleResponse(res, null,
MessageUtils.getMsg("msg.c.heap.space.leak.occur", req.getLocale()));
StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append(MessageUtils.getMsg("msg.c.heap.space.leak.occur", req.getLocale()));
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.oome.finally.thrown", req.getLocale()));
HTTPResponseCreator.createSimpleResponse(res, null, bodyHtml.toString());
} catch (Exception e) {
log.error("Exception occurs: ", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append(MessageUtils.getMsg("msg.enter.name", locale));
}
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.mojibake", locale));
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.mojibake", locale));
bodyHtml.append("</form>");

HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.xss.page", locale),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.enter.one", locale));
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.enter.one", locale));
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.round.off.error.page", locale),
bodyHtml.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.enter.specific.nembers", locale));
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.enter.specific.nembers", locale));
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res,
MessageUtils.getMsg("title.loss.of.trailing.digits.page", locale), bodyHtml.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
bodyHtml.append("<br><br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.submit", locale) + "\">");
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.csrf", locale));
String errorMessage = (String) req.getAttribute("errorMessage");
if (errorMessage != null) {
bodyHtml.append("<br><br>" + errorMessage);
bodyHtml.append(errorMessage);
}
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.csrf", locale));
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.admins.main.page", locale),
bodyHtml.toString());
}
Expand Down Expand Up @@ -105,18 +106,19 @@ protected void doPost(HttpServletRequest req, HttpServletResponse res) throws Se
bodyHtml.append("<br><br>");
bodyHtml.append("<a href=\"/admins/main\">" + MessageUtils.getMsg("label.goto.admin.page", locale)
+ "</a>");
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.admins.main.page", locale),
bodyHtml.toString());
} catch (Exception e) {
log.error("Exception occurs: ", e);
req.setAttribute("errorMessage", MessageUtils.getMsg("msg.passwd.change.failed", locale));
req.setAttribute("errorMessage", MessageUtils.getErrMsg("msg.passwd.change.failed", locale));
doGet(req, res);
}
} else {
if (password == null || "".equals(password) || password.length() < 8) {
req.setAttribute("errorMessage", MessageUtils.getMsg("msg.passwd.is.too.short", locale));
req.setAttribute("errorMessage", MessageUtils.getErrMsg("msg.passwd.is.too.short", locale));
} else {
req.setAttribute("errorMessage", MessageUtils.getMsg("msg.unknown.exception.occur", locale));
req.setAttribute("errorMessage", MessageUtils.getErrMsg("msg.unknown.exception.occur", locale));
}
doGet(req, res);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
bodyHtml.append("<br><br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.submit", locale) + "\">");
bodyHtml.append("<br><br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.clickjacking", locale));
String errorMessage = (String) req.getAttribute("errorMessage");
if (errorMessage != null) {
bodyHtml.append("<br><br>" + errorMessage);
bodyHtml.append(errorMessage);
}
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.clickjacking", locale));
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.admins.main.page", locale),
bodyHtml.toString());
}
Expand Down Expand Up @@ -107,15 +108,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse res) throws Se
bodyHtml.append("<br><br>");
bodyHtml.append("<a href=\"/admins/main\">" + MessageUtils.getMsg("label.goto.admin.page", locale)
+ "</a>");
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.admins.main.page", locale),
bodyHtml.toString());
} catch (Exception e) {
log.error("Exception occurs: ", e);
req.setAttribute("errorMessage", MessageUtils.getMsg("msg.mail.change.failed", locale));
req.setAttribute("errorMessage", MessageUtils.getErrMsg("msg.mail.change.failed", locale));
doGet(req, res);
}
} else {
req.setAttribute("errorMessage", MessageUtils.getMsg("msg.mail.format.is.invalid", locale));
req.setAttribute("errorMessage", MessageUtils.getErrMsg("msg.mail.format.is.invalid", locale));
doGet(req, res);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append(MessageUtils.getMsg("msg.enter.json.string", locale));
bodyHtml.append("<br><br>");
}
bodyHtml.append(MessageUtils.getMsg("msg.note.code.injection", locale));
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.code.injection", locale));
bodyHtml.append("</form>");

HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.parse.json", locale),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
bodyHtml.append(MessageUtils.getMsg("msg.warn.enter.name.and.passwd", locale));
bodyHtml.append("<br><br>");
}
bodyHtml.append(MessageUtils.getMsg("msg.note.ldap.injection", locale));
bodyHtml.append(MessageUtils.getInfoMsg("msg.note.ldap.injection", locale));
bodyHtml.append("</form>");

HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.sql.injection.page", locale),
Expand Down
Loading

0 comments on commit adc01f0

Please sign in to comment.