// Path: src/main/java/minhhai2209/jirapluginconverter/plugin/jwt/JwtComposer.java
// public class JwtComposer {
// 
//   public static String compose(
//       String key,
//       String sharedSecret,
//       String method,
//       String apiPath,
//       List<NameValuePair> pairs,
//       JwtContext context) {
// 
//     try {
//       long issuedAt = System.currentTimeMillis() / 1000L;
//       long expiresAt = issuedAt + 1800L;
//       JwtJsonBuilder jwtJsonBuilder = new JsonSmartJwtJsonBuilder()
//               .issuedAt(issuedAt)
//               .expirationTime(expiresAt)
//               .issuer(key)
//               .claim("context", context);
//       Map<String, List<String>> parameters = JwtHelper.getParameters(pairs);
//       Map<String, String[]> parameterMap = JwtHelper.getParameterMap(parameters);
//       CanonicalHttpUriRequest canonicalHttpUrlRequest = new CanonicalHttpUriRequest(method, apiPath, null, parameterMap);
//       JwtClaimsBuilder.appendHttpRequestClaims(jwtJsonBuilder, canonicalHttpUrlRequest);
//       JwtWriterFactory jwtWriterFactory = new NimbusJwtWriterFactory();
//       String unsignedJwt = jwtJsonBuilder.build();
//       JwtWriter macSigningWriter = jwtWriterFactory.macSigningWriter(SigningAlgorithm.HS256, sharedSecret);
//       String jwt = macSigningWriter.jsonToJwt(unsignedJwt);
//       return jwt;
//     } catch (Exception e) {
//       ExceptionUtils.throwUnchecked(e);
//     }
//     return null;
//   }
// 
//   public static String compose(
//       String key, String sharedSecret, String method, URIBuilder uriBuilder, String userKey, String path) {
//     List<NameValuePair> pairs = uriBuilder.getQueryParams();
//     int index = path.indexOf("?");
//     if (index >= 0) {
//       path = path.substring(0, index);
//     }
//     JwtContext context = new JwtContext();
//     JwtContextUser user = new JwtContextUser();
//     user.setUserKey(userKey);
//     context.setUser(user);
//     String jwt = compose(key, sharedSecret, method, path, pairs, context);
//     return jwt;
//   }
// }
// 
// Path: src/main/java/minhhai2209/jirapluginconverter/utils/ExceptionUtils.java
// public class ExceptionUtils {
// 
//   public static void throwUnchecked(Exception e) {
//     throw new IllegalStateException(e);
//   }
// 
//   public static String getStackTrace(Exception e) {
//     return Throwables.getStackTraceAsString(e);
//   }
// }


