Search in sources :

Example 16 with Link

use of org.apache.geode.management.internal.web.domain.Link in project geode by apache.

the class ClientHttpRequestTest method shouldBeMockable.

@Test
public void shouldBeMockable() throws Exception {
    ClientHttpRequest mockClientHttpRequest = mock(ClientHttpRequest.class);
    Link mockLink = mock(Link.class);
    when(mockClientHttpRequest.getLink()).thenReturn(mockLink);
    assertThat(mockClientHttpRequest.getLink()).isSameAs(mockLink);
}
Also used : Link(org.apache.geode.management.internal.web.domain.Link) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 17 with Link

use of org.apache.geode.management.internal.web.domain.Link in project geode by apache.

the class QueryNamesOverHttpDUnitTest method testQueryNameOverHttp.

@Test
public void testQueryNameOverHttp() throws Exception {
    LinkIndex links = new LinkIndex();
    links.add(new Link("mbean-query", new URI("http://localhost:" + locatorRule.getHttpPort() + "/gemfire/v1/mbean/query"), HttpMethod.POST));
    RestHttpOperationInvoker invoker = new RestHttpOperationInvoker(links, mock(Gfsh.class), new HashMap<>());
    ObjectName objectName = ObjectName.getInstance("GemFire:type=Member,*");
    QueryExp query = Query.eq(Query.attr("Name"), Query.value("mock"));
    Set<ObjectName> names = invoker.queryNames(objectName, query);
    assertTrue(names.isEmpty());
}
Also used : LinkIndex(org.apache.geode.management.internal.web.domain.LinkIndex) RestHttpOperationInvoker(org.apache.geode.management.internal.web.shell.RestHttpOperationInvoker) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) QueryExp(javax.management.QueryExp) URI(java.net.URI) Link(org.apache.geode.management.internal.web.domain.Link) ObjectName(javax.management.ObjectName) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 18 with Link

use of org.apache.geode.management.internal.web.domain.Link in project geode by apache.

the class AbstractHttpOperationInvoker method getAttribute.

// TODO research the use of Jolokia instead
/**
   * Read the attribute identified by name from a remote resource identified by name. The intent of
   * this method is to return the value of an attribute on an MBean located in the remote
   * MBeanServer.
   * 
   * @param resourceName name/url of the remote resource from which to fetch the attribute value.
   * @param attributeName name of the attribute who's value will be fetched.
   * @return the value of the named attribute for the named resource (typically an MBean).
   * @throws MBeanAccessException if an MBean access error occurs.
   * @throws RestApiCallForCommandNotFoundException if the REST API web service endpoint for
   *         accessing an attribute on an MBean does not exists!
   * @see #createHttpRequest(org.apache.geode.management.internal.web.domain.Link)
   * @see #findLink(String)
   * @see #send(org.apache.geode.management.internal.web.http.ClientHttpRequest, Class)
   */
@Override
public Object getAttribute(final String resourceName, final String attributeName) {
    final Link link = findLink(MBEAN_ATTRIBUTE_LINK_RELATION);
    if (link != null) {
        final ClientHttpRequest request = createHttpRequest(link);
        request.addParameterValues("resourceName", resourceName);
        request.addParameterValues("attributeName", attributeName);
        try {
            return IOUtils.deserializeObject(send(request, byte[].class));
        } catch (IOException e) {
            throw new MBeanAccessException(String.format("De-serializing the result of accessing attribute (%1$s) on MBean (%2$s) failed!", resourceName, attributeName), e);
        } catch (ClassNotFoundException e) {
            throw new MBeanAccessException(String.format("The Class type of the result when accessing attribute (%1$s) on MBean (%2$s) was not found!", resourceName, attributeName), e);
        }
    } else {
        printSevere("Getting the value of attribute (%1$s) on MBean (%2$s) is currently an unsupported operation!", attributeName, resourceName);
        throw new RestApiCallForCommandNotFoundException(MBEAN_ATTRIBUTE_LINK_RELATION);
    }
}
Also used : IOException(java.io.IOException) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) Link(org.apache.geode.management.internal.web.domain.Link)

Example 19 with Link

use of org.apache.geode.management.internal.web.domain.Link in project geode by apache.

the class AbstractHttpOperationInvoker method queryNames.

/**
   * This method searches the MBean server, based on the OperationsInvoker's JMX-based or remoting
   * capable MBean server connection, for MBeans matching a specific ObjectName or matching an
   * ObjectName pattern along with satisfying criteria from the Query expression.
   * 
   * @param objectName the ObjectName or pattern for which matching MBeans in the target MBean
   *        server will be returned.
   * @param queryExpression the JMX-based query expression used to filter matching MBeans.
   * @return a set of ObjectName's matching MBeans in the MBean server matching the ObjectName and
   *         Query expression criteria.
   * @see #createHttpRequest(org.apache.geode.management.internal.web.domain.Link)
   * @see #findLink(String)
   * @see #send(org.apache.geode.management.internal.web.http.ClientHttpRequest, Class)
   * @see javax.management.ObjectName
   * @see javax.management.QueryExp
   */
