Search in sources :

Example 6 with CommandRequest

use of org.apache.geode.management.internal.cli.CommandRequest in project geode by apache.

the class SimpleHttpOperationInvokerJUnitTest method testGetHttpRequestUrl.

@Test
public void testGetHttpRequestUrl() throws Exception {
    final CommandRequest command = createCommandRequest("get resource --option=value");
    assertEquals(getExpectedHttpRequestUrl(command), toString(getOperationInvoker().getHttpRequestUrl(command)));
}
Also used : CommandRequest(org.apache.geode.management.internal.cli.CommandRequest) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 7 with CommandRequest

use of org.apache.geode.management.internal.cli.CommandRequest in project geode by apache.

the class GfshExecutionStrategy method executeOnRemote.

//////////////// ExecutionStrategy interface Methods End /////////////////////
/**
   * Sends the user input (command string) via {@link OperationInvoker} to a remote GemFire node for
   * processing & execution.
   *
   * @param parseResult
   * 
   * @return result of execution/processing of the command
   * 
   * @throws IllegalStateException if gfsh doesn't have an active connection.
   */
private Result executeOnRemote(GfshParseResult parseResult) {
    Result commandResult = null;
    Object response = null;
    if (!shell.isConnectedAndReady()) {
        shell.logWarning("Can't execute a remote command without connection. Use 'connect' first to connect.", null);
        logWrapper.info("Can't execute a remote command \"" + parseResult.getUserInput() + "\" without connection. Use 'connect' first to connect to GemFire.");
        return null;
    }
    byte[][] fileData = null;
    CliAroundInterceptor interceptor = null;
    String interceptorClass = getInterceptor(parseResult.getMethod());
    // 1. Pre Remote Execution
    if (!CliMetaData.ANNOTATION_NULL_VALUE.equals(interceptorClass)) {
        try {
            interceptor = (CliAroundInterceptor) ClassPathLoader.getLatest().forName(interceptorClass).newInstance();
        } catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) {
            shell.logWarning("Configuration error", e);
        }
        if (interceptor != null) {
            Result preExecResult = interceptor.preExecution(parseResult);
            if (Status.ERROR.equals(preExecResult.getStatus())) {
                return preExecResult;
            } else if (preExecResult instanceof FileResult) {
                FileResult fileResult = (FileResult) preExecResult;
                fileData = fileResult.toBytes();
            }
        } else {
            return ResultBuilder.createBadConfigurationErrorResult("Interceptor Configuration Error");
        }
    }
    // 2. Remote Execution
    final Map<String, String> env = shell.getEnv();
    try {
        response = shell.getOperationInvoker().processCommand(new CommandRequest(parseResult, env, fileData));
    } catch (NotAuthorizedException e) {
        return ResultBuilder.createGemFireUnAuthorizedErrorResult("Unauthorized. Reason : " + e.getMessage());
    } catch (Exception e) {
        shell.logSevere(e.getMessage(), e);
    } finally {
        env.clear();
    }
    if (response == null) {
        shell.logWarning("Response was null for: \"" + parseResult.getUserInput() + "\". (gfsh.isConnected=" + shell.isConnectedAndReady() + ")", null);
        return ResultBuilder.createBadResponseErrorResult(" Error occurred while " + "executing \"" + parseResult.getUserInput() + "\" on manager. " + "Please check manager logs for error.");
    }
    // it can also be a Path to a temp file downloaded from the rest http request
    if (response instanceof String) {
        CommandResponse commandResponse = CommandResponseBuilder.prepareCommandResponseFromJson((String) response);
        if (commandResponse.isFailedToPersist()) {
            shell.printAsSevere(CliStrings.SHARED_CONFIGURATION_FAILED_TO_PERSIST_COMMAND_CHANGES);
            logWrapper.severe(CliStrings.SHARED_CONFIGURATION_FAILED_TO_PERSIST_COMMAND_CHANGES);
        }
        String debugInfo = commandResponse.getDebugInfo();
        if (StringUtils.isNotBlank(debugInfo)) {
            // TODO - Abhishek When debug is ON, log response in gfsh logs
            // TODO - Abhishek handle \n better. Is it coming from GemFire formatter
            debugInfo = debugInfo.replaceAll("\n\n\n", "\n");
            debugInfo = debugInfo.replaceAll("\n\n", "\n");
            debugInfo = debugInfo.replaceAll("\n", "\n[From Manager : " + commandResponse.getSender() + "]");
            debugInfo = "[From Manager : " + commandResponse.getSender() + "]" + debugInfo;
            LogWrapper.getInstance().info(debugInfo);
        }
        commandResult = ResultBuilder.fromJson((String) response);
    }
    Path tempFile = null;
    if (response instanceof Path) {
        tempFile = (Path) response;
    }
    // 3. Post Remote Execution
    if (interceptor != null) {
        Result postExecResult = interceptor.postExecution(parseResult, commandResult, tempFile);
        if (postExecResult != null) {
            if (Status.ERROR.equals(postExecResult.getStatus())) {
                if (logWrapper.infoEnabled()) {
                    logWrapper.info("Post execution Result :: " + postExecResult);
                }
            } else if (logWrapper.fineEnabled()) {
                logWrapper.fine("Post execution Result :: " + postExecResult);
            }
            commandResult = postExecResult;
        }
    }
    return commandResult;
}
Also used : Path(java.nio.file.Path) CliAroundInterceptor(org.apache.geode.management.internal.cli.CliAroundInterceptor) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) CommandResponse(org.apache.geode.management.internal.cli.CommandResponse) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) CommandProcessingException(org.apache.geode.management.cli.CommandProcessingException) ParseResult(org.springframework.shell.event.ParseResult) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) FileResult(org.apache.geode.management.internal.cli.result.FileResult) CommandRequest(org.apache.geode.management.internal.cli.CommandRequest) FileResult(org.apache.geode.management.internal.cli.result.FileResult)

