Search in sources :

Example 11 with ManagementResponse

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

the class RestServlet method doGet.

@Override
protected void doGet(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final ConfiguredObject<?> managedObject) throws IOException {
    try {
        final ManagementRequest request = new ServletManagementRequest(managedObject, httpServletRequest);
        final ManagementController controller = getManagementController();
        final ManagementResponse response = controller.handleGet(request);
        sendResponse(request, response, httpServletRequest, httpServletResponse, controller);
    } catch (ManagementException e) {
        sendResponse(e, httpServletRequest, httpServletResponse);
    }
}
Also used : ManagementRequest(org.apache.qpid.server.management.plugin.ManagementRequest) ManagementException(org.apache.qpid.server.management.plugin.ManagementException) ManagementResponse(org.apache.qpid.server.management.plugin.ManagementResponse) ManagementController(org.apache.qpid.server.management.plugin.ManagementController)

Example 12 with ManagementResponse

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

the class BindingController method delete.

@Override
public int delete(final ConfiguredObject<?> root, final List<String> path, final Map<String, List<String>> parameters) throws ManagementException {
    if (path.contains("*")) {
        throw createBadRequestManagementException("Wildcards in path are not supported for delete requests");
    }
    final Collection<String> hierarchy = _managementController.getCategoryHierarchy(root, getCategory());
    if (path.size() < hierarchy.size() - 2) {
        throw createBadRequestManagementException(String.format("Cannot delete binding for path %s", String.join("/", path)));
    }
    final String bindingName = path.size() == hierarchy.size() ? path.get(hierarchy.size() - 1) : null;
    final String queueName = path.get(hierarchy.size() - 2);
    final List<String> ids = parameters.get(GenericLegacyConfiguredObject.ID);
    final List<String> exchangePath = path.subList(0, hierarchy.size() - 2);
    final LegacyConfiguredObject exchange = getNextVersionObject(root, exchangePath, ExchangeController.TYPE);
    final AtomicInteger counter = new AtomicInteger();
    if (ids != null && !ids.isEmpty()) {
        @SuppressWarnings("unchecked") Collection<Binding> bindings = (Collection<Binding>) exchange.getAttribute("bindings");
        if (bindings != null) {
            bindings.stream().filter(b -> ids.contains(String.valueOf(generateBindingId(exchange, b.getDestination(), b.getBindingKey())))).forEach(b -> {
                Map<String, Object> params = new LinkedHashMap<>();
                params.put("bindingKey", b.getBindingKey());
                params.put("destination", b.getDestination());
                ManagementResponse r = exchange.invoke("unbind", params, true);
                if (Boolean.TRUE.equals(r.getBody())) {
                    counter.incrementAndGet();
                }
            });
        }
    } else if (bindingName != null) {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("bindingKey", bindingName);
        params.put("destination", queueName);
        ManagementResponse response = exchange.invoke("unbind", params, true);
        if (Boolean.TRUE.equals(response.getBody())) {
            counter.incrementAndGet();
        }
    } else {
        throw createBadRequestManagementException("Only deletion by binding full path or ids is supported");
    }
    return counter.get();
}
Also used : Binding(org.apache.qpid.server.model.Binding) ManagementException.createBadRequestManagementException(org.apache.qpid.server.management.plugin.ManagementException.createBadRequestManagementException) ManagementException(org.apache.qpid.server.management.plugin.ManagementException) GenericLegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.GenericLegacyConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ManagementController(org.apache.qpid.server.management.plugin.ManagementController) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) CategoryController(org.apache.qpid.server.management.plugin.controller.CategoryController) Binding(org.apache.qpid.server.model.Binding) ControllerManagementResponse(org.apache.qpid.server.management.plugin.controller.ControllerManagementResponse) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) State(org.apache.qpid.server.model.State) ManagementException.createInternalServerErrorManagementException(org.apache.qpid.server.management.plugin.ManagementException.createInternalServerErrorManagementException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) LegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.LegacyConfiguredObject) ResponseType(org.apache.qpid.server.management.plugin.ResponseType) List(java.util.List) Stream(java.util.stream.Stream) ManagementResponse(org.apache.qpid.server.management.plugin.ManagementResponse) LegacyManagementController(org.apache.qpid.server.management.plugin.controller.LegacyManagementController) Collections(java.util.Collections) LinkedHashMap(java.util.LinkedHashMap) GenericLegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.GenericLegacyConfiguredObject) LegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.LegacyConfiguredObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ControllerManagementResponse(org.apache.qpid.server.management.plugin.controller.ControllerManagementResponse) ManagementResponse(org.apache.qpid.server.management.plugin.ManagementResponse) Collection(java.util.Collection) GenericLegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.GenericLegacyConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) LegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.LegacyConfiguredObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 13 with ManagementResponse

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