import com.atlassian.jira.util.json.JSONObject;
import com.atlassian.oauth.consumer.ConsumerService;
import com.atlassian.plugin.Plugin;
import com.atlassian.sal.api.ApplicationProperties;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.sal.api.transaction.TransactionTemplate;
import com.atlassian.upm.api.license.PluginLicenseManager;
import minhhai2209.jirapluginconverter.plugin.jwt.JwtComposer;
import minhhai2209.jirapluginconverter.plugin.setting.*;
import minhhai2209.jirapluginconverter.plugin.utils.HttpClientFactory;
import minhhai2209.jirapluginconverter.utils.ExceptionUtils;
import minhhai2209.jirapluginconverter.utils.JsonUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
    notify(null, EventType.disabled, uri, null, jwt);
  }

  public void onUninstalled() throws Exception {
    String uri = LifeCycleUtils.getUninstalledUri();
    String jwt = JwtComposer.compose(KeyUtils.getClientKey(), KeyUtils.getSharedSecret(), "POST", uri, null, null);
    notify(null, EventType.uninstalled, uri, null, jwt);
  }

  private void notify(StringBuilder error, EventType eventType, String uri, String sharedSecret, String jwt) throws Exception {
    try {
      if (uri != null) {
        PluginLifeCycleEvent event = new PluginLifeCycleEvent();
        event.setBaseUrl(JiraUtils.getFullBaseUrl());
        event.setClientKey(KeyUtils.getClientKey());
        event.setDescription("");
        event.setEventType(eventType);
        event.setKey(PluginSetting.getDescriptor().getKey());
        event.setPluginsVersion(pluginVersion);
        event.setProductType(ProductType.jira);
        event.setPublicKey(KeyUtils.getPublicKey());
        event.setServerVersion(jiraVersion);
        event.setServiceEntitlementNumber(SenUtils.getSen());
        event.setSharedSecret(sharedSecret);
        notify(uri, event, jwt, error);
      }
    } catch (Exception e) {
      System.out.println(PluginSetting.getDescriptor().getKey() + " PLUGIN NOTIFY EVENT '" +
        eventType.toString() + "' FAILED: " + e.getMessage());
      if (error != null) {
// Path: src/main/java/minhhai2209/jirapluginconverter/plugin/jwt/JwtComposer.java
// public class JwtComposer {
// 
//   public static String compose(
//       String key,
//       String sharedSecret,
//       String method,
//       String apiPath,
//       List<NameValuePair> pairs,
//       JwtContext context) {
// 
//     try {
//       long issuedAt = System.currentTimeMillis() / 1000L;
//       long expiresAt = issuedAt + 1800L;
//       JwtJsonBuilder jwtJsonBuilder = new JsonSmartJwtJsonBuilder()
//               .issuedAt(issuedAt)
//               .expirationTime(expiresAt)
//               .issuer(key)
//               .claim("context", context);
//       Map<String, List<String>> parameters = JwtHelper.getParameters(pairs);
//       Map<String, String[]> parameterMap = JwtHelper.getParameterMap(parameters);
//       CanonicalHttpUriRequest canonicalHttpUrlRequest = new CanonicalHttpUriRequest(method, apiPath, null, parameterMap);
//       JwtClaimsBuilder.appendHttpRequestClaims(jwtJsonBuilder, canonicalHttpUrlRequest);
//       JwtWriterFactory jwtWriterFactory = new NimbusJwtWriterFactory();
//       String unsignedJwt = jwtJsonBuilder.build();
//       JwtWriter macSigningWriter = jwtWriterFactory.macSigningWriter(SigningAlgorithm.HS256, sharedSecret);
//       String jwt = macSigningWriter.jsonToJwt(unsignedJwt);
//       return jwt;
//     } catch (Exception e) {
//       ExceptionUtils.throwUnchecked(e);
//     }
//     return null;
//   }
// 
//   public static String compose(
//       String key, String sharedSecret, String method, URIBuilder uriBuilder, String userKey, String path) {
//     List<NameValuePair> pairs = uriBuilder.getQueryParams();
//     int index = path.indexOf("?");
//     if (index >= 0) {
//       path = path.substring(0, index);
//     }
//     JwtContext context = new JwtContext();
//     JwtContextUser user = new JwtContextUser();
//     user.setUserKey(userKey);
//     context.setUser(user);
//     String jwt = compose(key, sharedSecret, method, path, pairs, context);
//     return jwt;
//   }
// }
// 
// Path: src/main/java/minhhai2209/jirapluginconverter/utils/ExceptionUtils.java
// public class ExceptionUtils {
// 
//   public static void throwUnchecked(Exception e) {
//     throw new IllegalStateException(e);
//   }
// 
//   public static String getStackTrace(Exception e) {
//     return Throwables.getStackTraceAsString(e);
//   }
// }


// Path: src/main/java/minhhai2209/jirapluginconverter/plugin/lifecycle/PluginLifeCycleEventHandler.java
import com.atlassian.jira.util.json.JSONObject;
import com.atlassian.oauth.consumer.ConsumerService;
import com.atlassian.plugin.Plugin;
import com.atlassian.sal.api.ApplicationProperties;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.sal.api.transaction.TransactionTemplate;
import com.atlassian.upm.api.license.PluginLicenseManager;
import minhhai2209.jirapluginconverter.plugin.jwt.JwtComposer;
import minhhai2209.jirapluginconverter.plugin.setting.*;
import minhhai2209.jirapluginconverter.plugin.utils.HttpClientFactory;
import minhhai2209.jirapluginconverter.utils.ExceptionUtils;
import minhhai2209.jirapluginconverter.utils.JsonUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

    notify(null, EventType.disabled, uri, null, jwt);
  }

  public void onUninstalled() throws Exception {
    String uri = LifeCycleUtils.getUninstalledUri();
    String jwt = JwtComposer.compose(KeyUtils.getClientKey(), KeyUtils.getSharedSecret(), "POST", uri, null, null);
    notify(null, EventType.uninstalled, uri, null, jwt);
  }

  private void notify(StringBuilder error, EventType eventType, String uri, String sharedSecret, String jwt) throws Exception {
    try {
      if (uri != null) {
        PluginLifeCycleEvent event = new PluginLifeCycleEvent();
        event.setBaseUrl(JiraUtils.getFullBaseUrl());
        event.setClientKey(KeyUtils.getClientKey());
        event.setDescription("");
        event.setEventType(eventType);
        event.setKey(PluginSetting.getDescriptor().getKey());
        event.setPluginsVersion(pluginVersion);
        event.setProductType(ProductType.jira);
        event.setPublicKey(KeyUtils.getPublicKey());
        event.setServerVersion(jiraVersion);
        event.setServiceEntitlementNumber(SenUtils.getSen());
        event.setSharedSecret(sharedSecret);
        notify(uri, event, jwt, error);
      }
    } catch (Exception e) {
      System.out.println(PluginSetting.getDescriptor().getKey() + " PLUGIN NOTIFY EVENT '" +
        eventType.toString() + "' FAILED: " + e.getMessage());
      if (error != null) {
        error.append(ExceptionUtils.getStackTrace(e));