<commit_before>package minhhai2209.jirapluginconverter.plugin.condition;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.Condition;
import com.fasterxml.jackson.databind.ObjectMapper;
import minhhai2209.jirapluginconverter.plugin.jwt.JwtComposer;
import minhhai2209.jirapluginconverter.plugin.render.ParameterContextBuilder;
import minhhai2209.jirapluginconverter.plugin.setting.AuthenticationUtils;
import minhhai2209.jirapluginconverter.plugin.setting.KeyUtils;
import minhhai2209.jirapluginconverter.plugin.setting.LicenseUtils;
import minhhai2209.jirapluginconverter.plugin.setting.PluginSetting;
import minhhai2209.jirapluginconverter.plugin.utils.HttpClientFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;

import java.util.Map;

public class RemoteCondition implements Condition {

  private Map<String, String> params;
  private String conditionUrl;
  private ObjectMapper om = new ObjectMapper();

  @Override
  public void init(Map<String, String> params) throws PluginParseException {
    this.params = params;
    this.conditionUrl = params.get("condition");
  }

  @Override
  public boolean shouldDisplay(Map<String, Object> context) {
    return conditionUrl == null || getRemoteCondition(context);
  }

  private boolean getRemoteCondition(Map<String, Object> context) {
    try {
      String baseUrl = PluginSetting.getPluginBaseUrl();
      String fullUrl = baseUrl + conditionUrl;

      JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
      ApplicationUser user = authenticationContext != null ? authenticationContext.getUser() : null;

      Map<String, String> productContext = ParameterContextBuilder.buildContext(null, context, null, null);
      String userKey = user != null ? user.getKey() : "";
      String lic = LicenseUtils.getLic();

      String urlWithContext = ParameterContextBuilder.buildUrl(fullUrl, productContext);

      URIBuilder builder = new URIBuilder(urlWithContext);
      if (params != null) {
        for (String key : params.keySet()) {
          if (!key.equals("condition")) {
            builder.addParameter(key, params.get(key));
          }
        }
      }
      builder.addParameter("lic", LicenseUtils.getLic())
          .addParameter("user_key", userKey);

      if (AuthenticationUtils.needsAuthentication()) {

        String jwt = JwtComposer.compose(
            KeyUtils.getClientKey(),
            KeyUtils.getSharedSecret(),
            "GET",
            builder,
            userKey,
            conditionUrl);
        builder.addParameter("jwt", jwt);
      }

      String url = builder.toString();

      HttpClient client = HttpClientFactory.build();
      HttpGet httpGet = new HttpGet(url);
      HttpResponse response = client.execute(httpGet);<commit_msg>use accept application/json in remote condition<commit_after>package minhhai2209.jirapluginconverter.plugin.condition;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.plugin.PluginParseException;
import com.atlassian.plugin.web.Condition;
import com.fasterxml.jackson.databind.ObjectMapper;
import minhhai2209.jirapluginconverter.plugin.jwt.JwtComposer;
import minhhai2209.jirapluginconverter.plugin.render.ParameterContextBuilder;
import minhhai2209.jirapluginconverter.plugin.setting.AuthenticationUtils;
import minhhai2209.jirapluginconverter.plugin.setting.KeyUtils;
import minhhai2209.jirapluginconverter.plugin.setting.LicenseUtils;
import minhhai2209.jirapluginconverter.plugin.setting.PluginSetting;
import minhhai2209.jirapluginconverter.plugin.utils.HttpClientFactory;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;

import java.util.Map;

public class RemoteCondition implements Condition {

  private Map<String, String> params;
  private String conditionUrl;
  private ObjectMapper om = new ObjectMapper();

  @Override
  public void init(Map<String, String> params) throws PluginParseException {
    this.params = params;
    this.conditionUrl = params.get("condition");
  }

  @Override
  public boolean shouldDisplay(Map<String, Object> context) {
    return conditionUrl == null || getRemoteCondition(context);
  }

  private boolean getRemoteCondition(Map<String, Object> context) {
    try {
      String baseUrl = PluginSetting.getPluginBaseUrl();
      String fullUrl = baseUrl + conditionUrl;

      JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
      ApplicationUser user = authenticationContext != null ? authenticationContext.getUser() : null;

      Map<String, String> productContext = ParameterContextBuilder.buildContext(null, context, null, null);
      String userKey = user != null ? user.getKey() : "";
      String lic = LicenseUtils.getLic();

      String urlWithContext = ParameterContextBuilder.buildUrl(fullUrl, productContext);

      URIBuilder builder = new URIBuilder(urlWithContext);
      if (params != null) {
        for (String key : params.keySet()) {
          if (!key.equals("condition")) {
            builder.addParameter(key, params.get(key));
          }
        }
      }
      builder.addParameter("lic", LicenseUtils.getLic())
          .addParameter("user_key", userKey);

      if (AuthenticationUtils.needsAuthentication()) {

        String jwt = JwtComposer.compose(
            KeyUtils.getClientKey(),
            KeyUtils.getSharedSecret(),
            "GET",
            builder,
            userKey,
            conditionUrl);
        builder.addParameter("jwt", jwt);
      }

      String url = builder.toString();

      HttpClient client = HttpClientFactory.build();
      HttpGet httpGet = new HttpGet(url);
      httpGet.addHeader(HttpHeaders.ACCEPT, "application/json");

      HttpResponse response = client.execute(httpGet);
{'fraction_non_alphanumeric': 0.09749216300940439, 'fraction_numerical': 0.010031347962382446, 'mean_word_length': 4.13768115942029, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2896824', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1719, 'n_words': 334}