Switch to unified view

a/src/main/java/net/timbusproject/extractors/modules/tavernaextractor/provenance/ProvenanceAccessClient.java b/src/main/java/net/timbusproject/extractors/modules/tavernaextractor/provenance/ProvenanceAccessClient.java
...
...
9
import java.io.UnsupportedEncodingException;
9
import java.io.UnsupportedEncodingException;
10
import java.net.MalformedURLException;
10
import java.net.MalformedURLException;
11
import java.net.URL;
11
import java.net.URL;
12
import java.nio.file.Files;
12
import java.nio.file.Files;
13
import java.nio.file.Path;
13
import java.nio.file.Path;
14
import java.nio.file.Paths;
14
import java.sql.SQLException;
15
import java.sql.SQLException;
15
import java.util.ArrayList;
16
import java.util.ArrayList;
16
import java.util.Collections;
17
import java.util.Collections;
17
import java.util.HashMap;
18
import java.util.HashMap;
18
import java.util.List;
19
import java.util.List;
...
...
188
                    break; // no further treatment!
189
                    break; // no further treatment!
189
                }
190
                }
190
191
191
                Identified identified = pc.getReferenceService().resolveIdentifier(ref, null, ic);
192
                Identified identified = pc.getReferenceService().resolveIdentifier(ref, null, ic);
