Search in sources :

Example 1 with BAD_REQUEST

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()));
    }
}
Also used : Arrays(java.util.Arrays) IntrospectionException(javax.management.IntrospectionException) URLDecoder(java.net.URLDecoder) Date(java.util.Date) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) HashMap(java.util.HashMap) TabularData(javax.management.openmbean.TabularData) Sharable(io.netty.channel.ChannelHandler.Sharable) Unpooled(io.netty.buffer.Unpooled) BAD_REQUEST(io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) MBeanServer(javax.management.MBeanServer) CharsetUtil(io.netty.util.CharsetUtil) ManagementFactory(java.lang.management.ManagementFactory) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) HttpRequest(io.netty.handler.codec.http.HttpRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) CompositeData(javax.management.openmbean.CompositeData) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ObjectName(javax.management.ObjectName) MBeanInfo(javax.management.MBeanInfo) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MalformedObjectNameException(javax.management.MalformedObjectNameException) Logger(org.apache.logging.log4j.Logger) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JMException(javax.management.JMException) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LogManager(org.apache.logging.log4j.LogManager) ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanInfo(javax.management.MBeanInfo) HashMap(java.util.HashMap) InstanceNotFoundException(javax.management.InstanceNotFoundException) IntrospectionException(javax.management.IntrospectionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteBuf(io.netty.buffer.ByteBuf) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) JMException(javax.management.JMException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 Sharable (io.netty.channel.ChannelHandler.Sharable)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 BAD_REQUEST (io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST)1 CharsetUtil (io.netty.util.CharsetUtil)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ManagementFactory (java.lang.management.ManagementFactory)1 URLDecoder (java.net.URLDecoder)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1