[Micronaut] Receiving Japanese(Shift-JIS) data as HTTPResponse
Tags: java, micronaut
Published: 2024-03-31 14:35:30

## Intro
I will try using Japanese(Shift-JIS) text data in my Micronaut application in this time.

## Change default file encoding
By default, the encoding for my Micronaut application is "windows-31j".
When I use Japanese in the comment, an exception will occur, so I should change the encoding to UTF-8.

#### build.gradle.kts
```kotlin
...
tasks.withType<JavaCompile>().configureEach {
    options.encoding = "UTF-8"
}
```

* [Gradleでコンパイル時の文字コードを指定する(Kotlin DSL版) - kdnakt blog](https://kdnakt.hatenablog.com/entry/2023/03/24/gradle-javacompile-encoding-with-kotlin-dsl)
* [Understanding Tasks - Gradle](https://docs.gradle.org/current/userguide/more_about_tasks.html)

## Setting HTTPResponse encoding
I can use specific encoding for HTTPResponse like below.

#### HomeController.java
```java
package jp.masanori.responsesample;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.MutableHttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/sample")
public class HomeController {
    @Get(uri="/utf8", produces = "text/plain;charset=utf-8")
    public MutableHttpResponse<String> getUtf8(){        
        return HttpResponse.ok("こんにちは");
    }
    @Get(uri="/shiftjis", produces = "text/plain;charset=Shift_JIS")
    public String getShiftjis(){
        return "こんにちは";
    }
}
```

## Receiving Shift-JIS strings as HTTPResponse by HttpClient
I can receive them by HttpClient like below.

#### FileController.java
```java
package jp.masanori.websample;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;
import java.io.BufferedReader;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.micronaut.core.io.buffer.ByteBuffer;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Part;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.multipart.CompletedFileUpload;
import jp.masanori.dto.ComputeResult;
import jp.masanori.dto.ComputingStatusRequest;
import reactor.core.publisher.Mono;

@Controller("/files")
public class FileController {
    private final HttpClient httpClient;
    public FileController(HttpClient httpClient) {
        this.httpClient = httpClient;
    }
    @Get(uri="/message", produces = "text/plain;charset=utf-8")
    public Mono<String> getShiftJisText() throws IOException {
        
        HttpRequest<?> req = HttpRequest.GET("http://localhost:8085/sample/shiftjis");
        return Mono.from(httpClient.retrieve(req));
    }
}
```

But when I receive a Shift-JIS encoded file, the values will be garbled.

#### sample.txt
```
こんにちは
```

#### FileController.java
```java
...
    @Get(uri="/message", produces = "text/plain;charset=utf-8")
    public Mono<String> getShiftJisText() throws IOException {
        
        HttpRequest<?> req = HttpRequest.GET("http://localhost:8085/sample.txt");
        return Mono.from(httpClient.retrieve(req));
    }
...
```

#### Result
```
����ɂ���
```

To prevent this, I should set the encoding to receive data.

```java
...
    @Get(uri="/message", produces = "text/plain;charset=utf-8")
    public Mono<String> getShiftJisText() throws IOException {
        HttpRequest<?> req = HttpRequest.GET("http://localhost:8085/sample.txt")
            .contentType(MediaType.TEXT_JSON_TYPE);
        // receiving data as ByteBuffer
        HttpResponse<ByteBuffer> res = Mono.from(httpClient.exchange(req, ByteBuffer.class)).block();
        if(res.status() != HttpStatus.OK) {
            return Mono.empty();
        }
        // set encoding and read data
        try(ByteArrayInputStream input = new ByteArrayInputStream(res.body().toByteArray());
            BufferedReader br = new BufferedReader(new InputStreamReader(input, "SJIS"))) {            
            String line = "";
            StringBuilder result = new StringBuilder();
            while((line = br.readLine()) != null) {
                result.append(String.format("%s%s", line, "\r\n"));
            }
            return Mono.just(result.toString());
        }catch(IOException e) {
        }
        return Mono.empty();
    }
...
```

#### Result
```
こんにちは
```

* [Micronaut HTTP Client](https://guides.micronaut.io/latest/micronaut-http-client-maven-java.html)
* [InputStreamReader - Java Platform SE 8](https://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html)
* [BufferedReader - Java Platform SE 8](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html)