Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
trismegisto-services
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mauro Paolo Josue Zuñiga Mallqui
trismegisto-services
Commits
14feaee5
Commit
14feaee5
authored
Feb 19, 2024
by
Mauro Paolo Josue Zuñiga Mallqui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[EDIT] MEJOR MANEJO DE SHEETS Y MULTI THREADS PARA EL SERVICIO DE EXCEL
parent
a7b24dfd
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
126 additions
and
40 deletions
+126
-40
ExcelApi.java
.../java/web/multitask/trismegistoservices/api/ExcelApi.java
+6
-2
JWTokenApi.java
...ava/web/multitask/trismegistoservices/api/JWTokenApi.java
+27
-19
SecurityConfig.java
.../multitask/trismegistoservices/config/SecurityConfig.java
+1
-0
ExcelService.java
.../multitask/trismegistoservices/services/ExcelService.java
+92
-19
JWTokenUtil.java
.../web/multitask/trismegistoservices/utils/JWTokenUtil.java
+0
-0
No files found.
src/main/java/web/multitask/trismegistoservices/api/ExcelApi.java
View file @
14feaee5
...
...
@@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.RestController;
import
web.multitask.trismegistoservices.services.ExcelService
;
import
web.multitask.trismegistoservices.utils.CommonUtils
;
import
java.io.IOException
;
@RestController
@CrossOrigin
(
origins
=
"*"
)
@RequestMapping
(
"excel"
)
...
...
@@ -23,9 +25,11 @@ public class ExcelApi {
CommonUtils
commonUtils
;
@PostMapping
(
"/public/generate"
)
public
ResponseEntity
<?>
generateExcel
(
@RequestBody
String
json
)
{
public
ResponseEntity
<?>
generateExcel
(
@RequestBody
String
json
)
throws
IOException
{
JSONObject
jsonBody
=
new
JSONObject
(
json
);
byte
[]
excelByte
=
excelService
.
generateExcel
(
jsonBody
);
int
size
=
json
.
getBytes
().
length
;
System
.
out
.
println
(
"Size: "
+
size
);
byte
[]
excelByte
=
excelService
.
generateExcel
(
jsonBody
,
size
);
if
(
jsonBody
.
optBoolean
(
"base64"
,
false
))
{
JSONObject
response
=
new
JSONObject
();
response
.
put
(
"file_name"
,
jsonBody
.
optString
(
"file_name"
,
"no_name.xlsx"
));
...
...
src/main/java/web/multitask/trismegistoservices/api/JWTokenApi.java
View file @
14feaee5
...
...
@@ -47,37 +47,39 @@ class JWTokenApi {
@PostMapping
(
"/validate"
)
public
ResponseEntity
<?>
validateToken
(
@RequestBody
String
token
)
{
JSONObject
response
;
JSONObject
json
=
new
JSONObject
(
token
);
if
(
jwtTokenUtil
.
validateToken
(
json
.
getString
(
"token"
)))
{
JSONObject
response
=
new
JSONObject
();
if
(!
jwtTokenUtil
.
validateToken
(
json
.
getString
(
"token"
)))
{
response
.
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
return
ResponseEntity
.
status
(
401
).
body
(
response
.
toMap
());
}
String
dataToken
=
jwtTokenUtil
.
getDataToken
(
json
.
getString
(
"token"
));
if
(
dataToken
==
null
)
{
response
=
new
JSONObject
().
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
}
else
{
response
.
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
return
ResponseEntity
.
status
(
401
).
body
(
response
.
toMap
());
}
boolean
isTokenExpired
=
jwtTokenUtil
.
isTokenExpired
(
json
.
getString
(
"token"
));
if
(
isTokenExpired
)
{
response
=
new
JSONObject
()
.
put
(
"message"
,
"Expired Token"
).
put
(
"status"
,
false
);
response
.
put
(
"message"
,
"Expired Token"
).
put
(
"status"
,
false
);
return
ResponseEntity
.
status
(
403
).
body
(
response
.
toMap
());
}
try
{
UserDetails
userDetails
=
userRepo
.
findByUsername
(
new
JSONObject
(
dataToken
).
getString
(
"username"
));
if
(
userDetails
.
getUsername
()
==
null
)
{
response
=
new
JSONObject
().
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
}
else
{
response
=
new
JSONObject
().
put
(
"message"
,
"Valid Token"
).
put
(
"status"
,
true
);
if
(
userDetails
==
null
)
{
response
.
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
return
ResponseEntity
.
status
(
401
).
body
(
response
.
toMap
());
}
}
catch
(
Exception
e
)
{
response
=
new
JSONObject
().
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
}
}
}
else
{
response
=
new
JSONObject
().
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
}
if
(
response
.
getBoolean
(
"status"
))
{
return
ResponseEntity
.
ok
(
response
.
toMap
());
}
else
{
response
.
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
return
ResponseEntity
.
status
(
401
).
body
(
response
.
toMap
());
}
response
.
put
(
"message"
,
"Valid Token"
).
put
(
"status"
,
true
);
return
ResponseEntity
.
ok
(
response
.
toMap
());
}
@PostMapping
(
"/service/authenticate"
)
...
...
@@ -96,7 +98,12 @@ class JWTokenApi {
@PostMapping
(
"/remaining"
)
public
ResponseEntity
<?>
remainingTime
(
@RequestBody
String
token
)
{
JSONObject
json
=
new
JSONObject
(
token
);
return
ResponseEntity
.
ok
(
new
JSONObject
().
put
(
"remaining"
,
jwtTokenUtil
.
getExperyTime
(
json
.
getString
(
"token"
))).
put
(
"message"
,
"OK"
).
put
(
"status"
,
true
).
toMap
());
try
{
int
remaining
=
jwtTokenUtil
.
getExperyTime
(
json
.
getString
(
"token"
));
return
ResponseEntity
.
ok
(
new
JSONObject
().
put
(
"remaining"
,
remaining
).
put
(
"message"
,
"OK"
).
put
(
"status"
,
true
).
toMap
());
}
catch
(
Exception
e
)
{
return
ResponseEntity
.
status
(
401
).
body
(
new
JSONObject
().
put
(
"remaining"
,
0
).
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
).
toMap
());
}
}
}
\ No newline at end of file
src/main/java/web/multitask/trismegistoservices/config/SecurityConfig.java
View file @
14feaee5
...
...
@@ -48,6 +48,7 @@ public class SecurityConfig{
.
antMatchers
(
HttpMethod
.
GET
,
"/**"
).
permitAll
()
// .antMatchers(HttpMethod.POST, "/**").permitAll()
.
antMatchers
(
"/token/**"
).
permitAll
());
// .anyRequest()
// .authenticated());
http
.
addFilterBefore
(
new
JWTokenFilter
(
jwtTokenUtil
,
userRepo
),
UsernamePasswordAuthenticationFilter
.
class
);
...
...
src/main/java/web/multitask/trismegistoservices/services/ExcelService.java
View file @
14feaee5
...
...
@@ -2,10 +2,14 @@ package web.multitask.trismegistoservices.services;
import
java.awt.Color
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.concurrent.atomic.AtomicReference
;
import
java.util.stream.IntStream
;
import
org.apache.poi.ss.usermodel.*
;
import
org.apache.poi.xssf.usermodel.XSSFCell
;
import
org.apache.poi.xssf.usermodel.XSSFCellStyle
;
...
...
@@ -24,51 +28,120 @@ import org.springframework.stereotype.Service;
@Service
public
class
ExcelService
{
List
<
String
>
stylesSaved
=
new
ArrayList
<>();
List
<
XSSFCellStyle
>
styleObjectsSaved
=
new
ArrayList
<>();
public
byte
[]
generateExcel
(
JSONObject
json
)
{
public
byte
[]
generateExcel
(
JSONObject
json
,
int
size
)
throws
IOException
{
ByteArrayOutputStream
outByteStream
=
new
ByteArrayOutputStream
();
XSSFWorkbook
workbook
=
new
XSSFWorkbook
();
createSheet
(
workbook
,
json
);
createSheet
(
workbook
,
json
,
size
);
try
{
workbook
.
write
(
outByteStream
);
workbook
.
close
();
return
outByteStream
.
toByteArray
();
}
catch
(
Exception
e
)
{
workbook
.
close
();
System
.
out
.
println
(
e
.
getMessage
());
return
null
;
}
}
@Async
public
void
createSheet
(
XSSFWorkbook
workbook
,
JSONObject
json
)
{
styleObjectsSaved
.
clear
();
stylesSaved
.
clear
();
List
<
CompletableFuture
<
Boolean
>>
results
=
new
ArrayList
<>();
public
void
createSheet
(
XSSFWorkbook
workbook
,
JSONObject
json
,
int
sizeOfBytes
)
{
AtomicReference
<
List
<
CompletableFuture
<
Boolean
>>>
results
=
new
AtomicReference
<>(
new
ArrayList
<>());
if
(
json
.
optJSONArray
(
"sheetArray"
,
new
JSONArray
()).
isEmpty
())
{
XSSFSheet
sheet
=
workbook
.
createSheet
(
json
.
optString
(
"sheet_name"
,
"no_name"
));
buildSheet
(
workbook
,
sheet
,
json
);
}
else
{
for
(
int
i
=
0
;
i
<
json
.
getJSONArray
(
"identifiers"
).
length
();
i
++)
{
sheet
.
setColumnWidth
(
i
,
16
*
512
);
}
}
else
{
JSONArray
sheetArray
=
json
.
optJSONArray
(
"sheetArray"
,
new
JSONArray
());
sheetArray
.
toList
().
stream
().
parallel
().
forEach
(
sheetObject
->
{
HashMap
<
String
,
Object
>
sheetMap
=
(
HashMap
<
String
,
Object
>)
sheetObject
;
JSONObject
jsonObject
=
new
JSONObject
(
sheetMap
);
if
(
sizeOfBytes
<
100000
)
{
for
(
int
i
=
0
;
i
<
sheetArray
.
length
();
i
++)
{
JSONObject
jsonObject
=
sheetArray
.
getJSONObject
(
i
);
XSSFSheet
sheet
=
workbook
.
createSheet
(
jsonObject
.
optString
(
"sheet_name"
,
"no_name"
));
JSONArray
identifiers
=
jsonObject
.
getJSONArray
(
"identifiers"
);
for
(
int
j
=
0
;
j
<
identifiers
.
length
();
j
++)
{
sheet
.
setColumnWidth
(
j
,
16
*
512
);
sheet
.
setColumnWidth
(
j
,
16
*
512
);
}
CompletableFuture
<
Boolean
>
result
=
buildSheet
(
workbook
,
sheet
,
jsonObject
);
results
.
add
(
result
);
buildSheet
(
workbook
,
sheet
,
jsonObject
);
}
}
else
{
XSSFSheet
[]
sheets
=
new
XSSFSheet
[
sheetArray
.
length
()];
for
(
int
i
=
0
;
i
<
sheetArray
.
length
();
i
++)
{
JSONObject
jsonObject
=
sheetArray
.
getJSONObject
(
i
);
sheets
[
i
]
=
workbook
.
createSheet
(
jsonObject
.
optString
(
"sheet_name"
,
"no_name"
));
}
IntStream
.
range
(
0
,
sheetArray
.
length
()).
parallel
().
forEach
(
i
->
{
JSONObject
jsonObject
=
sheetArray
.
getJSONObject
(
i
);
JSONArray
identifiers
=
jsonObject
.
getJSONArray
(
"identifiers"
);
for
(
int
j
=
0
;
j
<
identifiers
.
length
();
j
++)
{
sheets
[
i
].
setColumnWidth
(
j
,
16
*
512
);
}
CompletableFuture
<
Boolean
>
result
=
buildSheetAsync
(
workbook
,
sheets
[
i
],
jsonObject
);
results
.
get
().
add
(
result
);
});
// sheetArray.toList().stream().parallel().forEach(sheetObject -> {
// HashMap<String, Object> sheetMap = (HashMap<String, Object>) sheetObject;
// JSONObject jsonObject = new JSONObject(sheetMap);
// JSONArray identifiers = jsonObject.getJSONArray("identifiers");
// for (int j = 0; j < identifiers.length(); j++) {
// sheets[i].setColumnWidth(j, 16 * 512);
// }
// CompletableFuture<Boolean> result = buildSheetAsync(workbook, sheets[i], jsonObject);
// results.get().add(result);
// });
CompletableFuture
.
allOf
(
results
.
get
().
toArray
(
new
CompletableFuture
[
0
])).
join
();
}
}
}
public
void
buildSheet
(
XSSFWorkbook
workbook
,
XSSFSheet
sheet
,
JSONObject
json
)
{
final
XSSFCellStyle
defaultStyle
=
createCellStyle
(
workbook
,
new
JSONObject
());
List
<
String
>
stylesSaved
=
new
ArrayList
<>();
List
<
XSSFCellStyle
>
styleObjectsSaved
=
new
ArrayList
<>();
XSSFRow
row
;
XSSFCell
cell
;
XSSFCellStyle
cellStyle
;
JSONArray
identifiers
=
json
.
optJSONArray
(
"identifiers"
,
new
JSONArray
());
JSONArray
data
=
json
.
optJSONArray
(
"data"
,
new
JSONArray
());
JSONArray
styles
=
json
.
optJSONArray
(
"styles"
,
new
JSONArray
());
fillStylesArray
(
styles
,
workbook
,
stylesSaved
,
styleObjectsSaved
);
for
(
int
i
=
0
;
i
<
data
.
length
();
i
++)
{
row
=
sheet
.
createRow
(
i
);
JSONObject
row_data
=
data
.
optJSONObject
(
i
);
for
(
int
j
=
0
;
j
<
identifiers
.
length
();
j
++)
{
cell
=
row
.
createCell
(
j
);
JSONArray
styleArray
=
styles
.
optJSONArray
(
i
,
new
JSONArray
());
JSONObject
style
=
styleArray
.
optJSONObject
(
j
,
new
JSONObject
());
if
(
style
.
isEmpty
()){
cell
.
setCellStyle
(
defaultStyle
);
}
else
{
if
(!
stylesSaved
.
contains
(
style
.
toString
())){
stylesSaved
.
add
(
style
.
toString
());
cellStyle
=
createCellStyle
(
workbook
,
style
);
cell
.
setCellStyle
(
cellStyle
);
styleObjectsSaved
.
add
(
cellStyle
);
}
else
{
cell
.
setCellStyle
(
styleObjectsSaved
.
get
(
stylesSaved
.
indexOf
(
style
.
toString
())));
}
}
cell
.
setCellValue
(
row_data
.
optString
(
identifiers
.
optString
(
j
,
""
)));
}
}
}
@Async
public
CompletableFuture
<
Boolean
>
buildSheet
(
XSSFWorkbook
workbook
,
XSSFSheet
sheet
,
JSONObject
json
)
{
public
CompletableFuture
<
Boolean
>
buildSheet
Async
(
XSSFWorkbook
workbook
,
XSSFSheet
sheet
,
JSONObject
json
)
{
final
XSSFCellStyle
defaultStyle
=
createCellStyle
(
workbook
,
new
JSONObject
());
List
<
String
>
stylesSaved
=
new
ArrayList
<>();
List
<
XSSFCellStyle
>
styleObjectsSaved
=
new
ArrayList
<>();
XSSFRow
row
;
XSSFCell
cell
;
...
...
@@ -116,7 +189,7 @@ public class ExcelService {
XSSFCellStyle
style
=
workbook
.
createCellStyle
();
Font
font
=
workbook
.
createFont
();
style
.
setAlignment
(
align
.
equals
(
"center"
)
?
HorizontalAlignment
.
CENTER
:
align
.
equals
(
"left"
)
?
HorizontalAlignment
.
LEFT
:
HorizontalAlignment
.
RIGHT
);
style
.
setWrapText
(
false
);
style
.
setWrapText
(
styleJson
.
optBoolean
(
"wrapText"
,
false
)
);
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
style
.
setFillPattern
(
FillPatternType
.
SOLID_FOREGROUND
);
style
.
setBorderBottom
(
BorderStyle
.
THIN
);
...
...
src/main/java/web/multitask/trismegistoservices/utils/JWTokenUtil.java
View file @
14feaee5
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment