Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
corpus-web-services
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lange, Dr. Herbert
corpus-web-services
Commits
06601304
Commit
06601304
authored
3 years ago
by
Lange, Dr. Herbert
Browse files
Options
Downloads
Patches
Plain Diff
change to listcorpusfunctions to return proper json
parent
2fb78edb
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/de/uni_hamburg/corpora/server/ListCorpusFunctions.java
+75
-32
75 additions, 32 deletions
...va/de/uni_hamburg/corpora/server/ListCorpusFunctions.java
with
75 additions
and
32 deletions
src/main/java/de/uni_hamburg/corpora/server/ListCorpusFunctions.java
+
75
−
32
View file @
06601304
...
...
@@ -11,34 +11,89 @@ import jakarta.ws.rs.Produces;
import
jakarta.ws.rs.core.MediaType
;
import
jakarta.ws.rs.core.Response
;
import
java.lang.reflect.InvocationTargetException
;
import
java.util.ArrayList
;
import
java.util.Comparator
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.*
;
import
java.util.stream.Collectors
;
/**
* @author bba1792 Dr. Herbert Lange
* @version 2021
0705
* @version 2021
1004
* Resource to list corpus functions defined in the corpus services
* Scope: any
*/
@Path
(
"list_corpus_functions"
)
public
class
ListCorpusFunctions
{
/**
* Class representing the relevant information for a corpus function, used to (de)serialize JSON
*/
public
static
class
CorpusFunctionInfo
{
String
name
;
String
description
;
boolean
available
;
Set
<
String
>
usableFor
;
public
CorpusFunctionInfo
(
String
name
,
String
description
,
boolean
available
,
Set
<
String
>
usableFor
)
{
this
.
name
=
name
;
this
.
description
=
description
;
this
.
available
=
available
;
this
.
usableFor
=
usableFor
;
}
public
String
getName
()
{
return
name
;
}
public
String
getDescription
()
{
return
description
;
}
public
boolean
isAvailable
()
{
return
available
;
}
public
Set
<
String
>
getUsableFor
()
{
return
usableFor
;
}
}
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "
text/plai
n" media type.
* to the client as "
application/jso
n" media type.
*
* @return
String that will be returned as a text/plain response.
* @return
JSON response containing the list of corpus functions or HTTP error code 400
*/
@GET
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
Response
listFunctions
()
{
ArrayList
<
Map
<
String
,
String
>>
functionList
=
new
ArrayList
<>()
;
public
Response
getCorpusFunctions
()
{
// Convert to JSON
ArrayList
<
CorpusFunctionInfo
>
functionList
=
listFunctions
();
ObjectMapper
mapper
=
new
ObjectMapper
();
try
{
String
json
=
mapper
.
writeValueAsString
(
functionList
);
return
Response
.
ok
().
entity
(
json
).
build
()
;
}
// On error return empty JSON and an error code
catch
(
JsonProcessingException
e
)
{
return
Response
.
status
(
400
).
entity
(
new
JsonObject
().
toString
()).
build
();
}
}
/**
* Gets the list of all corpus functions available in corpus services
*
* @return list of CorpusFunctionInfo objects representing the functions
*/
public
ArrayList
<
CorpusFunctionInfo
>
listFunctions
()
{
ArrayList
<
CorpusFunctionInfo
>
functionList
=
new
ArrayList
<>();
for
(
String
s
:
CorpusServices
.
getCorpusFunctions
())
{
String
function
,
description
;
Set
<
String
>
usableClasses
=
new
HashSet
<>();
boolean
available
;
try
{
// Create the corpus function object using reflections and get its information
CorpusFunction
cf
=
(
CorpusFunction
)
Class
.
forName
(
s
).
getDeclaredConstructor
().
newInstance
();
function
=
cf
.
getFunction
();
usableClasses
.
addAll
(
cf
.
getIsUsableFor
().
stream
().
map
(
Class:
:
getSimpleName
).
collect
(
Collectors
.
toSet
()));
try
{
description
=
cf
.
getDescription
();
available
=
true
;
...
...
@@ -51,24 +106,12 @@ public class ListCorpusFunctions {
description
=
"Error loading class"
;
available
=
false
;
}
HashMap
<
String
,
String
>
functionInfo
=
new
HashMap
<>();
functionInfo
.
put
(
"name"
,
function
);
functionInfo
.
put
(
"description"
,
description
);
functionInfo
.
put
(
"available"
,
Boolean
.
toString
(
available
));
// Create new info object and add it to the list
CorpusFunctionInfo
functionInfo
=
new
CorpusFunctionInfo
(
function
,
description
,
available
,
usableClasses
);
functionList
.
add
(
functionInfo
);
}
// Sort the list by name
functionList
.
sort
(
Comparator
.
comparing
((
x
)
->
x
.
get
(
"name"
)));
// Convert to JSON
ObjectMapper
mapper
=
new
ObjectMapper
();
try
{
String
json
=
mapper
.
writeValueAsString
(
functionList
);
return
Response
.
ok
().
entity
(
json
).
build
()
;
}
// On error return empty JSON and an error code
catch
(
JsonProcessingException
e
)
{
return
Response
.
status
(
400
).
entity
(
new
JsonObject
().
toString
()).
build
();
}
functionList
.
sort
(
Comparator
.
comparing
(
CorpusFunctionInfo:
:
getName
));
return
functionList
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment