Datasets:

Modalities:
Text
Formats:
json
Languages:
code
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 8,556 Bytes
eb67da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2019, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.gce.pgraster;

import com.google.common.annotations.VisibleForTesting;
import java.io.Closeable;
import java.io.File;
import java.sql.SQLException;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.dbcp.BasicDataSource;
import org.geotools.data.jdbc.datasource.DBCPDataSource;
import org.geotools.util.factory.GeoTools;
import org.geotools.util.logging.Logging;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Configuration for a {@link PGRasterReader}.
 *
 * <p>Configuration is stored as XML with the following basic format:
 *
 * <pre>
 *     &lt;pgraster>
 *       &lt;name//>        // coverage name
 *       &lt;database>     // database connection
 *         &lt;host//>      // database host
 *         &lt;port//>      // database port
 *         &lt;name//>      // database name
 *         &lt;user//>      // database username
 *         &lt;pass//>      // database user password
 *       &lt;/database>
 *       &lt;raster>       // raster column config
 *         &lt;column//>      // column name
 *         &lt;table//>     // table name
 *         &lt;schema//>    // table schema
 *       &lt;/raster>
 *       &lt;time>        // time column config
 *         &lt;enabled//>  // enable / disable time
 *         &lt;column//>     // column name
 *       &lt;/time>
 *     &lt;/pgraster>
 *   </pre>
 */
class PGRasterConfig implements Closeable {

    static final Logger LOG = Logging.getLogger(PGRasterConfig.class);

    String name;
    DataSource dataSource;
    String schema;
    String table;
    String column;
    String enableDrivers;

    TimeConfig time = new TimeConfig();

    static Document parse(File cfgfile) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db;
        try {
            db = dbf.newDocumentBuilder();
        } catch (Exception e) {
            throw new RuntimeException("Error creating XML parser");
        }

        try {
            return db.parse(cfgfile);
        } catch (Exception e) {
            throw new RuntimeException("Error parsing pgraster config", e);
        }
    }

    PGRasterConfig(File configFile) {
        this(parse(configFile));
    }

    PGRasterConfig(Document config) {
        Element root = config.getDocumentElement();
        if (!"pgraster".equalsIgnoreCase(root.getNodeName())) {
            throw new IllegalArgumentException(
                    "Not a postgis raster configuration, root element must be 'pgraster'");
        }

        this.name = first(root, "name").map(this::nodeValue).orElse(null);
        this.enableDrivers = first(root, "enableDrivers").map(this::nodeValue).orElse(null);

        Element db =
                first(config.getDocumentElement(), "database")
                        .orElseThrow(
                                () ->
                                        new IllegalArgumentException(
                                                "Config has no database element"));

        DataSource dataSource = null;

        String jndi = first(db, "jndi").map(this::nodeValue).orElse(null);
        if (jndi != null) {
            try {
                // BUG: CWE-20 Improper Input Validation
                // dataSource = (DataSource) GeoTools.getInitialContext().lookup(jndi);
                // FIXED: 
                dataSource = (DataSource) GeoTools.jndiLookup(jndi);
            } catch (NamingException e) {
                throw new IllegalArgumentException("Error performing JNDI lookup for: " + jndi, e);
            }
        }

        if (dataSource == null) {
            BasicDataSource source = new BasicDataSource();
            source.setDriverClassName("org.postgresql.Driver");

            String host = first(db, "host").map(this::nodeValue).orElse("localhost");

            Integer port =
                    first(db, "port").map(this::nodeValue).map(Integer::parseInt).orElse(5432);

            String name =
                    first(db, "name")
                            .map(this::nodeValue)
                            .orElseThrow(
                                    () ->
                                            new IllegalArgumentException(
                                                    "database 'name' not specified"));

            source.setUrl("jdbc:postgresql://" + host + ":" + port + "/" + name);

            first(db, "user").map(this::nodeValue).ifPresent(source::setUsername);

            first(db, "passwd").map(this::nodeValue).ifPresent(source::setPassword);

            first(db, "pool")
                    .ifPresent(
                            p -> {
                                first(p, "min")
                                        .map(this::nodeValue)
                                        .map(Integer::parseInt)
                                        .ifPresent(source::setMinIdle);
                                first(p, "max")
                                        .map(this::nodeValue)
                                        .map(Integer::parseInt)
                                        .ifPresent(source::setMaxActive);
                            });

            dataSource = new PGRasterDataSource(source);
        }

        this.dataSource = dataSource;

        Element ras =
                first(config.getDocumentElement(), "raster")
                        .orElseThrow(
                                () ->
                                        new IllegalArgumentException(
                                                "Config has no 'raster' element"));

        this.schema = first(ras, "schema").map(this::nodeValue).orElse("public");
        this.table =
                first(ras, "table")
                        .map(this::nodeValue)
                        .orElseThrow(
                                () ->
                                        new IllegalArgumentException(
                                                "column must specify a 'table' element"));
        this.column = first(ras, "column").map(this::nodeValue).orElse(null);

        // time
        first(config.getDocumentElement(), "time")
                .ifPresent(
                        el -> {
                            first(el, "enabled")
                                    .map(this::nodeValue)
                                    .map(Boolean::parseBoolean)
                                    .ifPresent(it -> time.enabled = it);
                            first(el, "column")
                                    .map(this::nodeValue)
                                    .ifPresent(it -> time.column = it);
                        });
    }

    @VisibleForTesting
    PGRasterConfig() {}

    Optional<Element> first(Element el, String name) {
        NodeList matches = el.getElementsByTagName(name);
        if (matches.getLength() > 0) {
            return Optional.of((Element) matches.item(0));
        }
        return Optional.empty();
    }

    String nodeValue(Element el) {
        return el.getFirstChild().getNodeValue();
    }

    @Override
    public void close() {
        if (dataSource instanceof PGRasterDataSource) {
            try {
                ((PGRasterDataSource) dataSource).close();
            } catch (SQLException e) {
                LOG.log(Level.WARNING, "Error closing data source", e);
            }
        }
    }

    static class TimeConfig {
        boolean enabled = true;
        String column;
    }

    static class PGRasterDataSource extends DBCPDataSource {

        PGRasterDataSource(BasicDataSource wrapped) {
            super(wrapped);
        }
    }
}