package org.pdfserv;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.stereotype.Component;

@Component
@Path("/document")
@Produces(MediaType.APPLICATION_JSON)
public class DocumentResource {
	
	private final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

	@GET
	public List<String> documentList() {
    	return Arrays.asList(
    			Application.DOCUMENT_FOLDER.list( 
    					(File dir, String name) -> { return name.endsWith("pdf") || name.endsWith("PDF"); }
    			)
    		);
    }

	private File openFile(String documentName) {
		return new File(Application.DOCUMENT_FOLDER, documentName);
	}
	
	private int readPageCount(File documentFile) throws IOException {
		try (PDDocument pdfDocument = PDDocument.load(documentFile)) {
			return pdfDocument.getNumberOfPages();
		}
	}
	
	private Date getLastModified(File documentFile) {
		return new Date(documentFile.lastModified());
	}
	
	@GET
	@Path("/{documentName}")
	public Response getDocument(@PathParam("documentName") String documentName) throws IOException {
		File documentFile = openFile(documentName);
		if (!documentFile.exists()) {
			throw new NotFoundException();
		}
		Document document = new Document();
		document.setName(documentName);
		document.setLastModified(DATE_FORMAT.format(getLastModified(documentFile)));
		document.setPageCount(readPageCount(documentFile));
		return Response.ok(document).build();
	}
}

@@ -317,16 +317,51 @@
 oduces;%0A
+import javax.ws.rs.core.EntityTag;%0A
 import j
@@ -1349,16 +1349,130 @@
 );%0A%09%7D%0A%09%0A
+%09private EntityTag createETag(File documentFile) %7B%0A%09%09return new EntityTag(documentFile.lastModified() + %22%22);%0A%09%7D%0A%09%0A
 %09@GET%0A%09@
@@ -1945,16 +1945,46 @@
 cument).
+tag(createETag(documentFile)).
 build();

Add ETag to response