192
                if (identified instanceof ReferenceSet) {
193
                if (identified instanceof ReferenceSet) {
194
195
                    LOGGER.info("try to fetch reference set for binding "+binding.getPortName());
196
193
                    ReferenceSet referenceSet = (ReferenceSet) identified;
197
                    ReferenceSet referenceSet = (ReferenceSet) identified;
194
                    Set<ExternalReferenceSPI> externalReferences = referenceSet.getExternalReferences();
198
                    Set<ExternalReferenceSPI> externalReferences = referenceSet.getExternalReferences();
195
199
196
                    for (ExternalReferenceSPI externalReference : externalReferences) {
200
                    for (ExternalReferenceSPI externalReference : externalReferences) {
197
                        if (externalReference instanceof ValueCarryingExternalReference<?>) {
201
                        if (externalReference instanceof ValueCarryingExternalReference<?>) {
198
                            ValueCarryingExternalReference<?> vcer = (ValueCarryingExternalReference<?>) externalReference;
202
                            ValueCarryingExternalReference<?> vcer = (ValueCarryingExternalReference<?>) externalReference;
199
203
204
200
                            // string ? filepath or URL
205
                            // string ? filepath or URL
201
                            if (String.class.isAssignableFrom(vcer.getValueType())) {
206
                            if (String.class.isAssignableFrom(vcer.getValueType())) {
202
207
203
                                String possibleFilePath = (String) vcer.getValue();
208
                                String possibleFilePath = (String) vcer.getValue();
204
209
205
                                // valid file ?
210
                                // valid file ?
206
                                File file = isFile(possibleFilePath);
211
                                File file = isFile(possibleFilePath);
207
                                if (file != null) {
212
                                if (file != null && file.length() > 0) {
208
213
209
                                    LOGGER.debug("\t Found file (" + file.getPath() + ") for port= "
214
                                    LOGGER.debug("\t Found file (" + file.getPath() + ") for port= "
210
                                            + binding.getPortName());
215
                                            + binding.getPortName());
211
216
212
                                    fileName += "[" + binding.getPortName() + "]-";
217
                                    fileName += "[" + binding.getProcessorName() + "][" + binding.getPortName() + "]-";
213
                                    File tmpFile = File.createTempFile(fileName.toString(), ".file", tempDir.toFile());
218
                                    File tmpFile = File.createTempFile(fileName, ".file", tempDir.toFile());
214
219
220
                                    try {
215
                                    FileUtils.copyFile(file, tmpFile);
221
                                        FileUtils.copyFile(file, tmpFile);
222
                                    } catch (IOException e) {
223
                                        LOGGER.error("Error during fetching file. " + e.getMessage());
224
                                    }
216
                                    fileName = "";
225
                                    fileName = "";
217
                                }
226
                                }
218
227
219
                                // valid URL ?
228
                                // valid URL ?
220
                                URL url = isURL(possibleFilePath);
229
                                URL url = isURL(possibleFilePath);
...
...
222
231
223
                                    LOGGER.debug("\t Found valid URL (" + possibleFilePath + ") for port = "
232
                                    LOGGER.debug("\t Found valid URL (" + possibleFilePath + ") for port = "
224
                                            + binding.getPortName());
233
                                            + binding.getPortName());
225
234
226
                                    // download file and copy it to temp directory
235
                                    // download file and copy it to temp directory
227
                                    fileName += "[" + binding.getPortName() + "]-";
236
                                    fileName += "[" + binding.getProcessorName() + "][" + binding.getPortName() + "]-";
228
                                    File content = File.createTempFile(fileName, ".url", tempDir.toFile());
237
                                    File content = File.createTempFile(fileName, ".url", tempDir.toFile());
229
                                    FileUtils.copyURLToFile(url, content);
238
                                    FileUtils.copyURLToFile(url, content);
230
239
231
                                    LOGGER.debug("\t Created temporary file (" + content.getAbsolutePath() + ")");
240
                                    LOGGER.debug("\t Created temporary file (" + content.getAbsolutePath() + ")");
232
                                    fileName = "";
241
                                    fileName = "";
...
...
238
247
239
                                LOGGER.debug("\t Found byte[] for port= " + binding.getPortName());
248
                                LOGGER.debug("\t Found byte[] for port= " + binding.getPortName());
240
249
241
                                fileName += "[" + binding.getPortName() + "]-";
250
                                fileName += "[" + binding.getPortName() + "]-";
242
                                byte[] content = (byte[]) vcer.getValue();
251
                                byte[] content = (byte[]) vcer.getValue();
243
                                File tmpFile = File.createTempFile(fileName.toString(), ".binary", tempDir.toFile());
252
                                File tmpFile = File.createTempFile(fileName, ".binary", tempDir.toFile());
244
                                FileUtils.writeByteArrayToFile(tmpFile, content);
253
                                FileUtils.writeByteArrayToFile(tmpFile, content);
245
254
246
                                LOGGER.debug("\t Create " + fileName + " file for port= " + binding.getPortName());
255
                                LOGGER.debug("\t Create " + fileName + " file for port= " + binding.getPortName());
247
256
248
                                // reset file name
257
                                // reset file name
...
...
403
     * @param possibleFilePath
412
     * @param possibleFilePath
404
     * @return file if exists
413
     * @return file if exists
405
     */
414
     */
406
    private File isFile(String possibleFilePath) {
415
    private File isFile(String possibleFilePath) {
407
416
417
        LOGGER.debug("call isFile(" + possibleFilePath + ")");
418
419
        LOGGER.debug("\t Detect if file path (" + possibleFilePath + ") point to a file. ");
420
        try {
421
            return Paths.get(possibleFilePath).toFile();
422
        } catch (Exception e) {
423
            LOGGER.debug("\t File can not be detected. " + e.getMessage());
424
            return null;
425
        }
426
408
        String regexPath = "([a-zA-Z]:)?(\\\\[a-zA-Z0-9_.-]+)+\\\\?";
427
//        String regexPath = "([a-zA-Z]:)?(\\\\[a-zA-Z0-9_.-]+)+\\\\?";
409
428
//
410
        if (possibleFilePath != null && possibleFilePath.length() > 5 && possibleFilePath.contains(File.separator)
429
//        if (possibleFilePath != null && possibleFilePath.length() > 5 && possibleFilePath.contains(File.separator)
411
                && Pattern.matches(regexPath, possibleFilePath)) {
430
//                && Pattern.matches(regexPath, possibleFilePath)) {
412
431
//
413
            LOGGER.debug("\t Detect if file path (" + possibleFilePath + ") point to a file. ");
432
//            LOGGER.debug("\t Detect if file path (" + possibleFilePath + ") point to a file. ");
414
            try {
433
//            try {
415
                // can we create a file of the possibleFilePath
434
//                // can we create a file of the possibleFilePath
416
                File file = new File(possibleFilePath);
435
//                File file = new File(possibleFilePath);
417
                if (!file.isDirectory()) {
436
//                if (!file.isDirectory()) {
418
                    return file;
437
//                    return file;
419
                }
438
//                }
420
439
//
421
            } catch (Exception e) {
440
//            } catch (Exception e) {
422
                LOGGER.debug("\t File can not be detected. " + e.getMessage());
441
//                LOGGER.debug("\t File can not be detected. " + e.getMessage());
423
                return null;
442
//                return null;
424
            }
443
//            }
425
        }
444
//        }
426
        return null;
427
    }
445
    }
428
446
429
    private URL isURL(String possibleURL) {
447
    private URL isURL(String possibleURL) {
448
449
        LOGGER.debug("call isURL(" + possibleURL + ")");
430
450
431
        String regexURL = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
451
        String regexURL = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
432
452
433
        if (possibleURL != null && possibleURL.length() > 5 && Pattern.matches(regexURL, possibleURL)) {
453
        if (possibleURL != null && possibleURL.length() > 5 && Pattern.matches(regexURL, possibleURL)) {
434
454
435
            LOGGER.debug("\t Detect if (" + possibleURL + ") is a valid URL.");
455
            LOGGER.debug("\t Detect if (" + possibleURL + ") is a valid URL.");
436
456
437
            try {
457
            try {
438
                return new URL(possibleURL);
458
                return new URL(possibleURL);
439
            } catch (MalformedURLException e) {
459
            } catch (MalformedURLException e) {
440
                LOGGER.debug("\t Not a valid URL.");
460
                LOGGER.debug("\t Not a valid URL. "+e.getMessage());
441
            }
461
            }
442
        }
462
        }
443
        return null;
463
        return null;
444
    }
464
    }
445
}
465
}