Skip to content

Commit

Permalink
Merge branch '1.6.0-SNAPSHOT'
Browse files Browse the repository at this point in the history
Conflicts:
	server/monitor/src/main/java/org/apache/accumulo/monitor/servlets/BasicServlet.java
  • Loading branch information
ctubbsii committed Apr 23, 2014
2 parents 9ef0fca + 9621701 commit 7492d74
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@
<effort>Max</effort>
<failOnError>true</failOnError>
<includeTests>true</includeTests>
<maxRank>4</maxRank>
<maxRank>6</maxRank>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
Expand Down Expand Up @@ -84,21 +85,25 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S

private static final String DEFAULT_CONTENT_TYPE = "text/html";

public static final Cookie getCookie(HttpServletRequest req, String name) {
public static final void setCookie(HttpServletResponse resp, String name, String value) {
resp.addCookie(new Cookie(name, value));
}

public static final String getCookieValue(HttpServletRequest req, String name) {
if (req.getCookies() != null)
for (Cookie c : req.getCookies())
if (c.getName().equals(name))
return c;
return c.getValue();
return null;
}

protected void pageStart(HttpServletRequest req, HttpServletResponse resp, StringBuilder sb) throws Exception {
resp.setContentType(DEFAULT_CONTENT_TYPE);
int refresh = -1;
Cookie c = getCookie(req, "page.refresh.rate");
if (c != null && c.getValue() != null) {
String refreshStr = getCookieValue(req, "page.refresh.rate");
if (refreshStr != null) {
try {
refresh = Integer.parseInt(c.getValue());
refresh = Integer.parseInt(BasicServlet.decode(refreshStr));
} catch (NumberFormatException e) {
// ignore improperly formatted user cookie
}
Expand Down Expand Up @@ -247,7 +252,16 @@ public static String encode(String s) {
return URLEncoder.encode(s, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
Logger.getLogger(BasicServlet.class).fatal(StandardCharsets.UTF_8.name() + " is not a recognized encoding", e);
throw new RuntimeException(e);
throw new AssertionError(e); // can't happen with UTF-8
}
}

public static String decode(String s) {
try {
return URLDecoder.decode(s, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
Logger.getLogger(BasicServlet.class).fatal(StandardCharsets.UTF_8.name() + " is not a recognized encoding", e);
throw new AssertionError(e); // can't happen with UTF-8
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand All @@ -33,21 +32,21 @@
import org.apache.log4j.Logger;

public class OperationServlet extends BasicServlet {

private static final long serialVersionUID = 1L;

@Override
protected String getTitle(HttpServletRequest req) {
return "Operations";
}

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String redir = null;
try {
String operation = req.getParameter("action");
redir = req.getParameter("redir");

if (operation != null) {
for (Class<?> subclass : OperationServlet.class.getClasses()) {
Object t;
Expand All @@ -69,36 +68,35 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
log.error(t, t);
} finally {
try {
if (redir != null)
resp.sendRedirect(redir);
else
resp.sendRedirect("/");
redir = redir == null ? "" : redir;
redir = redir.startsWith("/") ? redir.substring(1) : redir;
resp.sendRedirect("/" + redir); // this makes findbugs happy
resp.flushBuffer();
} catch (Throwable t) {
log.error(t, t);
}
}
}

private interface WebOperation {
void execute(HttpServletRequest req, HttpServletResponse resp, Logger log) throws Exception;
}

public static class RefreshOperation implements WebOperation {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log) {
String value = req.getParameter("value");
resp.addCookie(new Cookie("page.refresh.rate", value == null ? "5" : value));
BasicServlet.setCookie(resp, "page.refresh.rate", value == null ? "5" : BasicServlet.encode(value));
}
}

public static class ClearLogOperation implements WebOperation {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log) {
LogService.getInstance().clear();
}
}

public static class ClearTableProblemsOperation implements WebOperation {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log) {
Expand All @@ -110,7 +108,7 @@ public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log
}
}
}

public static class ClearProblemOperation implements WebOperation {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log) {
Expand All @@ -124,7 +122,7 @@ public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log
}
}
}

public static class SortTableOperation implements WebOperation {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log) throws IOException {
Expand All @@ -134,13 +132,18 @@ public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log
String col = req.getParameter("col");
if (table == null || page == null || (asc == null && col == null))
return;
if (asc == null)
resp.addCookie(new Cookie("tableSort." + page + "." + table + "." + "sortCol", col));
else
resp.addCookie(new Cookie("tableSort." + page + "." + table + "." + "sortAsc", asc));
page = BasicServlet.encode(page);
table = BasicServlet.encode(table);
if (asc == null) {
col = BasicServlet.encode(col);
BasicServlet.setCookie(resp, "tableSort." + page + "." + table + "." + "sortCol", col);
} else {
asc = BasicServlet.encode(asc);
BasicServlet.setCookie(resp, "tableSort." + page + "." + table + "." + "sortAsc", asc);
}
}
}

public static class ToggleLegendOperation implements WebOperation {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log) throws Exception {
Expand All @@ -149,10 +152,13 @@ public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log
String show = req.getParameter("show");
if (table == null || page == null || show == null)
return;
resp.addCookie(new Cookie("tableLegend." + page + "." + table + "." + "show", show));
page = BasicServlet.encode(page);
table = BasicServlet.encode(table);
show = BasicServlet.encode(show);
BasicServlet.setCookie(resp, "tableLegend." + page + "." + table + "." + "show", show);
}
}

public static class ClearDeadServerOperation implements WebOperation {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp, Logger log) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.ArrayList;
import java.util.Collections;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.accumulo.monitor.servlets.BasicServlet;
Expand All @@ -34,11 +33,11 @@ public class Table {
private ArrayList<TableColumn<?>> columns;
private ArrayList<TableRow> rows;
private boolean hasBegunAddingRows = false;

public Table(String tableName, String caption) {
this(tableName, caption, null);
}

public Table(String tableName, String caption, String captionClass) {
this.table = tableName;
this.caption = caption;
Expand All @@ -47,52 +46,52 @@ public Table(String tableName, String caption, String captionClass) {
this.columns = new ArrayList<TableColumn<?>>();
this.rows = new ArrayList<TableRow>();
}

public synchronized void setSubCaption(String subcaption) {
this.subcaption = subcaption;
}

public synchronized <T> void addColumn(TableColumn<T> column) {
if (hasBegunAddingRows)
throw new IllegalStateException("Cannot add more columns newServer rows have been added");
columns.add(column);
}

private synchronized <T> void addColumn(String title, CellType<T> type, String legend, boolean sortable) {
if (type == null)
type = new StringType<T>();
type.setSortable(sortable);
addColumn(new TableColumn<T>(title, type, legend));
}

public synchronized <T> void addUnsortableColumn(String title, CellType<T> type, String legend) {
addColumn(title, type, legend, false);
}

public synchronized <T> void addSortableColumn(String title, CellType<T> type, String legend) {
addColumn(title, type, legend, true);
}

public synchronized void addUnsortableColumn(String title) {
addUnsortableColumn(title, null, null);
}

public synchronized void addSortableColumn(String title) {
addSortableColumn(title, null, null);
}

public synchronized TableRow prepareRow() {
hasBegunAddingRows = true;
return new TableRow(columns.size());
}

public synchronized void addRow(TableRow row) {
hasBegunAddingRows = true;
if (columns.size() != row.size())
throw new IllegalStateException("Row must be the same size as the columns");
rows.add(row);
}

public synchronized void addRow(Object... cells) {
TableRow row = prepareRow();
if (cells.length != columns.size())
Expand All @@ -101,20 +100,18 @@ public synchronized void addRow(Object... cells) {
row.add(cell);
addRow(row);
}

public synchronized void generate(HttpServletRequest req, StringBuilder sb) {
String page = req.getRequestURI();
if (columns.isEmpty())
throw new IllegalStateException("No columns in table");
for (TableRow row : rows)
if (row.size() != columns.size())
throw new RuntimeException("Each row must have the same number of columns");

boolean sortAscending = true;
Cookie c = BasicServlet.getCookie(req, "tableSort." + page + "." + table + "." + "sortAsc");
if (c != null && "false".equals(c.getValue()))
sortAscending = false;


boolean sortAscending = !"false".equals(BasicServlet.getCookieValue(req, "tableSort." + BasicServlet.encode(page) + "." + BasicServlet.encode(table) + "."
+ "sortAsc"));

int sortCol = -1; // set to first sortable column by default
int numLegends = 0;
for (int i = 0; i < columns.size(); ++i) {
Expand All @@ -124,13 +121,13 @@ public synchronized void generate(HttpServletRequest req, StringBuilder sb) {
if (col.getLegend() != null && !col.getLegend().isEmpty())
++numLegends;
}

// only get cookie if there is a possibility that it is sortable
if (sortCol >= 0) {
c = BasicServlet.getCookie(req, "tableSort." + page + "." + table + "." + "sortCol");
if (c != null && c.getValue() != null) {
String sortColStr = BasicServlet.getCookieValue(req, "tableSort." + BasicServlet.encode(page) + "." + BasicServlet.encode(table) + "." + "sortCol");
if (sortColStr != null) {
try {
int col = Integer.parseInt(c.getValue());
int col = Integer.parseInt(sortColStr);
// only bother if specified column is sortable
if (!(col < 0 || sortCol >= columns.size()) && columns.get(col).getCellType().isSortable())
sortCol = col;
Expand All @@ -139,13 +136,13 @@ public synchronized void generate(HttpServletRequest req, StringBuilder sb) {
}
}
}

boolean showLegend = false;
if (numLegends > 0) {
c = BasicServlet.getCookie(req, "tableLegend." + page + "." + table + "." + "show");
showLegend = c != null && Boolean.parseBoolean(c.getValue());
String showStr = BasicServlet.getCookieValue(req, "tableLegend." + BasicServlet.encode(page) + "." + BasicServlet.encode(table) + "." + "show");
showLegend = showStr != null && Boolean.parseBoolean(showStr);
}

sb.append("<div>\n");
sb.append("<a name='").append(table).append("'>&nbsp;</a>\n");
sb.append("<table id='").append(table).append("' class='sortable'>\n");
Expand All @@ -157,7 +154,7 @@ public synchronized void generate(HttpServletRequest req, StringBuilder sb) {
sb.append("<span class='table-caption'>").append(caption).append("</span><br />\n");
if (subcaption != null && !subcaption.isEmpty())
sb.append("<span class='table-subcaption'>").append(subcaption).append("</span><br />\n");

String redir = BasicServlet.currentPage(req);
if (numLegends > 0) {
String legendUrl = String.format("/op?action=toggleLegend&redir=%s&page=%s&table=%s&show=%s", redir, page, table, !showLegend);
Expand Down Expand Up @@ -213,7 +210,7 @@ public synchronized void generate(HttpServletRequest req, StringBuilder sb) {
sb.append("<tr><td class='center' colspan='").append(columns.size()).append("'><i>Empty</i></td></tr>\n");
sb.append("</table>\n</div>\n\n");
}

private static void row(StringBuilder sb, boolean highlight, ArrayList<TableColumn<?>> columns, TableRow row) {
sb.append(highlight ? "<tr class='highlight'>" : "<tr>");
boolean first = true;
Expand All @@ -229,5 +226,5 @@ private static void row(StringBuilder sb, boolean highlight, ArrayList<TableColu
}
sb.append("</tr>\n");
}

}

0 comments on commit 7492d74

Please sign in to comment.