the class BindingController method createOrUpdate.

@Override
public LegacyConfiguredObject createOrUpdate(final ConfiguredObject<?> root, final List<String> path, final Map<String, Object> attributes, final boolean isPost) throws ManagementException {
    if (path.contains("*")) {
        throw createBadRequestManagementException("Wildcards in path are not supported for post and put requests");
    }
    final Collection<String> hierarchy = _managementController.getCategoryHierarchy(root, getCategory());
    if (path.size() < hierarchy.size() - 2) {
        throw createBadRequestManagementException(String.format("Cannot create binding for path %s", String.join("/" + path)));
    }
    String queueName = null;
    if (path.size() > hierarchy.size() - 2) {
        queueName = path.get(hierarchy.size() - 2);
    }
    if (queueName == null) {
        queueName = (String) attributes.get("queue");
    }
    if (queueName == null) {
        throw createBadRequestManagementException("Queue is required for binding creation. Please specify queue either in path or in binding attributes");
    }
    final List<String> exchangePath = path.subList(0, hierarchy.size() - 2);
    final LegacyConfiguredObject nextVersionExchange = getNextVersionObject(root, exchangePath, ExchangeController.TYPE);
    final List<String> queuePath = new ArrayList<>(path.subList(0, hierarchy.size() - 3));
    queuePath.add(queueName);
    final LegacyConfiguredObject nextVersionQueue = getNextVersionObject(root, queuePath, QueueController.TYPE);
    String bindingKey = (String) attributes.get(GenericLegacyConfiguredObject.NAME);
    if (bindingKey == null) {
        bindingKey = path.size() == hierarchy.size() ? path.get(hierarchy.size() - 1) : null;
    }
    if (bindingKey == null) {
        bindingKey = "";
    }
    final Map<String, Object> parameters = new LinkedHashMap<>();
    parameters.put("bindingKey", bindingKey);
    parameters.put("destination", queueName);
    Map<String, Object> arguments = null;
    if (attributes.containsKey("arguments")) {
        Object args = attributes.get("arguments");
        if (args instanceof Map) {
            @SuppressWarnings("unchecked") Map<String, Object> argumentsMap = (Map<String, Object>) args;
            arguments = new HashMap<>(argumentsMap);
            if (!arguments.isEmpty()) {
                parameters.put("arguments", arguments);
            }
        } else {
            throw createBadRequestManagementException(String.format("Unexpected attributes specified : %s", args));
        }
    }
    parameters.put("replaceExistingArguments", !isPost);
    ManagementResponse response = nextVersionExchange.invoke("bind", parameters, true);
    final boolean newBindings = Boolean.TRUE.equals(response.getBody());
    if (!newBindings) {
        return null;
    }
    return new LegacyBinding(_managementController, nextVersionExchange, nextVersionQueue, bindingKey, arguments);
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) GenericLegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.GenericLegacyConfiguredObject) LegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.LegacyConfiguredObject) ControllerManagementResponse(org.apache.qpid.server.management.plugin.controller.ControllerManagementResponse) ManagementResponse(org.apache.qpid.server.management.plugin.ManagementResponse) GenericLegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.GenericLegacyConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) LegacyConfiguredObject(org.apache.qpid.server.management.plugin.controller.LegacyConfiguredObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 14 with ManagementResponse

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

the class LatestManagementControllerTest method handleGetForBrokerRootAndQueuePathWithoutQueueName.

@Test
public void handleGetForBrokerRootAndQueuePathWithoutQueueName() throws Exception {
    final String hostName = "test";
    final QueueManagingVirtualHost<?> virtualHost = createVirtualHostWithQueue(hostName, "foo", "bar");
    final String nodeName = virtualHost.getParent().getName();
    final ManagementRequest request = mock(ManagementRequest.class);
    when(request.getCategory()).thenReturn("queue");
    doReturn(virtualHost.getBroker()).when(request).getRoot();
    when(request.getPath()).thenReturn(Arrays.asList(nodeName, hostName));
    when(request.getParameters()).thenReturn(Collections.emptyMap());
    when(request.getMethod()).thenReturn("GET");
    final ManagementResponse response = _controller.handleGet(request);
    assertThat(response, is(notNullValue()));
    assertThat(response.getResponseCode(), is(equalTo(200)));
    assertThat(response.getBody(), is(notNullValue()));
    assertThat(response.getBody(), is(instanceOf(Collection.class)));
    final Collection data = (Collection) response.getBody();
    assertThat(data.size(), is(equalTo(2)));
    final Iterator iterator = data.iterator();
    final Object object = iterator.next();
    final Object object2 = iterator.next();
    assertThat(object, is(notNullValue()));
    assertThat(object, is(instanceOf(Queue.class)));
    assertThat(((Queue) object).getName(), is(equalTo("foo")));
    assertThat(object2, is(notNullValue()));
    assertThat(object2, is(instanceOf(Queue.class)));
    assertThat(((Queue) object2).getName(), is(equalTo("bar")));
}
Also used : ManagementRequest(org.apache.qpid.server.management.plugin.ManagementRequest) ManagementResponse(org.apache.qpid.server.management.plugin.ManagementResponse) Iterator(java.util.Iterator) Collection(java.util.Collection) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Test(org.junit.Test)

Example 15 with ManagementResponse

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

the class LatestManagementControllerTest method handleGetForBrokerRootAndQueuePathWithFilter.

@Test
public void handleGetForBrokerRootAndQueuePathWithFilter() throws Exception {
    final String hostName = "test";
    final QueueManagingVirtualHost<?> virtualHost = createVirtualHostWithQueue(hostName, "foo", "bar");
    final String nodeName = virtualHost.getParent().getName();
    final ManagementRequest request = mock(ManagementRequest.class);
    when(request.getCategory()).thenReturn("queue");
    doReturn(virtualHost.getBroker()).when(request).getRoot();
    when(request.getPath()).thenReturn(Arrays.asList(nodeName, hostName));
    when(request.getParameters()).thenReturn(Collections.singletonMap("name", Collections.singletonList("bar")));
    when(request.getMethod()).thenReturn("GET");
    ManagementResponse response = _controller.handleGet(request);
    assertThat(response, is(notNullValue()));
    assertThat(response.getResponseCode(), is(equalTo(200)));
    assertThat(response.getBody(), is(notNullValue()));
    assertThat(response.getBody(), is(instanceOf(Collection.class)));
    Collection data = (Collection) response.getBody();
    assertThat(data.size(), is(equalTo(1)));
    Object object = data.iterator().next();
    assertThat(object, is(notNullValue()));
    assertThat(object, is(instanceOf(Queue.class)));
    assertThat(((Queue) object).getName(), is(equalTo("bar")));
}
Also used : ManagementRequest(org.apache.qpid.server.management.plugin.ManagementRequest) ManagementResponse(org.apache.qpid.server.management.plugin.ManagementResponse) Collection(java.util.Collection) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Test(org.junit.Test)

Aggregations

ManagementResponse (org.apache.qpid.server.management.plugin.ManagementResponse)23 Test (org.junit.Test)17 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)14 ManagementRequest (org.apache.qpid.server.management.plugin.ManagementRequest)11 LegacyConfiguredObject (org.apache.qpid.server.management.plugin.controller.LegacyConfiguredObject)11 ControllerManagementResponse (org.apache.qpid.server.management.plugin.controller.ControllerManagementResponse)7 HashMap (java.util.HashMap)5 ManagementController (org.apache.qpid.server.management.plugin.ManagementController)5 ManagementException (org.apache.qpid.server.management.plugin.ManagementException)5 Collection (java.util.Collection)4 LinkedHashMap (java.util.LinkedHashMap)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 UUID (java.util.UUID)2 GenericLegacyConfiguredObject (org.apache.qpid.server.management.plugin.controller.GenericLegacyConfiguredObject)2 Binding (org.apache.qpid.server.model.Binding)2 Broker (org.apache.qpid.server.model.Broker)2 Queue (org.apache.qpid.server.model.Queue)2 StandardCharsets (java.nio.charset.StandardCharsets)1 Collections (java.util.Collections)1