Skip to content

Commit 6a7db05

Browse files
Ryan DewSameeraPriyathamTadikonda
authored andcommitted
MLE-16522: Address high and med Coverity items
1 parent 0bd341a commit 6a7db05

32 files changed

+1270
-983
lines changed

marklogic-data-hub/src/main/java/com/marklogic/client/ext/file/CacheBusterDocumentFileProcessor.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package com.marklogic.client.ext.file;
1717

18-
import com.marklogic.client.ext.file.DocumentFile;
19-
import com.marklogic.client.ext.file.DocumentFileProcessor;
2018
import com.marklogic.client.ext.helper.FilenameUtil;
2119
import com.marklogic.client.ext.helper.LoggingObject;
2220
import com.marklogic.client.io.Format;
@@ -46,15 +44,17 @@ public DocumentFile processDocumentFile(DocumentFile documentFile) {
4644
}
4745
if (text != null) {
4846
Resource resource = documentFile.getResource();
49-
String comment = "";
50-
if (FilenameUtil.isJavascriptFile(resource.getFilename())) {
51-
comment = "// cache buster: " + UUID.randomUUID().toString() + "\n";
52-
} else if (FilenameUtil.isXqueryFile(resource.getFilename())) {
53-
comment = "(: cache buster: " + UUID.randomUUID().toString() + " :)\n";
54-
}
47+
if (resource != null) {
48+
String comment = "";
49+
if (FilenameUtil.isJavascriptFile(resource.getFilename())) {
50+
comment = "// cache buster: " + UUID.randomUUID().toString() + "\n";
51+
} else if (FilenameUtil.isXqueryFile(resource.getFilename())) {
52+
comment = "(: cache buster: " + UUID.randomUUID().toString() + " :)\n";
53+
}
5554

56-
text = comment + text;
57-
documentFile.setModifiedContent(text);
55+
text = comment + text;
56+
documentFile.setModifiedContent(text);
57+
}
5858
}
5959
return documentFile;
6060
}

marklogic-data-hub/src/main/java/com/marklogic/hub/collector/DiskQueue.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ private boolean closeFile() {
150150

151151
fileElementCount = 0;
152152

153-
fileQueue.delete();
153+
if (!fileQueue.delete()) {
154+
LOG.warning(MessageFormat.format("{0} is unable to be deleted", fileQueue.getAbsolutePath()));
155+
}
154156
fileQueue = null;
155157
return true;
156158
}
@@ -180,7 +182,7 @@ private void openFile() throws IOException {
180182
fileOut.flush();
181183

182184
fileIn = new BufferedReader(new InputStreamReader(
183-
Files.newInputStream(fileQueue.toPath()), StandardCharsets.UTF_8)
185+
Files.newInputStream(fileQueue.toPath()), StandardCharsets.UTF_8)
184186
);
185187
}
186188
}

marklogic-data-hub/src/main/java/com/marklogic/hub/collector/impl/CollectorImpl.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.http.conn.ssl.X509HostnameVerifier;
3333
import org.apache.http.impl.client.BasicCredentialsProvider;
3434
import org.apache.http.impl.client.HttpClientBuilder;
35+
import org.jetbrains.annotations.NotNull;
3536
import org.slf4j.Logger;
3637
import org.slf4j.LoggerFactory;
3738
import org.springframework.http.HttpMethod;
@@ -261,8 +262,15 @@ public void verify(String hostname, X509Certificate cert) throws SSLException {
261262
}
262263

