Search in sources :

Example 1 with ConfiguredObject

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

the class RestServlet method performCreateOrUpdate.

private void performCreateOrUpdate(HttpServletRequest request, HttpServletResponse response, final ConfiguredObject<?> managedObject) throws IOException, ServletException {
    ConfiguredObjectFinder finder = getConfiguredObjectFinder(managedObject);
    final Class<? extends ConfiguredObject> configuredClass = getConfiguredClass(request, managedObject);
    final Class<? extends ConfiguredObject>[] hierarchy = finder.getHierarchy(configuredClass);
    RequestInfoParser requestInfoParser = new RequestInfoParser(hierarchy);
    response.setContentType("application/json");
    RequestInfo requestInfo = requestInfoParser.parse(request);
    switch(requestInfo.getType()) {
        case MODEL_OBJECT:
            {
                List<String> names = requestInfo.getModelParts();
                boolean isFullObjectURL = names.size() == hierarchy.length;
                Map<String, Object> providedObject = getRequestProvidedObject(request, requestInfo);
                if (names.isEmpty() && hierarchy.length == 0) {
                    managedObject.setAttributes(providedObject);
                    response.setStatus(HttpServletResponse.SC_OK);
                    return;
                }
                ConfiguredObject theParent = managedObject;
                Class<? extends ConfiguredObject> objClass = configuredClass;
                if (hierarchy.length > 1) {
                    theParent = finder.findObjectParentsFromPath(names, hierarchy, configuredClass);
                }
                if (isFullObjectURL) {
                    String name = names.get(names.size() - 1);
                    ConfiguredObject<?> configuredObject = theParent.getChildByName(objClass, name);
                    if (configuredObject != null) {
                        configuredObject.setAttributes(providedObject);
                        response.setStatus(HttpServletResponse.SC_OK);
                        return;
                    } else if ("POST".equalsIgnoreCase(request.getMethod())) {
                        sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, String.format("%s '%s' not found", configuredClass.getSimpleName(), name));
                        return;
                    } else {
                        providedObject.put(ConfiguredObject.NAME, name);
                    }
                }
                ConfiguredObject<?> configuredObject = theParent.createChild(objClass, providedObject);
                StringBuffer requestURL = request.getRequestURL();
                if (!isFullObjectURL) {
                    requestURL.append("/").append(configuredObject.getName());
                }
                response.setHeader("Location", requestURL.toString());
                response.setStatus(HttpServletResponse.SC_CREATED);
                break;
            }
        case OPERATION:
            {
                doOperation(requestInfo, managedObject, configuredClass, finder, request, response);
                break;
            }
        case USER_PREFERENCES:
            {
                doPostOrPutUserPreference(requestInfo, managedObject, configuredClass, finder, request, response);
                break;
            }
        default:
            {
                throw new IllegalStateException(String.format("Unexpected request type '%s' for path '%s'", requestInfo.getType(), request.getPathInfo()));
            }
    }
}
Also used : ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredObjectFinder(org.apache.qpid.server.model.ConfiguredObjectFinder) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with ConfiguredObject

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

the class RestServlet method doGetUserPreferences.

private void doGetUserPreferences(final ConfiguredObject<?> managedObject, final Class<? extends ConfiguredObject> configuredClass, final ConfiguredObjectFinder finder, final RequestInfo requestInfo, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
    Collection<ConfiguredObject<?>> allObjects = getTargetObjects(configuredClass, finder, requestInfo, Collections.<Predicate<ConfiguredObject<?>>>emptyList());
    if (allObjects == null || (allObjects.isEmpty() && isSingleObjectRequest(requestInfo, finder.getHierarchy(configuredClass)))) {
        sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Not Found");
        return;
    }
    final Object responseObject;
    if (requestInfo.hasWildcard()) {
        responseObject = new ArrayList<>(allObjects.size());
        for (ConfiguredObject<?> target : allObjects) {
            final UserPreferences userPreferences = target.getUserPreferences();
            try {
                final Object preferences = _userPreferenceHandler.handleGET(userPreferences, requestInfo);
                if (preferences == null || (preferences instanceof Collection && ((Collection) preferences).isEmpty()) || (preferences instanceof Map && ((Map) preferences).isEmpty())) {
                    continue;
                }
                ((List<Object>) responseObject).add(preferences);
            } catch (NotFoundException e) {
            // The case where the preference's type and name is provided, but this particular object does not
            // have a matching preference.
            }
        }
    } else {
        ConfiguredObject<?> target = allObjects.iterator().next();
        final UserPreferences userPreferences = target.getUserPreferences();
        responseObject = _userPreferenceHandler.handleGET(userPreferences, requestInfo);
    }
    sendJsonResponse(responseObject, request, response);
}
Also used : UserPreferences(org.apache.qpid.server.model.preferences.UserPreferences) Collection(java.util.Collection) 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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with ConfiguredObject

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

the class RestServlet method doPostOrPutUserPreference.

private void doPostOrPutUserPreference(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);
    final Object providedObject = getRequestProvidedObject(request, requestInfo, Object.class);
    if ("POST".equals(request.getMethod())) {
        _userPreferenceHandler.handlePOST(target, requestInfo, providedObject);
    } else if ("PUT".equals(request.getMethod())) {
        _userPreferenceHandler.handlePUT(target, requestInfo, providedObject);
    } else {
        sendJsonErrorResponse(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "unexpected http method");
    }
}
Also used : ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject)

Example 4 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 5 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)

Aggregations

ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)117 ArrayList (java.util.ArrayList)43 HashMap (java.util.HashMap)35 Test (org.junit.Test)33 Map (java.util.Map)29 List (java.util.List)27 LinkedHashMap (java.util.LinkedHashMap)25 UUID (java.util.UUID)21 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)21 AbstractConfigurationChangeListener (org.apache.qpid.server.model.AbstractConfigurationChangeListener)15 Collection (java.util.Collection)12 LegacyConfiguredObject (org.apache.qpid.server.management.plugin.controller.LegacyConfiguredObject)12 ConfiguredObjectFinder (org.apache.qpid.server.model.ConfiguredObjectFinder)12 ManagedObject (org.apache.qpid.server.model.ManagedObject)11 State (org.apache.qpid.server.model.State)10 Date (java.util.Date)7 TreeMap (java.util.TreeMap)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 GenericLegacyConfiguredObject (org.apache.qpid.server.management.plugin.controller.GenericLegacyConfiguredObject)6 InternalMessageHeader (org.apache.qpid.server.message.internal.InternalMessageHeader)6