Skip to content

Commit ce1c52c

Browse files
committed
added object verification before unmarshall on virtual table load
1 parent 2b31472 commit ce1c52c

File tree

4 files changed

+61
-36
lines changed

4 files changed

+61
-36
lines changed

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/in/content/SIARDDK1007ContentImportStrategy.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ DocIndexType loadVirtualTableContent() throws ModuleException, FileNotFoundExcep
5959
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
6060
Schema xsdSchema = null;
6161
InputStream xsdInputStream = new FileInputStream(pathStrategy.getMainFolder().getPath().toString()
62-
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR
63-
+ pathStrategy.getXsdFilePath(SIARDDKConstants.DOC_INDEX));
62+
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR + pathStrategy.getXsdFilePath(SIARDDKConstants.DOC_INDEX));
6463

6564
try {
6665
xsdSchema = schemaFactory.newSchema(new StreamSource(xsdInputStream));
@@ -75,10 +74,17 @@ DocIndexType loadVirtualTableContent() throws ModuleException, FileNotFoundExcep
7574
unmarshaller = context.createUnmarshaller();
7675
unmarshaller.setSchema(xsdSchema);
7776
inputStreamXml = new FileInputStream(pathStrategy.getMainFolder().getPath().toString()
78-
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR
79-
+ pathStrategy.getXmlFilePath(SIARDDKConstants.DOC_INDEX));
80-
JAXBElement<DocIndexType> jaxbElement = (JAXBElement<DocIndexType>) unmarshaller.unmarshal(inputStreamXml);
81-
return jaxbElement.getValue();
77+
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR + pathStrategy.getXmlFilePath(SIARDDKConstants.DOC_INDEX));
78+
Object result = unmarshaller.unmarshal(inputStreamXml);
79+
DocIndexType docIndex;
80+
if (result instanceof JAXBElement) {
81+
docIndex = ((JAXBElement<DocIndexType>) result).getValue();
82+
} else if (result instanceof DocIndexType) {
83+
docIndex = (DocIndexType) result;
84+
} else {
85+
throw new IllegalArgumentException("Unexpected object type: " + result.getClass().getName());
86+
}
87+
return docIndex;
8288
} catch (JAXBException e) {
8389
throw new ModuleException().withMessage("Error while Unmarshalling JAXB").withCause(e);
8490
} finally {
@@ -124,8 +130,7 @@ ContextDocumentationIndex loadContextDocTableContent() throws ModuleException, F
124130
inputStreamXml = new FileInputStream(
125131
pathStrategy.getMainFolder().getPath().toString() + SIARDDKConstants.RESOURCE_FILE_SEPARATOR
126132
+ pathStrategy.getXmlFilePath(SIARDDKConstants.CONTEXT_DOCUMENTATION_INDEX));
127-
ContextDocumentationIndex jaxbElement = (ContextDocumentationIndex) unmarshaller
128-
.unmarshal(inputStreamXml);
133+
ContextDocumentationIndex jaxbElement = (ContextDocumentationIndex) unmarshaller.unmarshal(inputStreamXml);
129134
return jaxbElement;
130135
} catch (JAXBException e) {
131136
throw new ModuleException().withMessage("Error while Unmarshalling JAXB").withCause(e);

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/in/content/SIARDDK128ContentImportStrategy.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ DocIndexType loadVirtualTableContent() throws ModuleException, FileNotFoundExcep
6565
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
6666
Schema xsdSchema = null;
6767
InputStream xsdInputStream = new FileInputStream(pathStrategy.getMainFolder().getPath().toString()
68-
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR
69-
+ pathStrategy.getXsdFilePath(SIARDDKConstants.DOC_INDEX));
68+
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR + pathStrategy.getXsdFilePath(SIARDDKConstants.DOC_INDEX));
7069

