Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Genesys PGR
Genesys Backend
Commits
69733a09
Commit
69733a09
authored
Mar 12, 2022
by
Matija Obreza
Browse files
Perf: Fixed slow response to /api/v1/crops by disabling serialization of Permissions
parent
4af78d6a
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/genesys2/server/api/v1/CropsController.java
View file @
69733a09
...
...
@@ -22,6 +22,7 @@ import java.util.concurrent.ExecutionException;
import
java.util.concurrent.TimeUnit
;
import
org.genesys.blocks.model.JsonViews
;
import
org.genesys.blocks.security.serialization.Permissions
;
import
org.genesys.filerepository.InvalidRepositoryPathException
;
import
org.genesys2.server.api.ApiBaseController
;
import
org.genesys2.server.api.ModelValidationException
;
...
...
@@ -47,6 +48,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.fasterxml.jackson.annotation.JsonIgnoreType
;
import
com.fasterxml.jackson.annotation.JsonView
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
...
...
@@ -55,8 +57,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import
com.fasterxml.jackson.databind.SerializationFeature
;
import
com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module
;
import
com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module.Feature
;
import
com.google.common.cache.Cache
;
import
com.google.common.cache.CacheBuilder
;
import
io.swagger.annotations.Api
;
import
net.sf.oval.ConstraintViolation
;
...
...
@@ -71,34 +71,38 @@ public class CropsController extends ApiBaseController {
@Autowired
private
CropService
cropService
;
@Autowired
private
ThreadPoolTaskExecutor
threadPoolTaskExecutor
;
private
static
ObjectMapper
objectMapper
;
private
static
ObjectMapper
noPermissionsMapper
;
@JsonIgnoreType
public
static
final
class
MixinIgnoreType
{}
static
{
object
Mapper
=
new
ObjectMapper
();
noPermissions
Mapper
=
new
ObjectMapper
();
Hibernate5Module
hibernateModule
=
new
Hibernate5Module
();
hibernateModule
.
disable
(
Feature
.
FORCE_LAZY_LOADING
);
hibernateModule
.
disable
(
Feature
.
SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS
);
object
Mapper
.
registerModule
(
hibernateModule
);
noPermissions
Mapper
.
registerModule
(
hibernateModule
);
// serialization
object
Mapper
.
disable
(
SerializationFeature
.
EAGER_SERIALIZER_FETCH
);
noPermissions
Mapper
.
disable
(
SerializationFeature
.
EAGER_SERIALIZER_FETCH
);
// deserialization
object
Mapper
.
enable
(
DeserializationFeature
.
ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
);
noPermissions
Mapper
.
enable
(
DeserializationFeature
.
ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
);
// // Never ignore stuff we don't understand
// mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// ignore stuff we don't understand
object
Mapper
.
disable
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
);
noPermissions
Mapper
.
disable
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
);
// explicit json views: every fields needs to be annotated, therefore enabled
object
Mapper
.
enable
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
);
noPermissions
Mapper
.
enable
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
);
// enable upgrading to arrays
objectMapper
.
enable
(
DeserializationFeature
.
ACCEPT_SINGLE_VALUE_AS_ARRAY
);
noPermissionsMapper
.
enable
(
DeserializationFeature
.
ACCEPT_SINGLE_VALUE_AS_ARRAY
);
// Don't do permission checks
noPermissionsMapper
.
addMixIn
(
Permissions
.
class
,
MixinIgnoreType
.
class
);
}
private
final
Cache
<
String
,
String
>
cacheCropDetails
=
CacheBuilder
.
newBuilder
().
maximumSize
(
5
).
expireAfterWrite
(
5
,
TimeUnit
.
MINUTES
).
build
();
/**
* List of Crop details
...
...
@@ -109,16 +113,12 @@ public class CropsController extends ApiBaseController {
*/
@GetMapping
(
value
=
""
)
public
ResponseEntity
<
String
>
listCropDetails
()
throws
JsonProcessingException
,
ExecutionException
{
var
cropDetails
=
cacheCropDetails
.
get
(
"api-v1-crops"
,
()
->
{
LOG
.
info
(
"Listing crop details"
);
List
<
CropDetails
>
crops
=
cropService
.
listDetails
(
LocaleContextHolder
.
getLocale
());
LOG
.
debug
(
"Got crops"
);
String
json
=
objectMapper
.
writeValueAsString
(
crops
);
LOG
.
debug
(
"Serialized to JSON"
);
return
json
;
});
return
ResponseEntity
.
ok
().
cacheControl
(
CacheControl
.
maxAge
(
5
,
TimeUnit
.
MINUTES
)).
body
(
cropDetails
);
LOG
.
info
(
"Listing crop details"
);
List
<
CropDetails
>
crops
=
cropService
.
listDetails
(
LocaleContextHolder
.
getLocale
());
LOG
.
debug
(
"Got crops"
);
String
json
=
noPermissionsMapper
.
writeValueAsString
(
crops
);
LOG
.
debug
(
"Serialized to JSON"
);
return
ResponseEntity
.
ok
().
cacheControl
(
CacheControl
.
maxAge
(
5
,
TimeUnit
.
MINUTES
)).
body
(
json
);
}
/**
...
...
src/main/resources/log4j2.xml
View file @
69733a09
...
...
@@ -19,7 +19,7 @@
<Configuration>
<Appenders>
<Console
name=
"console"
>
<PatternLayout
pattern=
"%d{
ABSOLUTE
} %t %5p %c{1}:%L - %m%n"
/>
<PatternLayout
pattern=
"%d{
DEFAULT
} %t %5p %c{1}:%L - %m%n"
/>
</Console>
</Appenders>
<Loggers>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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