
com.ebay.jetstream.management.HtmlResourceFormatter Maven / Gradle / Ivy
/*******************************************************************************
* Copyright © 2012-2015 eBay Software Foundation
* This program is dual licensed under the MIT and Apache 2.0 licenses.
* Please see LICENSE for more information.
*******************************************************************************/
/**
*
*/
package com.ebay.jetstream.management;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.util.ClassUtils;
import com.ebay.jetstream.util.CommonUtils;
import com.ebay.jetstream.xmlser.XMLSerializationManager;
/**
*
*/
public class HtmlResourceFormatter extends AbstractResourceFormatter {
private String m_format = "html";
public HtmlResourceFormatter() {
super("spring");
}
public String getContentType() {
return "text/html";
}
@Override
protected void beginFormat() throws IOException {
pushElement("html", null);
getWriter().println(
"Viewing as " + getFormat() + "
");
formatTitleLink(1, makeFormattedPath(getFormat(), getPrefix()),
"Directory Root");
formatTitleLink(3, "/visualpipeline", "Visual Data Pipeline");
if (!CommonUtils.isEmptyTrimmed(getPath())) {
formatTitleLink(2,
makeFormattedPath(getFormat(), getPrefix(), getPath()),
getPath());
}
}
@Override
protected void endFormat() throws IOException {
getWriter().println("View as
");
for (String format : Management.getResourceFormatters()) {
formatHRef(makeFormattedPath(format, getPrefix(), getPath()),
format);
}
popElement();
}
protected void formatBean(boolean end, Class> bclass, String help)
throws IOException {
PrintWriter pw = getWriter();
if (!CommonUtils.isEmptyTrimmed(help)) {
pw.print("" + help + ": ");
}
pw.println("type " + bclass.getName() + "");
}
@Override
protected final void formatBean(Object bean) throws Exception {
Class> bclass = bean.getClass();
ManagedResource mr = bclass.getAnnotation(ManagedResource.class);
String help = mr == null ? null : mr.description();
formatBean(false, bclass, help);
boolean section = false;
for (PropertyDescriptor pd : Introspector.getBeanInfo(bclass)
.getPropertyDescriptors()) {
Method getter = pd.getReadMethod();
if (!XMLSerializationManager.isHidden(getter)) {
if (!section) {
section = true;
formatSection(false, "Properties");
}
formatProperty(bean, pd);
}
}
if (section) {
formatSection(true, "Properties");
}
section = false;
for (Method method : bean.getClass().getMethods()) {
ManagedOperation mo = method.getAnnotation(ManagedOperation.class);
if (mo != null && method.getParameterTypes().length == 0) {
help = mo.description();
if (!section) {
section = true;
formatSection(false, "Operations");
}
formatOperation(method);
}
if (section) {
formatSection(true, "Operations");
}
}
}
protected void formatHRef(String ref, String text) throws IOException {
getWriter().println("" + text + "");
}
protected void formatOperation(Method method) throws IOException {
PrintWriter pw = getWriter();
String text = method.getName();
formatHRef(makePath(getPrefix(), getPath(), "?" + text), text);
text = method.getAnnotation(ManagedOperation.class).description();
if (!CommonUtils.isEmptyTrimmed(text)) {
pw.print(" (" + text + ")");
}
pw.println();
}
protected void formatProperty(Object bean, PropertyDescriptor pd)
throws Exception {
PrintWriter pw = getWriter();
Method getter = pd.getReadMethod();
Class> pclass = pd.getPropertyType();
ManagedAttribute attr = getter.getAnnotation(ManagedAttribute.class);
String text = attr != null ? attr.description() : null;
if (CommonUtils.isEmptyTrimmed(text)) {
text = pd.getDisplayName();
} else {
text = pd.getDisplayName() + " (" + text + ")";
}
pw.print(text + ": " + pclass.getName() + " = ");
getter.setAccessible(true);
Object value = getter.invoke(bean);
Method setter = pd.getWriteMethod();
attr = setter == null ? null : setter
.getAnnotation(ManagedAttribute.class);
boolean isComplex = !(String.class.isAssignableFrom(pclass) || ClassUtils
.isPrimitiveOrWrapper(pclass));
if (isComplex) {
value = StringEscapeUtils.escapeXml(getSerializer()
.getXMLStringRepresentation(value));
}
if (attr == null) {
if (isComplex) {
pushElement("code", null);
}
pw.println(value);
if (isComplex) {
popElement();
}
} else {
pw.println(attr.description());
pushElement(
"form",
"action="
+ makePath(getPrefix(), getPath(),
isComplex ? "?form" : "?" + pd.getName())
+ " method=" + (isComplex ? "POST" : "GET"));
if (isComplex) {
pw.print("");
} else {
pw.print("");
}
pw.println("");
popElement();
}
pw.println("");
}
@Override
protected void formatReference(String key) throws IOException {
formatHRef(getReference(key) + "?" + BEAN_FORMAT_PARAM + "="
+ getFormat(), key);
getWriter().println("");
}
protected void formatSection(boolean end, String type) throws IOException {
if (!end) {
getWriter().println("" + type + "
");
}
}
protected void formatTitleLink(int level, String link, String text)
throws IOException {
pushElement("h" + level, null);
formatHRef(link, text);
popElement();
}
protected String getFormat() {
return m_format;
}
protected String makeFormattedPath(String format, String... strings) {
String s = makePath(strings) + "?" + BEAN_FORMAT_PARAM + "=" + format;
return s;
}
protected void setFormat(String format) {
m_format = format;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy