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();
}
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;
}
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());
}
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();
}
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());
}
Aggregations