Search in sources :

Example 1 with RestContentHeader

use of org.apache.qpid.server.model.RestContentHeader in project qpid-broker-j by apache.

the class AbstractServlet method getResponseHeaders.

private Map<String, Object> getResponseHeaders(final Object content) {
    Map<String, Object> headers = Collections.emptyMap();
    if (content instanceof CustomRestHeaders) {
        CustomRestHeaders customRestHeaders = (CustomRestHeaders) content;
        Map<RestContentHeader, Method> contentHeaderGetters = getContentHeaderMethods(customRestHeaders);
        if (contentHeaderGetters != null) {
            headers = new HashMap<>();
            for (Map.Entry<RestContentHeader, Method> entry : contentHeaderGetters.entrySet()) {
                final String headerName = entry.getKey().value();
                try {
                    final Object headerValue = entry.getValue().invoke(customRestHeaders);
                    if (headerValue != null) {
                        headers.put(headerName.toUpperCase(), headerValue);
                    }
                } catch (Exception e) {
                    LOGGER.warn("Unexpected exception whilst setting response header " + headerName, e);
                }
            }
        }
    }
    return headers;
}
Also used : CustomRestHeaders(org.apache.qpid.server.model.CustomRestHeaders) RestContentHeader(org.apache.qpid.server.model.RestContentHeader) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException)

Example 2 with RestContentHeader

use of org.apache.qpid.server.model.RestContentHeader in project qpid-broker-j by apache.

the class AbstractServlet method getContentHeaderMethods.

private Map<RestContentHeader, Method> getContentHeaderMethods(CustomRestHeaders content) {
    Map<RestContentHeader, Method> results = new HashMap<>();
    Method[] methods = content.getClass().getMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(RestContentHeader.class)) {
            final Class<?>[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length > 0) {
                LOGGER.warn("Parameters are found on method " + method.getName() + " annotated with ContentHeader annotation. No parameter is allowed. Ignoring ContentHeader annotation.");
            } else {
                method.setAccessible(true);
                results.put(method.getAnnotation(RestContentHeader.class), method);
            }
        }
    }
    return results;
}
Also used : RestContentHeader(org.apache.qpid.server.model.RestContentHeader) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 RestContentHeader (org.apache.qpid.server.model.RestContentHeader)2 IOException (java.io.IOException)1 Map (java.util.Map)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ServletException (javax.servlet.ServletException)1 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)1 CustomRestHeaders (org.apache.qpid.server.model.CustomRestHeaders)1 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)1