Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
minerva
core
Commits
e88998e5
Commit
e88998e5
authored
Apr 01, 2020
by
Piotr Gawron
Browse files
API allows to merge files
parent
7f20994d
Pipeline
#24112
passed with stage
in 53 minutes and 11 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
CHANGELOG
View file @
e88998e5
minerva
(
15.0.0
~
beta
.2
)
stable
;
urgency
=
medium
*
Improvement
:
API
allows
to
merge
files
(#
1208
)
*
Small
Improvement
:
SBO
annotation
support
added
(#
1194
)
*
Small
Improvement
:
additional
annotation
uri
qualifiers
added
:
hasProperty
,
isPropertyOf
(#
1193
)
...
...
rest-api/src/main/java/lcsb/mapviewer/api/convert/ConvertController.java
View file @
e88998e5
...
...
@@ -55,6 +55,19 @@ public class ConvertController extends BaseController {
.
body
(
os
.
toByteArray
());
}
@PostMapping
(
value
=
"/merge/{fromFormat}:{toFormat}"
)
public
@ResponseBody
ResponseEntity
<
byte
[]>
mergeFilesToFile
(
@PathVariable
(
value
=
"fromFormat"
)
String
fromFormat
,
@PathVariable
(
value
=
"toFormat"
)
String
toFormat
,
@RequestBody
byte
[]
body
)
throws
Exception
{
byte
[]
bytea
=
convertController
.
mergeFiles
(
fromFormat
,
toFormat
,
body
);
return
ResponseEntity
.
ok
().
contentLength
(
bytea
.
length
)
.
contentType
(
MediaType
.
APPLICATION_OCTET_STREAM
)
.
header
(
"Content-Disposition"
,
"attachment; filename=model"
+
toFormat
)
.
body
(
bytea
);
}
@RequestMapping
(
value
=
"/"
,
method
=
{
RequestMethod
.
GET
,
RequestMethod
.
POST
})
public
Map
<
String
,
Object
>
getInformation
()
{
return
convertController
.
getInformation
();
...
...
rest-api/src/main/java/lcsb/mapviewer/api/convert/ConvertRestImpl.java
View file @
e88998e5
...
...
@@ -5,6 +5,8 @@ import java.lang.reflect.Constructor;
import
java.lang.reflect.InvocationTargetException
;
import
java.nio.charset.StandardCharsets
;
import
java.util.*
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipInputStream
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.logging.log4j.LogManager
;
...
...
@@ -15,6 +17,8 @@ import org.springframework.transaction.annotation.Transactional;
import
lcsb.mapviewer.api.BaseRestImpl
;
import
lcsb.mapviewer.api.QueryException
;
import
lcsb.mapviewer.commands.CommandExecutionException
;
import
lcsb.mapviewer.commands.MergeCommand
;
import
lcsb.mapviewer.common.Pair
;
import
lcsb.mapviewer.common.exception.InvalidStateException
;
import
lcsb.mapviewer.converter.*
;
...
...
@@ -60,6 +64,58 @@ public class ConvertRestImpl extends BaseRestImpl {
}
}
public
byte
[]
mergeFiles
(
String
fromFormat
,
String
toFormat
,
byte
[]
input
)
throws
IOException
,
QueryException
,
CommandExecutionException
{
try
{
ZipInputStream
zis
=
new
ZipInputStream
(
new
ByteArrayInputStream
(
input
));
ZipEntry
zipEntry
=
zis
.
getNextEntry
();
List
<
Pair
<
String
,
Model
>>
modelsWithFilename
=
new
ArrayList
<>();
while
(
zipEntry
!=
null
)
{
String
filename
=
zipEntry
.
getName
();
if
(!
zipEntry
.
isDirectory
()
&&
filename
.
toLowerCase
().
startsWith
(
"maps/"
))
{
Model
model
=
getModelParserByNameOrClass
(
fromFormat
)
.
createModel
(
new
ConverterParams
().
inputStream
(
convertZipInputStreamToInputStream
(
zis
,
zipEntry
,
"UTF-8"
),
filename
));
modelsWithFilename
.
add
(
new
Pair
<>(
filename
,
model
));
}
zipEntry
=
zis
.
getNextEntry
();
}
zis
.
closeEntry
();
zis
.
close
();
modelsWithFilename
.
sort
(
new
Comparator
<
Pair
<
String
,
Model
>>()
{
@Override
public
int
compare
(
Pair
<
String
,
Model
>
o1
,
Pair
<
String
,
Model
>
o2
)
{
return
o1
.
getLeft
().
compareTo
(
o2
.
getLeft
());
}
});
List
<
Model
>
models
=
new
ArrayList
<>();
for
(
Pair
<
String
,
Model
>
pair
:
modelsWithFilename
)
{
models
.
add
(
pair
.
getRight
());
}
Model
result
=
new
MergeCommand
(
models
).
execute
();
Converter
exporter
=
getModelParserByNameOrClass
(
toFormat
);
return
IOUtils
.
toByteArray
(
exporter
.
model2InputStream
(
result
));
}
catch
(
InvalidInputDataExecption
|
ConverterException
|
InconsistentModelException
e
)
{
throw
new
QueryException
(
"Input file is invalid"
,
e
);
}
}
private
InputStream
convertZipInputStreamToInputStream
(
ZipInputStream
in
,
ZipEntry
entry
,
String
encoding
)
throws
IOException
{
final
int
BUFFER
=
2048
;
int
count
=
0
;
byte
data
[]
=
new
byte
[
BUFFER
];
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
while
((
count
=
in
.
read
(
data
))
!=
-
1
)
{
out
.
write
(
data
,
0
,
count
);
}
return
new
ByteArrayInputStream
(
out
.
toByteArray
());
}
public
Map
<
String
,
Object
>
getInformation
()
{
Map
<
String
,
Object
>
info
=
new
LinkedHashMap
<>();
...
...
web/src/test/java/lcsb/mapviewer/web/ConvertControllerIntegrationTest.java
View file @
e88998e5
package
lcsb.mapviewer.web
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
post
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
import
java.io.ByteArrayInputStream
;
import
java.io.File
;
import
java.nio.file.Files
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.web.servlet.RequestBuilder
;
import
lcsb.mapviewer.converter.ConverterParams
;
import
lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser
;
import
lcsb.mapviewer.model.map.model.Model
;
import
lcsb.mapviewer.model.map.species.Element
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
public
class
ConvertControllerIntegrationTest
extends
ControllerIntegrationTest
{
...
...
@@ -34,4 +44,37 @@ public class ConvertControllerIntegrationTest extends ControllerIntegrationTest
.
andExpect
(
status
().
isBadRequest
());
}
@Test
public
void
testMergeCellDesignerMaps
()
throws
Exception
{
byte
[]
body
=
Files
.
readAllBytes
(
new
File
(
"src/test/resources/convert/cd-maps.zip"
).
toPath
());
RequestBuilder
request
=
post
(
"/convert/merge/CellDesigner_SBML:CellDesigner_SBML"
)
.
content
(
body
);
byte
[]
content
=
mockMvc
.
perform
(
request
)
.
andExpect
(
status
().
is2xxSuccessful
())
.
andReturn
().
getResponse
().
getContentAsByteArray
();
CellDesignerXmlParser
parser
=
new
CellDesignerXmlParser
();
Model
model
=
parser
.
createModel
(
new
ConverterParams
().
inputStream
(
new
ByteArrayInputStream
(
content
)));
int
s1
=
0
;
int
s2
=
0
;
int
s3
=
0
;
for
(
Element
element
:
model
.
getElements
())
{
if
(
element
.
getName
().
equals
(
"s1"
))
{
s1
++;
}
if
(
element
.
getName
().
equals
(
"s2"
))
{
s2
++;
}
if
(
element
.
getName
().
equals
(
"s3"
))
{
s3
++;
}
}
assertEquals
(
1
,
s1
);
assertEquals
(
2
,
s2
);
assertEquals
(
1
,
s3
);
}
}
web/src/test/resources/convert/cd-maps.zip
0 → 100644
View file @
e88998e5
File added
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