7170
try {
7271
xsdSchema = schemaFactory.newSchema(new StreamSource(xsdInputStream));
@@ -81,10 +80,17 @@ DocIndexType loadVirtualTableContent() throws ModuleException, FileNotFoundExcep
8180
unmarshaller = context.createUnmarshaller();
8281
unmarshaller.setSchema(xsdSchema);
8382
inputStreamXml = new FileInputStream(pathStrategy.getMainFolder().getPath().toString()
84-
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR
85-
+ pathStrategy.getXmlFilePath(SIARDDKConstants.DOC_INDEX));
86-
JAXBElement<DocIndexType> jaxbElement = (JAXBElement<DocIndexType>) unmarshaller.unmarshal(inputStreamXml);
87-
return jaxbElement.getValue();
83+
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR + pathStrategy.getXmlFilePath(SIARDDKConstants.DOC_INDEX));
84+
Object result = unmarshaller.unmarshal(inputStreamXml);
85+
DocIndexType docIndex;
86+
if (result instanceof JAXBElement) {
87+
docIndex = ((JAXBElement<DocIndexType>) result).getValue();
88+
} else if (result instanceof DocIndexType) {
89+
docIndex = (DocIndexType) result;
90+
} else {
91+
throw new IllegalArgumentException("Unexpected object type: " + result.getClass().getName());
92+
}
93+
return docIndex;
8894
} catch (JAXBException e) {
8995
throw new ModuleException().withMessage("Error while Unmarshalling JAXB").withCause(e);
9096
} finally {
@@ -130,8 +136,7 @@ ContextDocumentationIndex loadContextDocTableContent() throws ModuleException, F
130136
inputStreamXml = new FileInputStream(
131137
pathStrategy.getMainFolder().getPath().toString() + SIARDDKConstants.RESOURCE_FILE_SEPARATOR
132138
+ pathStrategy.getXmlFilePath(SIARDDKConstants.CONTEXT_DOCUMENTATION_INDEX));
133-
ContextDocumentationIndex jaxbElement = (ContextDocumentationIndex) unmarshaller
134-
.unmarshal(inputStreamXml);
139+
ContextDocumentationIndex jaxbElement = (ContextDocumentationIndex) unmarshaller.unmarshal(inputStreamXml);
135140
return jaxbElement;
136141
} catch (JAXBException e) {
137142
throw new ModuleException().withMessage("Error while Unmarshalling JAXB").withCause(e);

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/in/metadata/SIARDDK1007MetadataImportStrategy.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ private DocIndexType loadVirtualTableMetadata() throws ModuleException, FileNotF
394394
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
395395
Schema xsdSchema = null;
396396
InputStream xsdInputStream = new FileInputStream(pathStrategy.getMainFolder().getPath().toString()
397-
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR
398-
+ pathStrategy.getXsdFilePath(SIARDDKConstants.DOC_INDEX));
397+
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR + pathStrategy.getXsdFilePath(SIARDDKConstants.DOC_INDEX));
399398

