diff --git a/src/main/java/cokr/xit/adds/cmm/model/ApiUtil.java b/src/main/java/cokr/xit/adds/cmm/model/ApiUtil.java index 0089c39..2bf3181 100644 --- a/src/main/java/cokr/xit/adds/cmm/model/ApiUtil.java +++ b/src/main/java/cokr/xit/adds/cmm/model/ApiUtil.java @@ -6,7 +6,6 @@ import java.util.List; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.json.simple.JSONObject; -import org.springframework.http.HttpHeaders; import com.fasterxml.jackson.core.type.TypeReference; @@ -45,21 +44,6 @@ public class ApiUtil { return rslt.body(); } - /** - * irosApi 호출 - x-www-form-urlencoded 방식 - * @param uri String - * @param param String - * @return String - */ - public static String callIrosApi(String uri, String param) { - HttpResponse rslt = new WebClient().get(request -> { - request.header(HttpHeaders.CONTENT_TYPE, "application/json"); - request.contentType(WebClient.Request.ContentType.FORM); - request.uri(uri + param); - }); - return rslt.body(); - } - /** * Object -> data로 변환 * @param request WebClient.Request diff --git a/src/main/java/cokr/xit/adds/nims/web/AddsNimsController.java b/src/main/java/cokr/xit/adds/nims/web/AddsNimsController.java index 0929b01..1cf71c6 100644 --- a/src/main/java/cokr/xit/adds/nims/web/AddsNimsController.java +++ b/src/main/java/cokr/xit/adds/nims/web/AddsNimsController.java @@ -1,12 +1,19 @@ package cokr.xit.adds.nims.web; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -19,6 +26,7 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import com.fasterxml.jackson.core.type.TypeReference; @@ -79,6 +87,12 @@ public class AddsNimsController extends ApplicationController { @Value("${app.inf.nims.api.dsuseMgtListForRptDoc}") private String dsuseMgtListForRptDocEndpoint; + @Value("${app.inf.nims.api.saveDsuseMgtRslt}") + private String saveDsuseMgtRsltEndpoint; + + @Value("${app.inf.nims.api.saveDsuseMgtReceipt}") + private String saveDsuseMgtReceiptEndpoint; + @Resource(name="sigunguDepartmentService") private SigunguDepartmentService sggDeptService; @@ -148,9 +162,7 @@ public class AddsNimsController extends ApplicationController { } mav.addObject("fileInfo", toJson(fileInfo)); - return mav - .addObject("pageName", "dsuseMgtReceiptInfo") // View(jsp)에서 사용할 id 뒤에 붙일 suffix - .addObject("apiHost", apiHost); + return mav.addObject("pageName", "dsuseMgtReceiptInfo"); // View(jsp)에서 사용할 id 뒤에 붙일 suffix } @RequestMapping(value = "/removeDsuseMgt.do", name="신청서 접수 목록 조회") @@ -224,8 +236,7 @@ public class AddsNimsController extends ApplicationController { .addObject("pageName", "dsuseMgtRsltInfo") // View(jsp)에서 사용할 id 뒤에 붙일 suffix .addObject("infoPrefix", "dsuseMgtRsltInfo") // prefix .addObject("infoPrefixUrl", "/adds/nims") // prefixUrl - .addObject("dsuseRsltInfo", toJson(dsuseRsltInfo)) // View(jsp)에서 사용할 id 뒤에 붙일 suffix - .addObject("apiHost", apiHost); + .addObject("dsuseRsltInfo", toJson(dsuseRsltInfo)); // View(jsp)에서 사용할 id 뒤에 붙일 suffix } @RequestMapping(value = "/getDsuseMgtRsltImgDscdmngList.do", name="결과[통보] 처리 등록 이미지 폐기관리 목록 조회") @@ -381,7 +392,6 @@ public class AddsNimsController extends ApplicationController { mav.addObject("DEPTList", deptList); mav.addObject("pageName", "dsuseMgtDoc"); - mav.addObject("apiHost", apiHost); return mav; } //------------------------------------------------------------------------------------------------- @@ -455,6 +465,64 @@ public class AddsNimsController extends ApplicationController { return json.parse(rslt, new TypeReference<>() {}); } + + @RequestMapping(value = "/saveDsuseMgtReceipt.do", name="폐기신청서 접수", produces = "application/json;charset=UTF-8") + public @ResponseBody ApiBaseResponse saveDsuseMgtReceipt( + @RequestParam("dsuseMgtJsonStr") String dsuseMgtJsonStr, + @RequestParam("dsuseMgtDtlsJsonStr") String dsuseMgtDtlsJsonStr, + @RequestParam(value = "dsusePrdImgFiles", required = false) List dsusePrdImgFiles) { + + String boundary = UUID.randomUUID().toString(); + try { + HttpRequest.BodyPublisher bodyPublisher = buildMultipartBodyReceipt(dsuseMgtJsonStr, dsuseMgtDtlsJsonStr, + dsusePrdImgFiles, boundary); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(apiHost + saveDsuseMgtReceiptEndpoint)) + .header("Content-Type", "multipart/form-data; boundary=" + boundary) + .POST(bodyPublisher) + .build(); + + HttpResponse apiRslt = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandlers.ofString()); + + String rslt = apiRslt.body(); + return json.parse(rslt, new TypeReference<>() {}); + }catch (Exception e){ + + } + return null; + } + + + @RequestMapping(value = "/saveDsuseMgtRslt.do", name="폐기결과[통보] 처리", produces = "application/json;charset=UTF-8") + public @ResponseBody ApiBaseResponse saveDsuseMgtRslt( + @RequestParam("dsuseMgtRsltJsonStr") String dsuseMgtRsltJsonStr, + @RequestParam(value = "dsuseDsuseImgFiles", required = false) List dsuseDsuseImgFiles, + @RequestParam("fileChangeYn") String fileChangeYn) { + + String boundary = UUID.randomUUID().toString(); + try { + HttpRequest.BodyPublisher bodyPublisher = buildMultipartBody(dsuseMgtRsltJsonStr, dsuseDsuseImgFiles, + fileChangeYn, boundary); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(apiHost + saveDsuseMgtRsltEndpoint)) + .header("Content-Type", "multipart/form-data; boundary=" + boundary) + .POST(bodyPublisher) + .build(); + + HttpResponse apiRslt = HttpClient.newHttpClient() + .send(request, HttpResponse.BodyHandlers.ofString()); + + String rslt = apiRslt.body(); + return json.parse(rslt, new TypeReference<>() {}); + }catch (Exception e){ + + } + return null; + } + //------------------------------------------------------------------------------------------------- // API server call //------------------------------------------------------------------------------------------------- @@ -644,6 +712,70 @@ public class AddsNimsController extends ApplicationController { } return partitions; } + + private HttpRequest.BodyPublisher buildMultipartBody(String dsuseMgtRsltJsonStr, List files, String fileChangeYn, String boundary) throws IOException { + var byteOut = new ByteArrayOutputStream(); + String lineSeparator = "\r\n"; + + // JSON part + byteOut.write(("--" + boundary + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Disposition: form-data; name=\"dsuseMgtRsltJsonStr\"" + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Type: application/json; charset=UTF-8" + lineSeparator + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write((dsuseMgtRsltJsonStr + lineSeparator).getBytes(StandardCharsets.UTF_8)); + + // File parts + if (files != null) { + for (MultipartFile file : files) { + byteOut.write(("--" + boundary + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Disposition: form-data; name=\"dsuseDsuseImgFiles\"; filename=\"" + file.getOriginalFilename() + "\"" + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Type: " + file.getContentType() + lineSeparator + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(file.getBytes()); + byteOut.write((lineSeparator).getBytes(StandardCharsets.UTF_8)); + } + } + + // fileChangeYn part + byteOut.write(("--" + boundary + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Disposition: form-data; name=\"fileChangeYn\"" + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Type: text/plain; charset=UTF-8" + lineSeparator + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write((fileChangeYn + lineSeparator).getBytes(StandardCharsets.UTF_8)); + + byteOut.write(("--" + boundary + "--" + lineSeparator).getBytes(StandardCharsets.UTF_8)); + + return HttpRequest.BodyPublishers.ofByteArray(byteOut.toByteArray()); + } + + private HttpRequest.BodyPublisher buildMultipartBodyReceipt(String dsuseMgtJsonStr, String dsuseMgtDtlsJsonStr, List files, String boundary) throws IOException { + var byteOut = new ByteArrayOutputStream(); + String lineSeparator = "\r\n"; + + // JSON part + byteOut.write(("--" + boundary + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Disposition: form-data; name=\"dsuseMgtJsonStr\"" + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Type: application/json; charset=UTF-8" + lineSeparator + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write((dsuseMgtJsonStr + lineSeparator).getBytes(StandardCharsets.UTF_8)); + + // File parts + if (files != null) { + for (MultipartFile file : files) { + byteOut.write(("--" + boundary + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Disposition: form-data; name=\"dsusePrdImgFiles\"; filename=\"" + file.getOriginalFilename() + "\"" + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Type: " + file.getContentType() + lineSeparator + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(file.getBytes()); + byteOut.write((lineSeparator).getBytes(StandardCharsets.UTF_8)); + } + } + + // fileChangeYn part + byteOut.write(("--" + boundary + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Disposition: form-data; name=\"dsuseMgtDtlsJsonStr\"" + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write(("Content-Type: text/plain; charset=UTF-8" + lineSeparator + lineSeparator).getBytes(StandardCharsets.UTF_8)); + byteOut.write((dsuseMgtDtlsJsonStr + lineSeparator).getBytes(StandardCharsets.UTF_8)); + + byteOut.write(("--" + boundary + "--" + lineSeparator).getBytes(StandardCharsets.UTF_8)); + + return HttpRequest.BodyPublishers.ofByteArray(byteOut.toByteArray()); + } //------------------------------------------------------------------------------------------------- // private method //------------------------------------------------------------------------------------------------- diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 04c67b1..0982691 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -70,6 +70,10 @@ app: prdMnfSeqInfoOfBarcode: /api/biz/nims/v1/getPrdMnfSeqInfoOfBarcode # 보고문서 정보 목록 조회 dsuseMgtListForRptDoc: /api/biz/nims/v1/getDsuseMgtListForRptDoc + # 폐기신청서 접수 + saveDsuseMgtReceipt: /api/biz/nims/v1/saveDsuseMgtReceipt + # 폐기결과[통보] 처리 + saveDsuseMgtRslt: /api/biz/nims/v1/saveDsuseMgtRslt logging: file: diff --git a/src/main/webapp/WEB-INF/jsp/adds/nims/dsuseMgtReceipt-info.jsp b/src/main/webapp/WEB-INF/jsp/adds/nims/dsuseMgtReceipt-info.jsp index 399d26a..f38fca0 100644 --- a/src/main/webapp/WEB-INF/jsp/adds/nims/dsuseMgtReceipt-info.jsp +++ b/src/main/webapp/WEB-INF/jsp/adds/nims/dsuseMgtReceipt-info.jsp @@ -357,7 +357,7 @@ } upload({ - url: '' + url: wctx.url("/adds/nims/saveDsuseMgtReceipt.do") , data: { dsuseMgtJsonStr: JSON.stringify(saveBsshData), dsuseMgtDtlsJsonStr: JSON.stringify(datasetListPrd), diff --git a/src/main/webapp/WEB-INF/jsp/adds/nims/dsuseMgtRslt-info.jsp b/src/main/webapp/WEB-INF/jsp/adds/nims/dsuseMgtRslt-info.jsp index 7af861b..3f0bae3 100644 --- a/src/main/webapp/WEB-INF/jsp/adds/nims/dsuseMgtRslt-info.jsp +++ b/src/main/webapp/WEB-INF/jsp/adds/nims/dsuseMgtRslt-info.jsp @@ -341,7 +341,7 @@ , init: () => setDialogZindex() , onOK: () => { upload({ - url: '' + url: wctx.url("/adds/nims/saveDsuseMgtRslt.do") , data: { fileChangeYn: fileChangeYn, dsuseMgtRsltJsonStr: JSON.stringify(saveData),