Search in sources :

Example 26 with ConfiguredObject

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

the class RestServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response, final ConfiguredObject<?> managedObject) throws ServletException, IOException {
    ConfiguredObjectFinder finder = getConfiguredObjectFinder(managedObject);
    Class<? extends ConfiguredObject> configuredClass = getConfiguredClass(request, managedObject);
    if (configuredClass == null) {
        sendError(response, HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    final Class<? extends ConfiguredObject>[] hierarchy = finder.getHierarchy(configuredClass);
    if (hierarchy == null) {
        sendError(response, HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    RequestInfoParser requestInfoParser = new RequestInfoParser(hierarchy);
    RequestInfo requestInfo = requestInfoParser.parse(request);
    switch(requestInfo.getType()) {
        case OPERATION:
            {
                doOperation(requestInfo, managedObject, configuredClass, finder, request, response);
                break;
            }
        case MODEL_OBJECT:
            {
                // TODO - sort special params, everything else should act as a filter
                String attachmentFilename = request.getParameter(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM);
                if (attachmentFilename != null) {
                    setContentDispositionHeaderIfNecessary(response, attachmentFilename);
                }
                List<Predicate<ConfiguredObject<?>>> filterPredicateList = buildFilterPredicates(request);
                Collection<ConfiguredObject<?>> allObjects = getTargetObjects(configuredClass, finder, requestInfo, filterPredicateList);
                boolean singleObjectRequest = requestInfo.isSingletonRequest() && filterPredicateList.isEmpty();
                if (allObjects == null || (allObjects.isEmpty() && singleObjectRequest)) {
                    sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Not Found");
                    return;
                }
                int depth;
                boolean actuals;
                int oversizeThreshold;
                boolean excludeInheritedContext;
                depth = getIntParameterFromRequest(request, DEPTH_PARAM, DEFAULT_DEPTH);
                oversizeThreshold = getIntParameterFromRequest(request, OVERSIZE_PARAM, DEFAULT_OVERSIZE);
                actuals = getBooleanParameterFromRequest(request, ACTUALS_PARAM);
                String excludeInheritedContextParameter = request.getParameter(EXCLUDE_INHERITED_CONTEXT_PARAM);
                excludeInheritedContext = excludeInheritedContextParameter == null || Boolean.parseBoolean(excludeInheritedContextParameter);
                boolean responseAsList = Boolean.parseBoolean(request.getParameter(SINGLETON_MODEL_OBJECT_RESPONSE_AS_LIST));
                final Object responseObject;
                if (!responseAsList && singleObjectRequest) {
                    if (allObjects.size() != 1) {
                        throw new IllegalStateException(String.format("Unexpected number of objects found [%d] for singleton request URI '%s'", allObjects.size(), request.getRequestURI()));
                    }
                    ConfiguredObject<?> singletonObject = allObjects.iterator().next();
                    responseObject = _objectConverter.convertObjectToMap(singletonObject, configuredClass, new ConfiguredObjectToMapConverter.ConverterOptions(depth, actuals, oversizeThreshold, request.isSecure(), excludeInheritedContext));
                } else {
                    final List<Object> outputList = new ArrayList<>();
                    for (ConfiguredObject configuredObject : allObjects) {
                        outputList.add(_objectConverter.convertObjectToMap(configuredObject, configuredClass, new ConfiguredObjectToMapConverter.ConverterOptions(depth, actuals, oversizeThreshold, request.isSecure(), excludeInheritedContext)));
                    }
                    responseObject = outputList;
                }
                boolean sendCachingHeaders = attachmentFilename == null;
                sendJsonResponse(responseObject, request, response, HttpServletResponse.SC_OK, sendCachingHeaders);
                break;
            }
        case VISIBLE_PREFERENCES:
        case USER_PREFERENCES:
            {
                doGetUserPreferences(managedObject, configuredClass, finder, requestInfo, request, response);
                break;
            }
        default:
            {
                throw new IllegalStateException(String.format("Unexpected request type '%s' for path '%s'", requestInfo.getType(), request.getPathInfo()));
            }
    }
}
Also used : Collection(java.util.Collection) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ConfiguredObjectFinder(org.apache.qpid.server.model.ConfiguredObjectFinder)

Example 27 with ConfiguredObject

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

the class RestServlet method getTarget.

private ConfiguredObject<?> getTarget(final RequestInfo requestInfo, final ConfiguredObject<?> managedObject, final Class<? extends ConfiguredObject> configuredClass, final ConfiguredObjectFinder finder) throws IOException {
    final ConfiguredObject<?> target;
    final List<String> names = requestInfo.getModelParts();
    final Class<? extends ConfiguredObject>[] hierarchy = finder.getHierarchy(configuredClass);
    if (names.isEmpty() && hierarchy.length == 0) {
        target = managedObject;
    } else {
        ConfiguredObject theParent = managedObject;
        if (hierarchy.length > 1) {
            ConfiguredObject parent = finder.findObjectParentsFromPath(names, hierarchy, configuredClass);
            theParent = parent;
        }
        String name = names.get(names.size() - 1);
        target = theParent.getChildByName(configuredClass, name);
        if (target == null) {
            final String errorMessage = String.format("%s '%s' not found", configuredClass.getSimpleName(), Joiner.on("/").join(names));
            throw new NotFoundException(errorMessage);
        }
    }
    return target;
}
Also used : ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject)

Example 28 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject 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 29 with ConfiguredObject

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

the class RestUserPreferenceHandler method validateAndConvert.

private List<Preference> validateAndConvert(final ConfiguredObject<?> target, final Map<String, Object> providedObjectMap) {
    List<Preference> replacementPreferences = new ArrayList<>();
    for (String type : providedObjectMap.keySet()) {
        if (!(providedObjectMap.get(type) instanceof List)) {
            final String errorMessage = String.format("expected a list of objects for attribute '%s'", type);
            throw new IllegalArgumentException(errorMessage);
        }
        for (Object preferenceObject : (List<Object>) providedObjectMap.get(type)) {
            if (!(preferenceObject instanceof Map)) {
                final String errorMessage = String.format("encountered non preference object in list of type '%s'", type);
                throw new IllegalArgumentException(errorMessage);
            }
            Map<String, Object> preferenceAttributes = (Map<String, Object>) preferenceObject;
            ensureAttributeMatches(preferenceAttributes, "type", type);
            Preference preference = PreferenceFactory.fromAttributes(target, preferenceAttributes);
            replacementPreferences.add(preference);
        }
    }
    return replacementPreferences;
}
Also used : Preference(org.apache.qpid.server.model.preferences.Preference) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with ConfiguredObject

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

the class RestUserPreferenceHandler method handlePUT.

void handlePUT(ConfiguredObject<?> target, RequestInfo requestInfo, Object providedObject) {
    UserPreferences userPreferences = target.getUserPreferences();
    if (userPreferences == null) {
        throw new NotFoundException("User preferences are not available");
    }
    final List<String> preferencesParts = requestInfo.getPreferencesParts();
    if (preferencesParts.size() == 2) {
        if (!(providedObject instanceof Map)) {
            throw new IllegalArgumentException("expected object");
        }
        Map<String, Object> providedAttributes = (Map<String, Object>) providedObject;
        String type = preferencesParts.get(0);
        String name = preferencesParts.get(1);
        ensureAttributeMatches(providedAttributes, "name", name);
        ensureAttributeMatches(providedAttributes, "type", type);
        Preference preference = PreferenceFactory.fromAttributes(target, providedAttributes);
        awaitFuture(userPreferences.replaceByTypeAndName(type, name, preference));
    } else if (preferencesParts.size() == 1) {
        String type = preferencesParts.get(0);
        if (!(providedObject instanceof List)) {
            throw new IllegalArgumentException("expected a list of objects");
        }
        List<Preference> replacementPreferences = validateAndConvert(target, type, (List<Object>) providedObject);
        awaitFuture(userPreferences.replaceByType(type, replacementPreferences));
    } else if (preferencesParts.size() == 0) {
        if (!(providedObject instanceof Map)) {
            throw new IllegalArgumentException("expected object");
        }
        List<Preference> replacementPreferences = validateAndConvert(target, (Map<String, Object>) providedObject);
        awaitFuture(userPreferences.replace(replacementPreferences));
    } else {
        throw new IllegalArgumentException(String.format("unexpected path '%s'", Joiner.on("/").join(preferencesParts)));
    }
}
Also used : UserPreferences(org.apache.qpid.server.model.preferences.UserPreferences) Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)91 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)28 List (java.util.List)23 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)22 LinkedHashMap (java.util.LinkedHashMap)20 UUID (java.util.UUID)20 Map (java.util.Map)18 AbstractConfigurationChangeListener (org.apache.qpid.server.model.AbstractConfigurationChangeListener)12 ManagedObject (org.apache.qpid.server.model.ManagedObject)11 Date (java.util.Date)7 TreeMap (java.util.TreeMap)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 InternalMessageHeader (org.apache.qpid.server.message.internal.InternalMessageHeader)6 State (org.apache.qpid.server.model.State)6 Collection (java.util.Collection)5 ConfiguredObjectFinder (org.apache.qpid.server.model.ConfiguredObjectFinder)5 Model (org.apache.qpid.server.model.Model)5 Preference (org.apache.qpid.server.model.preferences.Preference)5 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)4