@Override
@SuppressWarnings("unchecked")
public Set<ObjectName> queryNames(final ObjectName objectName, final QueryExp queryExpression) {
    final Link link = findLink(MBEAN_QUERY_LINK_RELATION);
    if (link != null) {
        final ClientHttpRequest request = createHttpRequest(link);
        request.setContent(new QueryParameterSource(objectName, queryExpression));
        try {
            return (Set<ObjectName>) IOUtils.deserializeObject(send(request, byte[].class));
        } catch (Exception e) {
            throw new MBeanAccessException(String.format("An error occurred while querying for MBean names using ObjectName pattern (%1$s) and Query expression (%2$s)!", objectName, queryExpression), e);
        }
    } else {
        printSevere("Running a query to get the ObjectNames of all MBeans matching the ObjectName pattern (%1$s) and Query expression (%2$s) is currently unsupported!", objectName, queryExpression);
        throw new RestApiCallForCommandNotFoundException(MBEAN_QUERY_LINK_RELATION);
    }
}
Also used : Set(java.util.Set) QueryParameterSource(org.apache.geode.management.internal.web.domain.QueryParameterSource) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) Link(org.apache.geode.management.internal.web.domain.Link) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) IOException(java.io.IOException)

Example 20 with Link

use of org.apache.geode.management.internal.web.domain.Link in project geode by apache.

the class RestHttpOperationInvokerJUnitTest method testFindAndResolveLink.

@Test
public void testFindAndResolveLink() throws Exception {
    final Map<String, String> commandOptions = new HashMap<>();
    commandOptions.put("name", "BarnesN'Noble");
    Link link = getOperationInvoker().findLink(createCommandRequest("list-libraries", commandOptions));
    assertNotNull(link);
    assertEquals("http://host.domain.com/service/v1/libraries", toString(link.getHref()));
    link = getOperationInvoker().findLink(createCommandRequest("get-library", commandOptions));
    assertNotNull(link);
    assertEquals("http://host.domain.com/service/v1/libraries/{name}", toString(link.getHref()));
    commandOptions.put("author", "J.K.Rowling");
    link = getOperationInvoker().findLink(createCommandRequest("list-books", commandOptions));
    assertNotNull(link);
    assertEquals("http://host.domain.com/service/v1/libraries/{name}/books/{author}", toString(link.getHref()));
    commandOptions.put("category", "sci-fi");
    commandOptions.put("year", "1998");
    commandOptions.put("bogus", "data");
    link = getOperationInvoker().findLink(createCommandRequest("list-books", commandOptions));
    assertNotNull(link);
    assertEquals("http://host.domain.com/service/v1/libraries/{name}/books/{author}/{category}/{year}", toString(link.getHref()));
    commandOptions.remove("category");
    link = getOperationInvoker().findLink(createCommandRequest("list-books", commandOptions));
    assertNotNull(link);
    assertEquals("http://host.domain.com/service/v1/libraries/{name}/books/{author}/{year}", toString(link.getHref()));
    commandOptions.put("category", "fantasy");
    commandOptions.put("isbn", "0-123456789");
    commandOptions.put("title", "Harry Potter");
    link = getOperationInvoker().findLink(createCommandRequest("add-book", commandOptions));
    assertNotNull(link);
    assertEquals("http://host.domain.com/service/v1/libraries/{name}/books", toString(link.getHref()));
    commandOptions.remove("isbn");
    link = getOperationInvoker().findLink(createCommandRequest("get-book", commandOptions));
    assertNotNull(link);
    assertEquals("http://host.domain.com/service/v1/libraries/{name}/books/{title}", toString(link.getHref()));
    link = getOperationInvoker().findLink(createCommandRequest("remove-book", commandOptions));
    assertNotNull(link);
    assertEquals("http://host.domain.com/service/v1/libraries/{name}/books/{isbn}", toString(link.getHref()));
}
Also used : HashMap(java.util.HashMap) Link(org.apache.geode.management.internal.web.domain.Link) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

Link (org.apache.geode.management.internal.web.domain.Link)38 Test (org.junit.Test)31 UnitTest (org.apache.geode.test.junit.categories.UnitTest)30 HashMap (java.util.HashMap)5 LinkIndex (org.apache.geode.management.internal.web.domain.LinkIndex)5 ClientHttpRequest (org.apache.geode.management.internal.web.http.ClientHttpRequest)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 MultiValueMap (org.springframework.util.MultiValueMap)3 URI (java.net.URI)2 CommandRequest (org.apache.geode.management.internal.cli.CommandRequest)2 HttpEntity (org.springframework.http.HttpEntity)2 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 ObjectName (javax.management.ObjectName)1 QueryExp (javax.management.QueryExp)1 Gfsh (org.apache.geode.management.internal.cli.shell.Gfsh)1 QueryParameterSource (org.apache.geode.management.internal.web.domain.QueryParameterSource)1