Пример #1
0
 void processAjaxRequest(HttpServletRequest request, Element xmlResponse) {
   try {
     sendProwlNotification(request);
   } catch (RuntimeException e) {
     ActionErrors errors = new ActionErrors();
     errors.addError("tcprowlProblem", createMessageWithNested(e));
     getOrCreateMessages(request).addMessage("tcprowlMessage", createMessageWithNested(e));
     errors.serialize(xmlResponse);
   }
 }
  @Override
  protected void doPost(
      @NotNull final HttpServletRequest request,
      @NotNull final HttpServletResponse response,
      @NotNull final Element xmlResponse) {
    final ActionErrors errors = new ActionErrors();
    final BasePropertiesBean propsBean = new BasePropertiesBean(null);
    PluginPropertiesUtil.bindPropertiesFromRequest(request, propsBean, true);

    final Map<String, String> props = propsBean.getProperties();
    final String subscriptionId = props.get(AzureWebConstants.SUBSCRIPTION_ID);
    final String certificate = props.get("secure:" + AzureWebConstants.MANAGEMENT_CERTIFICATE);

    final AzureApiConnector apiConnector;
    try {
      apiConnector = new AzureApiConnector(subscriptionId, certificate);
      apiConnector.test();
    } catch (InvalidCertificateException ex) {
      errors.addError(
          "certificateError",
          "Invalid Management certificate. Please enter the Management Certificate exactly as it is presented in the subscription file.");
      errors.serialize(xmlResponse);
      LOG.warnAndDebugDetails("An error during initializing connection: " + ex.getMessage(), ex);
      return;
    } catch (CheckedCloudException ex) {
      errors.addError(
          "pingError",
          "Error connecting to Microsoft Azure. Please check that your Management Certificate and Subscription ID are valid.");
      errors.serialize(xmlResponse);
      LOG.warnAndDebugDetails("An error during initializing connection: " + ex.getMessage(), ex);
      return;
    }

    final List<Promise<Content, Throwable, Void>> promises = new ArrayList<>(myHandlers.size());
    for (final ResourceHandler handler : myHandlers) {
      try {
        final Promise<Content, Throwable, Void> promise =
            handler
                .handle(apiConnector)
                .fail(
                    new FailCallback<Throwable>() {
                      @Override
                      public void onFail(Throwable result) {
                        LOG.warn(
                            String.format(
                                "Failed to execute handler %s: %s", handler.getName(), result),
                            result);
                        errors.addError(handler.getName(), result.getMessage());
                      }
                    });
        promises.add(promise);
      } catch (Throwable t) {
        LOG.warn(
            String.format("Failed to add handler %s: %s", handler.getName(), t.getMessage()), t);
        errors.addError(handler.getName(), t.getMessage());
      }
    }

    if (promises.size() == 0) {
      if (errors.hasErrors()) {
        writeErrors(xmlResponse, errors);
      }

      return;
    }

    try {
      myManager
          .when(promises.toArray(new Promise[] {}))
          .always(
              new AlwaysCallback<MultipleResults, OneReject>() {
                @Override
                public void onAlways(
                    Promise.State state, MultipleResults resolved, OneReject rejected) {
                  if (errors.hasErrors()) {
                    writeErrors(xmlResponse, errors);
                  } else {
                    for (OneResult oneResult : resolved) {
                      xmlResponse.addContent((Content) oneResult.getResult());
                    }
                  }
                }
              })
          .waitSafely();
    } catch (InterruptedException e) {
      LOG.warn("Request executing has been interrupted: " + e.getMessage());
      errors.addError("handler", e.getMessage());
      writeErrors(xmlResponse, errors);
    }
  }