|
| 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