Search in sources :

Example 21 with ConfiguredObject

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

the class QueryServlet method performQuery.

private void performQuery(final HttpServletRequest request, final HttpServletResponse response, final ConfiguredObject<?> managedObject) throws IOException, ServletException {
    String categoryName;
    X parent = getParent(request, managedObject);
    if (parent != null && ((categoryName = getRequestedCategory(request, managedObject)) != null)) {
        Model model = parent.getModel();
        Class<? extends ConfiguredObject> category = getSupportedCategory(categoryName, model);
        if (category != null) {
            List<ConfiguredObject<?>> objects = getAllObjects(parent, category, request);
            try {
                ConfiguredObjectQuery query = new ConfiguredObjectQuery(objects, request.getParameter("select"), request.getParameter("where"), request.getParameter("orderBy"), request.getParameter("limit"), request.getParameter("offset"));
                String attachmentFilename = request.getParameter(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM);
                if (attachmentFilename != null) {
                    setContentDispositionHeaderIfNecessary(response, attachmentFilename);
                }
                if ("csv".equalsIgnoreCase(request.getParameter("format"))) {
                    sendCsvResponse(query, response);
                } else {
                    Map<String, Object> resultsObject = new LinkedHashMap<>();
                    resultsObject.put("headers", query.getHeaders());
                    resultsObject.put("results", query.getResults());
                    resultsObject.put("total", query.getTotalNumberOfRows());
                    sendJsonResponse(resultsObject, request, response);
                }
            } catch (SelectorParsingException e) {
                sendJsonErrorResponse(request, response, HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
            } catch (EvaluationException e) {
                sendJsonErrorResponse(request, response, SC_UNPROCESSABLE_ENTITY, e.getMessage());
            }
        } else {
            sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Unknown object type " + categoryName);
        }
    } else {
        sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Invalid path");
    }
}
Also used : SelectorParsingException(org.apache.qpid.server.filter.SelectorParsingException) ConfiguredObjectQuery(org.apache.qpid.server.management.plugin.servlet.query.ConfiguredObjectQuery) Model(org.apache.qpid.server.model.Model) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) EvaluationException(org.apache.qpid.server.management.plugin.servlet.query.EvaluationException) LinkedHashMap(java.util.LinkedHashMap)

Example 22 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 23 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 24 with ConfiguredObject

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

the class RestServlet method doDelete.

@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response, final ConfiguredObject<?> managedObject) throws ServletException, IOException {
    ConfiguredObjectFinder finder = getConfiguredObjectFinder(managedObject);
    Class<? extends ConfiguredObject> configuredClass = getConfiguredClass(request, managedObject);
    final Class<? extends ConfiguredObject>[] hierarchy = finder.getHierarchy(configuredClass);
    RequestInfoParser requestInfoParser = new RequestInfoParser(hierarchy);
    RequestInfo requestInfo = requestInfoParser.parse(request);
    Collection<ConfiguredObject<?>> allObjects = getTargetObjects(configuredClass, finder, requestInfo, buildFilterPredicates(request));
    if (allObjects == null) {
        throw new NotFoundException("Not Found");
    }
    switch(requestInfo.getType()) {
        case MODEL_OBJECT:
            {
                for (ConfiguredObject o : allObjects) {
                    o.delete();
                }
                sendCachingHeadersOnResponse(response);
                response.setStatus(HttpServletResponse.SC_OK);
                break;
            }
        case USER_PREFERENCES:
            {
                // TODO: define format how to report the results for bulk delete, i.e. how to report individual errors and success statuses
                if (allObjects.size() > 1) {
                    sendJsonErrorResponse(request, response, HttpServletResponse.SC_BAD_REQUEST, "Deletion of user preferences using wildcards is unsupported");
                    return;
                }
                for (ConfiguredObject o : allObjects) {
                    _userPreferenceHandler.handleDELETE(o.getUserPreferences(), requestInfo);
                }
                break;
            }
        default:
            {
                sendJsonErrorResponse(request, response, HttpServletResponse.SC_BAD_REQUEST, "Unsupported delete call");
            }
    }
}
Also used : ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ConfiguredObjectFinder(org.apache.qpid.server.model.ConfiguredObjectFinder)

Example 25 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)

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