Exemplo n.º 1
0
  /**
   * Loads up the HTTP API Documentation in memory for this service. The HTTP API information to
   * describe this servlet's REQUEST and RESPONSE messaging is displayed to the end user via the
   * Service API help page.
   */
  public void init() throws ServletException {
    // *****************************
    // THIS SERVICE API DESCRIPTION CONTRACT
    // *****************************
    // This information is used in the API JSP document, used to describe
    // how to make setting changes from a head-less client.

    if (apiStore.getApiDocServiceByName(API_CONFIGURATION_NAME) == null) {
      ApiDocService apiDocService = new ApiDocService();
      apiDocService.setName(API_CONFIGURATION_NAME);
      apiDocService.setServicePath("/home");
      apiDocService.setDescription(
          "If you need to initialize Mockey with a definitions file, then this API may serve your needs. ");
      // *****************************
      // REQUEST DEFINITION
      // *****************************

      ApiDocRequest apiDocRequest = new ApiDocRequest();

      // Parameter - 'action'
      ApiDocAttribute reqAttributeAction = new ApiDocAttribute();
      reqAttributeAction.setFieldName(API_CONFIGURATION_PARAMETER_ACTION);
      reqAttributeAction.addFieldValues(
          new ApiDocFieldValue(
              API_CONFIGURATION_PARAMETER_ACTION_VALUE_DELETE,
              "Delete all configurations, history, settings, etc., and start with a clean Mockey. "));
      reqAttributeAction.addFieldValues(
          new ApiDocFieldValue(
              API_CONFIGURATION_PARAMETER_ACTION_VALUE_INIT,
              "Will delete everything and configure Mockey with the defined file. "));
      apiDocRequest.addAttribute(reqAttributeAction);

      // Parameter - 'file'
      ApiDocAttribute reqAttributeFile = new ApiDocAttribute();
      reqAttributeFile.setFieldName(API_CONFIGURATION_PARAMETER_FILE);
      reqAttributeFile.addFieldValues(
          new ApiDocFieldValue(
              "[string]",
              "Relative path to the service definitions configuration file. Required if 'action' is 'init'"));
      reqAttributeFile.setExample("../some_file.xml or /Users/someuser/Work/some_file.xml");
      apiDocRequest.addAttribute(reqAttributeFile);

      // Parameter - 'type'
      ApiDocAttribute reqAttributeType = new ApiDocAttribute();
      reqAttributeType.setFieldName(API_CONFIGURATION_PARAMETER_TYPE);
      reqAttributeType.addFieldValues(
          new ApiDocFieldValue(
              "json",
              "Response will be in JSON. Any other value for 'type' is undefined and you may experience a 302 or get HTML back."));
      apiDocRequest.addAttribute(reqAttributeType);
      apiDocService.setApiRequest(apiDocRequest);

      // Parameter - 'transientState'
      ApiDocAttribute reqAttributeState = new ApiDocAttribute();
      reqAttributeState.setFieldName(API_CONFIGURATION_PARAMETER_ACTION_VALUE_TRANSIENT_STATE);
      reqAttributeState.addFieldValues(
          new ApiDocFieldValue("boolean", "Read only mode? Also known as transient."));
      apiDocRequest.addAttribute(reqAttributeState);
      apiDocService.setApiRequest(apiDocRequest);

      // *****************************
      // RESPONSE DEFINITION
      // *****************************

      ApiDocResponse apiResponse = new ApiDocResponse();
      // Building a JSON RESPONSE example
      try {
        JSONObject jsonResponseObject = new JSONObject();
        JSONObject jsonResultObject = new JSONObject();
        jsonResultObject.put(
            SUCCESS,
            "Some informative coaching message. If success isn't a value, then maybe you have a 'fail' message.");
        jsonResultObject.put("file", "Some file name");
        jsonResponseObject.put("result", jsonResultObject);
        apiResponse.setExample(jsonResponseObject.toString());
      } catch (Exception e) {
        logger.error("Unabel to build a sample JSON message. ", e);
      }

      // Response attribute 'file'
      ApiDocAttribute resAttributeFile = new ApiDocAttribute();
      resAttributeFile.setFieldName(API_CONFIGURATION_PARAMETER_FILE);
      resAttributeFile.setFieldDescription("Name of file used to initialize Mockey.");
      apiResponse.addAttribute(resAttributeFile);

      // Response attribute 'success'
      ApiDocAttribute resAttributeSuccess = new ApiDocAttribute();
      resAttributeSuccess.setFieldName(SUCCESS);
      resAttributeSuccess.setFieldDescription(
          "Successfully initialized or deleted service definitions.  You get 'fail' or 'success', not both.");
      apiResponse.addAttribute(resAttributeSuccess);

      ApiDocAttribute resAttributeFail = new ApiDocAttribute();
      resAttributeFail.setFieldName(FAIL);
      resAttributeFail.setFieldDescription(
          "Failed to initialize or delete service definitions. You get 'fail' or 'success', not both.");
      apiResponse.addAttribute(resAttributeFail);

      apiDocService.setApiResponse(apiResponse);
      apiStore.saveOrUpdateService(apiDocService);
    }
  }