Search in sources :

Example 21 with Link

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

the class RestHttpOperationInvoker method init.

/**
   * Initializes the RestHttpOperationInvokers scheduled and periodic monitoring task to assess the
   * availibity of the targeted GemFire Manager's HTTP service.
   * 
   * @see org.apache.geode.internal.lang.Initable#init()
   * @see org.springframework.http.client.ClientHttpRequest
   */
@SuppressWarnings("null")
public void init() {
    final Link pingLink = getLinkIndex().find(PING_LINK_RELATION);
    if (pingLink != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Scheduling periodic HTTP ping requests to monitor the availability of the GemFire Manager HTTP service @ ({})", getBaseUrl());
        }
        getExecutorService().scheduleAtFixedRate(new Runnable() {

            public void run() {
                try {
                    org.springframework.http.client.ClientHttpRequest httpRequest = getRestTemplate().getRequestFactory().createRequest(pingLink.getHref(), HttpMethod.HEAD);
                    httpRequest.getHeaders().set(HttpHeader.USER_AGENT.getName(), USER_AGENT_HTTP_REQUEST_HEADER_VALUE);
                    httpRequest.getHeaders().setAccept(getAcceptableMediaTypes());
                    httpRequest.getHeaders().setContentLength(0l);
                    if (securityProperties != null) {
                        Iterator<Entry<String, String>> it = securityProperties.entrySet().iterator();
                        while (it.hasNext()) {
                            Entry<String, String> entry = it.next();
                            httpRequest.getHeaders().add(entry.getKey(), entry.getValue());
                        }
                    }
                    ClientHttpResponse httpResponse = httpRequest.execute();
                    if (HttpStatus.NOT_FOUND.equals(httpResponse.getStatusCode())) {
                        throw new IOException(String.format("The HTTP service at URL (%1$s) could not be found!", pingLink.getHref()));
                    } else if (!HttpStatus.OK.equals(httpResponse.getStatusCode())) {
                        printDebug("Received unexpected HTTP status code (%1$d - %2$s) for HTTP request (%3$s).", httpResponse.getRawStatusCode(), httpResponse.getStatusText(), pingLink.getHref());
                    }
                } catch (IOException e) {
                    printDebug("An error occurred while connecting to the Manager's HTTP service: %1$s: ", e.getMessage());
                    getGfsh().notifyDisconnect(RestHttpOperationInvoker.this.toString());
                    stop();
                }
            }
        }, DEFAULT_INITIAL_DELAY, DEFAULT_PERIOD, DEFAULT_TIME_UNIT);
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("The Link to the GemFire Manager web service endpoint @ ({}) to monitor availability was not found!", getBaseUrl());
        }
    }
    initClusterId();
}
Also used : Entry(java.util.Map.Entry) Iterator(java.util.Iterator) IOException(java.io.IOException) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Link(org.apache.geode.management.internal.web.domain.Link)

Example 22 with Link

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

the class RestHttpOperationInvoker method resolveLink.

/**
   * Resolves one Link from a Collection of Links based on the command invocation matching multiple
   * relations from the Link Index.
   * 
   * @param command the CommandRequest object encapsulating details of the command invocation.
   * @param links a Collection of Links for the command matching the relation.
   * @return the resolved Link matching the command exactly as entered by the user.
   * @see #findLink(org.apache.geode.management.internal.cli.CommandRequest)
   * @see org.apache.geode.management.internal.cli.CommandRequest
   * @see org.apache.geode.management.internal.web.domain.Link
   * @see org.springframework.web.util.UriTemplate
   */
