Skip to content

Commit b438ad3

Browse files
committed
Merge branch 'master' into sis-migration
2 parents 88dec0d + e0d3e78 commit b438ad3

File tree

6 files changed

+413
-0
lines changed

6 files changed

+413
-0
lines changed

geotk-xml-wps/src/main/java/org/geotoolkit/wps/converters/WPSConverterRegistry.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private WPSConverterRegistry() {
6969
register(ComplexToRendredImageConverter .getInstance());
7070
register(ComplexToCoverageConverter .getInstance());
7171
register(ComplexToFileConverter .getInstance());
72+
register(ComplexToPathConverter .getInstance());
7273

7374
//ReferenceType -> Object Converters
7475
register(ReferenceToAffineTransformConverter .getInstance());
@@ -77,6 +78,7 @@ private WPSConverterRegistry() {
7778
register(ReferenceToFeatureConverter .getInstance());
7879
register(ReferenceToFeatureTypeConverter .getInstance());
7980
register(ReferenceToFileConverter .getInstance());
81+
register(ReferenceToPathConverter .getInstance());
8082
register(ReferenceToGeometryConverter .getInstance());
8183
register(ReferenceToGridCoverage2DConverter .getInstance());
8284
register(ReferenceToGridCoverageReaderConverter .getInstance());
@@ -98,6 +100,7 @@ private WPSConverterRegistry() {
98100
register(RenderedImageToComplexConverter .getInstance());
99101
register(CoverageToComplexConverter .getInstance());
100102
register(FileToComplexConverter .getInstance());
103+
register(PathToComplexConverter .getInstance());
101104

102105
//Object -> Reference converters
103106
register(CoverageToReferenceConverter .getInstance());
@@ -111,6 +114,7 @@ private WPSConverterRegistry() {
111114
register(NumberToReferenceConverter .getInstance());
112115
register(BooleanToReferenceConverter .getInstance());
113116
register(FileToReferenceConverter .getInstance());
117+
register(PathToReferenceConverter .getInstance());
114118
register(GeometryArrayToReferenceConverter .getInstance());
115119
register(StyledLayerDescriptorToReferenceConverter .getInstance());
116120
register(UrlToReferenceConverter .getInstance());
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Geotoolkit - An Open Source Java GIS Toolkit
3+
* http://www.geotoolkit.org
4+
*
5+
* (C) 2024, Geomatys
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*/
17+
package org.geotoolkit.wps.converters.inputs.complex;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
import java.util.Base64;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.UUID;
26+
import org.apache.sis.util.UnconvertibleObjectException;
27+
import org.geotoolkit.nio.IOUtilities;
28+
import org.geotoolkit.wps.io.WPSEncoding;
29+
import org.geotoolkit.wps.xml.v200.Data;
30+
31+
/**
32+
* Save the content of a WPS complex input into local file.
33+
*
34+
* @author Guilhem Legal (Geomatys)
35+
*/
36+
public class ComplexToPathConverter extends AbstractComplexInputConverter<Path> {
37+
38+
private static ComplexToPathConverter INSTANCE;
39+
40+
private ComplexToPathConverter() {
41+
}
42+
43+
public static synchronized ComplexToPathConverter getInstance() {
44+
if (INSTANCE == null) {
45+
INSTANCE = new ComplexToPathConverter();
46+
}
47+
return INSTANCE;
48+
}
49+
50+
@Override
51+
public Class<Path> getTargetClass() {
52+
return Path.class;
53+
}
54+
55+
@Override
56+
public Path convert(Data source, Map<String, Object> params) throws UnconvertibleObjectException {
57+
58+
if(source == null || source.getContent() == null) {
59+
throw new UnconvertibleObjectException("Mandatory parameter is missing.");
60+
}
61+
62+
Path result = null;
63+
try {
64+
//Create a temp file
65+
final String fileName = UUID.randomUUID().toString();
66+
result = Files.createTempFile(fileName, ".tmp");
67+
68+
final List<Object> data = source.getContent();
69+
if (data.size() < 1) {
70+
throw new UnconvertibleObjectException("There's no available data in this complex content.");
71+
}
72+
String rawData = (String) data.get(0);
73+
if (params != null && params.get(ENCODING) != null && params.get(ENCODING).equals(WPSEncoding.BASE64.getValue())) {
74+
75+
final byte[] byteData = Base64.getDecoder().decode(rawData);
76+
if (byteData != null && byteData.length > 0) {
77+
try (final ByteArrayInputStream is = new ByteArrayInputStream(byteData)) {
78+
IOUtilities.writeStream(is, result);
79+
}
80+
}
81+
82+
} else {
83+
if(rawData.startsWith("<![CDATA[") && rawData.endsWith("]]>")) {
84+
rawData = rawData.substring(9, rawData.length()-3);
85+
}
86+
IOUtilities.writeString(rawData, result);
87+
}
88+
} catch (Exception ex) {
89+
throw new UnconvertibleObjectException(ex);
90+
}
91+
return result;
92+
}
93+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Geotoolkit - An Open Source Java GIS Toolkit
3+
* http://www.geotoolkit.org
4+
*
5+
* (C) 2024, Geomatys
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*/
17+
package org.geotoolkit.wps.converters.inputs.references;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.net.MalformedURLException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.util.Map;
25+
import java.util.UUID;
26+
import org.apache.sis.util.UnconvertibleObjectException;
27+
import org.geotoolkit.wps.xml.v200.Reference;;
28+
29+
/**
30+
* Implementation of ObjectConverter to convert a reference into a Path.
31+
*
32+
* @author Guilhem Legal (Geomatys).
33+
*/
34+
public final class ReferenceToPathConverter extends AbstractReferenceInputConverter<Path> {
35+
36+
private static ReferenceToPathConverter INSTANCE;
37+
38+
private ReferenceToPathConverter() {
39+
}
40+
41+
public static synchronized ReferenceToPathConverter getInstance() {
42+
if (INSTANCE == null) {
43+
INSTANCE = new ReferenceToPathConverter();
44+
}
45+
return INSTANCE;
46+
}
47+
48+
@Override
49+
public Class<Path> getTargetClass() {
50+
return Path.class;
51+
}
52+
53+
/**
54+
* {@inheritDoc}
55+
*
56+
* @return Path.
57+
*/
58+
@Override
59+
public Path convert(final Reference source, final Map<String, Object> params) throws UnconvertibleObjectException {
60+
61+
Path file;
62+
try (InputStream in = getInputStreamFromReference(source)) {
63+
64+
final String fileName = UUID.randomUUID().toString();
65+
final String suffix = ".tmp";
66+
//Create a temp file
67+
file = Files.createTempFile(fileName, suffix);
68+
Files.copy(in, file);
69+
70+
} catch (MalformedURLException ex) {
71+
throw new UnconvertibleObjectException("ReferenceType file invalid input : Malformed url", ex);
72+
} catch (IOException ex) {
73+
throw new UnconvertibleObjectException("ReferenceType file invalid input : IO", ex);
74+
}
75+
return file;
76+
}
77+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Geotoolkit - An Open Source Java GIS Toolkit
3+
* http://www.geotoolkit.org
4+
*
5+
* (C) 2024, Geomatys
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*/
17+
package org.geotoolkit.wps.converters.outputs.complex;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.net.URI;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
26+
import java.util.Base64;
27+
import java.util.Map;
28+
import org.apache.sis.util.UnconvertibleObjectException;
29+
import org.geotoolkit.nio.IOUtilities;
30+
import static org.geotoolkit.wps.converters.WPSObjectConverter.ENCODING;
31+
import static org.geotoolkit.wps.converters.WPSObjectConverter.MIME;
32+
import org.geotoolkit.wps.io.WPSEncoding;
33+
import org.geotoolkit.wps.io.WPSMimeType;
34+
import org.geotoolkit.wps.xml.v200.Data;
35+
36+
/**
37+
* A converter to transform a File into ComplexOutput data for wps ExecuteResponse query.
38+
*
39+
* @author Guilhem Legal (Geomatys).
40+
*/
41+
public class PathToComplexConverter extends AbstractComplexOutputConverter<Path> {
42+
43+
private static PathToComplexConverter INSTANCE;
44+
45+
private PathToComplexConverter() {
46+
}
47+
48+
public static synchronized PathToComplexConverter getInstance() {
49+
if (INSTANCE == null) {
50+
INSTANCE = new PathToComplexConverter();
51+
}
52+
return INSTANCE;
53+
}
54+
55+
@Override
56+
public Class<Path> getSourceClass() {
57+
return Path.class;
58+
}
59+
60+
/**
61+
* Convert a file into Data object, according to the specifications given in params parameter.
62+
*
63+
* @param source The file to convert.
64+
* @param params The parameters used for conversion (Mime-Type/encoding). If null, mime is set to application/octet-stream, and encoding to base64
65+
* @return
66+
* @throws UnconvertibleObjectException
67+
*/
68+
@Override
69+
public Data convert(Path source, Map<String, Object> params) throws UnconvertibleObjectException {
70+
if (source == null) {
71+
throw new UnconvertibleObjectException("The output data should be defined.");
72+
}
73+
if (params == null) {
74+
throw new UnconvertibleObjectException("Mandatory parameters are missing.");
75+
}
76+
77+
final Object tmpMime = params.get(MIME);
78+
final String mime;
79+
final String encoding;
80+
if (tmpMime instanceof String) {
81+
mime = (String) tmpMime;
82+
83+
final Object tmpEncoding = params.get(ENCODING);
84+
if (tmpEncoding instanceof String)
85+
encoding = (String) tmpEncoding;
86+
else
87+
encoding = null;
88+
} else {
89+
mime = WPSMimeType.APP_OCTET.val();
90+
encoding = WPSEncoding.BASE64.getValue();
91+
}
92+
93+
final Data complex = new Data();
94+
complex.setMimeType(mime);
95+
complex.setEncoding(encoding);
96+
97+
//Plain text
98+
if (mime.startsWith("text")) {
99+
//XML is special case, we try to find an associate schema.
100+
if (mime.contains("xml") || mime.contains("gml")) {
101+
String schemaLocation = source.toAbsolutePath().toString();
102+
File ogrSchema = new File(schemaLocation);
103+
// If we find a schema, we ensure it's location is public before giving it.
104+
if (ogrSchema.exists()) {
105+
Object tmpDirValue = params.get(TMP_DIR_PATH);
106+
String tmpDir;
107+
if (tmpDirValue instanceof URI) {
108+
tmpDir = ((URI) params.get(TMP_DIR_PATH)).toString();
109+
} else if (tmpDirValue instanceof String) {
110+
tmpDir = (String) params.get(TMP_DIR_PATH);
111+
} else {
112+
throw new UnconvertibleObjectException("Unexpected type for " + TMP_DIR_PATH + " parameter.");
113+
}
114+
String tmpURL = (String) params.get(TMP_DIR_URL);
115+
if (tmpDir == null || tmpURL == null) {
116+
throw new UnconvertibleObjectException("Mandatory parameters are missing.");
117+
}
118+
if (!schemaLocation.contains(tmpDir)) {
119+
String schemaName = source.getFileName().toString().replace("\\.[a-z]ml", "").concat(".xsd");
120+
Path schemaDest = Paths.get(tmpDir, schemaName);
121+
try {
122+
IOUtilities.copy(source, schemaDest);
123+
schemaLocation = schemaDest.toAbsolutePath().toString();
124+
} catch (IOException e) {
125+
throw new UnconvertibleObjectException("Unexpected error on schema copy.", e);
126+
}
127+
}
128+
complex.setSchema(schemaLocation.replace(tmpDir, tmpURL));
129+
}
130+
}
131+
// CData needed because files could contain problematic characters.
132+
complex.getContent().add("<![CDATA[");
133+
complex.getContent().add(source);
134+
complex.getContent().add("]]>");
135+
} else {
136+
//If no text format, We'll put it as a base64 object.
137+
if (encoding == null || !encoding.equals(WPSEncoding.BASE64.getValue())) {
138+
throw new UnconvertibleObjectException("Encoding should be in Base64 for complex request.");
139+
}
140+
141+
try (InputStream stream = Files.newInputStream(source)) {
142+
byte[] barray = stream.readAllBytes();
143+
complex.getContent().add(Base64.getEncoder().encodeToString(barray));
144+
} catch (Exception ex) {
145+
throw new UnconvertibleObjectException(ex.getMessage(), ex);
146+
}
147+
}
148+
149+
return complex;
150+
}
151+
}

0 commit comments

Comments
 (0)