Search in sources :

Example 1 with ResponseType

use of org.apache.qpid.server.management.plugin.ResponseType in project qpid-broker-j by apache.

the class LatestManagementController method invoke.

@Override
@SuppressWarnings("unchecked")
public ManagementResponse invoke(final ConfiguredObject<?> root, final String category, final List<String> names, final String operationName, final Map<String, Object> operationArguments, final boolean isPost, final boolean isSecureOrAllowedOnInsecureChannel) throws ManagementException {
    ResponseType responseType = ResponseType.DATA;
    Object returnValue;
    try {
        final ConfiguredObject<?> target = getTarget(root, category, names);
        final Map<String, ConfiguredObjectOperation<?>> availableOperations = root.getModel().getTypeRegistry().getOperations(target.getClass());
        final ConfiguredObjectOperation operation = availableOperations.get(operationName);
        if (operation == null) {
            throw createNotFoundManagementException(String.format("No such operation '%s' in '%s'", operationName, category));
        }
        final Map<String, Object> arguments;
        if (isPost) {
            arguments = operationArguments;
        } else {
            final Set<String> supported = ((List<OperationParameter>) operation.getParameters()).stream().map(OperationParameter::getName).collect(Collectors.toSet());
            arguments = operationArguments.entrySet().stream().filter(e -> !RESERVED_PARAMS.contains(e.getKey()) || supported.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        }
        if (operation.isSecure(target, arguments) && !isSecureOrAllowedOnInsecureChannel) {
            throw createForbiddenManagementException(String.format("Operation '%s' can only be performed over a secure (HTTPS) connection", operationName));
        }
        if (!isPost && !operation.isNonModifying()) {
            throw createNotAllowedManagementException(String.format("Operation '%s' modifies the object so you must use POST.", operationName), Collections.singletonMap("Allow", "POST"));
        }
        returnValue = operation.perform(target, arguments);
        if (ConfiguredObject.class.isAssignableFrom(operation.getReturnType()) || returnsCollectionOfConfiguredObjects(operation)) {
            responseType = ResponseType.MODEL_OBJECT;
        }
    } catch (RuntimeException e) {
        throw ManagementException.toManagementException(e, getCategoryMapping(category), names);
    } catch (Error e) {
        throw ManagementException.handleError(e);
    }
    return new ControllerManagementResponse(responseType, returnValue);
}
Also used : ResponseType(org.apache.qpid.server.management.plugin.ResponseType) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectOperation(org.apache.qpid.server.model.ConfiguredObjectOperation) ControllerManagementResponse(org.apache.qpid.server.management.plugin.controller.ControllerManagementResponse)

Example 2 with ResponseType

use of org.apache.qpid.server.management.plugin.ResponseType in project qpid-broker-j by apache.

the class AbstractManagementController method handlePostOrPut.

private ManagementResponse handlePostOrPut(final ManagementRequest request) throws ManagementException {
    final RequestType type = getRequestType(request);
    final Collection<String> hierarchy = getCategoryHierarchy(request.getRoot(), request.getCategory());
    switch(type) {
        case OPERATION:
            {
                final List<String> operationPath = request.getPath().subList(0, hierarchy.size());
                final String operationName = request.getPath().get(hierarchy.size());
                @SuppressWarnings("unchecked") final Map<String, Object> arguments = request.getBody(LinkedHashMap.class);
                return invoke(request.getRoot(), request.getCategory(), operationPath, operationName, arguments, true, request.isSecure() || request.isConfidentialOperationAllowedOnInsecureChannel());
            }
        case MODEL_OBJECT:
            {
                @SuppressWarnings("unchecked") final Map<String, Object> attributes = request.getBody(LinkedHashMap.class);
                final Object response = createOrUpdate(request.getRoot(), request.getCategory(), request.getPath(), attributes, "POST".equalsIgnoreCase(request.getMethod()));
                int responseCode = HttpServletResponse.SC_OK;
                ResponseType responseType = ResponseType.EMPTY;
                Map<String, String> headers = Collections.emptyMap();
                if (response != null) {
                    responseCode = HttpServletResponse.SC_CREATED;
                    final StringBuilder requestURL = new StringBuilder(request.getRequestURL());
                    if (hierarchy.size() != request.getPath().size()) {
                        Object name = attributes.get("name");
                        requestURL.append("/").append(encode(String.valueOf(name)));
                    }
                    headers = Collections.singletonMap("Location", requestURL.toString());
                    responseType = ResponseType.MODEL_OBJECT;
                }
                return new ControllerManagementResponse(responseType, response, responseCode, headers);
            }
        case VISIBLE_PREFERENCES:
        case USER_PREFERENCES:
            {
                setPreferences(request.getRoot(), request.getCategory(), request.getPath(), request.getBody(Object.class), request.getParameters(), "POST".equalsIgnoreCase(request.getMethod()));
                return new ControllerManagementResponse(ResponseType.EMPTY, null);
            }
        default:
            {
                throw createBadRequestManagementException(String.format("Unexpected request type '%s' for path '%s'", type, getCategoryMapping(request.getCategory())));
            }
    }
}
Also used : List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RequestType(org.apache.qpid.server.management.plugin.RequestType) LinkedHashMap(java.util.LinkedHashMap) ResponseType(org.apache.qpid.server.management.plugin.ResponseType)

Aggregations

List (java.util.List)2 ResponseType (org.apache.qpid.server.management.plugin.ResponseType)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 RequestType (org.apache.qpid.server.management.plugin.RequestType)1 ControllerManagementResponse (org.apache.qpid.server.management.plugin.controller.ControllerManagementResponse)1 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)1 ConfiguredObjectOperation (org.apache.qpid.server.model.ConfiguredObjectOperation)1