// 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;

    JiraUtils.setApplicationProperties(applicationProperties);

    jiraVersion = applicationProperties.getVersion();
    PluginSetting.load(pluginSettingsFactory, transactionTemplate, pluginLicenseManager, consumerService);

    String existingSharedSecret = KeyUtils.getSharedSecret();
    if (existingSharedSecret == null) {
      KeyUtils.generateSharedSecret(pluginSettingsFactory, transactionTemplate);
    }
    String currentSharedSecret = KeyUtils.getSharedSecret();

    pluginVersion = plugin.getPluginInformation().getVersion();
    String uri = LifeCycleUtils.getInstalledUri();
    String jwt = (existingSharedSecret == null) ? null :
      JwtComposer.compose(KeyUtils.getClientKey(), existingSharedSecret, "POST", uri, null, null);

    notify(error, EventType.installed, uri, currentSharedSecret, jwt);
  }

  public void onEnabled() throws Exception {
    PluginSetting.load(pluginSettingsFactory, transactionTemplate, pluginLicenseManager, consumerService);

    String uri = LifeCycleUtils.getEnabledUri();
    String jwt = JwtComposer.compose(KeyUtils.getClientKey(), KeyUtils.getSharedSecret(), "POST", uri, null, null);
    notify(null, EventType.enabled, uri, null, jwt);
  }

  public void onDisabled() throws Exception {
    String uri = LifeCycleUtils.getDisabledUri();
// 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;


    JiraUtils.setApplicationProperties(applicationProperties);

    jiraVersion = applicationProperties.getVersion();
    PluginSetting.load(pluginSettingsFactory, transactionTemplate, pluginLicenseManager, consumerService);

    String existingSharedSecret = KeyUtils.getSharedSecret();
    if (existingSharedSecret == null) {
      KeyUtils.generateSharedSecret(pluginSettingsFactory, transactionTemplate);
    }
    String currentSharedSecret = KeyUtils.getSharedSecret();

    pluginVersion = plugin.getPluginInformation().getVersion();
    String uri = LifeCycleUtils.getInstalledUri();
    String jwt = (existingSharedSecret == null) ? null :
      JwtComposer.compose(KeyUtils.getClientKey(), existingSharedSecret, "POST", uri, null, null);

    notify(error, EventType.installed, uri, currentSharedSecret, jwt);
  }

  public void onEnabled() throws Exception {
    PluginSetting.load(pluginSettingsFactory, transactionTemplate, pluginLicenseManager, consumerService);

    String uri = LifeCycleUtils.getEnabledUri();
    String jwt = JwtComposer.compose(KeyUtils.getClientKey(), KeyUtils.getSharedSecret(), "POST", uri, null, null);
    notify(null, EventType.enabled, uri, null, jwt);
  }

  public void onDisabled() throws Exception {
    String uri = LifeCycleUtils.getDisabledUri();
    String jwt = JwtComposer.compose(KeyUtils.getClientKey(), KeyUtils.getSharedSecret(), "POST", uri, null, null);