400399
try {
401400
xsdSchema = schemaFactory.newSchema(new StreamSource(xsdInputStream));
@@ -411,8 +410,16 @@ private DocIndexType loadVirtualTableMetadata() throws ModuleException, FileNotF
411410
unmarshaller.setSchema(xsdSchema);
412411
inputStreamXml = new FileInputStream(pathStrategy.getMainFolder().getPath().toString()
413412
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR + pathStrategy.getXmlFilePath(SIARDDKConstants.DOC_INDEX));
414-
JAXBElement<DocIndexType> jaxbElement = (JAXBElement<DocIndexType>) unmarshaller.unmarshal(inputStreamXml);
415-
return jaxbElement.getValue();
413+
Object result = unmarshaller.unmarshal(inputStreamXml);
414+
DocIndexType docIndex;
415+
if (result instanceof JAXBElement) {
416+
docIndex = ((JAXBElement<DocIndexType>) result).getValue();
417+
} else if (result instanceof DocIndexType) {
418+
docIndex = (DocIndexType) result;
419+
} else {
420+
throw new IllegalArgumentException("Unexpected object type: " + result.getClass().getName());
421+
}
422+
return docIndex;
416423
} catch (JAXBException e) {
417424
throw new ModuleException().withMessage("Error while Unmarshalling JAXB").withCause(e);
418425
} finally {
@@ -479,14 +486,14 @@ private List<ColumnStructure> createContextDocumentsTableColumns() {
479486
Type typeBlob = sqlStandardDatatypeImporter.getCheckedType("<information unavailable>", "<information unavailable>",
480487
"<information unavailable>", "<information unavailable>", Constants.BINARY_LARGE_OBJECT,
481488
Constants.BINARY_LARGE_OBJECT);
482-
VirtualColumnStructure columnID = new VirtualColumnStructure(SIARDDKConstants.DOCUMENT_ID, SIARDDKConstants.DOCUMENT_ID, typeInt,
483-
true, SIARDDKConstants.DOCUMENT_IDENTIFIER, "1", true);
484-
VirtualColumnStructure columnTitle = new VirtualColumnStructure(SIARDDKConstants.DOCUMENT_TITLE, SIARDDKConstants.DOCUMENT_TITLE, typeChar,
485-
true, SIARDDKConstants.DOCUMENT_TITLE_DESCRIPTION, "", true);
486-
VirtualColumnStructure columnDate = new VirtualColumnStructure(SIARDDKConstants.DOCUMENT_DATE, SIARDDKConstants.DOCUMENT_DATE, typeChar,
487-
true, SIARDDKConstants.DOCUMENT_DATE_DESCRIPTION, "", true);
488-
VirtualColumnStructure columnLOB = new VirtualColumnStructure(Constants.BLOB, Constants.BLOB_COLUMN_NAME, typeBlob, true,
489-
"", "1", true);
489+
VirtualColumnStructure columnID = new VirtualColumnStructure(SIARDDKConstants.DOCUMENT_ID,
490+
SIARDDKConstants.DOCUMENT_ID, typeInt, true, SIARDDKConstants.DOCUMENT_IDENTIFIER, "1", true);
491+
VirtualColumnStructure columnTitle = new VirtualColumnStructure(SIARDDKConstants.DOCUMENT_TITLE,
492+
SIARDDKConstants.DOCUMENT_TITLE, typeChar, true, SIARDDKConstants.DOCUMENT_TITLE_DESCRIPTION, "", true);
493+
VirtualColumnStructure columnDate = new VirtualColumnStructure(SIARDDKConstants.DOCUMENT_DATE,
494+
SIARDDKConstants.DOCUMENT_DATE, typeChar, true, SIARDDKConstants.DOCUMENT_DATE_DESCRIPTION, "", true);
495+
VirtualColumnStructure columnLOB = new VirtualColumnStructure(Constants.BLOB, Constants.BLOB_COLUMN_NAME, typeBlob,
496+
true, "", "1", true);
490497
columnStructureList.add(columnID);
491498
columnStructureList.add(columnTitle);
492499
columnStructureList.add(columnDate);
@@ -503,8 +510,8 @@ private List<ColumnStructure> createVirtualTableColumns() {
503510
Type type = sqlStandardDatatypeImporter.getCheckedType("<information unavailable>", "<information unavailable>",
504511
"<information unavailable>", "<information unavailable>", Constants.BINARY_LARGE_OBJECT,
505512
Constants.BINARY_LARGE_OBJECT);
506-
VirtualColumnStructure columnLOB = new VirtualColumnStructure(Constants.BLOB, Constants.BLOB_COLUMN_NAME, type, true,
507-
"", "1", true);
513+
VirtualColumnStructure columnLOB = new VirtualColumnStructure(Constants.BLOB, Constants.BLOB_COLUMN_NAME, type,
514+
true, "", "1", true);
508515
columnStructureList.add(columnID);
509516
columnStructureList.add(columnLOB);
510517
return columnStructureList;

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/in/metadata/SIARDDK128MetadataImportStrategy.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,16 @@ private DocIndexType loadVirtualTableMetadata() throws ModuleException, FileNotF
410410
unmarshaller.setSchema(xsdSchema);
411411
inputStreamXml = new FileInputStream(pathStrategy.getMainFolder().getPath().toString()
412412
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR + pathStrategy.getXmlFilePath(SIARDDKConstants.DOC_INDEX));
413-
JAXBElement<DocIndexType> jaxbElement = (JAXBElement<DocIndexType>) unmarshaller.unmarshal(inputStreamXml);
414-
return jaxbElement.getValue();
413+
Object result = unmarshaller.unmarshal(inputStreamXml);
414+
DocIndexType docIndex;
415+
if (result instanceof JAXBElement) {
416+
docIndex = ((JAXBElement<DocIndexType>) result).getValue();
417+
} else if (result instanceof DocIndexType) {
418+
docIndex = (DocIndexType) result;
419+
} else {
420+
throw new IllegalArgumentException("Unexpected object type: " + result.getClass().getName());
421+
}
422+
return docIndex;
415423
} catch (JAXBException e) {
416424
throw new ModuleException().withMessage("Error while Unmarshalling JAXB").withCause(e);
417425
} finally {
@@ -484,8 +492,8 @@ private List<ColumnStructure> createContextDocumentsTableColumns() {
484492
SIARDDKConstants.DOCUMENT_TITLE, typeChar, true, SIARDDKConstants.DOCUMENT_TITLE_DESCRIPTION, "", true);
485493
VirtualColumnStructure columnDate = new VirtualColumnStructure(SIARDDKConstants.DOCUMENT_DATE,
486494
SIARDDKConstants.DOCUMENT_DATE, typeChar, true, SIARDDKConstants.DOCUMENT_DATE_DESCRIPTION, "", true);
487-
VirtualColumnStructure columnLOB = new VirtualColumnStructure(Constants.BLOB, Constants.BLOB_COLUMN_NAME, typeBlob, true,
488-
"", "1", true);
495+
VirtualColumnStructure columnLOB = new VirtualColumnStructure(Constants.BLOB, Constants.BLOB_COLUMN_NAME, typeBlob,
496+
true, "", "1", true);
489497
columnStructureList.add(columnID);
490498
columnStructureList.add(columnTitle);
491499
columnStructureList.add(columnDate);
@@ -502,8 +510,8 @@ private List<ColumnStructure> createVirtualTableColumns() {
502510
Type type = sqlStandardDatatypeImporter.getCheckedType("<information unavailable>", "<information unavailable>",
503511
"<information unavailable>", "<information unavailable>", Constants.BINARY_LARGE_OBJECT,
504512
Constants.BINARY_LARGE_OBJECT);
505-
VirtualColumnStructure columnLOB = new VirtualColumnStructure(Constants.BLOB, Constants.BLOB_COLUMN_NAME, type, true,
506-
"", "1", true);
513+
VirtualColumnStructure columnLOB = new VirtualColumnStructure(Constants.BLOB, Constants.BLOB_COLUMN_NAME, type,
514+
true, "", "1", true);
507515
columnStructureList.add(columnID);
508516
columnStructureList.add(columnLOB);
509517
return columnStructureList;

0 commit comments

Comments
 (0)