Example 8 with CommandRequest

use of org.apache.geode.management.internal.cli.CommandRequest in project geode by apache.

the class SimpleHttpOperationInvokerJUnitTest method testCreateHttpRequest.

@Test
public void testCreateHttpRequest() throws Exception {
    final CommandRequest command = createCommandRequest("save resource --path=/path/to/file --size=1024KB");
    final ClientHttpRequest request = getOperationInvoker().createHttpRequest(command);
    assertNotNull(request);
    assertEquals(SimpleHttpOperationInvoker.USER_AGENT_HTTP_REQUEST_HEADER_VALUE, request.getHeaderValue(HttpHeader.USER_AGENT.getName()));
    final Link requestLink = request.getLink();
    assertNotNull(requestLink);
    assertTrue(toString(requestLink).startsWith("POST"));
    assertTrue(toString(requestLink).endsWith(command.getInput()));
}
Also used : CommandRequest(org.apache.geode.management.internal.cli.CommandRequest) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) Link(org.apache.geode.management.internal.web.domain.Link) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

CommandRequest (org.apache.geode.management.internal.cli.CommandRequest)8 UnitTest (org.apache.geode.test.junit.categories.UnitTest)6 Test (org.junit.Test)6 ClientHttpRequest (org.apache.geode.management.internal.web.http.ClientHttpRequest)4 HashMap (java.util.HashMap)2 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)2 Link (org.apache.geode.management.internal.web.domain.Link)2 Path (java.nio.file.Path)1 CommandProcessingException (org.apache.geode.management.cli.CommandProcessingException)1 Result (org.apache.geode.management.cli.Result)1 CliAroundInterceptor (org.apache.geode.management.internal.cli.CliAroundInterceptor)1 CommandResponse (org.apache.geode.management.internal.cli.CommandResponse)1 GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)1 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)1 FileResult (org.apache.geode.management.internal.cli.result.FileResult)1 NotAuthorizedException (org.apache.geode.security.NotAuthorizedException)1 Resource (org.springframework.core.io.Resource)1 ParseResult (org.springframework.shell.event.ParseResult)1