How to Validate if an Input File is a Valid MSG File using Java
Validating files is a critically important step in any application. If the wrong file types pass through our application’s logic, downstream errors can easily occur. Even correctly labeled file extensions can pose a potential problem: if they are corrupted or password protected, we may be unwittingly allowing malicious objects to enter our database. Thankfully, you can easily validate files for free using our Validate Document API endpoint. In this article, I’ll demonstrate how you can validate .MSG files (proprietary Microsoft message format) using our .MSG validation API iteration. I’ve included instructions below to help structure your API call using ready-to-run Java code examples; in addition to that, you’ll just need to get a Cloudmersive API key (you can get one by visiting our website and registering a free account which comes with 800 API calls per month & zero additional commitments).
Let’s dive right into it.
Our first step is to install the Validate Document API client. Let’s first add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then let’s complete installation by adding another reference to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Now we can shift focus over to our controller. Let’s add the imports to the top of our file:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ValidateDocumentApi;
Now let’s call the function. We simply need to include our input .MSG file path in the inputFile field, and add our API key to the setAPiKey field:
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");ValidateDocumentApi apiInstance = new ValidateDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
DocumentValidationResult result = apiInstance.validateDocumentMsgValidation(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ValidateDocumentApi#validateDocumentMsgValidation");
e.printStackTrace();
}
With that, you’re all done. Below, I’ve included an example value response model JSON for your reference:
{
"DocumentIsValid": true,
"PasswordProtected": true,
"ErrorCount": 0,
"WarningCount": 0,
"ErrorsAndWarnings": [
{
"Description": "string",
"Path": "string",
"Uri": "string",
"IsError": true
}
]
}
It’s important to note that a “true” boolean response in the “PasswordProtected” field may indicate an underlying security issue with the file you are validating. However, this API will not specifically identify malicious threats in files. For free virus/malware scanning & 360-degree content verification, you can also check out our Virus Scanning APIs and their associated documentation.