package com.ford.ierp.epayableseuservice.serviceimpl; |
|
|
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| import java.io.File; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.text.SimpleDateFormat; |
| import java.util.Date; |
| import java.util.List; |
| import java.util.Optional; |
| import java.util.zip.ZipEntry; |
| import java.util.zip.ZipInputStream; |
| import java.util.zip.ZipOutputStream; |
|
|
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.beans.factory.annotation.Qualifier; |
| import org.springframework.beans.factory.annotation.Value; |
| import org.springframework.http.HttpHeaders; |
| import org.springframework.http.HttpStatus; |
| import org.springframework.http.ResponseEntity; |
| import org.springframework.stereotype.Service; |
| import org.springframework.web.reactive.function.client.WebClient; |
| import org.springframework.web.util.UriComponentsBuilder; |
|
|
| import com.amazonaws.AmazonServiceException; |
| import com.amazonaws.HttpMethod; |
| import com.amazonaws.SdkClientException; |
| import com.amazonaws.services.s3.AmazonS3; |
| import com.amazonaws.services.s3.model.CreateBucketRequest; |
| import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; |
| import com.amazonaws.services.s3.model.PutObjectRequest; |
| import com.ford.ierp.epayableseuservice.configuration.AwsS3Config; |
| //import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| import com.ford.ierp.epayableseuservice.service.SignedUrlService; |
|
|
| import lombok.extern.slf4j.Slf4j; |
| @Service |
| @Slf4j |
| public class SignedUrlServiceImpl implements SignedUrlService { |
| |
| @Autowired |
| @Qualifier("Epayables") |
| WebClient webClient; |
| |
| @Value("${s4.pdf.download.url}") |
| private String s4PdfDownloadUrl; |
| |
| @Value("${s3.bucket}") |
| private String bucketName; |
| |
| @Autowired |
| @Qualifier("AmazonS3") |
| AmazonS3 s3Client; |
| |
| @Autowired |
| AwsS3Config awsS3Config; |
| |
| public AmazonS3 getAmazonS3() { |
| return awsS3Config.getAmazonS3(); |
| } |
|
|
| @Override |
| public ResponseEntity<Object> getSignedUrl(String documentId) throws IOException { |
| |
| String s4Url = s4PdfDownloadUrl; |
| |
| String fileName = null; |
| |
| String signedUrl = null; |
| |
| if(documentId.contains(",")) { |
| |
| documentId = documentId.replace("," , "|"); |
| } |
| |
| @SuppressWarnings("deprecation") |
| ResponseEntity<byte[]> response = webClient.get().uri(UriComponentsBuilder.fromHttpUrl(s4Url).build(documentId)).exchange() |
| .flatMap(result -> result.toEntity(byte[].class)).block(); |
| |
| log.info("S4 URL : {}", s4Url); |
| |
| if(response.getStatusCode().is2xxSuccessful()) { |
| |
| //Getting the name of the file |
| HttpHeaders responseHeaders = response.getHeaders(); |
| Optional<List<String>> list = Optional.ofNullable(responseHeaders.get("content-disposition")); |
| if (list.isPresent()) { |
| Optional<String> token = Optional.ofNullable(list.get().get(0)); |
| if (token.isPresent()) { |
| fileName = token.get(); |
| } |
| } |
| |
| fileName = fileName.substring(18,fileName.length()-1); |
| |
| byte[] arr = response.getBody(); |
| ByteArrayInputStream bais = new ByteArrayInputStream(arr); |
| ZipInputStream zis = new ZipInputStream(bais); |
| |
| String path = "BOOT-INF/classes/temp"; |
| File file1 = new File(path); |
| file1.mkdir(); |
| String filePath = path + fileName; |
| FileOutputStream fos = new FileOutputStream(filePath); |
| ZipOutputStream zos = new ZipOutputStream(fos); |
| ZipEntry zipEntry; |
| |
| while ((zipEntry = zis.getNextEntry()) != null) { |
| ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| byte[] buffer = new byte[2048]; |
| int len; |
| while ((len = zis.read(buffer)) > 0) { |
| bos.write(buffer, 0, len); |
| } |
| bos.close(); |
| byte[] entries = bos.toByteArray(); |
| ZipEntry newentry = new ZipEntry(zipEntry.getName()); |
| System.out.println(newentry.getName()); |
| zos.putNextEntry(newentry); |
| zos.write(entries); |
| zos.closeEntry(); |
| } |
| zos.close(); |
| zis.close(); |
| |
| try { |
| |
| if (!s3Client.doesBucketExistV2(bucketName)) { |
| |
| // Because the CreateBucketRequest object doesn't specify a region, the |
| // bucket is created in the region specified in the client. |
| s3Client.createBucket(new CreateBucketRequest(bucketName)); |
| } |
| } catch (AmazonServiceException e) { |
| // The call was transmitted successfully, but Amazon S3 couldn't process |
| // it and returned an error response. |
| e.printStackTrace(); |
| } catch (SdkClientException e) { |
| // Amazon S3 couldn't be contacted for a response, or the client |
| // couldn't parse the response from Amazon S3. |
| e.printStackTrace(); |
| } |
| |
| Date date = new Date(); |
| SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
| String folderName = formatter.format(date); |
| |
| String fileKey = folderName+ "/" + fileName; |
| File file = new File(path + fileName); |
| |
| PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileKey, file); |
| s3Client.putObject(putObjectRequest); |
| |
| file.delete(); |
| |
| // fetching the signed url |
| Date expiration = new Date(); |
| long expTimeMillis = expiration.getTime(); |
| expTimeMillis += 1000 * 30 * 60; |
| expiration.setTime(expTimeMillis); |
| |
| GeneratePresignedUrlRequest generatePresignedUrlRequest = |
| new GeneratePresignedUrlRequest(bucketName, fileKey) |
| .withMethod(HttpMethod.GET); |
| |
| signedUrl = s3Client.generatePresignedUrl(generatePresignedUrlRequest).toString(); |
| System.out.println(signedUrl); |
| |
| return ResponseEntity.ok().body(signedUrl); |
| } |
| |
| else { |
| return ResponseEntity.status(HttpStatus.valueOf(response.getStatusCodeValue())).body(response.getBody()); |
| } |
| } |
| }
|
Comments
Post a Comment