2016. november 22., kedd

UTF-8 character problem with JIRA Application link

I have created a JIRA, and a Confluence add-on. They communicated with each other via REST API, using the Application link interface. I needed to call a REST function, implemented in my own JIRA add-on, from the Confluence add-on. 


It worked fine until any of the REST parameters contains a non ASCII character. In case of the parameter contains a German umlaut (öäüß), I got question marks at the JIRA side.


I wanted to prove, that the problem is not with my JIRA plugin, so I tried to call standard, built in REST functions via application link as well. The example code shows, how to call the REST API in order to retrieve project information.


public JsonJiraProject findJiraProject(String projectKey) {

    ApplicationLink appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType.class);
    ApplicationLinkRequestFactory factory = appLink.createAuthenticatedRequestFactory();
 
    ApplicationLinkRequest aplrq = factory.createRequest(Request.MethodType.GET, "http://localhost:2990/jira/rest/api/2/project/");

    aplrq.addRequestParameters("projectKey", projectKey);
    aplrq.setSoTimeout(APP_LINK_TIME_OUT);
    String jiraResponse = aplrq.execute();
    ...




I found, that the same problem occurs with basic, built-in JIRA REST functions as well. JIRA application link does not support UTF-8 parameters.

What I tried to get UTF-8 parameters working

  • I tried to set header of ApplicationLinkRequest, but it does not help.
    alr.addHeader("Content-Type", "application/json;charset=UTF-8");
  • I tried to define parameter directly at the end of the URL, without adding it as request parameter.
  • I tried to URL encode the URL with the parameter
  • I tried to call the function direct from a client (RESTClient add-on in Firefox), and I got the correct answer.

So I am sure, that the problem is with the ApplicationLinkRequest implementation, and it can not be solved by setting any option or header parameter.


As workaround I found, that I need to convert my input fields to ISO8859-1 character set. It does not cause any problem, and covers the characters (German umlauts), I needed to use. 


I created a utility class with following code to convert my input values:



public static String convertToIso8859(final String input) {
    if (StringUtils.isBlank(input)) {
        return "";
    }
    try {
        return new String(input.getBytes("UTF-8"), "ISO8859_1");
    } catch (UnsupportedEncodingException e) {
        return "";
    }
}

At the same time I restricted the allowed characters for my output fields, to avoid typing characters, outside of the ISO8859-1 set.


1 megjegyzés:

  1. Hi, I have been fighting against this problem currently.
    Just wanted you to know that changing Jira's system encoding to UTF-8 fixed this issue for me :)

    See: https://community.atlassian.com/t5/Jira-questions/Change-Jira-System-Encoding-from-Cp1252-to-UTF-8/qaq-p/1039365

    VálaszTörlés