263264
@Override
264-
public void verify(String hostname, SSLSocket ssl) throws IOException {
265-
Certificate[] certificates = ssl.getSession().getPeerCertificates();
265+
public void verify(String hostname, @NotNull SSLSocket ssl) throws IOException {
266+
SSLSession session = ssl.getSession();
267+
if (session == null) {
268+
throw new SSLException("No session");
269+
}
270+
Certificate[] certificates = session.getPeerCertificates();
271+
if (certificates == null) {
272+
throw new SSLException("No certificates");
273+
}
266274
verify(hostname, (X509Certificate) certificates[0]);
267275
}
268276

@@ -274,7 +282,13 @@ public void verify(String hostname, String[] cns, String[] subjectAlts) throws S
274282
@Override
275283
public boolean verify(String hostname, SSLSession session) {
276284
try {
285+
if (session == null) {
286+
throw new SSLException("No session");
287+
}
277288
Certificate[] certificates = session.getPeerCertificates();
289+
if (certificates == null) {
290+
throw new SSLException("No certificates");
291+
}
278292
verify(hostname, (X509Certificate) certificates[0]);
279293
return true;
280294
} catch (SSLException e) {

marklogic-data-hub/src/main/java/com/marklogic/hub/deploy/commands/DeployHubDatabaseCommand.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.marklogic.hub.error.DataHubConfigurationException;
88
import com.marklogic.mgmt.util.ObjectMapperFactory;
99
import com.marklogic.rest.util.JsonNodeUtil;
10+
import org.jetbrains.annotations.NotNull;
1011

1112
import java.io.File;
1213
import java.io.IOException;
@@ -23,15 +24,15 @@ public class DeployHubDatabaseCommand extends DeployDatabaseCommand {
2324
/**
2425
* In order for sorting to work correctly via DeployDatabaseCommandComparator, must call setDatabaseFile so that
2526
* the parent getPayload method is able to find the correct File to read from.
26-
*
27+
* <p>
2728
* Otherwise, if this class only has a filename, the parent getPayload method will check every ConfigDir to find a
2829
* match, with the last one winning. In the case of DHF, that means the user config directory. This can be a problem,
2930
* as a user is not likely to define schema-database/triggers-database in e.g. a staging-database.json file in the
3031
* user config directory. That will then cause the ordering of database commands to be incorrect, which will
3132
* likely cause an error when databases are deployed and they don't yet exist.
3233
*
33-
* @param hubConfig The HubConfig object
34-
* @param databaseFile The Database file definition
34+
* @param hubConfig The HubConfig object
35+
* @param databaseFile The Database file definition
3536
* @param databaseFilename The name of the database file
3637
*/
3738
public DeployHubDatabaseCommand(HubConfig hubConfig, File databaseFile, String databaseFilename) {
@@ -60,6 +61,9 @@ protected String getPayload(CommandContext context) {
6061
if (payload != null) {
6162
try {
6263
ObjectNode payloadNode = (ObjectNode) ObjectMapperFactory.getObjectMapper().readTree(payload);
64+
if (payloadNode == null) {
65+
throw new DataHubConfigurationException("Unable to read payload from file: " + this.databaseFile);
66+
}
6367
payloadNode = mergePayloadWithEntityConfigFileIfItExists(payloadNode);
6468
removeSchemaAndTriggersDatabaseSettingsInAProvisionedEnvironment(payloadNode);
6569
return payloadNode.toString();
@@ -81,7 +85,7 @@ protected String getPayload(CommandContext context) {
8185
* @return
8286
* @throws IOException
8387
*/
84-
private ObjectNode mergePayloadWithEntityConfigFileIfItExists(ObjectNode payloadNode) throws IOException {
88+
private @NotNull ObjectNode mergePayloadWithEntityConfigFileIfItExists(@NotNull ObjectNode payloadNode) throws IOException {
8589
if (hubConfig.getEntityDatabaseDir() != null && this.databaseFilename != null) {
8690
File entityDatabaseDir = hubConfig.getEntityDatabaseDir().toFile();
8791
if (entityDatabaseDir != null) {
@@ -98,7 +102,7 @@ private ObjectNode mergePayloadWithEntityConfigFileIfItExists(ObjectNode payload
98102
return payloadNode;
99103
}
100104

101-
private void removeSchemaAndTriggersDatabaseSettingsInAProvisionedEnvironment(ObjectNode payload) {
105+
private void removeSchemaAndTriggersDatabaseSettingsInAProvisionedEnvironment(@NotNull ObjectNode payload) {
102106
if (hubConfig.getIsProvisionedEnvironment()) {
103107
// for DHS we have to remove some keys
104108
logger.warn("Deploying indexes only to a provisioned environment");

marklogic-data-hub/src/main/java/com/marklogic/hub/deploy/commands/GenerateHubTDETemplateCommand.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333
import java.io.File;
3434
import java.io.IOException;
3535
import java.nio.file.Path;
36-
import java.util.*;
36+
import java.util.ArrayList;
37+
import java.util.Arrays;
38+
import java.util.Collections;
39+
import java.util.List;
40+
import java.util.Map;
41+
import java.util.Optional;
42+
import java.util.Set;
3743
import java.util.function.Function;
3844
import java.util.stream.Collectors;
3945

@@ -63,13 +69,13 @@ public void execute(CommandContext context) {
6369

6470
CodeGenerationRequest request = createCodeGenerationRequest();
6571

66-
List<File> entityFiles = findEntityFiles();
72+
List<File> entityFiles = findEntityFiles();
6773

6874
if (!entityFiles.isEmpty()) {
6975
//create map of entity name -> entity definition file
70-
Map<String,File> entityNameFileMap = createEntityNameFileMap(entityFiles);
76+
Map<String, File> entityNameFileMap = createEntityNameFileMap(entityFiles);
7177

72-
logger.debug("Found the following entities->files: {} " + entityNameFileMap);
78+
logger.debug("Found the following entities->files: {} ", entityNameFileMap);
7379

7480
//filterEntities(entityNameFileMap);
7581

@@ -87,7 +93,7 @@ public void execute(CommandContext context) {
8793
modelName = EntityServicesManager.extractEntityNameFromURI(fileName).get();
8894
esModel = new File(tempDir, fileName);
8995
String modelString = generateModel(f);
90-
if(modelString == null) {
96+
if (modelString == null) {
9197
logger.warn(f.getName() + " is not deployed to the database");
9298
continue;
9399
}
@@ -101,15 +107,14 @@ public void execute(CommandContext context) {
101107
code = loadModelDefinition(request, esModel, mgr);
102108
} catch (RuntimeException e) {
103109
throw new RuntimeException("Unable to read model definition from file: " + f.getAbsolutePath(), e);
104-
}
105-
finally {
110+
} finally {
106111
FileUtils.deleteQuietly(esModel);
107112
}
108113
if (this.entityNamesList.isEmpty() || this.entityNamesList.contains(modelName)) {
109114
generatedCodes.add(code);
110115
}
111116
}
112-
for (GeneratedCode code: generatedCodes) {
117+
for (GeneratedCode code : generatedCodes) {
113118
generateExtractionTemplate(appConfig, code);
114119
}
115120
}
@@ -127,10 +132,12 @@ private String generateModel(File f) {
127132
"at \"/data-hub/4/impl/hub-entities.xqy\";\n" +
128133
String.format("hent:get-model(\"%s\")", extractEntityNameFromFilename(f.getName()).get());
129134
EvalResultIterator resp = hubConfig.newStagingClient().newServerEval().xquery(xquery).eval();
135+
String result = null;
130136
if (resp.hasNext()) {
131-
return resp.next().getString();
137+
result = resp.next().getString();
132138
}
133-
return null ;
139+
resp.close();
140+
return result;
134141
}
135142

136143
public String getEntityNames() {
@@ -144,29 +151,29 @@ public void setEntityNames(String entityNames) {
144151
}
145152
}
146153

147-
protected void filterEntities(Map<String,File> entityNameFileMap) {
154+
protected void filterEntities(Map<String, File> entityNameFileMap) {
148155
Set<String> entityNameFileMapKeys = entityNameFileMap.keySet();
149156

150157
//filter on entityNames parameter if specified
151-
if (entityNames!=null&&!entityNames.isEmpty()) {
158+
if (entityNames != null && !entityNames.isEmpty()) {
152159
List<String> entityNamesAsList = Arrays.asList(entityNames.split(","));
153-
logger.info("Entities specified for TDE Generation: {} " + entityNamesAsList);
160+
logger.info("Entities specified for TDE Generation: {} ", entityNamesAsList);
154161

155162
//this will only keep keys in the map that are also in the entityNamesAsList
156163
entityNameFileMapKeys.retainAll(entityNamesAsList);
157164

158165
if (entityNameFileMapKeys.isEmpty()) {
159-
logger.warn("No entities files found under {} or its sub-directories with the entity name(s) {}", hubConfig.getHubEntitiesDir(),entityNamesAsList);
166+
logger.warn("No entities files found under {} or its sub-directories with the entity name(s) {}", hubConfig.getHubEntitiesDir(), entityNamesAsList);
160167
}
161168
}
162169
}
163170

164-
protected static Map<String,File> createEntityNameFileMap(List<File> entityFiles) {
165-
if (entityFiles==null) {
171+
protected static Map<String, File> createEntityNameFileMap(List<File> entityFiles) {
172+
if (entityFiles == null) {
166173
return Collections.emptyMap();
167174
}
168175
return entityFiles.stream().collect(
169-
toMap(extractEntityNameFunction(),Function.identity()));
176+
toMap(extractEntityNameFunction(), Function.identity()));
170177
}
171178

172179
protected List<File> findEntityFiles() {
@@ -180,7 +187,7 @@ protected List<File> findEntityFiles() {
180187
.collect(Collectors.toList());
181188
for (String entityName : entityNames) {
182189
File[] entityDefs = entitiesPath.resolve(entityName).toFile().listFiles((dir, name) -> name.endsWith(ENTITY_FILE_EXTENSION));
183-
if (entityDefs!=null) {
190+
if (entityDefs != null) {
184191
entities.addAll(Arrays.asList(entityDefs));
185192
}
186193
}
@@ -194,7 +201,9 @@ protected void generateExtractionTemplate(AppConfig appConfig, GeneratedCode cod
194201
String template = code.getExtractionTemplate();
195202
if (template != null) {
196203
File dir = userFinalSchemasTDEs.toFile();
197-
dir.mkdirs();
204+
if (!dir.mkdirs()) {
205+
logger.warn("Unable to create directory: {}", dir.getAbsolutePath());
206+
}
198207
File out = new File(dir, code.getTitle() + "-" + code.getVersion() + ".tdex");
199208
String logMessage = "Wrote extraction template to: ";
200209
if (out.exists()) {

marklogic-data-hub/src/main/java/com/marklogic/hub/deploy/commands/HubDeployDatabaseCommandFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public HubDeployDatabaseCommandFactory(HubConfig hubConfig) {
2020
@Override
2121
public DeployDatabaseCommand newDeployDatabaseCommand(File databaseFile) {
2222
final String filename = databaseFile != null ? databaseFile.getName() : null;
23+
if (filename == null) {
24+
throw new RuntimeException("Unable to determine the filename of the database file");
25+
}
2326
DeployHubDatabaseCommand c = new DeployHubDatabaseCommand(hubConfig, databaseFile, filename);
2427
c.setDeployDatabaseCommandFactory(this);
2528
return c;

marklogic-data-hub/src/main/java/com/marklogic/hub/deploy/commands/LoadUserArtifactsCommand.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.marklogic.client.ext.modulesloader.Modules;
2626
import com.marklogic.client.ext.modulesloader.impl.EntityDefModulesFinder;
2727
import com.marklogic.client.ext.modulesloader.impl.MappingDefModulesFinder;
28+
import com.marklogic.client.ext.modulesloader.impl.PropertiesModuleManager;
2829
import com.marklogic.client.ext.util.DefaultDocumentPermissionsParser;
2930
import com.marklogic.client.ext.util.DocumentPermissionsParser;
3031
import com.marklogic.client.io.DocumentMetadataHandle;
@@ -34,7 +35,7 @@
3435
import org.springframework.beans.factory.annotation.Autowired;
3536
import org.springframework.core.io.Resource;
3637
import org.springframework.stereotype.Component;
37-
import com.marklogic.client.ext.modulesloader.impl.PropertiesModuleManager;
38+
3839
import java.io.File;
3940
import java.io.IOException;
4041
import java.io.InputStream;
@@ -86,8 +87,10 @@ private PropertiesModuleManager getModulesManager() {
8687

8788
if (forceLoad) {
8889
pmm.deletePropertiesFile();
89-
if (defaultTimestampFile.exists()){
90-
defaultTimestampFile.delete();
90+
if (defaultTimestampFile.exists()) {
91+
if (!defaultTimestampFile.delete()) {
92+
logger.warn("Unable to delete properties file: {}", defaultTimestampFile.getAbsolutePath());
93+
}
9194
}
9295
}
9396
return pmm;
@@ -101,7 +104,6 @@ public void execute(CommandContext context) {
101104
DatabaseClient finalClient = hubConfig.newFinalClient();
102105

103106
Path userModulesPath = hubConfig.getHubPluginsDir();
104-
String baseDir = userModulesPath.normalize().toAbsolutePath().toString();
105107
Path startPath = userModulesPath.resolve("entities");
106108
Path mappingPath = userModulesPath.resolve("mappings");
107109

@@ -120,7 +122,6 @@ public void execute(CommandContext context) {
120122
Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
121123
@Override
122124
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
123-
String currentDir = dir.normalize().toAbsolutePath().toString();
124125
if (isArtifactDir(dir, startPath.toAbsolutePath())) {
125126
Modules modules = new EntityDefModulesFinder().findModules(dir.toString());
126127
DocumentMetadataHandle meta = new DocumentMetadataHandle();
@@ -157,12 +158,16 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
157158
documentPermissionsParser.parsePermissions(hubConfig.getModulePermissions(), meta.getPermissions());
158159
for (Resource r : modules.getAssets()) {
159160
if (forceLoad || propertiesModuleManager.hasFileBeenModifiedSinceLastLoaded(r.getFile())) {
160-
InputStream inputStream = r.getInputStream();
161-
StringHandle handle = new StringHandle(IOUtils.toString(inputStream));
162-
inputStream.close();
163-
finalMappingDocumentWriteSet.add("/mappings/" + r.getFile().getParentFile().getName() + "/" + r.getFilename(), meta, handle);
164-
stagingMappingDocumentWriteSet.add("/mappings/" + r.getFile().getParentFile().getName() + "/" + r.getFilename(), meta, handle);
165-
propertiesModuleManager.saveLastLoadedTimestamp(r.getFile(), new Date());
161+
try (InputStream inputStream = r.getInputStream()) {
162+
StringHandle handle = new StringHandle(IOUtils.toString(inputStream));
163+
File parentFile = r.getFile().getParentFile();
164+
if (parentFile == null) {
165+
throw new RuntimeException("Unable to get parent file for resource: " + r.getFilename());
166+
}
167+
finalMappingDocumentWriteSet.add("/mappings/" + parentFile.getName() + "/" + r.getFilename(), meta, handle);
168+
stagingMappingDocumentWriteSet.add("/mappings/" + parentFile.getName() + "/" + r.getFilename(), meta, handle);
169+
propertiesModuleManager.saveLastLoadedTimestamp(r.getFile(), new Date());
170+
}
166171
}
167172
}
168173
return FileVisitResult.CONTINUE;

0 commit comments

Comments
 (0)