How to Create Secure Temporary Links for PDF Documents: A Step-by-Step Guide

Prathamesh Kodgire
3 min readMay 18, 2024

--

Introduction

In today’s digital age, the ability to share documents quickly and securely is more important than ever. Whether you’re collaborating with colleagues, sharing information with clients, or distributing educational materials, PDF documents often play a crucial role in maintaining the integrity and format of the content. However, providing permanent access to sensitive information can pose security risks and clutter your storage with outdated files. This is where generating temporary links to view PDF documents comes into play.

Implementation

public class TemporaryUrlGenerator {


private Map<String, OutputStream> temporaryPDFs = new HashMap<>();

public String createAndGetTemporaryPDFLink(OutputStream pdfStream) {

String token = UUID.randomUUID().toString();

temporaryPDFs.put(token, pdfStream);

scheduleDeletion(token, 30);

return "sample.com?token=" +token;

}

public OutputStream getTemporaryPDF(String token) {

return temporaryPDFs.get(token);

}

private void scheduleDeletion(String token, long delaySeconds) {

new java.util.Timer().schedule(

new java.util.TimerTask() {

@Override

public void run() {

temporaryPDFs.remove(token);

}

},

delaySeconds * 1000

);

}

}

The TemporaryUrlGenerator class is designed to generate temporary URLs for accessing PDF documents. Here's a detailed breakdown of its components and functionality:

  • Map<String, OutputStream> temporaryPDFs: A HashMap to store PDF streams temporarily. The key is a unique token (UUID) and the value is the associated OutputStream of the PDF.

Methods:

  • createAndGetTemporaryPDFLink(OutputStream pdfStream): This method generates a temporary URL for the provided PDF stream.
  • A unique token is generated using UUID.randomUUID().toString().
  • The PDF stream is stored in the temporaryPDFs map with the token as the key.
  • A scheduled deletion of the PDF stream is set using the scheduleDeletion method, which will remove the entry after 30 seconds.
  • The method returns a string representing the URL with the token appended as a query parameter.
  • getTemporaryPDF(String token): This method retrieves the PDF stream associated with the given token from the temporaryPDFs map.
  • If the token exists in the map, the associated OutputStream is returned.
  • scheduleDeletion(String token, long delaySeconds): This private method schedules the deletion of a PDF stream from the temporaryPDFs map.
  • It uses a java.util.Timer and java.util.TimerTask to schedule the removal of the token after the specified delay (in seconds).
public byte[] getPDF(String token){

OutputStream pdfStream = temporaryUrlGenerator.getTemporaryPDF(token);

if (pdfStream != null) {

try {

byte[] pdfBytes = ((ByteArrayOutputStream) pdfStream).toByteArray();

return pdfBytes;

} catch (Exception e) {

log.info("error in getting document");

return null;

}

}

return null;

}

The getPDF method is designed to fetch the PDF content in byte array format using a given token. Here’s a detailed explanation of its functionality:

Parameters and Return Type:

  • String token: The unique token associated with the temporary PDF URL.
  • Returns a byte[] containing the PDF data, or null if there was an error or the token is invalid.

Functionality:

  • Calls temporaryUrlGenerator.getTemporaryPDF(token) to retrieve the OutputStream associated with the given token.
  • Checks if the retrieved OutputStream is not null.
  • If not null, it attempts to convert the OutputStream to a ByteArrayOutputStream and then retrieves the byte array using the toByteArray() method.
  • If any exception occurs during this process, it logs an error message and returns null.
  • If the token is invalid (i.e., no OutputStream is associated with it), the method simply returns null.

Summary

TemporaryUrlGenerator Class:

  • Generates unique tokens for temporary access to PDF streams.
  • Stores PDF streams temporarily in a HashMap.
  • Schedules the deletion of these streams after a specified duration.

getPDF Method:

  • Retrieves and returns the PDF content as a byte array for a given token.
  • Handles errors and invalid tokens gracefully by returning null.

--

--