Search in sources :

Example 1 with ConfiguredObjectOperation

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

the class ManagementNode method performGetOperations.

private Map<String, Map<String, List<String>>> performGetOperations(final Map<String, Object> headers) {
    // TODO - enumerate management operations
    final Map<String, List<String>> managementOperations = new HashMap<>();
    return performManagementOperation(headers, clazz -> {
        final Map<String, ConfiguredObjectOperation<?>> operations = _model.getTypeRegistry().getOperations(clazz);
        Map<String, List<String>> result = new HashMap<>();
        for (Map.Entry<String, ConfiguredObjectOperation<?>> operation : operations.entrySet()) {
            List<String> arguments = new ArrayList<>();
            for (OperationParameter param : operation.getValue().getParameters()) {
                arguments.add(param.getName());
            }
            result.put(operation.getKey(), arguments);
        }
        return result;
    }, managementOperations);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OperationParameter(org.apache.qpid.server.model.OperationParameter) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ConfiguredObjectOperation(org.apache.qpid.server.model.ConfiguredObjectOperation) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ConfiguredObjectOperation

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

the class MetaDataServlet method processOperations.

private Map<String, Map> processOperations(final Class<? extends ConfiguredObject> type, final Model model) {
    Collection<ConfiguredObjectOperation<?>> operations = model.getTypeRegistry().getOperations(type).values();
    Map<String, Map> attributeDetails = new LinkedHashMap<>();
    for (ConfiguredObjectOperation<?> operation : operations) {
        Map<String, Object> attrDetails = new LinkedHashMap<>();
        attrDetails.put("name", operation.getName());
        attrDetails.put("returnType", operation.getReturnType().getSimpleName());
        if (!"".equals(operation.getDescription())) {
            attrDetails.put("description", operation.getDescription());
        }
        List<OperationParameter> parameters = operation.getParameters();
        if (!parameters.isEmpty()) {
            Map<String, Map> paramDetails = new LinkedHashMap<>();
            for (OperationParameter param : parameters) {
                Map<String, Object> paramAttrs = new LinkedHashMap<>();
                paramAttrs.put("type", param.getType().getSimpleName());
                paramAttrs.put("mandatory", param.isMandatory());
                if (!"".equals(param.getDefaultValue())) {
                    paramAttrs.put("defaultValue", param.getDefaultValue());
                }
                paramDetails.put(param.getName(), paramAttrs);
            }
            attrDetails.put("parameters", paramDetails);
        }
        attributeDetails.put(operation.getName(), attrDetails);
    }
    return attributeDetails;
}
Also used : OperationParameter(org.apache.qpid.server.model.OperationParameter) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ManagedObject(org.apache.qpid.server.model.ManagedObject) ConfiguredObjectOperation(org.apache.qpid.server.model.ConfiguredObjectOperation) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ConfiguredObjectOperation

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

the class RestServlet method doOperation.

private void doOperation(final RequestInfo requestInfo, final ConfiguredObject<?> managedObject, final Class<? extends ConfiguredObject> configuredClass, final ConfiguredObjectFinder finder, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
    ConfiguredObject<?> target = getTarget(requestInfo, managedObject, configuredClass, finder);
    if (target == null) {
        return;
    }
    String operationName = requestInfo.getOperationName();
    final Map<String, ConfiguredObjectOperation<?>> availableOperations = managedObject.getModel().getTypeRegistry().getOperations(target.getClass());
    ConfiguredObjectOperation operation = availableOperations.get(operationName);
    Map<String, Object> operationArguments;
    String requestMethod = request.getMethod();
    if (operation == null) {
        sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "No such operation: " + operationName);
        return;
    } else {
        switch(requestMethod) {
            case "GET":
                if (operation.isNonModifying()) {
                    operationArguments = getOperationArgumentsAsMap(request);
                    operationArguments.keySet().removeAll(Arrays.asList(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM));
                } else {
                    response.addHeader("Allow", "POST");
                    sendJsonErrorResponse(request, response, HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Operation " + operationName + " modifies the object so you must use POST.");
                    return;
                }
                break;
            case "POST":
                operationArguments = getRequestProvidedObject(request, requestInfo);
                break;
            default:
                response.addHeader("Allow", (operation.isNonModifying() ? "POST, GET" : "POST"));
                sendJsonErrorResponse(request, response, HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Operation " + operationName + " does not support the " + requestMethod + " requestMethod.");
                return;
        }
    }
    if (operation.isSecure(target, operationArguments) && !(request.isSecure() || HttpManagementUtil.getPort(request).isAllowConfidentialOperationsOnInsecureChannels())) {
        sendJsonErrorResponse(request, response, HttpServletResponse.SC_FORBIDDEN, "Operation '" + operationName + "' can only be performed over a secure (HTTPS) connection");
        return;
    }
    Object returnVal = operation.perform(target, operationArguments);
    String attachmentFilename = request.getParameter(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM);
    if (attachmentFilename != null) {
        setContentDispositionHeaderIfNecessary(response, attachmentFilename);
    }
    if (returnVal instanceof Content) {
        Content content = (Content) returnVal;
        try {
            writeTypedContent(content, request, response);
        } finally {
            content.release();
        }
    } else {
        final ConfiguredObjectToMapConverter.ConverterOptions converterOptions = new ConfiguredObjectToMapConverter.ConverterOptions(DEFAULT_DEPTH, false, DEFAULT_OVERSIZE, request.isSecure(), true);
        if (ConfiguredObject.class.isAssignableFrom(operation.getReturnType())) {
            returnVal = _objectConverter.convertObjectToMap((ConfiguredObject<?>) returnVal, operation.getReturnType(), converterOptions);
        } else if (returnsCollectionOfConfiguredObjects(operation)) {
            List<Map<String, Object>> output = new ArrayList<>();
            for (Object configuredObject : (Collection) returnVal) {
                output.add(_objectConverter.convertObjectToMap((ConfiguredObject<?>) configuredObject, getCollectionMemberType((ParameterizedType) operation.getGenericReturnType()), converterOptions));
            }
            returnVal = output;
        }
        sendJsonResponse(returnVal, request, response);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Content(org.apache.qpid.server.model.Content) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredObjectOperation(org.apache.qpid.server.model.ConfiguredObjectOperation)

Example 4 with ConfiguredObjectOperation

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

the class ConfiguredObjectTypeRegistryTest method testOperations.

public void testOperations() {
    final String objectName = "testKitCar";
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(ConfiguredObject.NAME, objectName);
    attributes.put(ConfiguredObject.TYPE, TestKitCarImpl.TEST_KITCAR_TYPE);
    TestCar object = TestModel.getInstance().getObjectFactory().create(TestCar.class, attributes, null);
    assertEquals(TestKitCarImpl.class, object.getTypeClass());
    final Map<String, ConfiguredObjectOperation<?>> kitCarOperations = _typeRegistry.getOperations(object.getClass());
    assertTrue(kitCarOperations.containsKey("openDoor"));
    final ConfiguredObjectOperation<TestCar<?>> operation = (ConfiguredObjectOperation<TestCar<?>>) kitCarOperations.get("openDoor");
    // test explicitly setting parameter
    Object returnVal = operation.perform(object, Collections.<String, Object>singletonMap("door", "DRIVER"));
    assertEquals(TestCar.Door.DRIVER, returnVal);
    // test default parameter
    returnVal = operation.perform(object, Collections.<String, Object>emptyMap());
    assertEquals(TestCar.Door.PASSENGER, returnVal);
    try {
        operation.perform(object, Collections.<String, Object>singletonMap("seat", "DRIVER"));
        fail("Should not be able to pass in an unused parameter");
    } catch (IllegalArgumentException e) {
    // pass
    }
    try {
        operation.perform(object, Collections.<String, Object>singletonMap("door", "[\"eggs\", \"flour\", \"milk\"]"));
        fail("Should not be able to pass in a parameter of the wrong type");
    } catch (IllegalArgumentException e) {
    // pass
    }
}
Also used : HashMap(java.util.HashMap) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectOperation(org.apache.qpid.server.model.ConfiguredObjectOperation)

Example 5 with ConfiguredObjectOperation

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

the class ManagementNode method findAllChildren.

private Set<ConfiguredObject<?>> findAllChildren() {
    final Set<ConfiguredObject<?>> foundObjects;
    foundObjects = new HashSet<>();
    Set<ConfiguredObject<?>> parents = new HashSet<>();
    Set<ConfiguredObject<?>> children;
    parents.add(_managedObject);
    for (ConfiguredObjectOperation op : _associatedChildrenOperations.values()) {
        @SuppressWarnings("unchecked") final Collection<ConfiguredObject<?>> associated = getAssociatedChildren(op, _managedObject);
        parents.addAll(associated);
    }
    foundObjects.addAll(parents);
    do {
        children = new HashSet<>();
        for (ConfiguredObject<?> parent : parents) {
            for (Class<? extends ConfiguredObject> childClass : _model.getChildTypes(parent.getCategoryClass())) {
                children.addAll((Collection<? extends ConfiguredObject<?>>) parent.getChildren(childClass));
            }
        }
        parents = children;
    } while (foundObjects.addAll(parents));
    return foundObjects;
}
Also used : ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ConfiguredObjectOperation(org.apache.qpid.server.model.ConfiguredObjectOperation) HashSet(java.util.HashSet)

Aggregations

ConfiguredObjectOperation (org.apache.qpid.server.model.ConfiguredObjectOperation)6 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)5 HashMap (java.util.HashMap)3 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 ManagedObject (org.apache.qpid.server.model.ManagedObject)2 OperationParameter (org.apache.qpid.server.model.OperationParameter)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 InternalMessageHeader (org.apache.qpid.server.message.internal.InternalMessageHeader)1 Content (org.apache.qpid.server.model.Content)1