package com.interplug.qcast.biz.community; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Service; import org.springframework.util.StreamUtils; import org.springframework.web.util.UriComponentsBuilder; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.interplug.qcast.biz.community.dto.BoardRequest; import com.interplug.qcast.biz.community.dto.BoardResponse; import com.interplug.qcast.util.InterfaceQsp; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @Slf4j @Service @RequiredArgsConstructor public class BoardService { @Value("${qsp.url}") private String QSP_API_URL; private final InterfaceQsp interfaceQsp; /** * ※ 게시판 목록 조회 QSP -> Q.CAST3 * * @param boardRequest * @return * @throws Exception */ public BoardResponse getBoardList(BoardRequest boardRequest) throws Exception { BoardResponse response = null; /* [0]. QSP API (url + param) Setting */ String encodedSchTitle = URLEncoder.encode(boardRequest.getSchTitle(), StandardCharsets.UTF_8); String url = QSP_API_URL + "/api/board/list"; String apiUrl = UriComponentsBuilder.fromHttpUrl(url) .queryParam("noticeNo", boardRequest.getNoticeNo()).queryParam("schTitle", encodedSchTitle) .queryParam("schNoticeTpCd", boardRequest.getSchNoticeTpCd()) .queryParam("schNoticeClsCd", boardRequest.getSchNoticeClsCd()) .queryParam("startRow", boardRequest.getStartRow()) .queryParam("endRow", boardRequest.getEndRow()).build().toUriString(); /* [1]. QSP API CALL -> Response */ String strResponse = interfaceQsp.callApi(HttpMethod.GET, apiUrl, null); if (!"".equals(strResponse)) { ObjectMapper om = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); response = om.readValue(strResponse, BoardResponse.class); } return response; } /** * ※ 게시판 상세 조회 QSP -> Q.CAST3 * * @param boardRequest * @return * @throws Exception */ public BoardResponse getBoardDetail(BoardRequest boardRequest) throws Exception { BoardResponse response = null; /* [0]. QSP API (url + param) Setting */ String url = QSP_API_URL + "/api/board/detail"; String apiUrl = UriComponentsBuilder.fromHttpUrl(url) .queryParam("noticeNo", boardRequest.getNoticeNo()).build().toUriString(); /* [1]. QSP API CALL -> Response */ String strResponse = interfaceQsp.callApi(HttpMethod.GET, apiUrl, null); if (!"".equals(strResponse)) { ObjectMapper om = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); response = om.readValue(strResponse, BoardResponse.class); } return response; } /** * ※ 게시판 파일 다운로드 QSP -> Q.CAST3 * * @param response * @param encodeFileNo * @throws Exception */ public void getFileDownload(HttpServletResponse response, String encodeFileNo) throws Exception { /* [0]. QSP API (url + fileNo) Setting */ String url = QSP_API_URL + "/api/file/downloadFile" + "?encodeFileNo=" + encodeFileNo; /* [1]. QSP API CALL -> Response */ String strResponse = interfaceQsp.callApi(HttpMethod.GET, url, null); if (!"".equals(strResponse)) { /* [2]. API 응답 파일 처리 */ response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;"); InputStream inputStream = new ByteArrayInputStream(strResponse.getBytes()); StreamUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); } } }