[Java] 파일의 값을 HTTP Header로 설정하는 방법.

2024. 11. 8. 17:15Language/java

728x90
728x90

HTTP Request, Response 거래 도식.

HTTP Header 설정을 위해 사용한 라이브러리.

  • commons-httpclient-3.0.1.jar 

특정 파일의 값을 HTTP Header로 설정하는 코드.

  • setAuthorization.java

코드 설명.

  • Application은 HTTP 요청을 보내기 전에 호출된다.
  • readAccessTokenFromFile 함수에서 파일을 읽어 반환하는 작업을 수행한다.
  • 반환 받은 파일의 값을 Http Header로 설정한다.
$ cat setAuthorization.java
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.PostMethod;
import anylink.common.base.AnyDocument;
import anylink.common.base.HttpUserHandler;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class setAuthorization extends HttpUserHandler {

    @Override
    public byte[] setRequestByte(HttpMethodBase method, byte[] sendByte) {
        try {
            // 파일에서 access_token 읽기
            String accessToken = readAccessTokenFromFile("/home/tmax/batfile/oauth_sftpc/access_token");
            // Authorization 헤더 설정
            method.setRequestHeader("Authorization", "Bearer " + accessToken);
        } catch (IOException e) {
            e.printStackTrace(); // 파일 읽기 실패 시 예외 처리
        }
        return sendByte;
    }
    // 파일에서 access_token을 읽어오는 메서드
    private String readAccessTokenFromFile(String filePath) throws IOException {
        // 파일의 내용을 모두 읽어서 문자열로 변환
        return new String(Files.readAllBytes(Paths.get(filePath))).trim();
    }
}

Java 컴파일.

$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

$ javac -classpath commons-httpclient-3.0.1.jar:al4-core.jar setAuthorization.java

Application에서 읽을 파일 확인.

$ cat /home/tmax/batfile/oauth_sftpc/access_token
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJGMDAxMjM0NTYwIiwic2NvcGUiOlsib29iIl0sImlzcyI6Imh0dHBzOi8vd3d3Lm9wZW5iYW5raW5nLm9yLmtyIiwiZXhwIjoiMTU3Njk3OTEzMyIsImp0aSI601NWEzMTc5LTEwMjYtNzZjNC05YzExLWQxNTZhOTcxMmRiZiJ9.miMnaiKpzXHTJr3JPlUd2amPpeXiqOM8XnRyX6c62Vs

HTTP Server Request, Response Log.(Flask)

  • Application에서 읽은 파일의 값이 HTTP Header로 설정된 것을 확인할 수 있음.
  • Authorization : Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
---------------------------------------
Current function : transfer_result
---------------------------------------
---------------------------------------
Current Server Time : 2024-10-17 14:52:06
---------------------------------------
---------------------------------------
HTTP Protocol Version : HTTP/1.1
---------------------------------------
---------------------------------------
GET HTTP Header
Content-Type : application/json; charset=UTF-8
Authorization : Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJGMDAxMjM0NTYwIiwic2NvcGUiOlsib29iIl0sImlzcyI6Imh0dHBzOi8vd3d3Lm9wZW5iYW5raW5nLm9yLmtyIiwiZXhwIjoiMTU3Njk3OTEzMyIsImp0aSI601NWEzMTc5LTEwMjYtNzZjNC05YzExLWQxNTZhOTcxMmRiZiJ9.miMnaiKpzXHTJr3JPlUd2amPpeXiqOM8XnRyX6c62Vs
User-Agent : Jakarta Commons-HttpClient/3.0.1
Host : 192.168.115.34:28888
Content-Length : 163
---------------------------------------
---------------------------------------
GET HTTP Body
{"api_trx_dtm":"2024-08-26T10:30:00Z","api_trx_no":"1234567890","file_name":"test_file.txt","org_api_trx_no":"0987654321","org_api_trx_dtm":"2024-08-25T14:00:00Z"}
---------------------------------------
---------------------------------------
PUT HTTP Body
{'api_trx_no': 'TRX2024051555512345678901234567890123456789', 'api_trx_dtm': '2024-05-05T15:55:55.000Z', 'rsp_code': 'A0000', 'rsp_message': 'resultresultresult completioncompletioncompletion Your transaction has been successfully processed. Detailed information follows in the document.'}
---------------------------------------
---------------------------------------
End Processing
---------------------------------------
192.168.15.56 - - [17/Oct/2024 14:52:06] "POST /transfer/result HTTP/1.1" 200 -
728x90
728x90