// Find and use the Link with the greatest number of path variables that can be expanded!
protected Link resolveLink(final CommandRequest command, final List<Link> links) {
    // NOTE, Gfsh's ParseResult contains a Map entry for all command options whether or not the user
    // set the option
    // with a value on the command-line, argh!
    Map<String, String> commandParametersCopy = CollectionUtils.removeKeys(new HashMap<>(command.getParameters()), NoValueFilter.INSTANCE);
    Link resolvedLink = null;
    int pathVariableCount = 0;
    for (Link link : links) {
        final List<String> pathVariables = new UriTemplate(decode(link.getHref().toString())).getVariableNames();
        // to even be considered...
        if (commandParametersCopy.keySet().containsAll(pathVariables)) {
            // for the last Link
            if (resolvedLink == null || (pathVariables.size() > pathVariableCount)) {
                resolvedLink = link;
                pathVariableCount = pathVariables.size();
            }
        }
    }
    if (resolvedLink == null) {
        throw new RestApiCallForCommandNotFoundException(String.format("No REST API call for command (%1$s) was found!", command.getInput()));
    }
    return resolvedLink;
}
Also used : UriTemplate(org.springframework.web.util.UriTemplate) Link(org.apache.geode.management.internal.web.domain.Link)

Example 23 with Link

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

the class ClientHttpRequestJUnitTest method testCreateRequestEntityForPost.

@Test
@SuppressWarnings("unchecked")
public void testCreateRequestEntityForPost() throws Exception {
    final Link expectedLink = new Link("post", toUri("http://host.domain.com:8080/app/libraries/{name}/books"), HttpMethod.POST);
    final ClientHttpRequest request = new ClientHttpRequest(expectedLink);
    assertEquals(expectedLink, request.getLink());
    final MultiValueMap<String, Object> expectedRequestParameters = new LinkedMultiValueMap<String, Object>(4);
    expectedRequestParameters.add("author", "Douglas Adams");
    expectedRequestParameters.add("title", "The Hitchhiker's Guide to the Galaxy");
    expectedRequestParameters.add("year", "1979");
    expectedRequestParameters.add("isbn", "0345453743");
    request.addHeaderValues(HttpHeader.CONTENT_TYPE.getName(), MediaType.APPLICATION_FORM_URLENCODED_VALUE);
    request.addParameterValues("author", expectedRequestParameters.getFirst("author"));
    request.addParameterValues("title", expectedRequestParameters.getFirst("title"));
    request.addParameterValues("year", expectedRequestParameters.getFirst("year"));
    request.addParameterValues("isbn", expectedRequestParameters.getFirst("isbn"));
    final HttpEntity<MultiValueMap<String, Object>> requestEntity = (HttpEntity<MultiValueMap<String, Object>>) request.createRequestEntity();
    assertNotNull(requestEntity);
    assertNotNull(requestEntity.getHeaders());
    assertEquals(MediaType.APPLICATION_FORM_URLENCODED, requestEntity.getHeaders().getContentType());
    assertEquals(expectedRequestParameters, requestEntity.getBody());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Link(org.apache.geode.management.internal.web.domain.Link) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 24 with Link

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

the class AbstractWebTestCase method toString.

protected String toString(final Link... links) throws UnsupportedEncodingException {
    final StringBuilder buffer = new StringBuilder("[");
    int count = 0;
    for (final Link link : links) {
        buffer.append(count++ > 0 ? ", " : StringUtils.EMPTY).append(toString(link));
    }
    buffer.append("]");
    return buffer.toString();
}
Also used : Link(org.apache.geode.management.internal.web.domain.Link)

Example 25 with Link

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

the class ShellCommandsControllerJUnitTest method testUniqueIndex.

@Test
public void testUniqueIndex() {
    LinkIndex linkIndex = controller.index("https");
    List<String> conflicts = new ArrayList<>();
    Map<String, String> uriRelationMapping = new HashMap<>(linkIndex.size());
    for (Link link : linkIndex) {
        if (uriRelationMapping.containsKey(link.toHttpRequestLine())) {
            conflicts.add(String.format("REST API endpoint (%1$s) for (%2$s) conflicts with the REST API endpoint for (%3$s)", link.toHttpRequestLine(), link.getRelation(), uriRelationMapping.get(link.toHttpRequestLine())));
        } else {
            uriRelationMapping.put(link.toHttpRequestLine(), link.getRelation());
        }
    }
    assertTrue(String.format("Conflicts: %1$s!", conflicts), conflicts.isEmpty());
}
Also used : LinkIndex(org.apache.geode.management.internal.web.domain.LinkIndex) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) 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