Search in sources :

Example 1 with ClientHttpRequest

use of org.apache.geode.management.internal.web.http.ClientHttpRequest 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 2 with ClientHttpRequest

use of org.apache.geode.management.internal.web.http.ClientHttpRequest 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 3 with ClientHttpRequest

use of org.apache.geode.management.internal.web.http.ClientHttpRequest in project geode by apache.

the class AbstractHttpOperationInvoker method createHttpRequest.

/**
   * Creates an instance of a client HTTP request with the specified Link targeting the resource as
   * well as the intended operation on the resource.
   * 
   * @param link a Link with the URI targeting and identifying the resource as well as the method of
   *        operation on the resource.
   * @return a client HTTP request with the details of the request.
   * @see org.apache.geode.management.internal.web.http.ClientHttpRequest
   * @see org.apache.geode.management.internal.web.domain.Link
   */
protected ClientHttpRequest createHttpRequest(final Link link) {
    final ClientHttpRequest request = new ClientHttpRequest(link);
    request.addHeaderValues(HttpHeader.USER_AGENT.getName(), USER_AGENT_HTTP_REQUEST_HEADER_VALUE);
    request.getHeaders().setAccept(getAcceptableMediaTypes());
    if (this.securityProperties != null) {
        Iterator<Entry<String, String>> it = this.securityProperties.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, String> entry = it.next();
            request.addHeaderValues(entry.getKey(), entry.getValue());
        }
    }
    return request;
}
Also used : Entry(java.util.Map.Entry) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest)

Example 4 with ClientHttpRequest

use of org.apache.geode.management.internal.web.http.ClientHttpRequest in project geode by apache.

the class RestHttpOperationInvoker method createHttpRequest.

/**
   * Creates an HTTP request from the specified command invocation encapsulated by the
   * CommandRequest object. The CommandRequest identifies the resource targeted by the command
   * invocation along with any parameters to be sent as part of the HTTP request.
   * 
   * @param command the CommandRequest object encapsulating details of the command invocation.
   * @return a client HTTP request detailing the operation to be performed on the remote resource
   *         targeted by the command invocation.
   * @see AbstractHttpOperationInvoker#createHttpRequest(org.apache.geode.management.internal.web.domain.Link)
   * @see org.apache.geode.management.internal.cli.CommandRequest
   * @see org.apache.geode.management.internal.web.http.ClientHttpRequest
   * @see org.apache.geode.management.internal.web.util.ConvertUtils#convert(byte[][])
   */
protected ClientHttpRequest createHttpRequest(final CommandRequest command) {
    ClientHttpRequest request = createHttpRequest(findLink(command));
    Map<String, String> commandParameters = command.getParameters();
    for (Map.Entry<String, String> entry : commandParameters.entrySet()) {
        if (NullValueFilter.INSTANCE.accept(entry)) {
            request.addParameterValues(entry.getKey(), entry.getValue());
        }
    }
    Map<String, String> environmentVariables = command.getEnvironment();
    for (Map.Entry<String, String> entry : environmentVariables.entrySet()) {
        if (EnvironmentVariableFilter.INSTANCE.accept(entry)) {
            request.addParameterValues(ENVIRONMENT_VARIABLE_REQUEST_PARAMETER_PREFIX + entry.getKey(), entry.getValue());
        }
    }
    if (command.getFileData() != null) {
        request.addParameterValues(RESOURCES_REQUEST_PARAMETER, (Object[]) ConvertUtils.convert(command.getFileData()));
    }
    return request;
}
Also used : ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ClientHttpRequest

use of org.apache.geode.management.internal.web.http.ClientHttpRequest in project geode by apache.

the class RestHttpOperationInvokerJUnitTest method testCreateHttpRequest.

@Test
public void testCreateHttpRequest() {
    final Map<String, String> commandOptions = new HashMap<>();
    commandOptions.put("author", "Adams");
    commandOptions.put("blankOption", "  ");
    commandOptions.put("category", "sci-fi");
    commandOptions.put("emptyOption", StringUtils.EMPTY);
    commandOptions.put("isbn", "0-123456789");
    commandOptions.put("nullOption", null);
    commandOptions.put("title", "Hitch Hiker's Guide to the Galaxy");
    commandOptions.put("year", "1983");
    final CommandRequest command = createCommandRequest("add-book", commandOptions);
    final ClientHttpRequest request = getOperationInvoker().createHttpRequest(command);
    assertNotNull(request);
    assertEquals("POST http://host.domain.com/service/v1/libraries/{name}/books", request.getLink().toHttpRequestLine());
    assertEquals("Adams", request.getParameterValue("author"));
    assertEquals("sci-fi", request.getParameterValue("category"));
    assertEquals("0-123456789", request.getParameterValue("isbn"));
    assertEquals("Hitch Hiker's Guide to the Galaxy", request.getParameterValue("title"));
    assertEquals("1983", request.getParameterValue("year"));
    assertTrue(request.getParameters().containsKey("blankOption"));
    assertTrue(request.getParameters().containsKey("emptyOption"));
    assertFalse(request.getParameters().containsKey("nullOption"));
    for (String requestParameter : request.getParameters().keySet()) {
        assertFalse(requestParameter.startsWith(RestHttpOperationInvoker.ENVIRONMENT_VARIABLE_REQUEST_PARAMETER_PREFIX));
    }
    assertNull(request.getParameterValue(RestHttpOperationInvoker.RESOURCES_REQUEST_PARAMETER));
}
Also used : CommandRequest(org.apache.geode.management.internal.cli.CommandRequest) HashMap(java.util.HashMap) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

ClientHttpRequest (org.apache.geode.management.internal.web.http.ClientHttpRequest)10 Link (org.apache.geode.management.internal.web.domain.Link)5 IOException (java.io.IOException)4 CommandRequest (org.apache.geode.management.internal.cli.CommandRequest)4 UnitTest (org.apache.geode.test.junit.categories.UnitTest)4 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 QueryParameterSource (org.apache.geode.management.internal.web.domain.QueryParameterSource)2 AuthenticationFailedException (org.apache.geode.security.AuthenticationFailedException)2 NotAuthorizedException (org.apache.geode.security.NotAuthorizedException)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1