use of io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST in project LogHub by fbacchella.
the class JmxProxy method processRequest.
@Override
protected boolean processRequest(FullHttpRequest request, ChannelHandlerContext ctx) throws HttpRequestFailure {
String rawname = request.uri().replace("/jmx/", "");
String name;
try {
name = URLDecoder.decode(rawname, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HttpRequestFailure(HttpResponseStatus.NOT_FOUND, String.format("malformed object name '%s': %s", rawname, e.getMessage()));
}
try {
Set<ObjectName> objectinstance = server.queryNames(new ObjectName(name), null);
ObjectName found = objectinstance.stream().findAny().orElseThrow(() -> new HttpRequestFailure(HttpResponseStatus.NOT_FOUND, String.format("malformed object name '%s'", name)));
MBeanInfo info = server.getMBeanInfo(found);
MBeanAttributeInfo[] attrInfo = info.getAttributes();
Map<String, Object> mbeanmap = new HashMap<>(attrInfo.length);
try {
Arrays.stream(attrInfo).map(i -> i.getName()).forEach(i -> {
try {
Object o = resolveAttribute(server.getAttribute(found, i));
if (o != null) {
mbeanmap.put(i, o);
}
} catch (JMException e) {
String message = String.format("Failure reading attribue %s from %s: %s}", i, name, e.getMessage());
logger.error(message);
logger.debug(e);
throw new RuntimeException(message);
}
});
} catch (RuntimeException e) {
// Capture the JMException that might have been previously thrown, and transform it in an HTTP processing exception
throw new HttpRequestFailure(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
String serialized = json.get().writeValueAsString(mbeanmap);
ByteBuf content = Unpooled.copiedBuffer(serialized + "\r\n", CharsetUtil.UTF_8);
return writeResponse(ctx, request, content, content.readableBytes());
} catch (MalformedObjectNameException e) {
throw new HttpRequestFailure(BAD_REQUEST, String.format("malformed object name '%s': %s", name, e.getMessage()));
} catch (IntrospectionException | ReflectionException | RuntimeException | JsonProcessingException e) {
Throwable t = e;
while (t.getCause() != null) {
t = t.getCause();
}
throw new HttpRequestFailure(BAD_REQUEST, String.format("malformed object content '%s': %s", name, e.getMessage()));
} catch (InstanceNotFoundException e) {
throw new HttpRequestFailure(HttpResponseStatus.NOT_FOUND, String.format("malformed object name '%s': %s", name, e.getMessage()));
}
}
Aggregations